using inheritance for a poker game

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 3790 Views - Last Post: 25 May 2009 - 07:02 PM Rate Topic: -----

#1 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

using inheritance for a poker game

Post icon  Posted 25 May 2009 - 05:59 AM

hi everyone, i need help with a project that i am doing.
i need to use inheritance and arrays to make a poker game, making a card class and a poker class that extends the card class
in the card class i would need to use arrays to make a deck, make suits and ranks, etc.
in the poker class i would need to check if the output cards are one pair, three of a kind, two pair, etc. and determine the winner
can anyone help me start off on how to use arrays in this project
before i used just regular statements to initialize a deck and do the other things
also can anyone give me suggestions on how to either not use if-else statements, greatly reduce the number of if-else statements used to determine the type of hand, or how to use arrays and inheritance to do this

i greatly appreciate any help and thank you so much for helping me
as of now im still working on this but i really need you help

i have my code so far
i forgot to add that the game must be two to four players so if anyone can help me with that i've tried to do that with the deck array there is no betting, checking, etc., only checking to see which hand wins and i need something that allows the game to be 2 to 4 players
would it be better to put a multidimensional array for the two to four players and the cards in the base class(card) or derived class(poker)?
would i need a toString method and equals method, accessor, mutator methods, constuctors?

note: this is the base class
the code doesn't exactly work, if you have any suggestions on how to make it work i would greatly appreciate it
also if you have any suggestions for the derived class(poker class) i would really appreciate it
thank you for any help

1.
import java.util.Random;
2.

3.
public class Card_Class
4.
{
5.
private int rank;
6.
private int suit;
7.

8.
public void makeCards()
9.
{
10.
//Class invariant: Deck of cards, shuffles deck, names ranks and suits
11.

12.
int [][] deck = new int[52][a];// deck of cards
13.

14.
for (int i = 0; i < 52; i++)//make a new deck
15.
for(int j = 0; j < i.length; j++)
16.
{
17.
deck[i] = i;
18.
}
19.

20.
Random generating = new Random();//create object of Random
21.

22.
for (int i = 0; i < 100; i++)// shuffle the deck by swapping random two cards 100 times
23.
for(int j = 0; j < i.length; j++)
24.
{
25.
int randPos1 = generating.nextInt(52);//generate two numbers
26.
int randPos2 = generating.nextInt(52);
27.

28.
int temp = deck[randPos1];//swap the two numbers to shuffle the deck
29.
deck[randPos1] = deck[randPos2];
30.
deck[randPos2] = temp;
31.
}
32.

33.
int rank = 0;
34.
int suit = 0;
35.
String output = null;
36.

37.
for (int i = 0; i < 5; i++)
38.
for(int j = 0; j < i.length; j++)
39.
{
40.
rank = (deck[i] % 13) + 1;
41.
suit = deck[i] / 13;
42.

43.
if (rank == 1)
44.
output = "Ace of ";
45.
else if (rank == 11)
46.
output = "Jack of ";
47.
else if (rank == 12)
48.
output = "Queen of ";
49.
else if (rank == 13)
50.
output = "King of ";
51.
else
52.
output = rank + " of ";
53.

54.
if (suit == 0)
55.
output += "Spades";
56.
else if (suit == 1)
57.
output += "Clubs";
58.
else if (suit == 2)
59.
output += "Hearts";
60.
else
61.
output += "Diamonds";
62.
System.out.println(output);
63.
}
64.
}
65.
} 

This post has been edited by java23: 25 May 2009 - 06:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: using inheritance for a poker game

#2 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 07:16 AM

i think i made some progress
public class Poker_Class extends Card_Class
{
	//Class invariant: Defines types of hands(straight, two pair, etc.), determines number of players, swap method to interchange up to 3 cards
	//Prints out the winner and what each person has 
	
	int x = (cardRank%13)-(cardRank2%13);//determine straight
				int ex = (cardRank2%13)-(cardRank3%13);
				int exx = (cardRank3%13)-(cardRank%13);
				
				boolean straight = false;
				boolean flush = false;
				boolean royal = false;
				
