Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot print cards in card game
    text
    copied!<p>I'm developing a java card game, which is close to the card game <em>hearts</em>. I have much of the structure, but now i'm stuck and i cannot get this to print. It has a variable amount of players and - I need it to deal out five cards to each player - To show which cards each player has received. I believe that there is a problem in the 'Session.dealHands'-method. Hope you guys can help.</p> <p><em><strong>Session class</em></strong></p> <pre><code>import java.util.ArrayList; import java.util.Collections; public class Session { // Variables static int numberOfPlayers = PlayGame.generateRandom(2, 10); Player player[] = new Player[numberOfPlayers]; static int numberOfRounds; static int leadPlayer = 1; // Initializes the class Deck and assign it to DeckOfCards static Deck deckOfCards = new Deck(); public static void initializeSession() { // initializes the deck from Deck class Deck.createDeck(); // Creates an array of players Player player[] = new Player[numberOfPlayers]; for (int i = 0; i &lt; Session.numberOfPlayers; i++) { player[i] = new Player(i); } // Makes the possible amount of rounds determined by numberOfPlayers maxRounds(); whoIsLeadPlayer(); System.out.println("Welcome - The Game has Startet"); System.out.println("\n" + "The number of Players " + numberOfPlayers); System.out.println("Number of rounds is: " + numberOfRounds); System.out.println("\n" + "The leadPlayer is: " + leadPlayer ); //playSession(player); } public static void whoIsLeadPlayer(){ //for(leadPlayer = 1; leadPlayer &lt; numberOfPlayers; leadPlayer++){ //Player pls = new Player(); //} } private static void maxRounds() { if (numberOfPlayers == 2) { numberOfRounds = 5; } else if (numberOfPlayers == 3) { numberOfRounds = 3; } else if (numberOfPlayers &lt; 6) { numberOfRounds = 2; } else { numberOfRounds = 1; } } public static void playSession(Player player[]) { for (int i = 0; i &lt; Session.numberOfRounds; i++) { dealHands(player); playRound(player); } } // Should be able to deal a Hand public static void dealHands(Player player[]) { for (int i = 0; i &lt; 4; i++){ Card currentCard = deckOfCards.getCard(PlayGame.generateRandom(0, 52)); player[i].hand.add(new Card(Card.rank, Card.suit)); //System.out.println(player[i].hand); } /*for (int i = 0; i &lt;= 4; i++) { Card currentCard = deckOfCards.getCard(PlayGame.generateRandom(0, deckOfCards.cards.size())); player[i].hand.add(new Card(currentCard.getRank(), Card.getSuit())); }*/ } public static void playRound(Player player[]) { for (int i = 0; i &lt; Session.numberOfPlayers; i++) { playTurn(player); } } public static void playTurn(Player player[]) { // placeCard(); } private static void placeCard() { } public static void printCards(Player player[]) { // Card card; for (int i = 0; i &lt;= Session.numberOfPlayers; i++) { for (int j = 0; j &lt;= player[i].hand.size(); j++) { Card card = player[i].hand.get(j); System.out.println("player" + i + "card " + card.getSuit() + "" + card.getRank()); } } } } </code></pre> <p><em><strong>Deck Class</em></strong></p> <pre><code>import java.util.ArrayList; public class Deck { // Create Arraylist to store deck of cards static ArrayList&lt;Card&gt; cards = new ArrayList&lt;Card&gt;(); // Adds numbers to the ArrayList public static void createDeck() { for (int suit = 0; suit &lt; 3; suit++) { for (int rank = 0; rank &lt; 12; rank++) { cards.add(new Card(suit, rank)); } } } // Get Card Method to get a card from the Card class public Card getCard(int iD) { //Card card = ID; Card gC = new Card(Card.getRank(), Card.getSuit()); cards.remove(iD); return gC; } } </code></pre> <p><em><strong>Card Class</em></strong></p> <pre><code>import java.util.*; public class Card { static int rank; static int suit; public static int getRank() { return rank; } public void setRank(int rank) { this.rank = rank; } public static int getSuit() { return suit; } public void setSuit(int suit) { this.suit = suit; } public Card(int rank, int suit) { this.rank = rank; this.suit = suit; } } </code></pre> <p><em><strong>Player Class</em></strong></p> <pre><code>import java.util.ArrayList; import java.util.Collections; public class Player { boolean leadPlayer; int score; ArrayList&lt;Card&gt; hand = new ArrayList&lt;Card&gt;(); // Getters and Setter methods to be implemented later public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getPlayerID() { return playerID; } public void setPlayerID(int playerID) { playerID = 1; this.playerID = playerID; } public ArrayList&lt;Card&gt; getHand() { return hand; } public void setHand(ArrayList&lt;Card&gt; hand) { this.hand = hand; } public boolean isLeadPlayer() { return leadPlayer; } public void setLeadPlayer(boolean leadPlayer) { Session.leadPlayer++; this.leadPlayer = leadPlayer; } int playerID; // Constructor to call when using a Player public Player(int playerID) { this.playerID = playerID; } } </code></pre> <p><em><strong>Main class</em></strong></p> <pre><code>import java.util.Random; public class PlayGame { // Starts the program public static void main(String[] args) { Session.initializeSession(); } public static int generateRandom(int min, int max) { Random r = new Random(); int random = r.nextInt(max - min) + min; return random; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload