问题难点 棋盘如何显示? 胜负如何判断? 程序特点 运用结构体数组的知识,结构体内保存字符型二维数组。 结构体数组表示了棋盘厚度,字符型二维数组表示当前棋盘状态。 胜负评
问题难点
- 棋盘如何显示?
- 胜负如何判断?
程序特点
- 运用结构体数组的知识,结构体内保存字符型二维数组。
- 结构体数组表示了棋盘厚度,字符型二维数组表示当前棋盘状态。
- 胜负评判系统分为当前平面棋盘检查与竖直方向检查。
程序不足
- 没有禁手限制,存在不公平
- 仅是双人对战版本,没有AI系统
C语言代码
#include<stdio.h>#include<Windows.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define MAXIMUS 5 //定义棋盘大小(5*5个方框)
#define chess_number 30//定义棋盘高度
int count = 0, move_x = 0, move_y = 0, x = 0, y = 0, chess_num = chess_number / 2, chess_num_0;
struct MyStruct_0
{
char ChessBoard[MAXIMUS][MAXIMUS];
}MyStruct_0[chess_number];
- 这里的棋盘高度是指:竖直方向棋盘个数
- move_x:为棋子横坐标。x:为光标横坐标
- 实际初始状态为第15个棋盘,但用户显示为chess_num第零个棋盘,这样能够避免判断胜负不方便判断的情况
void Initialize()
{
for (int i = 0; i < MAXIMUS; i++)
{
for (int j = 0; j < MAXIMUS; j++)
{
MyStruct_0[chess_num].ChessBoard[i][j] = ' ';
}
}
MyStruct_0[chess_num].ChessBoard[x][y] = '+';
}
- 该函数目的:将棋盘棋子的地方用’ ‘代替。
- 显示初始光标位置
void DrawBoard()
{
printf("\n当前棋盘位置(w,s调整):%d\n", chess_num_0);
printf("当前棋盘棋子情况:\n");
printf("o:\n");
for (int i = 0; i < MAXIMUS; i++)
{
for (int j = 0; j < MAXIMUS; j++)
{
if (MyStruct_0[chess_num].ChessBoard[i][j] == 'o')
printf("(%d,%d) ", i, j);
}
}
printf("\n*:\n");
for (int i = 0; i < MAXIMUS; i++)
{
for (int j = 0; j < MAXIMUS; j++)
{
if (MyStruct_0[chess_num].ChessBoard[i][j] == '*')
printf("(%d,%d) ", i, j);
}
}
printf("\n---------------------------------------------\n\n");
printf("┏");
for (int i = 0; i < MAXIMUS - 1; i++)
printf("─┳");
printf("─┓\n");
for (int i = 0; i< MAXIMUS - 1; i++)
{
for (int j = 0; j < MAXIMUS; j++)
{
printf("│%c ", MyStruct_0[chess_num].ChessBoard[i][j]);
}
printf("│\n");
printf("┣");
for (int i = 0; i < MAXIMUS - 1; i++)
{
printf("─╋");
}
printf("─┫\n");
}
for (int i = 0; i < MAXIMUS; i++)
{
printf("│%c ", MyStruct_0[chess_num].ChessBoard[MAXIMUS - 1][i]);
}
printf("│\n");
printf("┗");
for (int i = 0; i < MAXIMUS - 1; i++)
printf("─┻");
printf("─┛\n");
}/*判断胜负*/
int Win_Or_Not()
{
/*水平判断*/
int W_ = 0, B_ = 0, i;
for (i = 1; i < 5; i++){ if (move_x - i >= 0 && MyStruct_0[chess_num].ChessBoard[move_x - i][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else break; }
for (i = 1; i < 5; i++){ if (move_x + i<MAXIMUS&&MyStruct_0[chess_num].ChessBoard[move_x + i][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else break; }
if (W_ >= 4)
return 1;
for (i = 1; i < 5; i++){ if (move_x - i >= 0 && MyStruct_0[chess_num].ChessBoard[move_x - i][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else break; }
for (i = 1; i < 5; i++){ if (move_x + i<MAXIMUS&&MyStruct_0[chess_num].ChessBoard[move_x + i][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else break; }
if (B_ >= 4)
return 2;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else { break; } }
if (B_ == 4)
return 2;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x + i][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x - i][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x + i][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x - i][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else { break; } }
if (B_ == 4)
return 2;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x + i][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x - i][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o')W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x + i][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num].ChessBoard[move_x - i][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*')B_++; else { break; } }
if (B_ == 4)
return 2;
/*立体判断*/
/*0号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else {break;} }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*1号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*2号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x - i][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x + i][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x - i][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x + i][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*3号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x - i][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x + i][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x - i][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x + i][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*4号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x - i][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x + i][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x - i][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x + i][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*5号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*6号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x + i][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x - i][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x + i][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x - i][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*7号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x + i][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x - i][move_y] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x + i][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x - i][move_y] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
/*8号方向*/
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x + i][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x - i][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x + i][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x - i][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
int sum = 0;
for (int i = 0; i < MAXIMUS; i++)
{
for (int j = 0; j < MAXIMUS; j++)
{
if (MyStruct_0[chess_num].ChessBoard[i][j] == 'o' || MyStruct_0[chess_num].ChessBoard[i][j] == '*')
sum += 1;
}
}
if (sum == MAXIMUS*MAXIMUS)
return 3;
return 0;
}
这里是最重要的胜负判断函数。比较笨拙,能力有限。
这里以1号方向为例讲解。
/*1号方向*/W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y - i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y + i] == 'o'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == 'o') W_++; else { break; } }
if (W_ == 4)
return 1;
W_ = 0, B_ = 0;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num + i].ChessBoard[move_x][move_y - i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
for (i = 1; i < 5; i++){ if (MyStruct_0[chess_num - i].ChessBoard[move_x][move_y + i] == '*'&&MyStruct_0[chess_num].ChessBoard[move_x][move_y] == '*') B_++; else { break; } }
if (B_ == 4)
return 2;
- W_ = 0, B_ = 0。记录双方当前连子数目。达到五个子后返回不同的数值来判断胜方。
- MyStruct_0[chess_num + i].ChessBoard[move_x][move_y - i]表示当前第chess_num个棋盘的状态
- 每下一个子分为两部分。己方与对方。同时,将一条直线上的棋子数判断分为一半一半来判断。
- 一共九个方向。能想的来么?
{
int input;
system("cls");
DrawBoard();//打印棋盘
input = getch();//等待键盘按下一个字符
if (input == 27)//如果是ESC则退出程序
{
exit(0);
}
else if (char(input) == 'c')
{
MyStruct_0[chess_num].ChessBoard[x][y] = ' ', move_x = x, move_y = y;
}
else if (char(input) == 'w')
{
chess_num_0++;
chess_num++;
}
else if (char(input) == 's')
{
chess_num_0--;
chess_num--;
}
else if (input == 0x20)//如果是空格则开始走子
{
if (count % 2 == 0)
MyStruct_0[chess_num].ChessBoard[x][y] = 'o', move_x = x, move_y = y;
else
MyStruct_0[chess_num].ChessBoard[x][y] = '*', move_x = x, move_y = y;
count += 1;
}
else if (input == 0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键
{
if (MyStruct_0[chess_num].ChessBoard[x][y] != '*'&&MyStruct_0[chess_num].ChessBoard[x][y] != 'o')
MyStruct_0[chess_num].ChessBoard[x][y] = ' ';
input = getch();//获得第二次输入信息
switch (input)//判断方向键方向并移动光标位置
{
case 0x4B:
y--;
break;
case 0x48:
x--;
break;
case 0x4D:
y++;
break;
case 0x50:
x++;
break;
}
if (x<0)x = MAXIMUS - 1;//如果光标位置越界则移动到对侧
if (y<0)y = MAXIMUS - 1;
if (x >= MAXIMUS)x = 0;
if (y >= MAXIMUS)y = 0;
if (MyStruct_0[chess_num].ChessBoard[x][y] != '*'&&MyStruct_0[chess_num].ChessBoard[x][y] != 'o')
MyStruct_0[chess_num].ChessBoard[x][y] = '+';
}
system("cls");
DrawBoard();//打印棋盘
printf("当前对局数:%d\n", count);
printf("稍等...\n");
Sleep(100);
}
- 这里是主要的操作函数。
- input = getch();//等待键盘按下一个字符
if (input == 27)//如果是ESC则退出程序
{
exit(0);
} - 这个代码实现了对于键盘输入的类型转换,inpu为int型
- else if (char(input) == 'c')
{
MyStruct_0[chess_num].ChessBoard[x][y] = ' ', move_x = x, move_y = y;
} - 这里提供了悔一步棋的操作,很简单,就只是将棋子变成’ ‘即可。
- else if (char(input) == 'w')
{
chess_num_0++;
chess_num++;
} - chess_num_0:界面显示当前棋盘数
- chess_num:实际棋盘数组的数值大小
- `else if (input == 0x20)//如果是空格则开始走子
{
if (count % 2 == 0)
MyStruct_0[chess_num].ChessBoard[x][y] = ‘o’, move_x = x, move_y = y;
else
MyStruct_0[chess_num].ChessBoard[x][y] = ‘*’, move_x = x, move_y = y;
}`
- 如果输入空格则会将当前的光标坐标(x,y)依照当前局数奇偶来觉得是’o’还是’*’
- 执行完赋值操作后,局数加一
- `else if (input == 0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键
{
if (MyStruct_0[chess_num].ChessBoard[x][y] != ‘*’&&MyStruct_0[chess_num].ChessBoard[x][y] != ‘o’)
MyStruct_0[chess_num].ChessBoard[x][y] = ’ ‘;
input = getch();//获得第二次输入信息
{
case 0x4B:
y--;
break;
case 0x48:
x--;
break;
case 0x4D:
y++;
break;
case 0x50:
x++;
break;
}
if (x<0)x = MAXIMUS - 1;//如果光标位置越界则移动到对侧
if (y<0)y = MAXIMUS - 1;
if (x >= MAXIMUS)x = 0;
if (y >= MAXIMUS)y = 0;
if (MyStruct_0[chess_num].ChessBoard[x][y] != '*'&&MyStruct_0[chess_num].ChessBoard[x][y] != 'o')
MyStruct_0[chess_num].ChessBoard[x][y] = '+';
}`
- 当是方向键的时候,需要将’+’光标赋值给当前棋盘,并且将原来的位置的字符赋值为’ ‘
- 没进行一次方向键操作,便需要对于x和y值进行变动
- 当然还有超越棋盘边界的情况
if (y<0)y = MAXIMUS - 1;
if (x >= MAXIMUS)x = 0;
if (y >= MAXIMUS)y = 0;
这段代码便解决了这个问题
int main(){
system("title 五子棋(方框版) — J.m制作");//设置标题
system("mode con cols=60 lines=40");//设置窗口大小
system("color F0");//设置颜色
Initialize();
while (true)
{
if (Win_Or_Not() == 1)
{
system("cls");
DrawBoard();
printf("游戏结果: o Win\n");
system("pause");
}
if (Win_Or_Not() == 2)
{
system("cls");
DrawBoard();
printf("游戏结果: * Win\n");
system("pause");
}
if (Win_Or_Not() == 3)
{
system("cls");
DrawBoard();
printf("游戏结果: 平局\n");
system("pause");
}
RunGame();
}
return 0;
}
这里是主函数,需要注意的是这个程序永远不会退出,即使胜负已定。因为考虑到需要玩家查看当前棋盘状态,所以便没有设置结束。