				if((cardRank==10)&&(cardRank2==11)&&(cardRank3==12))
					royal = true;
				else if((cardRank==10)&&(cardRank2==12)&&(cardRank3==11))
					royal = true;	
				else if((cardRank==11)&&(cardRank2==10)&&(cardRank3==12))
					royal = true;	
				else if((cardRank==11)&&(cardRank2==12)&&(cardRank3==10))
					royal = true;	
				else if((cardRank==12)&&(cardRank2==11)&&(cardRank3==10))
					royal = true;		
				else if((cardRank==12)&&(cardRank2==10)&&(cardRank3==11))
					royal = true;		
				else if((straight==true) && (flush==true))//test for straight flush
					System.out.println("Straight Flush ");
				else if((royal==true) && (straight==true) && (flush==true))//test for royal straight flush
					System.out.println("Royal Straight Flush");	
				else if((Math.abs(x)==1)&&(Math.abs(ex)==1)&&(Math.abs(exx)==2))//test for straight
				{
					straight = true;
					System.out.println("Straight");
				}
				else if((Math.abs(x)==1)&&(Math.abs(ex)==2)&&(Math.abs(exx)==1))
				{
					straight = true;
					System.out.println("Straight");
				}
				else if((Math.abs(x)==2)&&(Math.abs(ex)==1)&&(Math.abs(exx)==1))
				{
					straight = true;
					System.out.println("Straight");
				}
				else if((cardRank==0&&cardRank2==11&&cardRank3==12)||(cardRank==0&&cardRank2==12&&cardRank3==11))
				{
					straight = true;
					System.out.println("Straight");
				}
				else if((cardRank==11&&cardRank2==0&&cardRank3==12)||(cardRank==11&&cardRank2==12&&cardRank3==0))
				{
					straight = true;
					System.out.println("Straight");
				}
				else if((cardRank==12&&cardRank2==11&&cardRank3==0)||(cardRank==12&&cardRank2==0&&cardRank3==11))
				{
					straight = true;
					System.out.println("Straight");
				}
				else if((cardSuit==0)&&(cardSuit2==0)&&(cardSuit3==0))//test for flush
				{
					  flush = true;
					System.out.println(" Flush ");
				}
				else if((cardSuit==1)&&(cardSuit2==1)&&(cardSuit3==1))
				{
					  flush = true;
					System.out.println(" Flush ");
				}
				else if((cardSuit==2)&&(cardSuit2==2)&&(cardSuit3==2))
				{
					  flush = true;
					System.out.println(" Flush ");
				}
				else if((cardSuit==3)&&(cardSuit2==3)&&(cardSuit3==3))
				{
					  flush = true;
					System.out.println(" Flush ");
				}
				else if((rank==rank2)&&(rank2==rank3))// test for three of a kind
					System.out.println(" Three of a kind ");
				else if((rank==rank2)||(rank2==rank3)||(rank==rank3))// test for one pair
					System.out.println(" One pair ");
				else// high card
				{
					System.out.println("High Card");
				}
	
}

first is there any way of making this code better and in methods since i need to inherit from the card class
and how can i inherit the deck of cards, etc. from the card class to run my poker game
please i really need help urgently
thank you
Was This Post Helpful? 0
  • +
  • -

#3 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 09:11 AM

Couple of things:

for (int i = 0; i < 52; i++)//make a new deck
	  for(int j = 0; j < i.length; j++) {
		 deck[i] = i;
	  }


can just be:
for (int i = 0; i < 52; i++)//make a new deck
		 deck[i] = i;
	  }


This code should A) be a method of its own, and B) take out "for(int j = 0; j < i.length; j++)" because you'd be doing it more then 100 times:
Random generating = new Random();//create object of Random

   for (int i = 0; i < 100; i++)// shuffle the deck by swapping random two cards 100 times
	  for(int j = 0; j < i.length; j++)
	  {
		 int randPos1 = generating.nextInt(52);//generate two numbers
		 int randPos2 = generating.nextInt(52);

		 int temp = deck[randPos1];//swap the two numbers to shuffle the deck
		 deck[randPos1] = deck[randPos2];	
		 deck[randPos2] = temp;
	  }


