C语言井字棋电脑相互对战

用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;
			}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值