小时候都玩的三子棋大家都还记得把,现在我们利用c语言来实现一个简单的版本,使用的软件是vs 2019.
下面展示代码:
game.c
#include "game.h"
void Initboard(char board[ROW][COL], int row, int col)//初始化棋盘
{
int i = 0, 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, 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)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void playermove(char board[ROW][COL], int row, int col)//玩家下棋
{
printf("玩家请下棋:\n");
int x = 0, y = 0;
scanf("%d %d", &x,&y);
while (1)
{
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)//电脑下棋
{
printf("电脑下棋:\n");
int x = 0, y = 0;
while (1)
{
x = rand() % row;
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++)
{
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[1][1] == board[2][2] && board[1][i] != ' ')
{
return board[1][1];//判断对角线
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][i] != ' ')
{
return board[1][1];//另一条对角线
}
if (IsFull(board,ROW,COL))
{
return 'Q';
}//平局
return 'C';//游戏继续
}
int IsFull(char board[ROW][COL], int row, int col)//判断棋盘是否已经被下满,如果下满就平局,没有就继续
{
int i = 0, j = 0;
for (i=0;i<row;i++)
{
for ( j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
text.c
#include "game.h"
void menu()
{
printf("------------------\n");
printf("------1.Play------\n");
printf("------0.Exit------\n");
printf("------------------\n");
}
void game()
{
srand((unsigned int)time(NULL));
char board[ROW][COL] = {0};
Initboard(board, ROW, COL);//初始化棋盘
Displayboard(board, ROW, COL);//打印棋盘
char ret=0;
while (1)
{
playermove(board, ROW, COL);//玩家下棋
ret = Iswin(board, ROW, COL);
if (ret!='C')
{
break;
}
Displayboard(board, ROW, COL);//打印棋盘
computermove(board, ROW, COL);//电脑下棋
ret = Iswin(board, ROW, COL);//判断输赢
if (ret != 'C')
{
break;
}
Displayboard(board, ROW, COL);//打印棋盘
}
if (ret=='#')
{
printf("电脑赢了,你个菜比\n");
}
else if (ret=='*')
{
printf("恭喜你,赢了!\n");
}
else
{
printf("平局了,你个菜比\n");
}
Displayboard(board, ROW, COL);//打印棋盘
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();//游戏功能的实现
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新选择!\n");
break;
}
} while (input);
return 0;
}
//玩家赢返回*,电脑赢返回#
//平局返回Q,游戏继续返回C
game.h
#define _CRT_SECURE_NO_WARNINGS//不定义这个无法正常使用scanf
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Initboard(char board[ROW][COL], int row, int col);
void Displayboard(char board[ROW][COL], int row, int col);
void playermove(char board[ROW][COL], int row, int col);
void computermove(char board[ROW][COL], int row, int col);
char Iswin(char board[ROW][COL], int row, int col);
int IsFull(char board[ROW][COL], int row, int col);
运行结果展示:
这个代码没有对电脑进行强化,只是最基本的让它随便下一个空白地方,感兴趣的话大家可以自行强化。