1.进入菜单函数,出现提示,输入1进入游戏,输入0退出游戏,输入其他数提示错误,可以再次输入;
void menu()
{
printf("###1.enter##0.exit###\n");
}
2.输入1,进入游戏函数;建立一个3*3的数组,进入初始化函数;通过两层while循环,将元素都设置为空格;
void chushihua(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.进入展示棋盘函数,将数组里的元素与 | 依次打印,并设置条件,每行打印完第三个元素后不打印 | ,然后换行,---与 | 间隔打印,并设置条件,最后一行打印完后不打印---,最后打印出完整棋盘;
void dispaly(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)
{
int j = 0;
for (j = 0;j < col;j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
4.设置判断赢的返回值,进入while循环,进入玩家函数,输入行列变量,设置变量范围,范围里的i,j可以进入函数;判断对应位置是否空白,空白则给他赋值'*';不空白就出现 已占用 三字,返回输入函数的部分,重新输入 i j,输入完毕,再次出现打印棋盘函数,可以看到棋盘的内容;
while(1)
{
player(board, ROW, COL);
dispaly(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'e')
{
break;
}
computer(board, ROW, COL);
dispaly(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'e')
{
break;
}
}
void player(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
hehe:
scanf("%d %d", &i, &j);
if (1<=i&&i<= row && j>=1 &&j <= col )
{
if (' ' == board[i - 1][j - 1])
{
board[i - 1][j - 1] = '*';
}
else
{
printf("已占用\n");
goto hehe;
}
}
}
5.进入iswin函数,用户来判断是否胜利,如果满足三子棋获胜的条件(行 列 对角线 一样,但不是空格)则返回其中某一元素;判断完毕,进入判断是否下满的函数,检查数组里有没有空格,有的话返回0,否则返回1,设置接收返回值,进入if函数,如果返回1,则返回 f ,否则,返回 e 。
char iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
int ret = 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];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][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[0][2] != ' ')
{
return board[0][0];
}
}
ret = isfull(board, ROW, COL);
if (1==isfull)
{
return 'f';
}
else
{
return 'e';
}
}
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;
}
6.回到game函数,判断iswin函数的返回值,判断是否为空,如果返回值是 * 或#或 f,就跳出函数,根据返回的字符来判断谁胜利或者平局;
if ('*' == ret)
{
printf("玩家赢\n");
}
else if ('#' == ret)
{
printf("电脑赢\n");
}
else if ('f' == ret)
{
printf("平局\n");
}
7.没有返回结束游戏的字符则进入电脑的下棋回合,利用产生的随机数与3取模,得到0 1 2,给电脑的i j赋上 # ,进入打印棋盘函数,显示出现在的棋盘,再进入判断函数,来看出谁赢;
void computer(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
haha:
i = rand() % row;
j = rand() % col;
if (' ' == board[i][j])
{
board[i][j] = '#';
}
else
{
goto haha;
}
}
8.完整代码;
声明函数的h文件:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void menu();
void game();
void chushihua(char board[ROW][COL], int i,int j);
void dispaly(char board[ROW][COL], int i, int j);
void player(char board[ROW][COL], int i, int j);
void computer(char board[ROW][COL], int i,int j);
char iswin(char board[ROW][COL], int i, int j);
int isfull(char board[ROW][COL], int i, int j);
函数实现的c文件:
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include"声明.h"
void menu()
{
printf("###1.enter##0.exit###\n");
}
void chushihua(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 dispaly(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)
{
int j = 0;
for (j = 0;j < col;j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
void player(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
hehe:
scanf("%d %d", &i, &j);
if (1<=i&&i<= row && j>=1 &&j <= col )
{
if (' ' == board[i - 1][j - 1])
{
board[i - 1][j - 1] = '*';
}
else
{
printf("已占用\n");
goto hehe;
}
}
}
void computer(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
haha:
i = rand() % row;
j = rand() % col;
if (' ' == board[i][j])
{
board[i][j] = '#';
}
else
{
goto haha;
}
}
char iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
int ret = 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];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][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[0][2] != ' ')
{
return board[0][0];
}
}
ret = isfull(board, ROW, COL);
if (1==isfull)
{
return 'f';
}
else
{
return 'e';
}
}
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;
}
函数调用的c文件:
#include"声明.h"
void game()
{
char board[ROW][COL] = { 0 };
chushihua(board, ROW, COL);
dispaly(board, ROW, COL);
char ret = 0;
while (1)
{
player(board, ROW, COL);
dispaly(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'e')
{
break;
}
computer(board, ROW, COL);
dispaly(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'e')
{
break;
}
}
if ('*' == ret)
{
printf("玩家赢\n");
}
else if ('#' == ret)
{
printf("电脑赢\n");
}
else if ('f' == ret)
{
printf("平局\n");
}
}
int main()
{
menu();
srand((unsigned int)time(NULL));
printf("请输入:> ");//数组接受不可以省略列;
int n = 0;//一般放在外面,放在do while里while用不了;
do
{
scanf("%d", &n);
switch (n)
{
case 1:
printf("进入游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (n);
return 0;
}
9.运行结果;
![](https://i-blog.csdnimg.cn/blog_migrate/c87200bdecb5597293ba4dd8d94684a3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c00aee8af79ac2a30bdd89b250bd7b0b.png)