想必大家都玩过吧,它又叫又叫九宫棋、圈圈叉叉、一条龙等。
将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。
话不多说,上代码!
首先我们创建出一个菜单,可以让玩家们自己选择是否进入游戏,像这样:
void menu()
{
printf("***********************************\n");
printf("********* 1.play ***************\n");
printf("********* 0.exit ***************\n");
printf("***********************************\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default :
printf("选择错误\n");
break;
}
} while (input);
}
当玩家确定进入游戏后,我们就要编写游戏的内容了
。
我们先思考整个游戏,要进行的步骤有:
1.初始化棋盘(InitBoard)
2.打印棋盘(DisplayBoard)
3.落子(Player_move ‘*’,Computer_move ‘#’)
4.判断输赢(is_win)
具体实现可以像这样:
void game()
{
//玩家“*”,电脑“#”;
char board[ROW][COL] = { 0 };//数组的内容全是空格
InitBoard(board, ROW,COL );//初始化棋盘,先将空格填到每个元素中;
//打印棋盘
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
Player_move(board,ROW,COL);
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret!='C')
{
break;
}
Computer_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if(ret=='#')
{
printf("电脑赢了\n");
}
else
{
printf("平局了\n");
}
}
我们先给游戏创建一个头文件 game.h :
#pragma once
#define ROW 3
#define COL 3
#include
#include
#include
void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void Player_move(char board[ROW][COL], int row, int col);
void Computer_move(char board[ROW][COL], int row, int col);
//判断游戏输赢:返回一个值
//玩家赢--*
//电脑赢--#
//平局--Q
//继续--C
char is_win(char board[ROW][COL], int row, int col);
然后我们分别对每一项任务进行具体的代码编写:
1.初始化棋盘(InitBoard)
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] = ' ';
}
}
}
2.打印棋盘(DisplayBoard)
void DisplayBoard(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++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
//分割行
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
打印出一个简易的三子棋棋盘:
3.落子(Player_move ‘*’)
void Player_move(char board[ROW][COL], int row, int col)
{
printf("玩家下棋:\n");
int x = 0;
int y = 0;
while (1)
{
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");
}
}
}
3.落子(Computer_move ‘#’)
void Computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋:\n");
while (1)
{
x = rand() % ROW;//0~2;
y = rand() % COL;//0~2
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
4.判断输赢(is_win)
int is_full(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++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char is_win(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];
}
}
if (board[0][0] == board[1][1] && board[2][2] == board[1][1] && board[1][1]!= ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
//判断平局
if (1 == is_full(board, row, col))
{
return 'Q';
}
else
{
return 'C';
}
}