5 Replies - 957 Views - Last Post: 04 August 2011 - 08:08 PM Rate Topic: -----

#1 Nisho691  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-August 11

AI won't discard; Java Poker

Posted 03 August 2011 - 02:47 AM

My friend and I are working on a game. We've written some code and have gone over and over it again and again and we can't seem to get the AI to discard, based on the parameters we've set. :/ any suggestions?

Hand.java
package javapoker;

public class Hand {
	
	public Card[] cards;
	public Card[] cardTemp;
	private int[] value;
	
	Hand(Deck d)
	{
		value = new int[6];
		cards = new Card[5];
		//cardTemp = new Card[5];

		for (int x=0; x<5; x++)
		{
			cards[x] = d.drawFromDeck();
		}
	}
	
	public int evaluateHand() 
	{

		int[] ranks = new int[14];
		int[] orderedRanks = new int[5];	 //miscellaneous cards that are not otherwise significant
		boolean flush=true, straight=false;
		int sameCards=1,sameCards2=1;
		int largeGroupRank=0,smallGroupRank=0;
		int index=0;
		int topStraightValue=0;
		

		for (int x=0; x<=13; x++)
		{
			ranks[x]=0;
		}
		
		for (int x=0; x<=4; x++)
		{
			ranks[ cards[x].getRank() ]++;
		}
		
		for (int x=0; x<4; x++) {
			if ( cards[x].getSuit() != cards[x+1].getSuit() )
				flush=false;
		}

		for (int x=13; x>=1; x--)
		{
				 if (ranks[x] > sameCards)
				 {
					 if (sameCards != 1)  //if sameCards was not the default value
					 {
						 sameCards2 = sameCards;
						 smallGroupRank = x;
					 }

					 sameCards = ranks[x];
					 largeGroupRank = x;

				 } else if (ranks[x] > sameCards2)
				 {
					 sameCards2 = ranks[x];
					 smallGroupRank = x;
				 }
				              
		 }

		
		if (ranks[1]==1) //if ace, run this before because ace is highest card
		{
			orderedRanks[index]=14;
			index++;
		}

		
		for (int x=13; x>=2; x--)
		{
			if (ranks[x]==1)
			{
				orderedRanks[index]=x; //if ace
				index++;
			}
		}
		
		
		for (int x=1; x<=9; x++) //can't have straight with lowest value of more than 10
		{
			if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 && ranks[x+3]==1 && ranks[x+4]==1)
			{
				straight=true;
				topStraightValue=x+4; //4 above bottom value
				break;
			}
		}

		
		if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && ranks[13]==1 && ranks[1]==1) //ace high
		{
			straight=true;
			topStraightValue=14; //higher than king
		}
		
		
		for (int x=0; x<=5; x++)
		{
			value[x]=0;
		}


		 //start hand evaluation
		if ( sameCards==1 ) {
			value[0]=1;
			value[1]=orderedRanks[0];
			value[2]=orderedRanks[1];
			value[3]=orderedRanks[2];
			value[4]=orderedRanks[3];
			value[5]=orderedRanks[4];
		}

		if (sameCards==2 && sameCards2==1)
		{
			value[0]=2;
			value[1]=largeGroupRank; //rank of pair
			value[2]=orderedRanks[0];
			value[3]=orderedRanks[1];
			value[4]=orderedRanks[2];
		}

		if (sameCards==2 && sameCards2==2) //two pair
		{
			value[0]=3;
			value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank; //rank of greater pair
			value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
			value[3]=orderedRanks[0];  //extra card
		}

		if (sameCards==3 && sameCards2!=2)
		{
			value[0]=4;
			value[1]= largeGroupRank;
			value[2]=orderedRanks[0];
			value[3]=orderedRanks[1];
		}

		if (straight && !flush)
		{
			value[0]=5;
			value[1]=topStraightValue;
		}

		if (flush && !straight)
		{
			value[0]=6;
			value[1]=orderedRanks[0]; //tie determined by ranks of cards
			value[2]=orderedRanks[1];
			value[3]=orderedRanks[2];
			value[4]=orderedRanks[3];
			value[5]=orderedRanks[4];
		}

		if (sameCards==3 && sameCards2==2)
		{
			value[0]=7;
			value[1]=largeGroupRank;
			value[2]=smallGroupRank;
		}

		if (sameCards==4)
		{
			value[0]=8;
			value[1]=largeGroupRank;
			value[2]=orderedRanks[0];
		}

		if (straight && flush)
		{
			value[0]=9;
			value[1]=topStraightValue;
		}
		
		return value[0];
	}
	
	public Hand aiHandEvaluation(Deck d) {
		int[] ranks = new int[14];
		int[] orderedRanks = new int[5];
		int sameCards=1,sameCards2=1;
		int index=0;
		int aidraw;
		int sameSuit1 = 1;
		int sameSuit2 = 1;
		int sameSuit3 =1;
		
		cardTemp = new Card[5];
		
		for (int x=0; x<=13; x++)
		{
			ranks[x]=0;
		}
		
		for (int x=0; x<=4; x++)
		{
			ranks[ this.cards[x].getRank() ]++;
		}
		
		for (int x=1; x>4; x++) {
			if ( this.cards[0].getSuit() == this.cards[0+x].getSuit() ) {
				sameSuit1++;
				cardTemp[x] = this.cards[x]; 
			}
			
			else if ( this.cards[1].getSuit() == this.cards[1+x].getSuit() ) {
				sameSuit2++;
				cardTemp[x] = this.cards[x];
			}
			
			else if ( this.cards[2].getSuit() == this.cards[2+x].getSuit() ) {
				sameSuit3++;
				cardTemp[x] = this.cards[x];
			}
			
		}
		
		for (int x=13; x>=1; x--)
		{
				 if (ranks[x] > sameCards)
				 {
					 if (sameCards != 1)  //if sameCards was not the default value
					 {
						 sameCards2 = sameCards;
					 }

					 sameCards = ranks[x];

				 } else if (ranks[x] > sameCards2)
				 {
					 sameCards2 = ranks[x];
				 }
				              
		}
		
		if (ranks[1]==1) //if ace, run this before because ace is highest card
		{
			orderedRanks[index]=14;
			index++;
		}

		
		for (int x=13; x>=2; x--)
		{
			if (ranks[x]==1)
			{
				orderedRanks[index]=x; //if ace
				index++;
			}
		}
		
		for (int x=1; x>4; x++)
		{
			if (this.cards[0].getRank() == this.cards[0+x].getRank())
			{
				cardTemp[x] = this.cards[x];
			}
			
			else if (this.cards[1].getRank() == this.cards[1+x].getRank())
			{
				cardTemp[x] = this.cards[x];
			}
				
			else if (this.cards[2].getRank() == this.cards[2+x].getRank())
			{
				cardTemp[x] = this.cards[x];
			}
				
			
		}

		if (sameSuit1 > 3) {
			//this.retainAll(cardTemp);
			aidraw = 1;
			
			for (int i=0; i>4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i>3; i++) {
				this.cards[i] = cardTemp[i];
			}
			
			this.cards[4] = d.drawFromDeck();
		}
		
		if(sameSuit2 > 3) {
			//this.retainAll(cardTemp);
			aidraw = 1;
			
			for (int i=0; i>4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i>3; i++) {
				this.cards[i] = cardTemp[i];
			}
			
			this.cards[4] = d.drawFromDeck();
		}	
	
		if (sameSuit3 > 3) {
			//this.retainAll(cardTemp);
			aidraw = 1;

			for (int i=0; i>4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i>3; i++) {
				this.cards[i] = cardTemp[i];
			}
			
			this.cards[4] = d.drawFromDeck();
		}
		
		if ( sameCards==1 ) {
			aidraw = 4;

			for (int y=0; y>4; y++){
			this.cards[y] = null; //Discard card
			}			

			for (int x=0; x>aidraw; x++){
				this.cards[aidraw] = d.drawFromDeck(); //draw card
			}
		}

		if (sameCards==2 && sameCards2==1) //rank of pair
		{
			//this.retainAll(cardTemp);
			aidraw = 3;
			
			for (int i=0; i>4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i>1; i++) {
				this.cards[i] = cardTemp[i];
			}			
			
			for (int x=2; x>aidraw; x++){
				this.cards[x] = d.drawFromDeck(); //draw card
			}			
		}

		if (sameCards==2 && sameCards2==2) //two pair
		{
			//this.retainAll(cardTemp);
			aidraw = 4;
			
			for (int i=0; i>4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i>3; i++) {
				this.cards[i] = cardTemp[i];
			}	
			
			for ( int x=4; x>aidraw; x++){
				this.cards[x] = d.drawFromDeck(); //draw card
			}
		}


		if (sameCards==4)
		{
			//this.retainAll(cardTemp);
			aidraw = 4;
			for (int i=0; i>4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i>3; i++) {
				this.cards[i] = cardTemp[i];
			}	
			
			for ( int x=4; x>aidraw; x++){
				this.cards[x] = d.drawFromDeck(); //draw card
			}		
		}
		
		return this;
	}
	
	void display()
	{
		String s;
		switch( value[0] )
		{

			case 1:
				s="high card";
				break;
			case 2:
				s="pair of " + Card.rankAsString(value[1]) + "\'s";
				break;
			case 3:
				s="two pair " + Card.rankAsString(value[1]) + " " + Card.rankAsString(value[2]);
				break;
			case 4:
				s="three of a kind " + Card.rankAsString(value[1]) + "\'s";
				break;
			case 5:
				s=Card.rankAsString(value[1]) + " high straight";
				break;
			case 6:
				s="flush";
				break;
			case 7:
				s="full house " + Card.rankAsString(value[1]) + " over " + Card.rankAsString(value[2]);
				break;
			case 8:
				s="four of a kind " + Card.rankAsString(value[1]);
				break;
			case 9:
				s="straight flush " + Card.rankAsString(value[1]) + " high";
				break;
			default:
				s="error in Hand.display: value[0] contains invalid value";
		}
		s = "				" + s;
		System.out.println(s);
	}

	void displayAll()
	{
		for (int x=0; x<5; x++)
			System.out.println(cards[x]);
	}

	String compareTo(Hand that)
	{
		String win = "";
		for (int x=0; x<6; x++)
		{
			if (this.value[x]>that.value[x])
				return win = "You Win!";//return 1;
			else if (this.value[x]<that.value[x])
				return win = "You lose!";//return -1;
		}
		return win = "It's a tie!";//return 0; //if hands are equal
		
	}
}


Deck.Java

package javapoker;

import java.util.Random;
import java.util.ArrayList;

public class Deck {
	private ArrayList<Card> cards;

	 Deck()
	{
		cards = new ArrayList<Card>();
		int index_1, index_2;
		Random generator = new Random();
		Card temp;

		for (short a=0; a<=3; a++)
		{
			for (short b=0; b<=12; b++)
			 {
			   cards.add( new Card(a,B)/> );
			 }
		}

		int size = cards.size() -1;

		for (short i=0; i<100; i++)
		{
			index_1 = generator.nextInt( size );
			index_2 = generator.nextInt( size );

			temp = (Card) cards.get( index_2 );
			cards.set( index_2 , cards.get( index_1 ) );
			cards.set( index_1, temp );
		}
	}

	public Card drawFromDeck()
	{	   
		return cards.remove( cards.size()-1 );
	}

	public int getTotalCards()
	{
		return cards.size();  //we could use this method when making a complete poker game to see if we needed a new deck
	}
} 



Card.Java
package javapoker;

public class Card
{
	private short rank, suit;

	private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
	private static String[] ranks  = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

	public static String rankAsString( int __rank ) {
		return ranks[__rank];
	}

	Card(short suit, short rank)
	{
		this.rank=rank;
		this.suit=suit;
	}

