1、菜单页面,实现三子棋我们得先制作一个菜单界面,
输入1则进入游戏,2则退出游戏
void meat()//打印菜单
{
printf("---------------------\n");
printf("-----1.开始游戏------\n");
printf("-----2.退出游戏------\n");
printf("---------------------\n");
printf("---------------------\n");
}
2.棋盘初始化
2.用一个三行三列的二维数组 char类型,进行初始化。
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] = ' ';
}
}
}
3.打印棋盘
dispalyBoard(char board[ROW][COL], int row, int col)//打印棋盘
{
printf("+---+---+---+\n");
int i = 0;
for (i = 0; i < row; i++)
{
printf("| %c | %c | %c |\n", board[i][0], board[i][1], board[i][2]);
printf("+---+---+---+\n");
}
printf("\n");
}
打印效果
写到这里就可以开始下棋了。
4.玩家落子void playerMove(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] = 'x';
break;
}
}
else
{
printf("坐标被占用,请换位\n");
}
}
}
玩家输入行列坐标落子。
玩家要下在棋盘范围内或是空位,如果不符合要求会要求玩家重新输入!!
4.电脑下棋 srand((unsigned int)time(NULL));
void ComputerMoce(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] = 'o';
break;
}
}
}
电脑下棋利用srand((unsigned int)time(NULL));生成一个随机数(记得引用头文件),再用随机数%上3得到一个0~2的随机数,用这种方式就不用判断是否合法,只需要判断是否落子,这是最最最简易的方法,但是人机的下法非常随机非常好赢,这时候就可以给人机加入一些算法来提高人机的胜率。
5.判断输赢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];
}
}
//判断列
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];
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && 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 (ret == 'x')
{
printf("玩家赢了!\n");
}
else if (ret == 'o')
{
printf("电脑赢了!\n");
}
else
{
printf("平局\n");
}
返回x则玩家赢返回o则电脑赢。
6.判断是否平局
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;
}
7.整体代码
写到这里代码的整体思路就清晰了,接下来是整体代码。
//这里是定义的头文件以及声明 头文件名game.h
#pragma once
#include
#include
#include
#define ROW 3
#define COL 3
//初始化棋盘
InitBoard( char board[ROW][COL],int row,int col);
//打印棋盘
dispalyBoard( char board[ROW][COL], int row,int col);
//玩家下棋
void playerMove(char board[ROW][COL], int row, int col);
电脑下棋
void ComputerMoce(char board[ROW][COL], int row, int col);
//判断输赢
char Iswin(char board[ROW][COL], int row, int col);
//这里是所有的自定义函数的实现 文件名game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
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] = ' ';
}
}
}
dispalyBoard(char board[ROW][COL], int row, int col)//打印棋盘
{
printf("+---+---+---+\n");
int i = 0;
for (i = 0; i < row; i++)
{
printf("| %c | %c | %c |\n", board[i][0], board[i][1], board[i][2]);
printf("+---+---+---+\n");
}
printf("\n");
}
void playerMove(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] = 'x';
break;
}
}
else
{
printf("坐标被占用,请换位\n");
}
}
}
void ComputerMoce(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] = 'o';
break;
}
}
}
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][1] != ' ')
{
return board[i][1];
}
}
//判断列
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];
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && 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 (IsFull(board, row, col))
{
return 'Q';//平局
}
return 'c';//继续游戏
}
//这里是代码的整体逻辑 文件名为test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void meat()
{
printf("---------------------\n");
printf("-----1.开始游戏------\n");
printf("-----2.退出游戏------\n");
printf("---------------------\n");
printf("---------------------\n");
}
void game()
{
char ret = 0;
char board[ROW][COL] = {0};
InitBoard(board,ROW,COL);
dispalyBoard(board, ROW, COL);
while (1)
{
playerMove(board,ROW,COL);//玩家下棋
ret = Iswin(board,ROW,COL);//判断输赢
if (ret != 'c')
{
break;
}
dispalyBoard(board, ROW, COL);//打印棋盘
ComputerMoce(board,ROW,COL);//电脑下棋
ret = Iswin(board, ROW, COL);//判断输赢
if (ret != 'c')
{
break;
}
dispalyBoard(board, ROW, COL);//打印棋盘
}
if (ret == 'x')
{
printf("玩家赢了!\n");
}
else if (ret == 'o')
{
printf("电脑赢了!\n");
}
else
{
printf("平局\n");
}
dispalyBoard(board, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do {
meat();
printf("请选择>:");
scanf("%d", &input);
if (input == 1)
{
game();
}
else if (input == 0)
{
printf("退出游戏\n");
}
else
{
printf("请重新选择\n");
}
} while (input);
return 0;
}
8.代码效果展示
今后希望与大家共同努力,早日成为大师!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)