U E D R , A S I H C RSS

Poker Hands/Celfin

~cpp
#include <iostream>
#include <algorithm>
using namespace std;

struct Poker {
	int number;
	char color;
};

Poker black[6];
Poker white[6];

////// 비길경우 비교를 위해 

Poker blackPair[5];
Poker whitePair[5];

int i;
char temp[255];

////////////////////////////// 소팅 ////////////////////////////////////

bool compare(Poker a, Poker b)
{
	return a.number>b.number;
}

/////////////////////////////// 입 력 //////////////////////////////////

bool input()
{
	temp[28]=NULL;
	cin.getline(temp, 255);
	for(i=0; i<10; i++)
	{
		if(temp[28]==NULL)
			return false;
		if(i<5)
		{
			if(temp[3*i]=='T')
				black[i].number = 10;
			else if(temp[3*i]=='J')
				black[i].number = 11;
			else if(temp[3*i]=='Q')
				black[i].number = 12;
			else if(temp[3*i]=='K')
				black[i].number = 13;
			else if(temp[3*i]=='A')
				black[i].number = 14;
			else
				black[i].number = temp[3*i]-48;
			black[i].color = temp[3*i+1];
		}
		else
		{
			if(temp[3*i]=='T')
				white[i-5].number = 10;
			else if(temp[3*i]=='J')
				white[i-5].number = 11;
			else if(temp[3*i]=='Q')
				white[i-5].number = 12;
			else if(temp[3*i]=='K')
				white[i-5].number = 13;
			else if(temp[3*i]=='A')
				white[i-5].number = 14;
			else
				white[i-5].number = temp[3*i]-48;
			white[i-5].color = temp[3*i+1];
		}
	}

	black[5].number = 0; white[5].number = 0;

	sort(&black[0], &black[5], compare);
	sort(&white[0], &white[5], compare);
	return true;
}

///////////////////////////// 풀하우스 결정 ////////////////////////////

bool isFullHouse(Poker *card, int turn)
{
	if((card[0].number==card[2].number) && (card[3].number==card[4].number))
	{
		if(turn==1)
		{
			blackPair[0].number = card[2].number;
			blackPair[1].number = card[3].number;
		}
		else
		{
			whitePair[0].number = card[2].number;
			whitePair[1].number = card[3].number;
		}
		return true;
	}
	else if((card[0].number==card[1].number) && (card[2].number==card[4].number))
	{
		if(turn==1)
		{
			blackPair[0].number = card[2].number;
			blackPair[1].number = card[1].number;
		}
		else
		{
			whitePair[0].number = card[2].number;
			whitePair[1].number = card[1].number;
		}
		return true;
	}
	return false;
}

///////////////////////////// 포카드 결정 ////////////////////////////

bool isFourCard(Poker *card, int turn)
{
	if(card[0].number==card[3].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[0].number;
			blackPair[1].number = card[4].number;
		}
		else
		{
			whitePair[0].number = card[0].number;
			whitePair[1].number = card[4].number;
		}
		return true;
	}
	else if(card[1].number==card[4].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[1].number;
			blackPair[1].number = card[0].number;
		}
		else
		{
			whitePair[0].number = card[1].number;
			whitePair[1].number = card[0].number;
		}
		return true;
	}
	return false;
}

///////////////////////////// 트리플 결정 ////////////////////////////

bool isThreeCard(Poker *card, int turn)
{
	if(card[0].number==card[2].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[0].number;
			blackPair[1].number = card[3].number;
			blackPair[2].number = card[4].number;
		}
		else
		{
			whitePair[0].number = card[0].number;
			whitePair[1].number = card[3].number;
			whitePair[2].number = card[4].number;
		}
		return true;
	}
	else if(card[1].number==card[3].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[1].number;
			blackPair[1].number = card[0].number;
			blackPair[2].number = card[4].number;
		}
		else
		{
			whitePair[0].number = card[1].number;
			whitePair[1].number = card[0].number;
			whitePair[2].number = card[4].number;
		}
		return true;
	}
	else if(card[2].number==card[4].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[2].number;
			blackPair[1].number = card[0].number;
			blackPair[2].number = card[1].number;
		}
		else
		{
			whitePair[0].number = card[2].number;
			whitePair[1].number = card[0].number;
			whitePair[2].number = card[1].number;
		}
		return true;
	}
	return false;
}

///////////////////////////// 투페어 결정 ////////////////////////////