	public @Override String toString()
	{
		  return ranks[rank] + " of " + suits[suit];
	}

	public short getRank() {
		 return rank;
	}

	public short getSuit() {
		return suit;
	}
}



Main.java
package javapoker;

import java.util.Scanner;

public class Main {
	

	
	public static void main(String[] args) {
		Deck deck = new Deck();
		int stop = 0;
		int dck;
		
		while (stop != 1) {
			Hand hand1 = new Hand(deck);
			Hand hand2 = new Hand(deck);
			String answer;
			String play;
			
			int discard = 0;
			int dcard;
			int disc=0;
			/*int[] ranks = new int[14];
			int[] orderedRanks = new int[5];
			int sameCards=1,sameCards2=1;
			int index=0;
			int aidraw;
			int sameSuit1 = 1;
			int sameSuit2 = 1;
			int sameSuit3 = 1;*/
			Scanner scn = new Scanner(System.in);
		
			hand1.evaluateHand();
			hand1.display();
			hand1.displayAll();

			hand2.evaluateHand();
			hand2.display();
			hand2.displayAll();				
		
		
			do  
			{
				System.out.println("Would you like to discard? y/n"); //Ask the player if they would like to discard.
				answer = scn.next();
			
			
				if ( answer.equalsIgnoreCase("y") ) {
					System.out.println("How many would you like to discard?"); //Ask them how many.
					discard = scn.nextInt();
								
					while (discard != 0) {
						System.out.println("Which card would you like to discard? (1-5)"); //Ask them which one.
						dcard = scn.nextInt();
						dcard = dcard-1;
						hand1.cards[dcard] = null; //Discard card
						hand1.cards[dcard] = deck.drawFromDeck(); //draw card
						discard--;
					} 
					
					hand1.evaluateHand();
					hand1.display();
					hand1.displayAll();

					hand2.aiHandEvaluation(deck);
				
						
					hand2.display();
					hand2.displayAll();	
					System.out.println(hand1.compareTo(hand2));
					disc++;
				}
				
				if (answer.equalsIgnoreCase("n")) {
					hand1.evaluateHand();
					hand1.display();
					hand1.displayAll();

					//hand2.aiHandEvaluation(hand2, deck);
					//AI opponent = new AI(hand2, deck);
					hand2.evaluateHand();
					hand2.display();
					hand2.displayAll();	
					System.out.println(hand1.compareTo(hand2));
					disc++;
				}
				
			} while (disc != 1);
			
			dck = deck.getTotalCards();
			
			if (dck <= 15 ){
				System.out.println("I'm sorry there aren't enough cards left.");
				break;
			}
			
			System.out.println("Would you like to play again?");
			play=scn.next();
			
			if ( play.equalsIgnoreCase("n") ) {
				stop++;
			}	
		}
			System.out.println("Aww... That's too bad. Come back again! Goodbye!");
	}
	
}

