创建目录:
![image-20230327214411876](https://i-blog.csdnimg.cn/blog_migrate/a031bda9147f3b78dbf60a0cb4f1dd4b.png)
游戏基本框架:
#include <stdio.h>
void menu() {
printf("******************************\n");
printf("********* 1. play **********\n");
printf("********* 0. exit **********\n");
printf("******************************\n");
}
void game() {
printf("三子棋\n");
}
int main() {
int input = 0;
do {
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
} while (input);
return 0;
}
![image-20230327215307503](https://i-blog.csdnimg.cn/blog_migrate/c7ae1ffb8acc6c678e002873e988c12c.png)
完整代码:
game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
void InitBoard(char borad[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 CheckWin(char board[ROW][COL], int row, int col);
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu() {
printf("******************************\n");
printf("********* 1. play **********\n");
printf("********* 0. exit **********\n");
printf("******************************\n");
}
void game() {
char ret = 0;
char board[ROW][COL];
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1) {
PlayerMove(board, ROW, COL);
ret = CheckWin(board, ROW, COL);
if (ret != 'C') {
break;
}
DisplayBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
ret = CheckWin(board, ROW, COL);
if (ret != 'C') {
break;
}
DisplayBoard(board, ROW, COL);
}
if (ret == '*') {
printf("玩家赢了\n");
}
else if (ret == '#') {
printf("电脑赢了\n");
}
else if (ret == 'Q') {
printf("平局\n");
}
DisplayBoard(board, ROW, COL);
}
int main() {
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
} while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#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 DisplayBoard(char board[ROW][COL], int row, int col) {
int i = 0;
for (i = 0; i < row; i++) {
int j = 0;
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 PlayerMove(char board[ROW][COL], int row, int col) {
int x = 0;
int y = 0;
printf("玩家走:>");
while (1) {
printf("请输入一个坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (board[x-1][y-1] != ' ') {
printf("坐标被占用,请重新输入!\n");
}
else {
board[x-1][y-1] = '*';
break;
}
}
else {
printf("坐标非法,请重新输入!\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col) {
printf("电脑走:>\n");
while (1) {
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ') {
board[x][y] = '#';
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 CheckWin(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];
}
}
int j = 0;
for (j = 0; j < col; j++) {
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') {
return board[0][j];
}
}
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[0][2] != ' ') {
return board[0][2];
}
if (isFull(board, row, col) == 1) {
return 'Q';
}
return 'C';
}
效果:
![image-20230327230603958](https://i-blog.csdnimg.cn/blog_migrate/b283340d9d1e7f32939794015932267f.png)
![image-20230327230637530](https://i-blog.csdnimg.cn/blog_migrate/f29065f92b6654311cbca3d362fa55d3.png)
![image-20230327230651272](https://i-blog.csdnimg.cn/blog_migrate/8ac4fa717f35b8013341e5b315260f0a.png)
![image-20230327230658345](https://i-blog.csdnimg.cn/blog_migrate/fb97b2708e04a34ec5d266bbdcfbf484.png)