14 Replies - 1371 Views - Last Post: 15 February 2012 - 10:32 PM Rate Topic: -----

#1 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Discard function for Java poker game

Posted 13 February 2012 - 08:48 PM

I'm writing a poker game (I really don't even know how to play poker). Currently, I'm writing a discard function inside of a class called humanPlayer that extends from a class called poker player.

What the discard function does is asks the human user how many and which cards to discard. Then, the cards will be removed from the player’s hand; the number of discarded cards will be returned to the main program which will then deal additional cards to the player.

I have some questions about the humanPlayer class first. First off, pokerPlayer is an abstract class so I think that means I can't do...
public humanPlayer(){
	super();	
	}


But now, the compiler is complaining because I don't have a constructor, but I wouldn't even have anything to put in the constructor anyway.

Here's what I'm thinking for the discard function (I have the idea down but am having some trouble applying it).
A message will print asking the user how many cards they want to discard (must be less than number of cards in their hand).
Then, I will print their cards on screen and ask them to enter the corresponding number of the card they want to remove, this question will be asked however for however many cards they want to remove. I'm thinking nested for loops?


For reference, here is the the classes:
-

Any help I get here I feel I can easily apply to the aiPlayer class.

Thanks guys, I could really use some tips and hints and ideas. This is only my second java program and it's exponentially harder than the first but I really want to get everything done right so I can have a good program in the end.

Thanks

This post has been edited by skatingrocker17: 14 February 2012 - 06:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Discard function for Java poker game

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Discard function for Java poker game

Posted 13 February 2012 - 09:07 PM

Since there is not a default constructor in the parent abstract class, you need a specific constructor the child class. for example in humanPlayer:
 

 public humanPlayer( int s, String N){

      pokerPlayer(s, N)   

 }




But you said that you do not need a constructor in humanPlayer, but then why do you need a constructor in pokerPlayer.
Was This Post Helpful? 1
  • +
  • -

#3 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Re: Discard function for Java poker game

Posted 13 February 2012 - 09:14 PM

View Postguido-granobles, on 13 February 2012 - 09:07 PM, said:

Since there is not a default constructor in the parent abstract class, you need a specific constructor the child class. for example in humanPlayer:
 

 public humanPlayer( int s, String N){

      pokerPlayer(s, N)   

 }




But you said that you do not need a constructor in humanPlayer, but then why do you need a constructor in pokerPlayer.


There's actually things to set in the parent class, but in the child class I don't have any values to initialize and I don't know what I would ever send to the constructor of the humanPlayer class because things like name are set in the parent class pokerPlayer.
Was This Post Helpful? 0
  • +
  • -

#4 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Discard function for Java poker game

Posted 13 February 2012 - 09:18 PM

If your intention is procted stake and name in such way that they cannot be aaccessible from child class that's the right approach but it just make sense if you are doing something especial with them. By now I can see that these attributes are use just in the methods get and set.

This post has been edited by guido-granobles: 13 February 2012 - 09:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Discard function for Java poker game

Posted 13 February 2012 - 09:24 PM

I think that you do not need that constructor in pokerplayer because there is only one place where you can call it (in the constructor of humanplayer) so
just remove it and also remove the constructor in the class humanplayer use the getter and setter in order to initialize the stake and name.
Was This Post Helpful? 1
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Discard function for Java poker game

Posted 13 February 2012 - 09:27 PM

To start off, typically class names in java start with an uppercase letter. A lower case letter signifies a method or variable identifier to most of us. Just a way to keep things standardized, but it in no way affects your code, so take that as you will.

As far as a discard method, you understand that the reason it is left abstract is because the way you get the cards to be discarded can be implemented differently (If you have a console program, a GUI, or if you are computer, or even advanced AI). So, if you are going to implement console input what you have looks really good. I would only suggest printing out the visible cards in your hand with the corresponding number by them so that there is no confusion:
> Which card would you like to discard?
> [0]Ace of Spades, [1]Queen of Hearts, [2]... etc.
> // user input here
So maybe a private void printHand() method in your humanPlayer class?

