#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 3
#define COL 3
void menu();
void game();
void Print_board(char board[ROW][COL],int row,int col);
void player(char board[ROW][COL],int row,int col);
void computer(char board[ROW][COL],int row,int col);
char Is_win(char board[ROW][COL],int row,int col);
int main()
{
int n;
srand((unsigned int)time(NULL));
do
{
//打印菜单
menu();
printf("请输入>:");
scanf("%d",&n);
//选择
switch(n)
{
case 1:
game();
break;
case 0:
printf("退出游戏");
break;
default:
printf("输入错误,请重新输入!!!\n");
break;
}
}while(n);
return 0;
}
void menu()
{
printf("******************************\n");
printf("******** 1.进入游戏***********\n");
printf("******** 0.退出游戏***********\n");
printf("******************************\n");
}
//打印棋盘
void Print_board(char board[ROW][COL],int row,int col)
{
int i=0,j=0;
for(i=0;i<row;i++)
{
//打印棋盘内容 *|*|*
for(j=0;j<col;j++)
{
printf(" %c ",board[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 P_board(char board[ROW][COL],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
board[i][j]=' ';
}
void game()
{
char ret=0;
char board[ROW][COL]={0};
//初始化棋盘
P_board(board,ROW,COL);
//打印棋盘
Print_board(board,ROW,COL);
while(1)
{
//玩家下棋
player(board,ROW,COL);
//打印棋盘
Print_board(board,ROW,COL);
//判断输赢
ret = Is_win(board,ROW,COL);
if(ret!='M')
break;
//电脑下棋
computer(board,ROW,COL);
//打印棋盘
Print_board(board,ROW,COL);
//判断输赢
ret = Is_win(board,ROW,COL);
if(ret!='M')
break;
}
if(ret=='*')
{
printf("玩家赢\n");
}
else if(ret=='#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
//玩家下棋
void player(char board[ROW][COL],int row,int col)
{
int r,c;
printf("玩家下棋");
while(1)
{
printf("请输入位置:");
scanf("%d%d",&r,&c);
if(r>=1&&r<=row&&c>=1&&c<=col)
{
if(board[r-1][c-1]==' ')
{
board[r-1][c-1]='*';
break;
}
else
printf("位置已占用,请重新输入");
}
else
printf("位置不正确,请重新输入");
}
}
//电脑下棋
void computer(char board[ROW][COL],int row,int col)
{
int c,r;
printf("电脑下棋\n");
while(1)
{
c=rand()%col;
r=rand()%row;
if(board[r][c]==' ')
{
board[r][c]='#';
break;
}
}
}
//判断输赢
//玩家赢 *
//电脑赢 #
//平局 C
//继续 M
char Is_win(char board[ROW][COL],int row,int col)
{
int i=0,j=0;
//行
for(i=0;i<row;i++)
{
for(j=0;j<col-2;j++)
{
if(board[i][j]==board[i][j+1]&&board[i][j+1]==board[i][j+2]&&board[i][j+1]!=' ')
return board[i][j+1];
}
}
//列
for(j=0;j<col;j++)
{
for(i=0;i<row-2;i++)
{
if(board[i][j]==board[i+1][j]&&board[i+1][j]==board[i+2][j]&&board[i+1][j]!=' ')
return board[i][j];
}
}
//左对角线
for(i=0;i<row-2;i++)
{
for(j=0;j<col-2;j++)
if(board[i][j]==board[i+1][j+1]&&board[i+1][j+1]==board[i+2][j+2]&&board[i+1][j+1]!=' ')
return board[i+1][j+1];
}
//右对角线
for(i=0;i<row-2;i++)
{
for(j=col-1;j>1;j--)
if(board[i][j]==board[i+1][j-1]&&board[i+1][j-1]==board[i+2][j-2]&&board[i+2][j-2]!=' ')
return board[i][j];
}
//平局
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
if(board[i][j]==' ')
return 'M';
}
return 'C';
}
实现三子棋
最新推荐文章于 2024-11-05 21:58:11 发布