初始化的必要性说明 如果不将棋盘初始化 下棋的时候就会出现 判断的bug
void lnitboard(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] = ' ';
}
}
}
代码分析
通过列数控制最小单元打印次数
通过行数控制需要打印多少
void displayboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < COL; j++)
{
printf(" %c |", board[i][j]);
}
printf("\n");
for (j = 0; j < COL; j++)
{
printf("---|");
}
printf("\n");
}
}
玩家下棋需求
1 不能超出棋盘范围
2 已经下了不能在下
3 一次只能走一步
4 玩家二和玩家一下棋方法为一致的
1和2 我们通过if条件语句 不满足则 不能下 继续循环 满足则跳出循环
void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家走→");
printf("请输入坐标");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) //满足1 2条件可跳出循环
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标已经被占用");
}
}
else {
printf("坐标不合法");
}
}
}
电脑下棋为产生随机数
想进一步了解随机数产生机制 戳这里 随机数产生
void cpmove(char board[ROW][COL], int row, int col)
{
printf("电脑走\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x ][y ] == ' ')
{
board[x ][y] = '#';
break;
}
}
}
三子棋赢的方式有三种 一行 一列 一条对角线 三字相同即可
也要注意平局和棋盘下满了
char iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//printf("%c%c%c", board[i][0], board[i][1], board[i][2]);
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
break;
}
}
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[1][j];
break;
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
else if (full(board, ROW, COL) == 0)
{
return 'a';
}
else
return 'c';
}
如果棋盘满了就将数据导入full(棋盘满了返回0)提示平局
#include"game.h"
void game2()
{
char ret = 0;
char board[ROW][COL] = { 0 };
lnitboard(board, ROW, COL);
displayboard(board, ROW, COL);
while (1)
{
playermove(board, ROW, COL);
displayboard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
{
break;
}
player2move(board, ROW, COL);
displayboard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == '*')
{
printf("player1 win");
}
else if (ret == '#')
{
printf("player2 win");
}
else
{
printf("the game has drawn");
}
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
lnitboard(board, ROW, COL);
displayboard(board, ROW, COL);
while (1)
{
playermove(board, ROW, COL);
displayboard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
{
break;
}
cpmove(board, ROW, COL);
displayboard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == '*')
{
printf("player1 win");
}
else if (ret == '#')
{
printf("player2 win");
}
else
{
printf("the game has drawn");
}
}
void menu()
{
printf("*********************** \n");
printf("********1.单人开始游戏***** \n");
printf("********2.双人开始游戏***** \n");
printf("********0.退出游戏***** \n");
printf("*********************** \n");
}
//void test()
//{
// int input = 0;
// do
// {
// menu();
// printf("\n******请选择****");
// scanf_s("%d", &input);
// switch (input)
// {
// case 1:
// game();
// break;
// case 2:
// game2();
// break;
// case 0:
// break;
// printf("goodbye");
// default:
// printf("woring!!");
// break;
// }
// } while (input);
//}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("\n******请选择****");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
game2();
break;
case 0:
break;
printf("goodbye");
default:
printf("woring!!");
break;
}
} while (input);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
# define ROW 3
# define COL 3
#include
#include
#include
void lnitboard(char board[ROW][COL], int cow, int col);
void displayboard(char board[ROW][COL], int row, int col);
void playermove(char board[ROW][COL], int row, int col);
void player2move(char board[ROW][COL], int row, int col);
void cpmove(char board[ROW][COL], int row, int col);
char iswin(char board[ROW][COL], int row, int col);
int full(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
# include"game.h"
void lnitboard(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] = ' ';
}
}
}
void displayboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < COL; j++)
{
printf(" %c |", board[i][j]);
}
printf("\n");
for (j = 0; j < COL; j++)
{
printf("---|");
}
printf("\n");
}
}
void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家走→");
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("该坐标已经被占用");
}
}
else {
printf("坐标不合法");
}
}
}
void player2move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家2走→");
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("该坐标已经被占用");
}
}
else {
printf("坐标不合法");
}
}
}
int full(char board[ROW][COL], int row, int col)
{
int k = 0;
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
k++;
}
}
return k;
}
char iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//printf("%c%c%c", board[i][0], board[i][1], board[i][2]);
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
break;
}
}
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[1][j];
break;
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
else if (full(board, ROW, COL) == 0)
{
return 'a';
}
else
return 'c';
}
void cpmove(char board[ROW][COL], int row, int col)
{
printf("电脑走\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x ][y ] == ' ')
{
board[x ][y] = '#';
break;
}
}
}