Again, This code should A) be a method of its own, and B) take out "for(int j = 0; j < i.length; j++)":
for (int i = 0; i < 5; i++)
	  for(int j = 0; j < i.length; j++)
	  {
	  rank = (deck[i] % 13) + 1;
	  suit = deck[i] / 13;

	  if (rank == 1)
		 output = "Ace of ";
	  else if (rank == 11)
		 output = "Jack of ";
	  else if (rank == 12)
		 output = "Queen of ";
	  else if (rank == 13)
		 output = "King of ";
	  else
		 output = rank + " of ";

	  if (suit == 0)
		 output += "Spades";
	  else if (suit == 1)
		 output += "Clubs";
	  else if (suit == 2)
		 output += "Hearts";
	  else
		 output += "Diamonds";
	  System.out.println(output);
   }


What are you trying to accomplish with this? the variables are too vague and the math doesn't make sense:
int x = (cardRank%13)-(cardRank2%13);//determine straight
	int ex = (cardRank2%13)-(cardRank3%13);
	int exx = (cardRank3%13)-(cardRank%13);


and now after looking through both files, why are you doing this in the Card Class:
int [][] deck = new int[52][a];// deck of cards

you never access the second dimension.

Hope this helps,
Philippe
Was This Post Helpful? 0
  • +
  • -

#4 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 10:40 AM

here is my "improved" card class code(forget about the derived class i really need help and need to work on it)
the thing is that for the number of players thing the cards repeat themselves if you don't understand please compile my code and see what i mean i really need help with this
thank you for your help

import java.util.Random;
import java.util.Scanner;

public class Card_Class 
{
	private static int rank;
	private static int suit;
	
	public static void main(String[] args)
	{
		//Class invariant: Deck of cards, shuffles deck, names ranks and suits
		
		int [] deck = new int[52];// deck of cards

		for(int i = 0; i < 52; i++)//make a new deck
		{
			deck[i] = i;
		}
	
		Random generating = new Random();//create object of Random
		
		for(int i = 0; i < 1000; i++)// shuffle the deck by swapping five random cards 1000 times 
		{
			int randPos1 = generating.nextInt(52);//generate five numbers
			int randPos2 = generating.nextInt(52);
			int randPos3 = generating.nextInt(52);
			int randPos4 = generating.nextInt(52);
			int randPos5 = generating.nextInt(52);

			int temp = deck[randPos1];//swap the five numbers to shuffle the deck
			deck[randPos1] = deck[randPos2];
			deck[randPos2] = deck[randPos3];
			deck[randPos3] = deck[randPos4];
			deck[randPos4] = deck[randPos5];
			deck[randPos5] = temp;
		}
		
		int rank = 0;
		int suit = 0;
		String output = null;
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("Enter the number of players.");
		int p = keyboard.nextInt();
		
		System.out.println();
		
		if(p == 4)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 4; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else if(p == 3)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 3; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else if(p == 2)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 2; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else
			System.out.println("Error: only two to four players allowed.");
	}		
}

Was This Post Helpful? 0
  • +
  • -

#5 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 10:59 AM

oh, so you're saying there are repeating cards in people's decks?

This post has been edited by phil1000: 25 May 2009 - 11:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 11:29 AM

View Postphil1000, on 25 May, 2009 - 09:59 AM, said:

oh, so you're saying there are repeating cards in people's decks?

well, in each person's hand
do you know how i can fix that
and could you give me any suggestions on how to proceed if you can
thank you
Was This Post Helpful? 0
  • +
  • -

#7 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 11:42 AM

Yeah, I actually had a similar project, but i was coding the game of war. All you gotta do is start with a main master deck with all the cards. Then you need to shuffle, then deal out.

Hope that helps,
Philippe
Was This Post Helpful? 0
  • +
  • -

#8 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 11:45 AM

View Postphil1000, on 25 May, 2009 - 10:42 AM, said:

Yeah, I actually had a similar project, but i was coding the game of war. All you gotta do is start with a main master deck with all the cards. Then you need to shuffle, then deal out.

Hope that helps,
Philippe

so is what i did so far ok
or can i add more to this class to make it better
and can you give me a suggestion with the repeating cards thing on how to fix it
because i am really stuck on that
thanks

This post has been edited by java23: 25 May 2009 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#9 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 11:58 AM

first off change the top of your class to this:
public class Card_Class 
{
	private static int rank;
	private static int suit;
	
	public Card_Class(rank, suit){
	   this.rank = rank;
	   this.suit = suit;
	}


Then to create your master deck:
		//Class invariant: Deck of cards, shuffles deck, names ranks and suits
		
