4 Replies - 790 Views - Last Post: 19 February 2012 - 03:40 PM Rate Topic: -----

#1 skatingrocker17  Icon User is offline

  • D.I.C Head

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

Cannot make a static reference to the non-static method

Posted 19 February 2012 - 02:52 PM

I'm writing a poker function in which I need to discard cards back into a poker deck. I'm writing the function in a class called humanPlayer that extends a class called pokerPlayer. The problem is that I need to access a function a class called Hand. The function is called removeCard. Hand also has a function that extends it called pokerHand.

Is there any way around this so I can send what I need to the remove card function to remove the card?

Here's an exmaple of one of my discard functions:
	if (i ==  2){
			for (int h =0; h < i; h++){
				System.out.println("What card would you like to discard? Please enter the corresponding number.");
				printAll();
				j = sc.nextInt();
				pokerHand.removeCard(j);	
			}

			return 2;

		}


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot make a static reference to the non-static method

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Cannot make a static reference to the non-static method

Posted 19 February 2012 - 03:03 PM

What functionality does the class HumanPlayer add to the class PokerPlayer? I'm wondering if you need it, and that may not have anything to do with your question, because I'm not sure I understand your question or what the problem is.

But I'll try: If you need to access a function in a class called Hand, why don't you make an instance of Hand which I assume could be used to define your poker hand? Then you wouldn't be making a non-static reference, you'd be referencing removeCard() from an instance of Hand.

Hand pokerHand = new Hand();
// build the poker hand, then as needed:
pokerHand.removeCard( card );
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: Cannot make a static reference to the non-static method

Posted 19 February 2012 - 03:27 PM

View PostGregBrannon, on 19 February 2012 - 03:03 PM, said:

What functionality does the class HumanPlayer add to the class PokerPlayer? I'm wondering if you need it, and that may not have anything to do with your question, because I'm not sure I understand your question or what the problem is.

But I'll try: If you need to access a function in a class called Hand, why don't you make an instance of Hand which I assume could be used to define your poker hand? Then you wouldn't be making a non-static reference, you'd be referencing removeCard() from an instance of Hand.

Hand pokerHand = new Hand();
// build the poker hand, then as needed:
pokerHand.removeCard( card );



There's humanPlayer and aiPlayer. Both will have a similar discard function. They both extend a class called pokerPlayer which has functions like reset, takeCard, setStake, getStake, printAll, printVisible, bestHand, getHand, getName and downCard.

The discard function is also in the pokerPlayer function as
	/**
	 * Method discard
	 *
	 * TODO: override in subclasses
	 *       add appropriate logic for sub-class type
	 *       may alter orginal ordering of cards
	 * return # of cards discarded
	 * handCode: the value of the hand 
	 * downCards:  the other players visible cards
	 * @return
	 */
	public abstract int discard( int handCode, Hand downCards);


There needs to be two classes that extend off pokerPlayer because humanPlayer will get input from the user and aiPlayer will use artificial intelligence of some kind by looking at the humanPlayers down cards and discarding based on those. I'm not sure how yet because I don't even know how to play poker :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Cannot make a static reference to the non-static method

Posted 19 February 2012 - 03:37 PM

Just a thought: I don't know your complete design so could be way off, but I don't understand why you can't create instances of PokerPlayer for both humanPlayer and aiPlayer:

PokerPlayer humanPlayer = new PokerPlayer();
PokerPlayer aiPlayer = new PokerPlayer();

Since both humanPlayer and aiPlayer need to discard cards, removeCard() (or I like disCard()) could be a method in the PokerPlayer class, or maybe it should be in the Hand class.

The reason to extend an existing class is to add functionality to it or change something about it (E.g., override existing methods), and I'm not sure you need those additional classes when an instance of your existing class might do.

A good discussion, but we did wander from your original question. Come back to it when you're ready.
Was This Post Helpful? 1
  • +
  • -

#5 skatingrocker17  Icon User is offline

  • D.I.C Head

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

Re: Cannot make a static reference to the non-static method

Posted 19 February 2012 - 03:40 PM

View PostGregBrannon, on 19 February 2012 - 03:37 PM, said:

Just a thought: I don't know your complete design so could be way off, but I don't understand why you can't create instances of PokerPlayer for both humanPlayer and aiPlayer:

PokerPlayer humanPlayer = new PokerPlayer();
PokerPlayer aiPlayer = new PokerPlayer();

Since both humanPlayer and aiPlayer need to discard cards, removeCard() (or I like disCard()) could be a method in the PokerPlayer class, or maybe it should be in the Hand class.

The reason to extend an existing class is to add functionality to it or change something about it (E.g., override existing methods), and I'm not sure you need those additional classes when an instance of your existing class might do.

A good discussion, but we did wander from your original question. Come back to it when you're ready.


The new instances of the players are created in the driver class. I agree with you, if I had written all of the code I wouldn't of laid it out that way but most of it is provided for us and we're just supposed to complete a few functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1