用C语言实现的井字棋电脑相互对战游戏,仅作个人记录
head.c
#include<stdio.h>
#include<stdbool.h>
#include<windows.h>
#define ROW 3
#define ROL 3
static chess_board[ROW][ROL];
void WelcomeInterface();
void InitGame();
void PrintChess();
void PlayerMove();
void ComputerMove();
bool InputLegality(int row,int rol);
char CheckChess();
bool CheckFull();
void JudgeWinner(char input,int *px,int *po,int *ph);
main.c
#include"head.h"
int main()
{
char winner;
int select;
int n;
int i;
int x=0,o=0,h=0;
int *px=&x,*po=&o,*ph=&h;
WelcomeInterface();
scanf("%d",&select);
printf("Input times\n");
scanf("%d",&n);
if(select==1)
{
for(i=0;i<n;i++)
{
InitGame();
PrintChess();
while('c'==CheckChess())
{
if(CheckFull())
break;
ComputerMovex();
system("cls");
PrintChess();
winner=CheckChess();
if(winner=='x')
break;
if(CheckFull())
break;
ComputerMoveo();
system("cls");
PrintChess();
winner=CheckChess();
if(winner=='o')
break;
}
JudgeWinner(CheckChess(),px,po,ph);
}
}
printf("x won %d times\n",*px);
printf("o won %d times\n",*po);
printf("It was tied %d times\n",*ph);
return 0;
}
function.c
#include"head.h"
void WelcomeInterface()
{
printf("---------------\n");
printf("|[1] StartGame|\n");
printf("|[0] OverGame |\n");
printf("---------------\n");
}
void InitGame()
{
int i,j;
for(i=0;i<ROW;i++)
{
for(j=0;j<ROL;j++)
{
chess_board[i][j]=' ';
}
}
}
void PrintChess()
{
int i,j;
for(i=0;i<ROW;i++)
{
printf("|| %c || %c || %c ||\n",chess_board[i][0],chess_board[i][1],chess_board[i][2]);
if(i<2)
printf("||---||---||---||\n");
}
}
void ComputerMoveo()
{
srand(time(0));
int row,rol;
printf("Computero'turn......\n");
do
{
row=rand()%ROW;
rol=rand()%ROL;
if(InputLegality(row,rol))
break;
}while(1);
chess_board[row][rol]='o';
}
void ComputerMovex()
{
srand(time(0));
int row,rol;
printf("Computerx'turn......\n");
do
{
row=rand()%ROW;
rol=rand()%ROL;
if(InputLegality(row,rol))
break;
}while(1);
chess_board[row][rol]='x';
}
bool InputLegality(int row,int rol)
{
if(row>=0&&row<=3)
{
if(rol>=0&&rol<=3)
{
if(chess_board[row][rol]==' ')
return true;
}
}
return false;
}
char CheckChess()
{
int i,j;
char ret;
for(i=0;i<ROW;i++)
{
if(chess_board[i][0]!=' '&&
chess_board[i][0]==chess_board[i][1]&&
chess_board[i][0]==chess_board[i][2])
return chess_board[i][0];
}
for(j=0;j<ROL;j++)
{
if(chess_board[0][j]!=' '&&
chess_board[0][j]==chess_board[1][j]&&
chess_board[0][j]==chess_board[2][j])
return chess_board[0][j];
}
if(chess_board[0][0]!=' '&&
chess_board[0][0]==chess_board[1][1]&&
chess_board[0][0]==chess_board[2][2])
return chess_board[0][0];
if(chess_board[0][2]!=' '&&
chess_board[0][2]==chess_board[1][1]&&
chess_board[0][2]==chess_board[2][0])
return chess_board[0][2];
if(CheckFull())
return 'h';
return 'c';
}
bool CheckFull()
{
int row,rol;
for(row=0;row<ROW;row++)
{
for(rol=0;rol<ROL;rol++)
{
if(chess_board[row][rol]==' ')
return false;
}
}
return true;
}
void JudgeWinner(char input,int *px,int *po,int *ph)
{
switch(input)
{
case 'x':
{
printf("x win!\n");
*px+=1;
break;
}
case 'o':
{
printf("o win!\n");
*po+=1;
break;
}
case 'h':
{
printf("Fight to a draw.\n");
*ph+=1;
break;
}
}
}