Lastly, and purely a choice I might consider renaming the class to ConsolePlayer to better express the implementation, but again... totally your choice. Your code looks really good so far, especially since you say it is only your second project! Congrats!

I hope that I was helpful!
Was This Post Helpful? 1
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Discard function for Java poker game

Posted 13 February 2012 - 09:28 PM

well with regard to the method reset() if you need call it in there then you can just let the constructor without any parameter.
Was This Post Helpful? 1
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Discard function for Java poker game

Posted 13 February 2012 - 09:38 PM

View Postguido-granobles, on 13 February 2012 - 11:24 PM, said:

I think that you do not need that constructor in pokerplayer because there is only one place where you can call it (in the constructor of humanplayer) -snip-
On the contrary, by implementing the constructor in the abstract superclass, you can avoid writing the code for it in ALL of the subclasses. Although, I believe if there is no default constructor in the abstract class, you must find a way to explicitly call it from a subclass. (ie. Using super(params...). ) And honestly, in your defense there isn't much code to avoid re-writing, but I see it as getting into good habit building for larger classes.
Was This Post Helpful? 1
  • +
  • -

#9 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Re: Discard function for Java poker game

Posted 13 February 2012 - 10:03 PM

View PostSheph, on 13 February 2012 - 09:27 PM, said:

To start off, typically class names in java start with an uppercase letter. A lower case letter signifies a method or variable identifier to most of us. Just a way to keep things standardized, but it in no way affects your code, so take that as you will.

As far as a discard method, you understand that the reason it is left abstract is because the way you get the cards to be discarded can be implemented differently (If you have a console program, a GUI, or if you are computer, or even advanced AI). So, if you are going to implement console input what you have looks really good. I would only suggest printing out the visible cards in your hand with the corresponding number by them so that there is no confusion:
> Which card would you like to discard?
> [0]Ace of Spades, [1]Queen of Hearts, [2]... etc.
> // user input here
So maybe a private void printHand() method in your humanPlayer class?

Lastly, and purely a choice I might consider renaming the class to ConsolePlayer to better express the implementation, but again... totally your choice. Your code looks really good so far, especially since you say it is only your second project! Congrats!

I hope that I was helpful!


Unfortunately, most of the code was provided my the instructor so I can't take credit for it, which is probably why it seems harder than it actually is because working with someone else's code as opposed to something you've written yourself is harder.

Most of what you're seeing besides the humanPlayer class was not written by me, I have written about 8 other function but I haven't posted them because they aren't relevant to the current issue. I can't really test them yet because I don't have enough done to have a working program yet, I've taken 3 prior C++ classes and this is the first program I've ever written dealing with AI.

In my humanPlayer class, after deleting my constructor I'm getting a warning on the line that says "public class humanPlayer.....". It says
-The type humanPlayer must implement the inherited abstract method pokerPlayer.discard(int, Hand)
-Implicit super constructor pokerPlayer() is undefined for default constructor. Must defube explicit constructor
Was This Post Helpful? 0
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Discard function for Java poker game

Posted 13 February 2012 - 10:21 PM

Yes, all abstract methods must be implemented by the subclass. You must define it with the exact parameters. Also, since your pokerPlayer class doesn't have a default constructor, you should call it like this in your humanPlayer constructor.
public class humanPlayer {
    public humanPlayer(int stake, String name) {
       super(stake, name);
    }

    public int discard(int handCode, Hand downCards) {
        // Don't forget to return int, and have your 2 parameters.
        // See your instructors comments above the abstract declaration in pokerPlayer.java
    }
}

Was This Post Helpful? 1
  • +
  • -

#11 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Re: Discard function for Java poker game

Posted 13 February 2012 - 10:38 PM

View PostSheph, on 13 February 2012 - 10:21 PM, said:

Yes, all abstract methods must be implemented by the subclass. You must define it with the exact parameters. Also, since your pokerPlayer class doesn't have a default constructor, you should call it like this in your humanPlayer constructor.
public class humanPlayer {
    public humanPlayer(int stake, String name) {
       super(stake, name);
    }

    public int discard(int handCode, Hand downCards) {
        // Don't forget to return int, and have your 2 parameters.
        // See your instructors comments above the abstract declaration in pokerPlayer.java
    }
}



Thank you that was very helpful. The constructor seems right and doing bring up and warnings or syntax errors. It seems that my method for discard with need some tweaking too.
Was This Post Helpful? 0
  • +
  • -

#12 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Re: Discard function for Java poker game

Posted 15 February 2012 - 08:11 PM

Okay so I've revised my code. Here's my question, when the user enters a number, how do I get that number to correspond with the card that they want to discard?

Also, I'm not sure how to actually discard the card back into the deck.

I'd like to program some AI in there but I really want to just get the function working first, by AI I mean if the computer player has certain down cards then I will discard cards based on that.

import java.util.Scanner;
public class humanPlayer extends pokerPlayer {

	public humanPlayer(int stake, String name) {
		super(stake, name);
	}



	public int discard(int handCode, Hand downCards){
		int i = 0;
		int j = 0;
		System.out.println("How many cards would you like to discard");
		System.out.println("0");
		System.out.println("1");
		System.out.println("2");
		Scanner sc = new Scanner(System.in);
		i = sc.nextInt();


		System.out.println("What card would you like to discard? Please enter the corresponding number.");

		if (i ==  1){
			printAll();
			j = sc.nextInt();
			return 1;

		}

		if (i ==  2){
			for (int h =0; h < i; h++){
				printAll();
				j = sc.nextInt();
				System.out.println("What card would you like to discard? Please enter the corresponding number.");
			}

			return 2;

		}
		
		else 
			return 0;

	}
}

Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Discard function for Java poker game

Posted 15 February 2012 - 10:21 PM

This is Java, an Object Oriented Programming language. I suggest you make a class called Card that has a value and a suit, also a Class called Deck that is an ArrayList of 52 Cards and methods to perform actions with the cards. Then you can create a driver class that will handle the users selection of actions that they want to perform with that deck of cards. Then once you have that working you can work on getting AI in there.

Here is a class similiar to what I am talking about. See how it has an object Item, then the ShoppingCart handles all actions taken with the Item. And the Shop class is the users selection of actions to be performed. This is what you need to consider doing for your poker game in a way with some minor changes to interaction to accommodate all features of your program.
Spoiler


Make Sense?
Was This Post Helpful? 0
  • +
  • -

#14 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Re: Discard function for Java poker game

Posted 15 February 2012 - 10:25 PM

I already have all of that.

I have
pokerDriver.java
pokerHand.java (extends Hand)
Hand.java
Deck.java
Card.java
pokerPlayer.java
humanPlayer.java (extends pokerPlayer)
aiPlayer.java (extends pokerPlayer)

But thank you, I will check out your example.
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Discard function for Java poker game

Posted 15 February 2012 - 10:32 PM

Sorry I saw words "Revised code" and didn't look up my apologies. So you are trying to figure out how to discard a card?

TO be able to tell you that we need to know how the classes interact with each other and we have only seen 1 class. Really I do not see the logic of separating a persons Hand from the Player class. Would it not be easier to make an array of 5 and have their hand in the Player class so you have direct access to it?

here is how I would do this:
Card.java - Object of a deck, has a value and suit.. duh. :)
Deck.java - ArrayList of 52 (Simply because I prefer arraylists over arrays) and will handle reshuffling and removing from the deck and card transfer into a players hand and dealing
Player1.java - Will handle the players hand, create methods to pass call fold hit etc.
Player2.java - similiar to 1
Poker.java - will be the driver to Run the program. Will have a deck object and options, use the Deck object's methods to get cards to a person etc. Using its methods to perform the Players selected option

This post has been edited by Fuzzyness: 15 February 2012 - 10:39 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1