用c语言,做一个三子棋(井字棋)游戏,实现用户与电脑之间的博弈,并自动判断棋局情况:用户赢、电脑赢、平局。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#define Row 3 //定义棋盘横行数
#define Col 3 //定义棋盘纵行数
int i, j;
//打印棋局情况
void grade(int ret)
{
if (ret == 1)
{
printf("恭喜,你获得了胜利!!!\n");
}
else if (ret == 2)
{
printf("差一点,下次再努力一点吧!!!\n");
}
else if (ret == 4)
{
printf("平局了,加油,下次一定要赢!!!\n");
}
}
//判断棋局情况
int Iswin(char board[Row][Col])
{
//判断横行输赢情况
for (i = 0; i < Row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
if (board[i][0] == '#')
return 1;//用户赢
else if (board[i][0] == '*')
return 2;//电脑赢
}
}
//判断纵行输赢情况
for (i = 0; i < Col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
if (board[0][i] == '#')
return 1;
else if (board[0][i] == '*')
return 2;
}
}
//判断对角线输赢情况
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
if (board[0][0] == '#')
return 1;
else if (board[0][0] == '*')
return 2;
}
//判断对角线输赢情况
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')
{
if (board[0][2] == '#')
return 1;
else if (board[0][2] == '*')
return 2;
}
//判断是否还能继续走棋
for (i = 0; i < Row; i++)
for (j = 0; j < Col; j++)
if (board[i][j] == ' ')
return 3;
//平局
return 4;
}
//菜单
void menu()
{
printf("******************************\n");
printf("*** 1 -> play 0 -> quit ***\n");
printf("******************************\n");
}
//棋盘初始化
void InitBoard(char board[Row][Col])
{
for (i = 0; i < Row; i++)
{
for (j = 0; j < Col; j++)
{
board[i][j] = ' ';
}
}
}
//打印表盘
void Display(char board[Row][Col])
{
for (i = 0; i < Row; i++)
{
printf("+---+---+---+\n");
for (j = 0; j < Col; j++)
{
printf("|");
printf(" %c ", board[i][j]);
}
printf("|\n");
}
printf("+---+---+---+\n");
}
//玩家走棋
void Playermove(char board[Row][Col])
{
while (1)
{
int x, y;
printf("玩家走棋:\n");
//用户输入坐标
printf("请输入横坐标:");
scanf("%d", &x);
printf("请输入纵坐标:");
scanf("%d", &y);
system("cls");
if (x < 0 || x>2)
{
printf("横坐标违法,请重新输入\n");
continue;
}
if (y < 0 || y>2)
{
printf("纵坐标违法,请重新输入\n");
continue;
}
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
else
{
printf("该坐标已被占领,请重新输入\n");
continue;
}
}
}
//电脑走棋
void Computed(char board[Row][Col])
{
int x, y;
printf("电脑走棋:\n");
system("cls");
while (1)
{
//产生位于[0,2]的随机数
x = rand() % Row;
y = rand() % Col;
//判断范围
if (x >= 0 && x <= 2 && y >= 0 && y <= 2)
{
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
}
}
}
//谁先下
int Xianshou()
{
printf("******************************\n");
printf("*** 1 -> user 2 -> phone ***\n");
printf("******************************\n");
int Oneinput;
do
{
printf("请选择:>");
scanf("%d", &Oneinput);
system("cls");
switch (Oneinput)
{
//用户先下
case 1:
return 1;
break;
case 2:
//电脑先下
return 2;
break;
default:
printf("输入错误,请重新输入\n");
Sleep(1000);
system("cls");
break;
}
} while (Oneinput);
}
//开始游戏
void game()
{
char board[Row][Col] = { 0 };//定义一个 3×3 的棋盘
InitBoard(board);//初始化棋盘
int result = Xianshou();//用户选择谁先走棋
Display(board);//打印棋盘
int ret;
//用户先走棋
if (result == 1)
{
while (1)
{
//Playermove - 用户走棋
//Computed - 电脑走棋
//Iswin - 判断棋局情况:1-用户赢 2-电脑赢 4-平局 3-需要继续下棋
Playermove(board);
Display(board);//打印棋盘
ret = Iswin(board);
if (ret != 3)
break;
Computed(board);
Display(board);
ret = Iswin(board);
if (ret != 3)
break;
}
grade(ret);
}
//电脑先走棋
if (result == 2)
{
while (1)
{
Computed(board);
Display(board);
ret = Iswin(board);
if (ret != 3)
break;
Playermove(board);
Display(board);
ret = Iswin(board);
if (ret != 3)
break;
}
grade(ret);
}
}
//测试
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();//菜单
printf("请输入:>");
scanf("%d", &input);//1-开始游戏 0-退出游戏
system("cls");
switch (input)
{
case 1:
game();//开始游戏
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择");
Sleep(1000);
system("cls");
break;
}
} while (input);
}
int main()
{
//进入游戏
test();
return 0;
}