五子棋 v1.0.0
代码见下,或前往团队下载
#include<bits/stdc++.h>
#include<windows.h>
#include <stdlib.h>
#include<conio.h>
using namespace std;
int node[1000][1000][2], nxt, vis[1000][1000][2];
void SetSize(unsigned uCol,unsigned uLine)
{
char cmd[64];
sprintf(cmd,"mode con cols=%d lines=%d",uCol,uLine);
system(cmd);
}
void printStr(int startX, int startY, char str[])
{
HANDLE hd;
COORD pos;
pos.X = startX;
pos.Y = startY;
hd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hd, pos);
printf("%s", str);
}
int Check(int x, int y, int kind)
{
int num[10] = {0}, ans = 0;
int dx[10] = {0, -2, -2, -2, 0, 0, 2, 2, 2};
int dy[10] = {0, -1, 0, 1, -1, 1, -1, 0, 1};
for (int i = 1; i <= 8; i++)
{
int tx = x + dx[i], ty = y + dy[i];
while (ty >= 0 && tx <= 54 && ty >= 0 && ty <= 27 && node[tx][ty][kind] == 1)
{
num[i]++;
tx += dx[i];
ty += dy[i];
}
}
for (int i = 1; i <= 4; i++)
{
ans = max(ans, num[i] + num[9 - i] + 1);
}
return ans;
}
int move(int x, int y, int kind)
{
char ch;
while ( ( ch = _getch() ) != '\r' )
{
if (ch == 'w') y--;
else if (ch == 's') y++;
else if (ch == 'a') x -= 2;
else if (ch == 'd') x += 2;
if (x < 0) x += 2;
if (x >= 54) x -= 2;
if (y < 0) y++;
if (y >= 27) y--;
if (node[x][y][0])
{
printStr(x, y, "@");
}
else if (node[x][y][1])
{
printStr(x, y, "O");
}
else printStr(x, y, "+");
}
if (node[x][y][(kind + 1) % 2])
{
nxt++;
return 0;
}
else if (node[x][y][kind])
{
nxt++;
return 0;
}
else
{
if (kind) printStr(x, y, "O");
else printStr(x, y, "@");
}
node[x][y][kind]++;
return Check(x, y, kind);
}
void Color()
{
for (int i = 1; i <= 20; i++)
{
system("color 10");
system("color 20");
system("color 30");
system("color 40");
system("color 50");
system("color 60");
system("color 70");
system("color 80");
system("color 90");
system("color a0");
system("color b0");
system("color c0");
system("color d0");
system("color f0");
system("color e0");
}
}
int main(int argc, char *argv[])
{
SetSize(54, 29);
for (int i = 0; i < 27; i++)
for (int j = 0; j < 27; j++)
printStr(i * 2, j, "+");
int i = 0, last = 0, time = 0;
while (1)
{
last = i;
time = move(0, 0, i);
i = ((i + 1) + nxt) % 2;
printStr(0, 28, "");
if (time == 5)
{
system("CLS");
printStr(15, 14, "");
printf("恭喜持%s的选手获胜!", (last == 0)? "黑棋": "白棋");
Color();
printStr(15, 16, "");
system("pause");
return 0;
}
if (last == i)
{
printf("落子不合法,请%s重下!", (i == 0)? "黑棋": "白棋");
printStr(0, 28, "");
}
else
{
printf("落子合法。 ");
printStr(0, 28, "");
Sleep(1000);
printf("请%s下! ", (i == 0)? "黑棋": "白棋");
printStr(0, 28, "");
}
nxt = 0;
}
return 0;
}