设计思路:

—一。主体函数的设计:

设计一个菜单函数,通过选择判定是否选择进入游戏,还是退出游戏
int main() { int input = 0; srand((unsigned)time(NULL));//用时间戳完成随机数 do {
menu(); printf("请选择>:"); scanf_s("%d", &input); switch (input) { case 1:
game(); break; case 0: break; default: printf("选择错误,请重新选择\n"); } } while
(input); return 0; } void menu() { printf("**************************\n");
printf("****1.玩 0.退出游戏****\n"); printf("**************************\n"); }
二.game()函数的设计

1.棋子的储存需要一个二维数组,board[3][3]

2.然后设计一个Intboard()函数将数组初始化为空格

3.设计一个 Display()用来打印棋盘

4.设计一个PlayMove()函数,让玩家可以下棋,还要设计一个IS_winer()来判断玩家是否胜利

5.设计一个ComputerMove()函数,让电脑下棋,同样用IS_winer()函数来判断电脑是否胜利
void game() { char ret = 0; char board[ROW][COL] = { 0 };//初始化数组,全是空格
Intboard(board, ROW, COL); Display(board, ROW, COL); while (1) {
PlayMove(board,ROW,COL); Display(board, ROW, COL); ret=IS_winer(board, ROW,
COL); if (ret !='c') { break; } ComputerMove(board,ROW,COL); Display(board,
ROW, COL); ret=IS_winer(board, ROW, COL); if (ret != 'c') { break; } } if (ret
== '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else {
printf("平局\n"); } }
三。各种函数的实现

初始化函数
void Intboard(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] = ' '; } } }
Display函数
void Display(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("|"); } else { break; } } printf("\n"); if (i < row -
1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|");
} else { break; } } printf("\n"); } } }
PlayMove函数
void PlayMove(char board[ROW][COL], int row, int col) { int x=0; int y=0;
while (1) { printf("玩家走>:\n"); printf("请输入你想要走的坐标>:"); scanf_s("%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"); } } }
ComputerMove函数
void ComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y =
0; while (1) { printf("电脑走>:\n"); x = rand() % row; y = rand() % col; if
(board[x][y] == ' ') { board[x - 1][y - 1] = '#'; break; } } }
    char IS_winer(char board[ROW][COL], int row, int col)函数
char IS_winer(char board[ROW][COL], int row, int col) { int i = 0; int j = 0;
for (i = 0; i < row; i++) { if (board[i][1] == board[i][2] && board[i][3] ==
board[i][2] && board[i][1] != ' ') { return board[i][1]; } } for (i = 0; i <
col; i++) { if (board[1][i] == board[2][i] && board[3][i] == board[1][i] &&
board[1][i] != ' ') { return board[1][i]; } } if (board[0][0] == board[1][1] &&
board[2][2] == board[0][0] && board[0][0] != ' ') { return board[0][0]; } if
(board[0][2] == board[1][1] && board[2][0] == board[1][1] && board[0][2] != '
') { return board[0][2]; } if (1 == IsFull(board, ROW, COL)) { return 'q'; }
else { return 'c'; } }
 IsFull函数
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;//满了 }
然后就是我设计的game.h文件
#pragma once #define ROW 3 #define COL 3 #include<stdio.h> #include<stdlib.h>
#include<time.h> void Intboard(char board[ROW][COL], int row, int col); void
Display(char board[ROW][COL], int row, int col); void PlayMove(char
board[ROW][COL], int row, int col); void ComputerMove(char board[ROW][COL], int
row, int col); char IS_winer(char board[ROW][COL], int row, int col);

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信