#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int Manu() {
printf("==============\n");
printf("欢迎来到三子棋游戏!\n");
printf("1. 开始游戏\n");
printf("2. 结束游戏\n");
printf("==============\n");
printf("请输入您的选择:");
int choice = 0;
scanf("%d", &choice);
return choice;
}
#define MAX_ROW 3
#define MAX_COL 3
void Init(char chessBoard[MAX_ROW][MAX_COL]) {
memset(chessBoard, ' ', MAX_ROW * MAX_COL);
//三个参数,第一个是要改的数组;第二个是要改成的字符;第三个是要改的字节数
//要注意的是memset 函数是按照字节数改
}
void Print(char chessBoard[MAX_ROW][MAX_COL]) {
printf("+---+---+---+\n");
for (int row = 0; row < MAX_ROW; row++) {
printf("| %c | %c | %c |\n",chessBoard[row][0],
chessBoard[row][1], chessBoard[row][2]);
printf("+---+---+---+\n");
}
}
void playerMove(char chessBorad[MAX_ROW][MAX_COL]) {
while (1) {
printf("请输入您要落子的坐标:");
int row = 0;
int col = 0;
scanf("%d%d", &row, &col);
//要判断用户输入的坐标是否合法,合法则落子,不合法则重新输入
if (row < 0 || row > MAX_ROW || col < 0 || col >MAX_COL) {
printf("您输入的坐标有误,请重新输入:\n");
continue;
}
else if (chessBorad[row][col] != ' ') {
printf("该坐标已有子,请重新输入:");
continue;
}
else {
chessBorad[row][col] = 'X';
break;
}
}
}
void computerMove(char chessBoard[MAX_ROW][MAX_COL]) {
//电脑落子是随机生成的,这里采用随机数来实现
while (1) {
int row = rand() % MAX_ROW;
int col = rand() % MAX_COL;
if (chessBoard[row][col] != ' ') {
continue;
}
else {
chessBoard[row][col] = 'O';
break;
}
}
}
//我们约定,返回'X'代表玩家获胜
//返回'O'代表电脑获胜
//返回'Q'代表和棋
char Cheak(char chessBoard[MAX_ROW][MAX_COL]) {
//检查所有行
for (int row = 0; row < MAX_ROW; row++) {
if (chessBoard[row][0] != ' '
&& chessBoard[row][0] == chessBoard[row][1]
&& chessBoard[row][0] == chessBoard[row][2]) {
return chessBoard[row][0];
}
}
//检查所有的列
for(int col = 0; col < MAX_COL; col++) {
if (chessBoard[0][col] != ' '
&& chessBoard[0][col] == chessBoard[1][col]
&& chessBoard[0][col] == chessBoard[2][col]) {
return chessBoard[0][col];
}
}
//检查斜线
if (chessBoard[0][0] != ' '
&& chessBoard[0][0] == chessBoard[1][1]
&& chessBoard[0][0] == chessBoard[2][2]) {
return chessBoard[0][0];
}
if (chessBoard[0][2] != ' '
&& chessBoard[0][2] == chessBoard[1][1]
&& chessBoard[0][2] == chessBoard[2][0]) {
return chessBoard[0][2];
}
//检查是否和棋
int count = 0;
for (int row = 0; row < MAX_ROW; row++) {
for (int col = 0; col < MAX_COL; col++) {
if (chessBoard[row][col] != ' ') {
count++;
}
}
}
if (count == 9) {
return 'Q';
}
}
void Game(char chessBoard[MAX_ROW][MAX_COL]) {
//1.对棋盘进行初始化,也就是初始化一个二元数组
Init(chessBoard);
printf("\n");
printf("举例输入坐标格式为:0 0\n");
while (1) {
//2.打印棋盘
Print(chessBoard);
//3.提示玩家落子,让玩家输入坐标
playerMove(chessBoard);
//4.检查游戏是否结束
if (Cheak(chessBoard) == 'X') {
Print(chessBoard);
printf("恭喜你,你赢了!\n");
break;
}
if (Cheak(chessBoard) == 'O') {
Print(chessBoard);
printf("你真菜,连电脑都下不过!\n");
break;
}
if (Cheak(chessBoard) == 'Q') {
Print(chessBoard);
printf("你真菜,和电脑五五开!\n");
break;
}
//5.电脑进行落子(随机落)
computerMove(chessBoard);
//6.检查游戏是否结束
if (Cheak(chessBoard) == 'X') {
Print(chessBoard);
printf("恭喜你,你赢了!\n");
break;
}
if (Cheak(chessBoard) == 'O') {
Print(chessBoard);
printf("你真菜,连电脑都下不过!\n");
break;
}
if (Cheak(chessBoard) == 'Q') {
Print(chessBoard);
printf("你真菜,和电脑五五开!\n");
break;
}
//7.如果两次都没结束,继续回到2.开始执行
}
}
int main() {
char chessBoard[MAX_ROW][MAX_COL] = { 0 };
srand((unsigned int)time(NULL));
while (1) {
int choice = Manu();
if (choice == 1) {
Game(chessBoard);
}
else if (choice == 0) {
printf("goodbye!\n");
return 0;
}
else {
printf("您的输入有误,请重新输入!\n");
continue;
}
printf("\n");
}
system("pause");
return 0;
}
三子棋
最新推荐文章于 2024-04-19 15:41:06 发布