1、text.c实现
准备实现三子棋的过程我们可以一步步完成,先定义头文件:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3 //三子棋行
#define COL 3 //三子棋列
接着写出一个界面函数,如下:
void menu()
{
printf("**************\n");
printf("****1.play****\n");
printf("****0.exit****\n");
printf("**************\n");
}
接着为了方便调试,我们把实现代码写入到text函数中,如下:
void text()
{
srand((unsigned int)time(NULL));//使用srand和rand以及time生成随机数
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();//game函数是用来实现整个游戏
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
}
} while (input != 0);
}
主函数只需调用text函数即可:
int main()
{
text();
return 0;
}
运行界面如下:
2、实现game函数
game函数实现如下:
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
//初始化棋盘全为空格
init_board(board, ROW, COL);
//打印棋盘
displayboard(board, ROW, COL);
while (1)
{
//玩家下棋
play_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");
displayboard(board, ROW, COL);
}
首先是定义棋盘和初始化棋盘,初始化我们选择使用init_board函数实现:
void init_board(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++)
{
board[i][j] = ' ';
}
}
}
接着是打印棋盘,需要注意棋盘的美观,实现函数为displayboard,如下:
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]);
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");
}
}
}
打印界面显示如下
接着就是玩家下棋,往数组里面填充‘*’代码如下:
void play_move(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标:\n");
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 computer_move(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % ROW;
y = rand() % COL;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '#';
break;
}
else
continue;
}
}
}
最后是判断输赢的代码,如下:
static int is_full(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++)
{
int count = 9;
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][0] == board[i][2] && board[i][0] != ' ')
return board[i][1];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
return board[1][i];
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != ' ')
{
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 (is_full(board, row, col) == 1)
{
return 'q';
}
return 'c';
}
输赢判断就是看行列和对角线是否为一样的元素,一样就赢了。
3、实现界面如下:
全部代码地址:c_practice: C语言练习代码
文件名字为game_sanziqi