三子棋程序

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAX_ROW 3
#define MAX_COW 3
#define MAX_SIZE 3
int menu()
{
printf("=\n");
printf(“欢迎来到东东三子棋\n”);
printf(“1.游戏开始\n”);
printf(“0.退出游戏\n”);
printf("
=\n");
printf(“请输入您的选择:”);
int choice = 0;
scanf("%d", &choice);
return choice;
}
void Init(char cheesBoard[MAX_ROW][MAX_COW])
{
for (int row = 0; row < MAX_ROW; row++)
{
for (int cow = 0; cow < MAX_COW; cow++)
{
cheesBoard[row][cow] = ’ ';
}
}
}
void Print_Map(char cheesBoard[MAX_ROW][MAX_COW])
{
int row = 0;
printf(“±–±--±–+\n”);
for (row = 0; row < MAX_ROW; row++)
{
printf("| %c | %c | %c |\n", cheesBoard[row][0], cheesBoard[row][1], cheesBoard[row][2]);
printf(“±–±--±–+\n”);
}
}
void Play_Move(char cheesBoard[MAX_ROW][MAX_COW])
{
//提示玩家输入一个坐标
//判断玩家输入的坐标是否合法(1.判断是否越界2.判断玩家所输入的地是否有子)
//若果玩家输入非法,就循环进入下一次操作
//若果玩家输入合法,就把棋盘上的对应坐标设置为X
while (1)
{
int row = -1;
int cow = -1;
printf(“请输入您所要落子的坐标: “);
scanf(”%d %d”, &row, &cow);
if (row < 0 || row >= 3 || cow < 0 || cow >= 3)
{
printf(“您的输入有误\n”);
continue;
}
if (cheesBoard[row][cow] != ’ ')
{
printf(“您所输入的这个位置都已经有字了!请重新输入\n”);
continue;
}
cheesBoard[row][cow] = ‘x’;
break;
}
}
void Computer_Move(char cheesBoard[MAX_ROW][MAX_COW])
{
//1.请电脑落子
//2.电脑落子的坐标是一个行和列都是随机数的坐标
//3.若果产生的随机数上边有子那么就再重新产生一个随机数
printf(“电脑落子\n”);
int row = 0;
int cow = 0;
while (1)
{
row = rand() % MAX_ROW;
cow = rand() % MAX_COW;
if (cheesBoard[row][cow] == ’ ')
{
cheesBoard[row][cow] = ‘o’;
break;
}
continue;
}
}
int IsFull(char cheesBoard[MAX_ROW][MAX_COW])
{
for (int row = 0; row < MAX_ROW; row++)
{
for (int cow = 0; cow < MAX_COW; cow++)
{
if (cheesBoard[row][cow] == ’ ')
return 0;
}
}
return 1;
}
char CheckBoard(char cheesBoard[MAX_ROW][MAX_COW])
{
//检查游戏是否结束
//使用x代表玩家胜利;使用o代表电脑胜利,q代表和棋
//如果返回空格代表胜负未定继续进入下一重循环
//1.竖三成型
for (int cow = 0; cow < MAX_COW; cow++)
{
if (cheesBoard[0][cow] != ’ '&&cheesBoard[0][cow] == cheesBoard[1][cow] && cheesBoard[0][cow] == cheesBoard[2][cow])
{
return cheesBoard[0][cow];
}
}
for (int row = 0; row < MAX_COW; row++)
{
if (cheesBoard[row][0] != ’ '&&cheesBoard[row][0] == cheesBoard[row][1] && cheesBoard[row][0] == cheesBoard[row][2])
{
return cheesBoard[row][0];
}
}
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[0][2];
if (IsFull(cheesBoard))
{
return ‘q’;
}
return ’ ';
}
void Game()
{
char winner = ’ ';
//定义并且初始化
char cheesBoard[MAX_ROW][MAX_COW];
Init(cheesBoard);
while (1)
{
//打印地图
Print_Map(cheesBoard);
//判断是否和棋
//玩家落子
Play_Move(cheesBoard);
//判断游戏是否结束
//电脑落子
winner = CheckBoard(cheesBoard);
if (winner != ’ ')
break;
Computer_Move(cheesBoard);
winner = CheckBoard(cheesBoard);
if (winner != ’ ')
break;
//判断游戏是否结束
}
if (winner == ‘x’)
{
Print_Map(cheesBoard);
printf(“恭喜你!!你赢了\n”);
}
else if (winner == ‘o’)
{
Print_Map(cheesBoard);
printf(“你是真的菜,连电脑都下不过\n”);
}
else if (winner == ‘n’)
{
Print_Map(cheesBoard);
printf(“你真够菜的!!和电脑五五开\n”);
}
}
int main()
{

while (1)
{
	int choice = menu();
	if (choice == 1)
	{
		Game();
	}
	else if (choice == 0)
	{
		printf("goodbye");
		break;
	}
	else
	{
		printf("您的输入有误,请重新输入");
		continue;
	}
}
system("pause");
return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值