This post has been edited by macosxnerd101: 03 August 2011 - 06:41 AM
Reason for edit:: Added code tags around the Card and Main classes


Is This A Good Question/Topic? 0
  • +

Replies To: AI won't discard; Java Poker

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,175
  • Joined: 27-December 08

Re: AI won't discard; Java Poker

Posted 03 August 2011 - 06:44 AM

Look at this loop for discarding. To start, y = 0. Then the condition is evaluated. Since (0 > 4) == false, the loop is not executed. Perhaps you should compare to the length of the array (arrName.length) rather than 4, as well as use the < operator rather than the > operator.
for (int y=0; y>4; y++){


Was This Post Helpful? 3
  • +
  • -

#3 Nisho691  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-August 11

Re: AI won't discard; Java Poker

Posted 03 August 2011 - 04:08 PM

thanks for your help. The only other problem with the rest of it is we're having trouble telling the opponent what cards to save and which to discard. :/ Like say on a pair of twos, it seems to randomly save cards.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,175
  • Joined: 27-December 08

Re: AI won't discard; Java Poker

Posted 03 August 2011 - 04:31 PM

First things first. How do you determine which card(s) to discard? What criterion are you using? What is your logic?
Was This Post Helpful? 0
  • +
  • -

#5 Thess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-August 11

Re: AI won't discard; Java Poker

Posted 03 August 2011 - 11:37 PM

First I would really like to say I appreciate your help. It would have taken me forever to see that error. I only recently started learning Java from a few tutorials on here and I had thought for loops were more like a do until sort of thing rather than another do while thing. (Hence my major error) But here is the re-worked Hand Class, I modified it and cleaned it up a bit. Currently it looks first to see if it has a straight or a flush and if it does then it doesn't discard. Then it checks for the ranks of the cards and if they are the same it saves them into cardTemp. I have it setup so that it will recognize a pair, 2 pair, 3 of a kind and four of a kind. It saves the exact index of the cards of the hand into the cardTemp so the array is essentially the same. My idea was to have the cardTemp nulls be filled in by the drawFromDeck() method and then just set all the cards in the hand to be the ones in cardTemp. But the problem is that I am not sure how to tell it to find a null and fill that one via the drawFromDeck() method. Thanks for the help.

package javapoker;

import java.util.ArrayList;

public class Hand {
	
	public Card[] cards;
	public Card[] cardTemp;
	private int[] value;
	
	Hand(Deck d)
	{
		value = new int[6];
		cards = new Card[5];
		cardTemp = new Card[5];
		
		for (int x=0; x<5; x++)
		{
			cards[x] = d.drawFromDeck();
		}
	}
	
	public int evaluateHand() 
	{

		int[] ranks = new int[14];
		int[] orderedRanks = new int[5];	 //miscellaneous cards that are not otherwise significant
		boolean flush=true, straight=false;
		int sameCards=1,sameCards2=1;
		int largeGroupRank=0,smallGroupRank=0;
		int index=0;
		int topStraightValue=0;
		

		for (int x=0; x<=13; x++)
		{
			ranks[x]=0;
		}
		
		for (int x=0; x<=4; x++)
		{
			ranks[ cards[x].getRank() ]++;
		}
		
		for (int x=0; x<4; x++) {
			if ( cards[x].getSuit() != cards[x+1].getSuit() )
				flush=false;
		}

		for (int x=13; x>=1; x--)
		{
				 if (ranks[x] > sameCards)
				 {
					 if (sameCards != 1)  //if sameCards was not the default value
					 {
						 sameCards2 = sameCards;
						 smallGroupRank = x;
					 }

					 sameCards = ranks[x];
					 largeGroupRank = x;

				 } else if (ranks[x] > sameCards2)
				 {
					 sameCards2 = ranks[x];
					 smallGroupRank = x;
				 }
				              
		 }

		
		if (ranks[1]==1) //if ace, run this before because ace is highest card
		{
			orderedRanks[index]=14;
			index++;
		}

		
		for (int x=13; x>=2; x--)
		{
			if (ranks[x]==1)
			{
				orderedRanks[index]=x; //if ace
				index++;
			}
		}
		
		
		for (int x=1; x<=9; x++) //can't have straight with lowest value of more than 10
		{
			if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 && ranks[x+3]==1 && ranks[x+4]==1)
			{
				straight=true;
				topStraightValue=x+4; //4 above bottom value
				break;
			}
		}

		
		if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && ranks[13]==1 && ranks[1]==1) //ace high
		{
			straight=true;
			topStraightValue=14; //higher than king
		}
		
		
		for (int x=0; x<=5; x++)
		{
			value[x]=0;
		}


		 //start hand evaluation
		if ( sameCards==1 ) {
			value[0]=1;
			value[1]=orderedRanks[0];
			value[2]=orderedRanks[1];
			value[3]=orderedRanks[2];
			value[4]=orderedRanks[3];
			value[5]=orderedRanks[4];
		}

		if (sameCards==2 && sameCards2==1)
		{
			value[0]=2;
			value[1]=largeGroupRank; //rank of pair
			value[2]=orderedRanks[0];
			value[3]=orderedRanks[1];
			value[4]=orderedRanks[2];
		}

		if (sameCards==2 && sameCards2==2) //two pair
		{
			value[0]=3;
			value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank; //rank of greater pair
			value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
			value[3]=orderedRanks[0];  //extra card
		}

		if (sameCards==3 && sameCards2!=2)
		{
			value[0]=4;
			value[1]= largeGroupRank;
			value[2]=orderedRanks[0];
			value[3]=orderedRanks[1];
		}

		if (straight && !flush)
		{
			value[0]=5;
			value[1]=topStraightValue;
		}

		if (flush && !straight)
		{
			value[0]=6;
			value[1]=orderedRanks[0]; //tie determined by ranks of cards
			value[2]=orderedRanks[1];
			value[3]=orderedRanks[2];
			value[4]=orderedRanks[3];
			value[5]=orderedRanks[4];
		}

		if (sameCards==3 && sameCards2==2)
		{
			value[0]=7;
			value[1]=largeGroupRank;
			value[2]=smallGroupRank;
		}

		if (sameCards==4)
		{
			value[0]=8;
			value[1]=largeGroupRank;
			value[2]=orderedRanks[0];
		}

		if (straight && flush)
		{
			value[0]=9;
			value[1]=topStraightValue;
		}
		
		return value[0];
	}
	
	public Hand aiHandEvaluation(Deck d) {
		int[] ranks = new int[14];
		int[] orderedRanks = new int[5];
		boolean flush=true, straight=false;
		int sameCards=1,sameCards2=1;
		int index=0;
		int aidraw;
		
		
		cardTemp = new Card[5];
		
		for (int x=0; x<=13; x++)
		{
			ranks[x]=0;
		}
		
		for (int x=0; x<=4; x++)
		{
			ranks[ this.cards[x].getRank() ]++;
		}
		
		for (int x=13; x>=1; x--)
		{
				 if (ranks[x] > sameCards)
				 {
					 if (sameCards != 1)  //if sameCards was not the default value
					 {
						 sameCards2 = sameCards;
					 }

					 sameCards = ranks[x];

				 } else if (ranks[x] > sameCards2)
				 {
					 sameCards2 = ranks[x];
				 }
			
		}
		
		if (ranks[1]==1) //if ace, run this before because ace is highest card
		{
			orderedRanks[index]=14;
			index++;
		}

		
		for (int x=13; x>=2; x--)
		{
			if (ranks[x]==1)
			{
				orderedRanks[index]=x; //if ace
				index++;
			}
		}
		
		for (int x=0; x<4; x++) {
		if ( cards[x].getSuit() != cards[x+1].getSuit() )
				flush=false;
		}
		
		for (int x=1; x<=9; x++) //can't have straight with lowest value of more than 10
		{
			if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 && ranks[x+3]==1 && ranks[x+4]==1)
			{
				straight=true;
				break;
			}
		}

		
		if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && ranks[13]==1 && ranks[1]==1) //ace high
		{
			straight=true;
		}
	
		for (int x=1; x<5; x++)
		{
			if (this.cards[0].getRank() == this.cards[0+x].getRank())
			{	
				cardTemp[x] = this.cards[0+x];
				cardTemp[0] = this.cards[0];
			}

			if (this.cards[4].getRank() == this.cards[4-x].getRank())
			{	
				cardTemp[4-x] = this.cards[4-x];
				cardTemp[4] = this.cards[4];
			}
			
			if (this.cards[2].getRank() == this.cards[3].getRank())
			{	
				cardTemp[3] = this.cards[3];
				cardTemp[2] = this.cards[2];
			}
			
			if (this.cards[2].getRank() == this.cards[1].getRank())
			{	
				cardTemp[1] = this.cards[1];
				cardTemp[2] = this.cards[2];
			}			
			
			if (this.cards[1].getRank() == this.cards[3].getRank())
			{
				cardTemp[1] = this.cards[1];
				cardTemp[3] = this.cards[3];
			}
			
			if (this.cards[0].getRank() == this.cards[4].getRank())
			{
				cardTemp[0] = this.cards[0];
				cardTemp[4] = this.cards[4];
			}			
		}
		
		for (int x=0; x<5; x++)
		{
			System.out.println("cardTemp = " + cardTemp[x]);
		}
		
		if ( sameCards==1 && flush == true || straight == true ) {
		
			return this;
		}

		else if( sameCards==1 && flush != true || straight != true )
		{
			aidraw = 5;

			for (int y=0; y<5; y++){
			this.cards[y] = null; //Discard card
			}			
	
			for (int x=0; x<aidraw; x++){
				this.cards[x] = d.drawFromDeck(); //draw card
			}
		}
		
		if (sameCards==2 && sameCards2==1) //rank of pair
		{
				
			for ( int i=0; i<3; i++)
			{
				aidraw = cardTemp.indexOf("null");
				cardTemp[aidraw] = d.drawFromDeck();
			}

			for (int i=0; i<5; i++)
			{
				this.cards[i] = cardTemp[i];
			}
		
/*			aidraw = 4;
			
			for (int i=0; i<4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i<1; i++) {
				this.cards[i] = cardTemp[i];
			}			
			
			for (int x=2; x<aidraw; x++){
				this.cards[x] = d.drawFromDeck(); //draw card
			}			*/
		}

		if (sameCards==2 && sameCards2==2) //two pair
		{
			aidraw = 4;
			
			for (int i=0; i<4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i<3; i++) {
				this.cards[i] = cardTemp[i];
			}	
			
			for ( int x=4; x<aidraw; x++){
				this.cards[x] = d.drawFromDeck(); //draw card
			}
		}


		if (sameCards==4)
		{
			aidraw = 4;
			for (int i=0; i<4; i++) {
				this.cards[i] = null; //clear all cards
			}
			
			for (int i=0; i<3; i++) {
				this.cards[i] = cardTemp[i];
			}	
			
			for ( int x=4; x<aidraw; x++){
				this.cards[x] = d.drawFromDeck(); //draw card
			}		
		}
		
		return this;
	}
	
	void display()
	{
		String s;
		switch( value[0] )
		{

			case 1:
				s="high card";
				break;
			case 2:
				s="pair of " + Card.rankAsString(value[1]) + "\'s";
				break;
			case 3:
				s="two pair " + Card.rankAsString(value[1]) + " " + Card.rankAsString(value[2]);
				break;
			case 4:
				s="three of a kind " + Card.rankAsString(value[1]) + "\'s";
				break;
			case 5:
				s=Card.rankAsString(value[1]) + " high straight";
				break;
			case 6:
				s="flush";
				break;
			case 7:
				s="full house " + Card.rankAsString(value[1]) + " over " + Card.rankAsString(value[2]);
				break;
			case 8:
				s="four of a kind " + Card.rankAsString(value[1]);
				break;
			case 9:
				s="straight flush " + Card.rankAsString(value[1]) + " high";
				break;
			default:
				s="error in Hand.display: value[0] contains invalid value";
		}
		s = "				" + s;
		System.out.println(s);
	}

	void displayAll()
	{
		for (int x=0; x<5; x++)
			System.out.println(cards[x]);
	}

	String compareTo(Hand that)
	{
		String win = "";
		for (int x=0; x<6; x++)
		{
			if (this.value[x]>that.value[x])
				return win = "You Win!";//return 1;
			else if (this.value[x]<that.value[x])
				return win = "You lose!";//return -1;
		}
		return win = "It's a tie!";//return 0; //if hands are equal
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#6 Thess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-August 11

Re: AI won't discard; Java Poker

Posted 04 August 2011 - 08:08 PM

Never mind, I figured it out. Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1