该项目将井字棋分为多个函数实现:
1.棋盘初始化函数
2.打印棋盘函数
3.判断输赢函数
4.双人下棋函数
5.人机对战函数
6.游戏运行函数
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define row 3
#define col 3
void init_borad(char borad[row][col])//棋盘初始化函数
{
int i,j;
for(i = 0;i < row;i++)
{
for(j = 0;j < col;j++)
{
borad[i][j] = ' ';
}
}
}
void print_borad(char borad[row][col])//打印棋盘函数
{
int i = 0,j = 0,n = 0;
for(n = 0;n < row;n++)
{
printf("_____");
}
printf("\n");
for(i = 0;i < row;i++)
{
for(j = 0;j < col;j++)
{
printf(" %c |",borad[i][j]);
}
printf("\n");
for(n = 0;n < row;n++)
{
printf("_____");
}
printf("\n");
}
}
int win(char borad[row][col])//判断输赢函数
{
int i = 0,j = 0,sum = 0;
//右对角线相连
for(i = 0;i < row-1;i++)
{
if(borad[i][i] == borad[i+1][i+1]&&borad[i][i] != ' ')
{
sum++;
}
}
if(sum == row-1)
{
if(borad[i][i] == 'X')
{
printf("X方胜利\n");
}
else
{
printf("O方胜利\n");
}
return 0;
}
//左对角线相连
sum = 0;
for(i = 0;i < row-1;i++)
{
if(borad[i][row-i-1] == borad[i+1][row-i-2] && borad[i][row-i-1] != ' ')
{
sum++;
}
}
if(sum == row-1)
{
if(borad[i][row-i-1] == 'X')
{
printf("X方胜利\n");
}
else
{
printf("O方胜利\n");
}
return 0;
}
//整行相连
sum = 0;
for(i = 0;i < row;i++)
{
for(j = 0;j < col-1;j++)
{
if(borad[i][j] == borad[i][j+1] && borad[i][j] != ' ')
{
sum++;
}
}
if(sum != row-1)
{
sum = 0;
}
else if(sum = row-1)
{
break;
}
}
if(sum == row-1)
{
if(borad[i][j] == 'X')
{
printf("X方胜利\n");
}
else
{
printf("O方胜利\n");
}
return 0;
}
//整列相连
sum = 0;
for(i = 0;i < col;i++)
{
for(j = 0;j < row-1;j++)
{
if(borad[j][i] == borad[j+1][i] && borad[j][i] != ' ')
{
sum++;
}
}
if(sum != row-1)
{
sum = 0;
}
else if(sum = row-1)
{
break;
}
}
if(sum == row-1)
{
if(borad[j][i] == 'X')
{
printf("X方胜利\n");
}
else
{
printf("O方胜利\n");
}
return 0;
}
int n = 0;//判断平局
for(i = 0;i<row;i++)
{
for(j = 0;j < col;j++)
{
if(borad[i][j] != ' ')
{
n++;
}
}
}
if(n == row*col)
{
printf("平局\n");
return 0;
}
if(sum != row-1)
return 1;
}
void real_play(char borad[row][col])
{
init_borad(borad);
print_borad(borad);
do
{
int r = 0,c = 0,t = 0;
printf("请第一位玩家输入棋格的坐标\n");
loop:
scanf("%d %d",&r,&c);
if(borad[r-1][c-1] == ' ')
{
borad[r-1][c-1] = 'X';
}
else
{
printf("输入坐标已被占用或非法,请重新输入\n");
goto loop;
}
print_borad(borad);
t = win(borad);
if(t == 0)
break;
printf("请第二位玩家输入棋格的坐标\n");
loop1:
scanf("%d %d",&r,&c);
if(borad[r-1][c-1] == ' ')
{
borad[r-1][c-1] = 'O';
}
else
{
printf("输入坐标已被占用或非法,请重新输入\n");
goto loop1;
}
print_borad(borad);
win(borad);
if(t == 0)
break;
}while(1);
}
void computer_play(char borad[row][col])//人机运行主函数
{
srand((unsigned)time(NULL));
init_borad(borad);
print_borad(borad);
do
{
int r = 0,c = 0,t = 1;
printf("请玩家输入棋格的坐标\n");
loop1:
scanf("%d %d",&r,&c);
if(borad[r-1][c-1] == ' ')
{
borad[r-1][c-1] = 'X';
}
else
{
printf("输入坐标已被占用或非法,请重新输入\n");
goto loop1;
}
print_borad(borad);
t = win(borad);
if(t == 0)
break;
printf("电脑下棋\n");
loop:
r = rand()%row + 1;
c = rand()%col + 1;
if(borad[r-1][c-1] == ' ')
{
borad[r-1][c-1] = 'O';
}
else
{
goto loop;
}
print_borad(borad);
win(borad);
if(t == 0)
break;
}while(1);
}
void play_game(char borad[row][col])//游戏运行主函数
{
int n;
printf("双人游戏请输入1,人机对战请输入0\n");
loop:
scanf("%d",&n);
switch(n)
{
case 0:computer_play(borad);break;
case 1:real_play(borad);break;
default:
{
printf("输入非法,请重新输入\n");
goto loop;
}
}
}
int main()
{
int x = 0,i = 0,j = 0;
char borad[row][col];
printf("开始游戏请输入1,结束游戏请输入0\n");
scanf("%d",&x);
init_borad(borad);
do
{
switch(x)
{
case 1:
{
printf("游戏开始\n");
play_game(borad);
break;
}
case 0:
{
printf("游戏已退出\n");
return 0;
}
default:
{
printf("操作错误,请重新输入\n");
scanf("%d",&x);
break;
}
}
printf("继续游戏请输入1,退出游戏请输入0\n");
scanf("%d",&x);
}while(x!=0);
return 0;
}