bool isTwoPair(Poker *card, int turn)
{
	if((card[0].number==card[1].number)&&(card[2].number==card[3].number))
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[0].number;
			blackPair[1].number = card[2].number;
			blackPair[2].number = card[4].number;
		}
		else
		{
			whitePair[0].number = card[0].number;
			whitePair[1].number = card[2].number;
			whitePair[2].number = card[4].number;
		}
		return true;
	}
	else if((card[0].number==card[1].number)&&(card[3].number==card[4].number))
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[0].number;
			blackPair[1].number = card[3].number;
			blackPair[2].number = card[2].number;
		}
		else
		{
			whitePair[0].number = card[0].number;
			whitePair[1].number = card[3].number;
			whitePair[2].number = card[2].number;
		}
		return true;
	}
	else if((card[1].number==card[2].number)&&(card[3].number==card[4].number))
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[1].number;
			blackPair[1].number = card[3].number;
			blackPair[2].number = card[0].number;
		}
		else
		{
			whitePair[0].number = card[1].number;
			whitePair[1].number = card[3].number;
			whitePair[2].number = card[0].number;
		}
		return true;
	}
	return false;
}

///////////////////////////// 원페어 결정 ////////////////////////////

bool isOnePair(Poker *card, int turn)
{
	if(card[0].number==card[1].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[0].number;
			blackPair[1].number = card[2].number;
			blackPair[2].number = card[3].number;
			blackPair[3].number = card[4].number;
		}
		else
		{
			whitePair[0].number = card[0].number;
			whitePair[1].number = card[2].number;
			whitePair[2].number = card[3].number;
			whitePair[3].number = card[4].number;
		}
		return true;
	}
	else if(card[1].number==card[2].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[1].number;
			blackPair[1].number = card[0].number;
			blackPair[2].number = card[3].number;
			blackPair[3].number = card[4].number;
		}
		else
		{
			whitePair[0].number = card[1].number;
			whitePair[1].number = card[0].number;
			whitePair[2].number = card[3].number;
			whitePair[3].number = card[4].number;
		}
		return true;
	}
	else if(card[2].number==card[3].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[2].number;
			blackPair[1].number = card[0].number;
			blackPair[2].number = card[1].number;
			blackPair[3].number = card[4].number;
		}
		else
		{
			whitePair[0].number = card[2].number;
			whitePair[1].number = card[0].number;
			whitePair[2].number = card[1].number;
			whitePair[3].number = card[4].number;
		}
		return true;
	}
	else if(card[3].number==card[4].number)
	{
		if(turn==1)  //블랙 턴일 경우 
		{
			blackPair[0].number = card[3].number;
			blackPair[1].number = card[0].number;
			blackPair[2].number = card[1].number;
			blackPair[3].number = card[2].number;
		}
		else
		{
			whitePair[0].number = card[3].number;
			whitePair[1].number = card[0].number;
			whitePair[2].number = card[1].number;
			whitePair[3].number = card[2].number;
		}
		return true;
	}
	return false;
}

////////////////////////////////// 플러시 결정 //////////////////////////////////

bool isFlush(Poker *card)
{
	for(i=1; i<5; i++)
		if(card[i].color!=card[i-1].color)
			return false;
	return true;
}

///////////////////////////////// 스트레이트 결정 ////////////////////////////////

bool isStreit(Poker *card)
{
	for(i=1; i<5; i++)
		if(card[i].number != card[i-1].number-1)
			return false;
	return true;
}

/////////////////////////////// 카드 우선 순위 결정 /////////////////////////////////

int cardpower(Poker *card, int turn)
{
	if(isStreit(card) && isFlush(card))
		return 1;
	else if(isFourCard(card, turn))
		return 2;
	else if(isFullHouse(card, turn))
		return 3;
	else if(isFlush(card))
		return 4;
	else if(isStreit(card))
		return 5;
	else if(isThreeCard(card, turn))
		return 6;
	else if(isTwoPair(card, turn))
		return 7;
	else if(isOnePair(card, turn))
		return 8;
	else 
		return 9;
}

/////////////////////////////// 큰수 결정 ///////////////////////////////

int decideBigger(Poker *card1, Poker *card2)
{
	for(i=0; i<5; i++)
	{
		if(card1[i].number>card2[i].number)
			return 1;
		else if(card1[i].number<card2[i].number)
			return -1;
	}
	return 0;
}

/////////////////////////////////// 승자 결정 ///////////////////////////////////

int decideWinner()
{
	if(cardpower(black, 1)<cardpower(white, 2))
		return 1;
	else if(cardpower(black, 1)>cardpower(white, 2))
		return -1;
	else
	{
		if(cardpower(black, 1)==1 || cardpower(black, 1)==4 || cardpower(black, 1)==5 || cardpower(black, 1)==9)
			return decideBigger(black, white);
		else 
			return decideBigger(blackPair, whitePair);
	}
}

int main()
{
	while(input())
	{
		for(i=0; i<5; i++)
		{
			blackPair[i].number=0;
			whitePair[i].number=0;
		}
		int result = decideWinner();
		if(result==1)
			cout <<"Black wins." <<endl;
		else if(result==-1)
			cout <<"White wins." <<endl;
		else
			cout <<"Tie."<<endl;
	}
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:24:00
Processing time 0.0222 sec