/**
 PokerRole.java
 Copyright (c) 1997 by Hidetaka Manu- Masuda
 This class is for HitoriPoker.java.
 Non-Commercial use only.
*/ 

import java.awt.*;
import java.applet.Applet;
import java.io.*;
import java.net.*;

public class PokerRole {
	static final int RoyalStraightFlush = 9;
	static final int StraightFlush = 8;
	static final int FourCards = 7;
	static final int FullHouse = 6;
	static final int Flush = 5;
	static final int Straight = 4;
	static final int ThreeCards = 3;
	static final int TwoPair = 2;
	static final int OnePair = 1;
	static final int NoPair = 0;

    	static final int spade = 0;
    	static final int heart = 1;
    	static final int diamond = 2;
    	static final int club = 3;

	private int name;
	private int point;
	private PokerCard[] hand;

PokerRole() {
     	setRole(NoPair);
}

PokerCard[] getHand() {
	return hand;
}

void setHand(PokerCard[] aHand) {
	hand = aHand;
}

int getRole() {
	return name;
}

void setRole(int aRole) {
	name = aRole;
	setPoint(getRoleToPoint());
}

String getRoleName() {
	switch (name) {
        	case RoyalStraightFlush:
			return "R.S.Flush";
		case StraightFlush:        
			return "S.Flush";
		case FourCards:            
			return "4 Cards";
		case FullHouse:            
			return "F.House";
		case Flush:                
			return "Flush";
		case Straight:             
			return "Straight";
		case ThreeCards:           
			return "3 Cards";
		case TwoPair:              
			return "2 Pair";
		case OnePair:              
			return "1 Pair";
		default:
                	return "NoPair";
	}
}

int getRoleToPoint() {
	switch (name) {
        	case RoyalStraightFlush:
			return 100;
		case StraightFlush:        
			return 75;
		case FourCards:            
			return 50;
		case FullHouse:            
			return 25;
		case Flush:                
			return 20;
		case Straight:             
			return 15;
		case ThreeCards:           
			return 10;
		case TwoPair:              
			return 5;
		case OnePair:              
			return 2;
		default:
                	return 0;
	}
}

int getPoint() {
	return point;
}

String printPoint() {
	String tmp;
	tmp = "   "+point;
     return tmp.substring(tmp.length()-3, tmp.length());
}

void setPoint(int aPoint) {
	point = aPoint;
}

void sortHand(PokerCard[] aHand) {
	int i, j;
	PokerCard tmp;
	for (i=0; i<=3; i++) {
		for (j=i+1; j<=4; j++) {
			if (aHand[i].getNumber() > aHand[j].getNumber()) {
				tmp = aHand[i];
              			aHand[i] = aHand[j];
              			aHand[j] = tmp;
			}
		}
	}
	setHand(aHand);
}

boolean isFlush() {
	int i;
	int mark;
	mark = hand[0].getMark();
	for (i=1; i<=4; i++) {
		if (mark != hand[i].getMark()) {
			return false;
		}
	}
	name = Flush;
     	point = getRoleToPoint();
     	return true;
}

boolean isRoyalStraightFlush() {
	int total, i, num;
	total = 0;
     	for (i=0; i<=4; i++) {
		num = hand[i].getNumber();
		if (num == 1) {
			num = 14;
		}
		total = total + num;
	}
	if (total == (14+10+11+12+13)) {
          	name = RoyalStraightFlush;
          	point = getRoleToPoint();
          	return true;
	}     
	else {
		return false;
	}
}

boolean isFourCards() {
	int first, last;
	boolean bool;

	first = hand[0].getNumber();
     	bool = (first == hand[1].getNumber()) & (first == hand[2].getNumber())
          & (first == hand[3].getNumber());
     	if (bool) {
          	name = FourCards;
          	point = getRoleToPoint();
          	return true;
	};
     	last = hand[4].getNumber();
     	bool = (last == hand[3].getNumber()) & (last == hand[2].getNumber())
          & (last == hand[1].getNumber());
     	if (bool) {
          	name = FourCards;
          	point = getRoleToPoint();
	        return true;
	}
	return false;
}

boolean isStraight() {
	int first, i, total, num;
     	first = hand[0].getNumber();
	total = 0;
	for (i=0; i<=4; i++) {
		num = hand[i].getNumber();
		total = total + num;
	}
	if (first == 1) {
		total = total + 13;
	}
	if (total == (14+10+11+12+13)) {
		name = Straight;
          	point = getRoleToPoint();
          	return true;
	}     
     	for (i=1; i<=4; i++) {
          	if (hand[i].getNumber() != (first+i)) {
                	return false;
		}
	}
	name = Straight;
     	point = getRoleToPoint();
	return true;
}

boolean isStraightFlush() {
	boolean bool;
     	bool = isStraight();
     	if (bool) {
          	name = StraightFlush;
          	point = getRoleToPoint();
	}
	return bool;
}

boolean isFullHouse() {
	int first, middle, last;
     	first = hand[0].getNumber();
     	middle = hand[2].getNumber();
     	last = hand[4].getNumber();
     	if (first == middle) {
     		if (hand[3].getNumber() == last) {
               		name = FullHouse;
               		point = getRoleToPoint();
	               	return true;
		}
	}
     	if (last == middle) {
          	if (hand[1].getNumber() == first) {
          		name = FullHouse;
               		point = getRoleToPoint();
			return true;
		}
	}
	return false;
}

boolean isThreeCards() {
	int first, middle, last;
	first = hand[0].getNumber();
     	middle = hand[2].getNumber();
     	last = hand[4].getNumber();
     	if ((first == middle) | (middle == last)) {
          	name = ThreeCards;
          	point = getRoleToPoint();
          	return true;
	}
     	if ((middle == hand[1].getNumber()) & (middle == hand[3].getNumber())) {
          	name = ThreeCards;
          	point = getRoleToPoint();
          	return true;
	}
	return false;
}


boolean isTwoPair() {
	boolean bool;

     	bool = (hand[0].getNumber() == hand[1].getNumber())
          & (hand[2].getNumber() == hand[3].getNumber());
     	if (bool) {
          	name = TwoPair;
          	point = getRoleToPoint();
          	return true;
	}
     	bool = (hand[0].getNumber() == hand[1].getNumber())
          & (hand[3].getNumber() == hand[4].getNumber());
     	if (bool) {
		name = TwoPair;
          	point = getRoleToPoint();
		return true;
	}
     	bool = (hand[1].getNumber() == hand[2].getNumber())
          & (hand[3].getNumber() == hand[4].getNumber());
     	if (bool) {
		name = TwoPair;
          	point = getRoleToPoint();
	        return true;
	}
	return false;
}

boolean isOnePair() {
	int i, j;
     	for (i=0; i<=3; i++) {
         	for (j=i+1; j<=4; j++) {
         		if (hand[i].getNumber() == hand[j].getNumber()) {
              			name = OnePair;
              			point = getRoleToPoint();
              			return true;
			}
		}
	}
	return false;
}

boolean evaluate(PokerCard[] aHand) {
	sortHand(aHand);
     	if (isFlush()) {
     		if (isRoyalStraightFlush()) {
			return true;
		}
          	else {
	        	if (isStraightFlush()) {
				return true;
			}
		}
	}
	else {
          	if (isStraight()) {return true;};
          	if (isFourCards()) {return true;};
          	if (isFullHouse()) {return true;};
          	if (isThreeCards()) {return true;};
          	if (isTwoPair()) {return true;};
          	if (isOnePair()) {return true;};
	}
	return false;
}

}
