[C语言] 三子棋
Posted 咸菜萝卜干
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[C语言] 三子棋相关的知识,希望对你有一定的参考价值。
三子棋
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = \' \';
//printf("%c \\n", board[i][j]);
}
}
}
// 3*3 的三子棋
//void DisplayBoard(char board[ROW][COL], int row, int col)
//{
// int i = 0;
//
// for (i = 0; i < row; i++)
// {
// printf(" %c 丨 %c 丨 %c \\n", board[i][0], board[i][1], board[i][2]);
// if (i < row -1)
// {
// printf("---丨---丨---\\n");
// }
// }
//}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int m = 0;
for (m = 0; m < row; m=m+1) //打印行
{
int n = 0;
for (n = 0;n<col;n++) //打印列
{
printf(" %c ",board[m][n]);
if (n < col - 1)
{
printf("丨");
}
}
printf("\\n");
if (m < row - 1)
{
int m = 0;
for (m = 0; m < col; m++)
{
//printf("%d", col);
printf("---");
if (m < col - 1)
printf("丨");
}
printf("\\n");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家走>\\n");
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
//判断坐标合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//下棋
//是否坐标被占用
if (board[x - 1][y - 1] == \' \')
{
board[x - 1][y - 1] = \'*\';
break;
}
else
{
printf("坐标被占用\\n");
}
}
else
{
printf("坐标非法, 请重新输入\\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
while (1)
{
printf("电脑走;>\\n");
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == \' \');
{
board[x][y] = \'#\';
break;
}
/*else
{
printf("坐标专用");
}*/
}
}
char IsWin(char board[ROW][COL], int row, int col)
{
int i = 0;
//判断三行
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != \' \')
{
return board[i][1];
}
}
//判断三列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != \' \');
{
return board[1][i];
}
}
//判断对角线
C语言三子棋(附详细代码)