C语言实现三子棋
C语言实现三子棋要从以下几个方面进行考虑:
- 打印菜单界面
- 打印棋盘
- 玩家下棋
- 电脑下棋
- 判断输赢
这次我们仍然采用分文件进行书写,建立三个文件,test.c, game.c,game.h。
test.c用来实现程序的主体
game.c 用来对主体函数进行封装
game.h存放头文件,行数和列数以及对函数的定义
test.c和game.h的代码如下:
//test.c
#include"game.h"
void menu()
{
printf("\t\t 三子棋小游戏\n" );
printf( "\t\t ---------------------\n");
printf( "\t\t|\t1.play\t |\n");
printf( "\t\t|\t0.exit\t |\n");
printf("\t\t ---------------------\n");
}
void game()
{
char board[ROW][LOW] = { 0 };
char ret = 0;
InBoard(board, ROW, LOW);//初始化数组
Print_board(board, ROW, LOW);//打印棋盘
while (1)
{
Player_Move(board, ROW, LOW);//玩家走,用*号表示
Print_board(board, ROW, LOW);//电脑走,用#号表示
ret = Is_Winner(board, ROW, LOW);//判断游戏是否继续
if (ret != 'c')
{
break;
}
Computer_Move(board, ROW, LOW);
Print_board(board, ROW, LOW);
ret = Is_Winner(board, ROW, LOW);
if (ret != 'c')
{
break;
}
}
//判断输赢
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入您要实现的功能:>\n");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
//game.h
#pragma once
#include<stdio.h>
#include<time.h>
#define ROW 3
#define LOW 3
void InBoard(char board[ROW][LOW], int row, int low);
void Print_board(char board[ROW][LOW], int row, int low);
void Player_Move(char board[ROW][LOW], int row, int low);
void Computer_Move(char board[ROW][LOW], int row,int low);
char Is_Winner(char board[ROW][LOW], int row, int low);
//# 电脑赢
//*玩家赢
//c 代表还要继续
//Q 代表平局
在程序的主体部分定义一个3*3的数组,用来存放玩家和电脑所走的内容,将其初始化为空格;然后定义函数实现三子棋基础功能。
接下来依次分析各个函数的书写:
1.打印开始菜单
定义一个menu函数,里面用最基础的printf函数打印菜单界面,输入1代表玩游戏,输入0代表退出游戏。
void menu()
{
printf("\t\t 三子棋小游戏\n" );
printf( "\t\t ---------------------\n");
printf( "\t\t|\t1.play\t |\n");
printf( "\t\t|\t2.exit\t |\n");
printf("\t\t ---------------------\n");
}
显示效果如下:
2.打印棋盘界面
定义Print_board(board, ROW, LOW)函数,传入存放所走内容的数组,行数及列数
```c
```c
void Print_board(char board[ROW][LOW], int row, int low)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < low; j++)
{
printf("%c ",board[i][j]);
if (j < low - 1)
printf(" |");
}
printf("\n");
for (int j = 0; j < low; j++)
{
printf("---");
if (j < low - 1)
printf("|");
}
printf("\n");
}
}
显示效果如下:
3.玩家下棋
定义Player_Move函数实现玩家下棋的功能
void Player_Move(char board[ROW][LOW], int row, int low)
{
int x = 0, y = 0;
printf("玩家走:>\n");
while (1)
{
printf("请输入要下的坐标:>");
scanf_s("%d%d", &x, &y);
//判断x,y坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <=low)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标已被占用\n");
}
}
else
{
printf("坐标非法,请重新输入!\n");
}
}
}
4.电脑下棋
void Computer_Move(char board[ROW][LOW], int row, int low)
{
int x = 0;
int y = 0;
while (1)
{
printf("电脑走\n");
x = rand() % row;
y = rand() % low;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
5.判断胜负
char Is_Winner(char board[ROW][LOW], int row, int low)
{
//判断一行元素是否相等
for (int i = 0; i < low; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != ' ')
{
return board[i][0];
}
}
//判断一列元素是否相等
for (int i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != ' ')
{
return board[0][i];
}
}
//判断对角线是否相等
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')
{
return board[1][1];
}
//判断棋盘是否走满,为走满利用Is_Full函数返回0,然后返回c代表游戏继续,否则返回Q代表平局;
int ret=Is_Full(board, ROW, LOW);
if (ret == 0)
{
return 'c';
}
if (ret == 1)
{
return 'Q';
}
}
game.c源文件如下
#include"game.h"
void InBoard(char board[ROW][LOW], int row, int low)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < low; j++)
{
board[i][j] = ' ';
}
}
}
void Print_board(char board[ROW][LOW], int row, int low)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < low; j++)
{
printf("%c ",board[i][j]);
if (j < low - 1)
printf(" |");
}
printf("\n");
for (int j = 0; j < low; j++)
{
printf("---");
if (j < low - 1)
printf("|");
}
printf("\n");
}
}
void Player_Move(char board[ROW][LOW], int row, int low)
{
int x = 0, y = 0;
printf("玩家走:>\n");
while (1)
{
printf("请输入要下的坐标:>");
scanf_s("%d%d", &x, &y);
//判断x,y坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <=low)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标已被占用\n");
}
}
else
{
printf("坐标非法,请重新输入!\n");
}
}
}
void Computer_Move(char board[ROW][LOW], int row, int low)
{
int x = 0;
int y = 0;
while (1)
{
printf("电脑走\n");
x = rand() % row;
y = rand() % low;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int Is_Full(char board[ROW][LOW], int row, int low)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < low; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char Is_Winner(char board[ROW][LOW], int row, int low)
{
//判断一行元素是否相等
for (int i = 0; i < low; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != ' ')
{
return board[i][0];
}
}
//判断一列元素是否相等
for (int i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != ' ')
{
return board[0][i];
}
}
//判断对角线是否相等
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')
{
return board[1][1];
}
//判断棋盘是否走满,为走满利用Is_Full函数返回0,然后返回c代表游戏继续,否则返回Q代表平局;
int ret=Is_Full(board, ROW, LOW);
if (ret == 0)
{
return 'c';
}
if (ret == 1)
{
return 'Q';
}
}
整个代码实现效果如下:
以上便是C语言实现三子棋的全部内容了,如果你喜欢我的文章请多多评论,转发。