Need help with a card game

Need to make Go Fish

Page 1 of 1

6 Replies - 3099 Views - Last Post: 04 November 2009 - 05:42 PM Rate Topic: -----

#1 JebusCrust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-May 09

Need help with a card game

Post icon  Posted 04 November 2009 - 07:19 AM

I have to make the beginning of the card game Go Fish.
The output will look similar to this:

0) Ace of Spades: 0
1) Ace of Hearts: 0
2) Ace of Clubs: 0
3) Ace of Diamonds: 2
4) Two of Spades: 0
5) Two of Hearts: 1
...and so on.

It will also display the contents of both the player and computers hand

The numbers at the end (0,1,2) represent the location of the card. 0 being in the deck, 1 being in the players hand, and 2 being in the computers hand.

I am currently having trouble assigning the cards to the player and computer. Also having problems showing the hands at the end. I am not really sure how to fix the methods to show the right output.

Here is the current code I have written.

import java.util.Random;


public class GoFish {
	
  public static int[] cardLoc = {
	  0,
	  1,
	  2,
  };
  
  public static String[] suitName = {
	  "Spades",
	  "Hearts",
	  "Clubs",
	  "Diamonds",
  };
  public static String[] rankName = {
	  "Ace",
	  "Two",
	  "Three",
	  "Four",
	  "Five",
	  "Six",
	  "Seven",
	  "Eight",
	  "Nine",
	  "Ten",
	  "Jack",
	  "Queen",
	  "King",
  };
  
  public static final int DECK = 0;
  public static final int PLAYER = 1;
  public static final int COMP = 2;
  
  public static void main(String[] args) {
	clearDeck();
	for (int i = 0; i < 5; i++){
	  assignCard(PLAYER);
	  assignCard(COMP);
	} // end for loop
	showDeck();
  
	showHand(PLAYER);
	showHand(COMP);
  
  } // end main

public static int assignCard(int cards) {
	//give cards to player
	Random generator = new Random();
	
	for(cards = 0; cards < 5; cards++){
	cards = generator.nextInt(52); //number of cards
	}
	
	return cards;
}

public static void clearDeck() {
	//clear contents of deck
	
}


	
public static void showDeck() {
	//display cards in deck
	int x =1;
	for (int j = 0; j < rankName.length; j++){
		for (int i = 0; i < suitName.length; i++){
		System.out.println(x + ") " + rankName[j] + " of " + suitName[i]);
		x++;
		}//end for
	}//end for
}



public static void showHand(int player) {
	//show hand of players
	System.out.println(assignCard(player));
	
}
} // end class def



Any help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a card game

#2 JebusCrust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-May 09

Re: Need help with a card game

Posted 04 November 2009 - 10:21 AM

can anyone tell me what I'm doing wrong???

This post has been edited by JebusCrust: 04 November 2009 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 JebusCrust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-May 09

Re: Need help with a card game

Posted 04 November 2009 - 11:50 AM

anyone at all
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Need help with a card game

Posted 04 November 2009 - 11:55 AM

To start with, what do you think this is doing, and what is it really supposed to do?
public static int assignCard(int cards) {
	//give cards to player
	Random generator = new Random();
	
	for(cards = 0; cards < 5; cards++){
		cards = generator.nextInt(52); //number of cards
	}
	
	return cards;
}

Was This Post Helpful? 0
  • +
  • -

#5 JebusCrust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-May 09

Re: Need help with a card game

Posted 04 November 2009 - 12:10 PM

Honestly I dont know what it is doing, which is why I came here to get a bit of help.
it is supposed to give 5 cards to the player and 5 to the computer without repeating any certain card.
I am not sure how I could do this.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Need help with a card game

Posted 04 November 2009 - 01:06 PM

The best approach for your problem, would be to hold all cards inside a list (not in array)
use
Collections.shuffle(List list);//list is your card list


this method will shuffle your list, just as you shuffle your game cards before starting to play.
and then all you have to do is retrieving the first element pass it to a player, and then delete it from the list.
it is very similar to a deck of cards.
in a card game you actually first shuffle the deck.
then fetch the first card form the deck.
then the card is no longer in the deck, but it is in one of the player's hand.
that is what we want to implement in the code.

remember, if you choose to shuffle your list, you dont have to use random any more.
the shuffle method will actually "create" a random list for you.
hope it helps :)
Was This Post Helpful? 0
  • +
  • -

#7 JebusCrust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-May 09

Re: Need help with a card game

Posted 04 November 2009 - 05:42 PM

I have almost finished it!

I have just a couple more problems.

0) Ace of Spades: 0
1) Ace of Hearts: 0
2) Ace of Clubs: 0
3) Ace of Diamonds: 2
4) Two of Spades: 0
5) Two of Hearts: 1

I am having problems with the showhand() method and showdeck() method.
The showhand() method is supposed to display the five cards that are in the hand of PLAYER and COMP.
I need to use

suit = cardNum / 13
and
rank = cardNum % 13

to find and display the suit and rank of the card but I am having problems with this. any input would be appreciated


import java.util.*;

public class Tester {
  public static int[] cardLoc = new int[52];
  
  public static String[] suitName = {"Hearts", "Spades", "Clubs", "Diamonds"};
  public static String[] rankName = {"Ace", "2", "3", "4", "5", "6", "7", "8",
									   "9", "10", "Jack", "Queen", "King"};
  
  public static final int DECK = 0;
  public static final int PLAYER = 1;
  public static final int COMP = 2;
  
  public static void main(String[] args) {
	clearDeck();
	for (int i = 0; i < 5; i++){
	  assignCard(PLAYER);
	  assignCard(COMP);
	} // end for loop
	showDeck();
  
	showHand(PLAYER);
	showHand(COMP);
  
  } // end main

  public static void clearDeck(){
	  //go through the deck
	  for (int cardNum = 0; cardNum < cardLoc.length; cardNum++){
		  //set each card's location to zero
		  cardLoc[cardNum] = 0;
	  } // end for
  
  } // end clearDeck
  
  public static void assignCard(int hand){
	  Random generator = new Random();
	  //pick a random number between 0 and 51
	  int cardNum = generator.nextInt(52);
	  
	  //check to see if the card is already taken  -
	  int currentLoc = cardLoc[cardNum];
	  
	  while (currentLoc != 0){
		  cardNum = generator.nextInt(52);
		  currentLoc = cardLoc[cardNum];
	  } // end while
	  cardLoc[cardNum] = hand;
  }// end assignCard

  public static void showDeck(){
	  //go through the deck, 
	  for (int cardNum = 0; cardNum < cardLoc.length; cardNum++){
		//printing the location of each card
		System.out.println(cardNum + ") " + cardLoc[cardNum]);
	  } // end for
  } // end showdeck
  
  public static void showHand(int hand){
	 int cardNum = cardLoc[52];
	 int suit = cardNum / 13;
	 int rank = cardNum % 13;
	 System.out.println(rank + suit);
  }
} // end class def


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1