#include"head.h"
int main()
{
char board[Hang][Lie] = { 0 };
//游戏结束接着游戏
int a = 0;
do {
start_screen();//开始界面
scanf("%d", &a);
if (a == 1)
{
printf("开始\n");
//游戏主体
game(board,Hang,Lie);
}
else
{
printf("结束游戏\n");
break;
}
} while (1);
return 0;
}
#include"head.h"
void start_screen()
{
printf("***************************************\n");
printf("***************************************\n");
printf("********* 输入1开始游戏 *************\n");
printf("***************************************\n");
printf("********* 输入其他结束游戏 ************\n");
printf("***************************************\n");
}
void game(char board[Hang][Lie], int hang, int lie)//游戏主体
{
//游戏初始化
reboot(board,hang,lie);
//游戏界面
game_screen(board,hang,lie);
while (1)
{
int tep = 3;
while (1)
{
int a = 0;
a=(gamer(board, hang, lie));//玩家输入
if(a==3)
break;
}
system("cls");
game_screen(board, hang, lie);
// 判断
tep = decide(board,hang,lie);
if (tep == '*')
{
printf("玩家获胜\n"); break;
}
else if (tep == '#')
{
printf("人机获胜\n"); break;
}
else if (tep == 0)
{
printf("平局\n"); break;
}
else
{
printf("继续\n");
}
//电脑输入
gamer1(board, hang, lie);
game_screen(board, hang, lie);
// 判断
tep = decide(board, hang, lie);
if (tep == '*')
{
printf("玩家获胜\n"); break;
}
else if (tep == '#')
{
printf("人机获胜\n"); break;
}
else if (tep == 0)
{
printf("平局\n"); break;
}
else
{
printf("继续\n");
}
}
}
void reboot(char board[Hang][Lie], int hang, int lie)//初始化
{
int i = 0,j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
board[i][j] = ' ';
}
}
}
void game_screen(char board[Hang][Lie], int hang, int lie)//游戏界面
{
int i = 0, j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
printf(" %c ", board[i][j]);
if(j<hang-1)
printf("|");
}
printf("\n");
if (i < hang - 1)
{
for (j = 0; j < lie; j++)
{
printf("---");
if (j < hang - 1)
printf("|");
}
printf("\n");
}
}
}
//玩家输入
int gamer(char board[Hang][Lie], int hang, int lie)
{
int i = 0, j = 0;
printf("玩家输入坐标:\n");
scanf("%d%d",&i,&j);
if (i<=3 && i>0 && j>0 && j<=3)
{
if (board[i - 1][j - 1] == ' ')
{
board[i - 1][j - 1] = '*';
return 3;
}
else
{
printf("输入位置已有棋子\n");
}
}
else
{
printf("输入错误,请重新输入\n");
return 5;
}
}
//判断
//玩家胜利返回*
//人机胜利返回#
//平局返回0
//继续返回1
int decide(char board[Hang][Lie], int hang, int lie)
{
int i = 0, j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
return board[i][1];
else if (board[0][j] == board[1][j] && board[2][j] == board[1][j] && board[1][j] != ' ')
return board[1][j];
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
else if (board[0][3] == board[1][1] && board[1][1] == board[2][1] && board[1][1] != ' ')
return board[1][1];
else
return 1; //继续
}
}
return 0;
}
void gamer1(char board[Hang][Lie], int hang, int lie)
{
int i = 0, j = 0;
printf("人机输入坐标:\n");
scanf("%d%d", &i, &j);
if (i <= 3 && i > 0 && j > 0 && j <= 3)
{
if (board[i - 1][j - 1] == ' ')
{
board[i - 1][j - 1] = '#';
return 3;
}
else
{
printf("输入位置已有棋子\n");
}
}
else
{
printf("输入错误,请重新输入\n");
}
}
#include<stdio.h>
#include<windows.h>
#define Hang 3
#define Lie 3
void start_screen();
void game(char board[Hang][Lie], int hang, int lie);
void reboot(char board[Hang][Lie],int hang,int lie);
void game_screen(char board[Hang][Lie], int hang, int lie);
int gamer(char board[Hang][Lie],int hang,int lie);
int decide(char board[Hang][Lie], int hang, int lie);
void gamer1(char board[Hang][Lie], int hang, int lie);
新手,大佬勿喷