game.h
# ifndef __GAME_H__
# define __GAME_H__
# define __GAME_H__
# define ROWS 3
# define COLS 3
# define COLS 3
void InitBoard(char arr[ROWS][COLS], int row, int col);
void PrintBoard(char arr[ROWS][COLS], int row, int col);
void ComputerMove(char arr[ROWS][COLS], int row, int col);
void UsrMove(char arr[ROWS][COLS], int row, int col);
char IsWin(char arr[ROWS][COLS], int row, int col);
# endif
void PrintBoard(char arr[ROWS][COLS], int row, int col);
void ComputerMove(char arr[ROWS][COLS], int row, int col);
void UsrMove(char arr[ROWS][COLS], int row, int col);
char IsWin(char arr[ROWS][COLS], int row, int col);
# endif
game.c
# include"game.h"
void InitBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
for (i=0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}
void PrintBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
for (; i < row; i++)
{
printf(" %c | %c | %c \n",arr[i][0],arr[i][1],arr[i][2]);
if (i < row - 1)
printf("---|---|---\n");
}
}
void ComputerMove(char arr[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (arr[x][y] == ' ')
{
arr[x][y] = 'D';
break;
}
}
}
void UsrMove(char arr[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走\n");
printf("输入坐标\n");
scanf("%d %d", &x, &y);
if ((x >= 1) && (x <= 3) && (y >= 1) && (y <= 3))
{
if (arr[x-1][y-1] == ' ')
{
arr[x-1][y-1] = '*';
}
else
{
printf("此位置已被占,请重新输入\n");
}
}
else
{
printf("选择错误\n");
}
}
static int IsFull(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
return 0;
}
}
return 1;
}
char IsWin(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][1] != ' '))
return arr[i][1];
}
for (j = 0; j < col; j++)
{
if ((arr[0][j] == arr[1][j]) && (arr[1][j] == arr[2][j]) && (arr[1][j] != ' '))
return arr[i][1];
}
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && arr[1][1] != ' ')
{
return arr[1][1];
}
if ((arr[2][0] == arr[1][1])&&(arr[1][1] == arr[0][2]))
{
return arr[1][1];
}
if (IsFull)
return 'q';
return ' ';
}
{
int i = 0;
for (i=0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}
void PrintBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
for (; i < row; i++)
{
printf(" %c | %c | %c \n",arr[i][0],arr[i][1],arr[i][2]);
if (i < row - 1)
printf("---|---|---\n");
}
}
void ComputerMove(char arr[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (arr[x][y] == ' ')
{
arr[x][y] = 'D';
break;
}
}
}
void UsrMove(char arr[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走\n");
printf("输入坐标\n");
scanf("%d %d", &x, &y);
if ((x >= 1) && (x <= 3) && (y >= 1) && (y <= 3))
{
if (arr[x-1][y-1] == ' ')
{
arr[x-1][y-1] = '*';
}
else
{
printf("此位置已被占,请重新输入\n");
}
}
else
{
printf("选择错误\n");
}
}
static int IsFull(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
return 0;
}
}
return 1;
}
char IsWin(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][1] != ' '))
return arr[i][1];
}
for (j = 0; j < col; j++)
{
if ((arr[0][j] == arr[1][j]) && (arr[1][j] == arr[2][j]) && (arr[1][j] != ' '))
return arr[i][1];
}
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && arr[1][1] != ' ')
{
return arr[1][1];
}
if ((arr[2][0] == arr[1][1])&&(arr[1][1] == arr[0][2]))
{
return arr[1][1];
}
if (IsFull)
return 'q';
return ' ';
}
test.c
# define _CRT_SECURE_NO_WARNINGS 1
# include<stdio.h>
# include<stdlib.h>
# include"game.h"
# include<time.h>
# include<stdio.h>
# include<stdlib.h>
# include"game.h"
# include<time.h>
void menu()
{
printf("*************************************\n");
printf("****1.play 0.exit****\n");
printf("*************************************\n");
}
void game()
{
char Board[ROWS][COLS] = { 0 };
InitBoard(Board, ROWS, COLS);
PrintBoard(Board, ROWS, COLS);
int ret = 0;
do{
ComputerMove(Board, ROWS, COLS);
PrintBoard(Board, ROWS, COLS);
{
printf("*************************************\n");
printf("****1.play 0.exit****\n");
printf("*************************************\n");
}
void game()
{
char Board[ROWS][COLS] = { 0 };
InitBoard(Board, ROWS, COLS);
PrintBoard(Board, ROWS, COLS);
int ret = 0;
do{
ComputerMove(Board, ROWS, COLS);
PrintBoard(Board, ROWS, COLS);
ret = IsWin(Board, ROWS, COLS);
{
if(ret != ' ')
break;
}
{
if(ret != ' ')
break;
}
UsrMove(Board, ROWS, COLS);
PrintBoard(Board, ROWS, COLS);
ret = IsWin(Board, ROWS, COLS);
} while(ret == ' ');
if (ret == 'D')
printf("电脑赢\n");
else if (ret == '*')
printf("玩家赢\n");
else if (ret == 'q')
printf("平局\n");
}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do{
menu();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}
PrintBoard(Board, ROWS, COLS);
ret = IsWin(Board, ROWS, COLS);
} while(ret == ' ');
if (ret == 'D')
printf("电脑赢\n");
else if (ret == '*')
printf("玩家赢\n");
else if (ret == 'q')
printf("平局\n");
}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do{
menu();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}
结果:
![](https://i-blog.csdnimg.cn/blog_migrate/b1c60606fcf04e896762d78d2b25177a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4c060270c6a9cdc2ae11593485f0f1c2.png)
选择错误时的结果:
![](https://i-blog.csdnimg.cn/blog_migrate/f2a1d7b373feb82fc976306ad7cd6702.png)