相信许多小伙伴都玩过三子棋游戏,而这里将编辑代码进行人机对弈。不过这里小编的技术还不太成熟所以‘机’还不算很智能,欢迎大家来讨论。
这里我将代码分为三个文件,分别为头文件代码符号的定义game.h;代码中函数的实现game.c;代码的主体test.c。
1.选择是否进行游戏
代码语言:javascript复制void menu()
{
printf("**************************n");
printf("******* 1.plsy **********n");
printf("******* 0.exit **********n");
printf("**************************n");
}
2.游戏实现
(1)输出棋盘
输出一个3*3的棋盘
这里更改数字可以控制棋盘的大小。
代码语言:javascript复制//初始化棋盘
void DisplayBord(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");
}
}
(2)玩家下棋
代码语言:javascript复制void PlarerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋n");
while (1)
{
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");
}
}
}
(3)电脑下棋
代码语言:javascript复制void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑下棋n");
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ');
{
board[x][y] = '#';
break;
}
}
}
(4)判断输赢
代码语言:javascript复制static int IsFull(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 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][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i )
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (IsFull(board, row, col))
{
return 'Q';
}
return 'C';
}
3.头文件gaem.h
代码语言:javascript复制#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//输出棋盘
void DisplayBord(char board[ROW][COL], int row, int col);
//玩家下棋·
void PlarerMove(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);
4.主函数test.c
代码语言:javascript复制#include "game.h" //调用头文件
void menu()
{
printf("**************************n");
printf("******* 1.plsy **********n");
printf("******* 0.exit **********n");
printf("**************************n");
}
void game()
{
char ret = 0;
char board[ROW][COL];//board 背景,板
//开始的时候内容应该都是空格
InitBoard(board, ROW, COL);
DisplayBord(board, ROW, COL);
//下棋
while (1)
{
PlarerMove(board, ROW, COL);
DisplayBord(board, ROW, COL);
ret=IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
ComputerMove(board,ROW,COL);
DisplayBord(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢n");
}
else if (ret == '#')
{
printf("电脑赢n");
}
else
{
printf("平局");
}
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏");
break;
default:
printf("选择错误,请重新选择:>");
break;
}
} while (input);
return 0;
}
5.函数的实现
代码语言:javascript复制#include "game.h"
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] = ' ';
}
}
}
//初始化棋盘
void DisplayBord(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");
}
}
void PlarerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋n");
while (1)
{
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)
{
printf("电脑下棋n");
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ');
{
board[x][y] = '#';
break;
}
}
}
static int IsFull(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 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][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i )
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (IsFull(board, row, col))
{
return 'Q';
}
return 'C';
}
到这里就结束了,希望对大家有所收获。