#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char chess[3][3] = { 0 };
int size = 9;
int winer() //判断胜利者
{
int i = 0;
for (i = 0; i < 3; i++)
{
if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][2] == 'X' || chess[0][i] == chess[1][i] && chess[1][i] == chess[2][i] && chess[2][i] == 'X' || chess[i][i] == chess[i+1][i+1] && chess[i+1][i+1] == chess[i+2][i+2] && chess[i+2][i+2] == 'X')
return 1; //玩家胜利
else if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][2] == 'O' || chess[0][i] == chess[1][i] && chess[1][i] == chess[2][i] && chess[2][i] == 'O' || chess[i][i] == chess[i + 1][i + 1] && chess[i + 1][i + 1] == chess[i + 2][i + 2] && chess[i + 2][i + 2] == 'O')
return -1; //电脑胜利
}
return 0;
}
void chess_board() //打印棋盘
{
int i = 0;
printf(" | | \n");
for (i = 0; i < 3; i++)
{
printf("_%c_|_%c_|_%c_\n",chess[i][0],chess[i][1],chess[i][2]);
}
}
void player() //玩家下棋
{
int line = 0;
int rank = 0;
scanf("%d %d", &line, &rank);
if (line>0 && line <= 3 && rank > 0 && rank <= 3)
{
chess[line - 1][rank - 1] = 'X';
size--;
}
else
{
printf("输入错误\n");
player();
}
}
void compu() //电脑下棋
{
int m = 0;
int n = 0;
srand((unsigned)time(NULL));
m = rand() % 3;
n = rand() % 3;
while (chess[m][n] != 0)
{
m = rand() % 3;
n = rand() % 3;
}
chess[m][n] = 'O';
size--;
}
int play() //游戏主体
{
int win = 0;
while (1)
{
printf("请玩家输入要下棋子坐标:\n");
player();
compu();
chess_board();
win = winer();
if (win == 1)
{
printf("玩家胜利\n");
return 0;
}
else if (win == -1)
{
printf("电脑胜利\n");
return 0;
}
else if (size < 1)
{
printf("游戏结束\n");
return 0;
}
}
return 0;
}
void menu() //菜单
{
printf("1: play\n");
printf("0: exit\n");
}
int main()
{
int n = 0;
menu();
scanf("%d", &n);
switch (n)
{
case 1:
play();
case 2:
break;
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char chess[3][3] = { 0 };
int size = 9;
int winer() //判断胜利者
{
int i = 0;
for (i = 0; i < 3; i++)
{
if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][2] == 'X' || chess[0][i] == chess[1][i] && chess[1][i] == chess[2][i] && chess[2][i] == 'X' || chess[i][i] == chess[i+1][i+1] && chess[i+1][i+1] == chess[i+2][i+2] && chess[i+2][i+2] == 'X')
return 1; //玩家胜利
else if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][2] == 'O' || chess[0][i] == chess[1][i] && chess[1][i] == chess[2][i] && chess[2][i] == 'O' || chess[i][i] == chess[i + 1][i + 1] && chess[i + 1][i + 1] == chess[i + 2][i + 2] && chess[i + 2][i + 2] == 'O')
return -1; //电脑胜利
}
return 0;
}
void chess_board() //打印棋盘
{
int i = 0;
printf(" | | \n");
for (i = 0; i < 3; i++)
{
printf("_%c_|_%c_|_%c_\n",chess[i][0],chess[i][1],chess[i][2]);
}
}
void player() //玩家下棋
{
int line = 0;
int rank = 0;
scanf("%d %d", &line, &rank);
if (line>0 && line <= 3 && rank > 0 && rank <= 3)
{
chess[line - 1][rank - 1] = 'X';
size--;
}
else
{
printf("输入错误\n");
player();
}
}
void compu() //电脑下棋
{
int m = 0;
int n = 0;
srand((unsigned)time(NULL));
m = rand() % 3;
n = rand() % 3;
while (chess[m][n] != 0)
{
m = rand() % 3;
n = rand() % 3;
}
chess[m][n] = 'O';
size--;
}
int play() //游戏主体
{
int win = 0;
while (1)
{
printf("请玩家输入要下棋子坐标:\n");
player();
compu();
chess_board();
win = winer();
if (win == 1)
{
printf("玩家胜利\n");
return 0;
}
else if (win == -1)
{
printf("电脑胜利\n");
return 0;
}
else if (size < 1)
{
printf("游戏结束\n");
return 0;
}
}
return 0;
}
void menu() //菜单
{
printf("1: play\n");
printf("0: exit\n");
}
int main()
{
int n = 0;
menu();
scanf("%d", &n);
switch (n)
{
case 1:
play();
case 2:
break;
}
system("pause");
return 0;
}