设置了头文件
Game.H
#ifndef GAME_H
#define GAME_H
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROWS 3
#define LISTS 3
void game();
void pane(char arr[ROWS][LISTS], int rows, int lists);
void init(char arr[ROWS][LISTS], int rows, int lists);
void player_go(char arr[ROWS][LISTS], int rows, int lists);
void computer_go(char arr[ROWS][LISTS], int rows, int lists);
char check_win(char arr[ROWS][LISTS], int rows, int lists);
#endif //GAME_H
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void test()
{
int input = 0;
do
{
srand((unsigned int)time(NULL));
void screen();
void game();
screen();
printf("请选择:>\n");
scanf("%d", &input);
switch (input)
{
case 0: break;
case 1:game(); break;
default:printf("输入错误\n"); break;
}
} while (input);
}
void screen()
{
printf("---------------------------\n");
printf("----1.play----0.exit--------\n");
printf("---------------------------\n");
}
int main()
{
test();
return 0;
}
Game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
char w = 0;
char arr[ROWS][LISTS] = { 0 };
init(arr, ROWS, LISTS);
pane(arr, ROWS, LISTS);
while (1)
{
player_go(arr, ROWS, LISTS);
pane(arr, ROWS, LISTS);
w = check_win(arr, ROWS, LISTS);
if (w == '#')
{
printf("平局\n");
break;
}
computer_go(arr, ROWS, LISTS);
pane(arr, ROWS, LISTS);
w = check_win(arr, ROWS, LISTS);
if (w == '*')
{
printf("玩家赢\n");
break;
}
if (w == 'I')
{
printf("电脑赢\n");
break;
}
}
}
void player_go(char arr[ROWS][LISTS], int rows, int lists)
{
int i = 0;
int j = 0;
printf("玩家走:\n");
printf("请输入坐标:>");
flag:
scanf("%d,%d", &i, &j);
if (i >= 0 && i < (rows + 1) && j >= 0 && j < (lists + 1))
{
if (arr[i - 1][j - 1] == ' ')
arr[i - 1][j - 1] = '*';
else
{
printf("该位置被占用,请重新输入:>");
goto flag;
}
}
else
{
printf("输入错误,请重新输入:>");
goto flag;
}
}
void computer_go(char arr[ROWS][LISTS], int rows, int lists)
{
int x = 0;
int y = 0;
printf("电脑走:\n");
while (1)
{
x = rand() % rows;
y = rand() % lists;
if (arr[x][y] == ' ')
{
arr[x][y] = 'I';
break;
}
}
}
char check_win(char arr[ROWS][LISTS], int rows, int lists)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
if (arr[i][0] == arr[i][1] && arr[i][2] == arr[i][1] && (arr[i][1] != ' '))
return arr[i][0];
else if (arr[0][i] == arr[1][i] && arr[2][i] == arr[1][i] && (arr[1][i] != ' '))
return arr[0][i];
}
if (arr[0][0] == arr[1][1] && arr[2][2] == arr[1][1] && (arr[1][1] != ' '))
return arr[0][0];
if (arr[0][2] == arr[2][0] && arr[1][1] == arr[0][2] && (arr[1][1] != ' '))
return arr[0][2];
{
i = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < lists; j++)
{
if (arr[i][j] == ' ')
goto flag;
}
}
if (i == rows)
return '#';
}
flag:
return ' ';
}
void init(char arr[ROWS][LISTS], int rows, int lists)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < lists; j++)
arr[i][j] = ' ';
}
}
void pane(char arr[ROWS][LISTS], int rows, int lists)
{
int i = 0;
for (i = 0; i < rows; i++)
{
if (i == 0)
printf("----------------\n");
printf("| %c | %c | %c |\n", arr[i][0], arr[i][1], arr[i][2]);
printf("----------------\n");
}
}