三子棋程序完美版

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAX_ROW 3
#define MAX_COL 3
int menu()
{
printf("###############\n");
printf(“欢迎来到东东三子棋\n”);
printf(“1.游戏开始\n”);
printf(“0.退出游戏\n”);
printf("###############\n");
int choice = 0;
printf(“请输入您的选择: “);
scanf(”%d”, &choice);
return choice;
}
void Init(char cheesBoard[MAX_ROW][MAX_COL])
{
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
cheesBoard[row][col] = ’ ‘;
}
}
}
void Show_cheesBoard(char cheesBoard[MAX_ROW][MAX_COL])
{
printf(“±–±--±–+\n”);
for (int row = 0; row < MAX_ROW; row++)
{
printf("| %c | %c | %c |\n", cheesBoard[row][0], cheesBoard[row][1], cheesBoard[row][2]);
printf(“±–±--±–+\n”);
}
}
void Player_Move(char cheesBoard[MAX_ROW][MAX_COL])
{
//提示玩家输入棋子的坐标
//判断玩家输入的坐标是否合法(不能越出数组界限,玩家落子的地方不能有子)
//若输入合法则将该坐标位置的元素设置为’x’
//若不合法重新执行
while (1)
{
printf(“请输入你所要落子的坐标: “);
int row = 0;
int col = 0;
scanf(”%d %d”, &row, &col);
if (row > MAX_ROW-1 || col > MAX_COL-1 || row < 0 || col < 0)
{
printf(“你的输入有误,请重新输入\n”);
continue;
}
if (cheesBoard[row][col] != ’ ')
{
printf(“此地已经有子,请重新输入\n”);
continue;
}
cheesBoard [row][col]= ‘x’;
break;
}
}
int Last_chees(char cheesBoard[MAX_ROW][MAX_COL])
{
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
if (cheesBoard[row][col] == ’ ')
return 0;
}
}
return 1;
}
char Check_cheesBoard(char cheesBoard[MAX_ROW][MAX_COL])
{
//横三子成线,游戏结束
//竖着三子成线,游戏结束
//对角线三子成线,游戏结束
//地图无空位,游戏结束
for (int row = 0; row < MAX_ROW; row++)
{
if (cheesBoard[row][0] != ’ '&&cheesBoard[row][0] == cheesBoard[row][1] && cheesBoard[row][0] == cheesBoard[row][2])
{
return cheesBoard[row][0];
break;
}
}
for (int col = 0; col < MAX_COL; col++)
{
if (cheesBoard[0][col] != ’ '&&cheesBoard[0][col] == cheesBoard[1][col] && cheesBoard[0][col] == cheesBoard[2][col])
{
return cheesBoard[0][col];
break;
}
}
if (cheesBoard[0][0] != ’ '&&cheesBoard[0][0] == cheesBoard[1][1] && cheesBoard[0][0] == cheesBoard[2][2])
{
return cheesBoard[0][0];
}
if (cheesBoard[0][2] != ’ '&&cheesBoard[0][2] == cheesBoard[1][1] && cheesBoard[0][2] == cheesBoard[2][0])
{
return cheesBoard[2][0];
}
if (Last_chees(cheesBoard))
{
return ‘q’;
}
return ’ ‘;
}
void Computer_Move(char cheesBoard[MAX_ROW][MAX_COL])
{
//提示电脑落子
//电脑输入的两个坐标数字都为随机数
//判断电脑输入是否合法(上边有没有子)
//若果合法那么在次坐标上填上’o’
//若果不合法,重复操作
printf(“电脑落子\n”);
while (1)
{
int row = rand() % MAX_ROW;
int col = rand() % MAX_COL;
if (cheesBoard[row][col] != ’ ')
{
continue;
}
cheesBoard[row][col] = ‘o’;
break;
}
}
void Game()
{
srand((unsigned int)time(NULL));
//建立并且初始化地图
char cheesBoard[MAX_ROW][MAX_COL] = {0};
Init(cheesBoard);
char winner = ’ ';
//打印地图
//玩家落子顺便判断游戏是否结束
//电脑落子并判断游戏是否结束
//重复操作
while (1)
{

	Show_cheesBoard(cheesBoard);
	Player_Move(cheesBoard);
    winner=Check_cheesBoard(cheesBoard);
	if (winner != ' ')
	{
		break;
	}
	Computer_Move(cheesBoard);
	winner = Check_cheesBoard(cheesBoard);
	if (winner != ' ')
	{
		break;
	}
}
if (winner == 'x')
{
	Show_cheesBoard(cheesBoard);
	printf("恭喜你!!!!!!,你赢了\n");
}
if (winner == 'o')
{
	Show_cheesBoard(cheesBoard);
	printf("你是真的菜,连电脑都下不过!\n");
}
if (winner == 'q')
{
	Show_cheesBoard(cheesBoard);
	printf("你够菜的,和电脑五五开!\n");
}

}
int main()
{
while (1)
{
int choice = menu();
if (choice == 1)
{
Game();
}
else if (choice == 0)
{
printf(“Good Bey!!!”);
break;
}
else
{
printf(“您的输入有误,please重新输入”);
continue;
}
}
system(“pause”);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值