我的基本思路是先自编函数来实现三子棋的棋盘,再写game的思维逻辑,最后写开头菜单。
来自大一废柴
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3 //可以通过改变数字来调整棋盘大小
#define COL 3 //ROW,COL 一行一列
#include<stdio.h>
#include<Windows.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
//三子棋游戏开始界面
void meun()
{
Sleep(5000);//让程序休息5000毫秒,包含于windons.h中
char a1[] = "******** 1. play *******";
char a2[] = "******** 0. exit *******";
char a3[] = "######## #######";
char a4[] = "######## #######";
int left1 = 0;
int right1 = strlen(a1) - 1;//strlen()计算一个字符串 (字符数组)中元素的个数,包含于string.h中
int left2 = 0;
int right2 = strlen(a2) - 1;
while (left1 < right1 && left2 < right2)
{
a3[left1] = a1[left1];
a3[right1] = a1[right1];
a4[left2] = a2[left2];
a4[right2] = a2[right2];
printf("%s\n", a3);
printf("%s\n", a4);
left1++;
right1--;
left2++;
right2--;
Sleep(100);
system("cls");
}
printf("%s\n", a3);
printf("%s\n", a4);
}
//初始化棋盘,都打上空格
void kongge(char a1[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
a1[i][j] = ' ';
}
}
}
//用符号来升级棋盘
void QIPANG(char a1[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", a1[i][j]);//打印三个元素
if (j < col - 1)//只需在三个元素中用两个|符号隔开
{
printf("|");
}
}
printf("\n");
//打印分割的行
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
//玩家下棋
void wanjia(char a1[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家输入下棋位置:\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)//位置正确时的情况
{
if (a1[x - 1][y - 1] == ' ')
{
a1[x - 1][y - 1] = '*';
break;
}
else
{
printf("被占用\n");
continue;
}
}
else//位置错误时的情况
{
printf("输入错误,请重试\n");
}
}
}
//电脑下棋
void diannao(char a1[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("电脑输入下棋位置:\n");
x = rand() % row;//0-2的随机数
y = rand() % col;//0-2的随机数
if (a1[x][y] == ' ')
{
a1[x][y] = '#';
break;
}
}
}
//判断平局
static int pingju(char a1[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (a1[i][j] == ' ')//还有空说明棋盘没满
{
return 1;
}
}
}
return 0;//棋盘满了返回0
}
char shuying(char a1[ROW][COL], int row, int col)
{
int i = 0;
//赢的几种条件
for (i = 0; i < row; i++)
{
if (a1[i][0] == a1[i][1] && a1[i][1] == a1[i][2] && a1[i][0] != ' ')
{
return a1[i][0];
}
}
for (i = 0; i < row; i++)
{
if (a1[0][i] == a1[1][i] && a1[1][i] == a1[2][i] && a1[0][i] != ' ')
{
return a1[0][i];
}
}
if (a1[0][0] == a1[1][1] && a1[1][1] == a1[2][2] && a1[1][1] != ' ')
{
return a1[1][1];
}
if (a1[0][2] == a1[1][1] && a1[1][1] == a1[2][0] && a1[1][1] != ' ')
{
return a1[1][1];
}
if (pingju(a1, row, col) == 0)//说明棋盘满了
{
return 'q';//游戏以平局结束
}
return 'c';//继续下棋
}
void game()
{
char fu = 0;
char braod[ROW][COL] = { 0 };
//初始化
kongge(braod, ROW, COL);
//棋盘
QIPANG(braod, ROW, COL);
while (1)//死循环
{
//玩家
wanjia(braod, ROW, COL);
QIPANG(braod, ROW, COL);//下子一次展现一次棋盘局面
fu = shuying(braod, ROW, COL);
if (fu != 'c')//判断是否继续
{
break;
}
//电脑
diannao(braod, ROW, COL);
QIPANG(braod, ROW, COL);
fu = shuying(braod, ROW, COL);
if (fu != 'c')
{
break;
}
}
printf("游戏结束\n");
if (fu == '*')
{
printf("玩家赢\n");
}
else if (fu == '#')
{
printf("电脑赢\n");
}
else if (fu == 'p')
{
printf("平局\n");
}
//展现最终棋局
QIPANG(braod, ROW, COL);
}
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'p'
//继续 - 'c'
void sanziqi()
{
int xuanze = 0;
srand((unsigned int)time(NULL));
do
{
meun();
printf("请输入:");
scanf("%d", &xuanze);
switch (xuanze)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误\n");
break;
}
} while (xuanze);
}
int main()
{
sanziqi();
return 0;
}
|