c++的井字棋程序(免费版)

#include<bits/stdc++.h>
using namespace std;
bool shuying = false;
char a[3][3];
void pan(char a[3][3])
{
	// Judge the winner or loser vertically
	for(int j=0;j<3;j++)
	{
		if(a[1][j]!='O')
		{
			char PAN = a[1][j];
			if(a[0][j]==PAN&&a[2][j]==PAN)
			{
				if(PAN=='B')
				{
					cout<<"------ black wins -----";
					shuying = true;
				}
				if(PAN=='W')
				{
					cout<<"------ white wins -----";
					shuying = true;
				}
			}
		}
	}
	//Win or lose by line
	for(int i=0;i<3;i++)
	{
		if(a[i][1]!='O')
		{
			char PAN = a[i][1];
			if(a[i][0]==PAN&&a[1][2]==PAN)
			{
				if(PAN=='B')
				{
	 				cout<<"------ black wins -----";
					shuying = true;
				}
				if(PAN=='W')
				{
					cout<<"------ white wins -----";
					shuying = true;
				}
			}
		}
	}
	//Diagonally judge wins and loses
	if(a[1][1]!='O')
	{
		char PAN = a[1][1];
		if(a[0][0]==PAN&&a[2][2]==PAN||a[0][2]==PAN&&a[2][0]==PAN)
		{
			if(PAN=='B')
			{
	 			cout<<"------ black wins -----";
				shuying = true;
			}
			if(PAN=='W')
			{
				cout<<"------ white wins -----";
				shuying = true;
			}
		}
	}
}
void shuchu(char a[3][3])
{
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			cout<<a[i][j]<<' ';
		}
		cout<<endl;
	}
}
int main()
{
	cout<<"It's a tic-tac-toe program"<<endl<<endl;
	cout<<"'O' for 'empty' 'B' for 'black' 'W' for 'white'"<<endl<<endl;
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			a[i][j] = 'O';
		
	cout<<"This is the initial state:"<<endl<<endl;	
	shuchu(a);
	cout<<endl;
	cout<<"Okay, now the game begins"<<endl<<endl<<endl;
	
	for(int ci=0;ci<9;ci++)
	{
		if(ci%2==0)
		{
			cout<<"For black players, please enter rows and columns"<<endl;
			shuchu(a);
			cout<<endl;
			
			int i,j;
			char P_i,P_j;
			cin>>P_i>>P_j;
			if(P_i>='1'&&P_i<='3'&&P_j>='1'&&P_j<='3')
			{
				i = P_i-48;
				j = P_j-48;
			}
			else
			{
				cout<<endl<<"Typing error"<<endl<<endl;
				ci--;
				continue;
			}
			if(a[i-1][j-1]=='O') a[i-1][j-1] = 'B';
			else 
			{
				cout<<endl<<"The position has been subordinated"<<endl<<endl;
				ci--;
				continue;
			}
			cout<<endl;
		}
		if(ci%2==1)
		{
			cout<<"For white players, please enter rows and columns"<<endl;
			shuchu(a);
			cout<<endl;
			
			int i,j;
			char P_i,P_j;
			cin>>P_i>>P_j;
			if(P_i>='1'&&P_i<='3'&&P_j>='1'&&P_j<='3')
			{
				i = P_i-48;
				j = P_j-48;
			}
			else
			{
				cout<<endl<<"Typing error"<<endl<<endl;
				ci--;
				continue;
			}
			if(a[i-1][j-1]=='O') a[i-1][j-1] = 'W';
			else 
			{
				cout<<endl<<"The position has been subordinated"<<endl<<endl;
				ci--;
				continue;
			}
			cout<<endl;
		}
		pan(a);
		if(shuying) return 0;
	}
	cout<<"-----Well, this matchup didn't decide the winner-----";
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值