		Card_Class [] deck = new Card_Class[52];// deck of cards

		for(int  x = 0; x < 4; x++){
		 for(int i = 0; i < 13; i++)//make a new deck
		 {
			deck[i + x] = new Card_Class(x, i);
		 }
	   }

Was This Post Helpful? 0
  • +
  • -

#10 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 12:09 PM

public Card_Class(rank, suit){
this.rank = rank;
this.suit = suit;
}

for this line i get a compiler error saying that an identifier is expected

also is there any way that my code doesn't have to be in the main method
and would i need accessor and mutator methods
and a toString and equals methods
thanks
Was This Post Helpful? 0
  • +
  • -

#11 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 12:25 PM

oh, my bad.
public Card_Class(int rank, int suit){
this.rank = rank;
this.suit = suit;
}

Accessors:
public int getRank(){
   return rank;
}

public int getSuit(){
   return suit;
}


Mutators:
public void setRank(int rank){
   this.rank = rank;
}

public void setSuit(int suit){
   this.suit = suit;
}



your toString method:
public String toString(){
   return suit + " " + rank;
}



your equals method:
public boolean equals(Card_Class card){
   return this.rank.equals(card.getRank) && this.suit.equals(card.getSuit);
}


to get your code out of main you need to make a driver class say.... Game.java and that just has main and runs the game.

Hope this helps,
Philippe
Was This Post Helpful? 0
  • +
  • -

#12 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 12:42 PM

im sorry to be a bother but is it possible for you to check my code because im getting a lot of errors
thank you so much for your help

import java.util.Random;
import java.util.Scanner;

public class Card_Class_Final 
{
	private static int rank;
	private static int suit;
	
	public Card_Class_Final()
	{
		rank = 0;
		suit = 0;
	}
	
	public Card_Class_Final(int rank, int suit)
	{
		this.rank = rank;
		this.suit = suit;
	}
	
	public int getRank()
	{
   		return rank;
	}

	public int getSuit()
	{
   		return suit;
	}
	
	public void setRank(int rank)
	{
   		this.rank = rank;
	}

	public void setSuit(int suit)
	{
   		this.suit = suit;
	}
	
	public String toString()
	{
   		return (suit + " " + rank);
	}

	public boolean equals(Card_Class_Final card)
	{
   		return (this.rank.equals(card.getRank()) && this.suit.equals(card.getSuit()));
	}

	
	public static void main(String[] args)
	{
		//Class invariant: Deck of cards, shuffles deck, names ranks and suits
		
		Card_Class_Final[] deck = new Card_Class_Final[52];// deck of cards
		
		for(int x = 0; x < 4; x++)
			for(int i = 0; i < 13; i++)//make a new deck
			{
				deck[i+x] = new Card_Class_Final (x,i);
			}
	
		Random generating = new Random();//create object of Random
		
		for(int i = 0; i < 1000; i++)// shuffle the deck by swapping two random cards 1000 times 
		{
			int randPos1 = generating.nextInt(52);//generate five numbers
			int randPos2 = generating.nextInt(52);
			int randPos3 = generating.nextInt(52);
			int randPos4 = generating.nextInt(52);
			int randPos5 = generating.nextInt(52);

			int temp = deck[randPos1];//swap the five numbers to shuffle the deck
			deck[randPos1] = deck[randPos2];
			deck[randPos2] = deck[randPos3];
			deck[randPos3] = deck[randPos4];
			deck[randPos4] = deck[randPos5];
			deck[randPos5] = temp;
		}
		
		int rank = 0;
		int suit = 0;
		String output = null;
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("Enter the number of players.");
		int p = keyboard.nextInt();
		
		System.out.println();
		
		if(p == 4)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 4; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else if(p == 3)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 3; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else if(p == 2)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 2; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else
			System.out.println("Error: only two to four players allowed.");
	}		
}

Was This Post Helpful? 0
  • +
  • -

#13 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 01:03 PM

What errors are you getting?
Was This Post Helpful? 0
  • +
  • -

#14 java23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-May 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 01:09 PM

all sorts of errors coming from inside the main method
i tried to fix them but failed
Was This Post Helpful? 0
  • +
  • -

#15 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: using inheritance for a poker game

Posted 25 May 2009 - 01:19 PM

That doesn't help me at all.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2