2013-01-12 85 views
-2

我正在開發一款java卡片遊戲,它接近卡片遊戲心臟。我有很多結構,但現在我卡住了,我無法打印。它擁有數量可變的玩家和 - 我需要它爲每個玩家發放五張牌 - 顯示每個玩家收到的牌。 我相信'Session.dealHands'方法存在問題。希望你們能幫忙。卡片遊戲不能打印卡片

Session類

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 < 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 < numberOfPlayers; leadPlayer++){ 
     //Player pls = new Player(); 

    //} 


} 

    private static void maxRounds() { 
      if (numberOfPlayers == 2) { 
       numberOfRounds = 5; 
      } else if (numberOfPlayers == 3) { 
       numberOfRounds = 3; 
      } else if (numberOfPlayers < 6) { 
       numberOfRounds = 2; 
      } else { 
       numberOfRounds = 1; 
      }  
    } 

    public static void playSession(Player player[]) { 
     for (int i = 0; i < 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 < 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 <= 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 < 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 <= Session.numberOfPlayers; i++) { 
      for (int j = 0; j <= player[i].hand.size(); j++) { 
       Card card = player[i].hand.get(j); 
       System.out.println("player" + i + "card " + card.getSuit() + "" 
         + card.getRank()); 

      } 
     } 
    } 
} 

甲板類

import java.util.ArrayList; 

public class Deck { 
    // Create Arraylist to store deck of cards 
    static ArrayList<Card> cards = new ArrayList<Card>(); 

    // Adds numbers to the ArrayList 
    public static void createDeck() { 
     for (int suit = 0; suit < 3; suit++) { 
      for (int rank = 0; rank < 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; 
    } 
} 

卡類

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; 
    } 
} 

球員

import java.util.ArrayList; 
import java.util.Collections; 

public class Player { 

    boolean leadPlayer; 
    int score; 
    ArrayList<Card> hand = new ArrayList<Card>(); 

    // 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<Card> getHand() { 
     return hand; 
    } 

    public void setHand(ArrayList<Card> 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; 
    } 
} 

主類

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; 
    } 
} 
+0

請發佈[SSCCE](http://sscce.org/),而不是整個項目。 –

+0

您可能還想告訴我們您的代碼是如何行事不端的。你是否遇到過例外情況?每張卡都是一樣的嗎(我懷疑是這種情況)? –

回答

3

請改掉這些靜態方法和字段!

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; 
    } 

這是沒有意義的,因爲沒有卡都會有自己的西裝和排名領域,因爲它們將是類而不是實例的屬性。讓它們全部爲非靜態實例字段和方法。

事實上,除了主要的方法,上面的代碼中可能沒有任何東西是靜態的。

編輯:
此外,這需要改變:

player[i].hand.add(new Card(Card.rank, Card.suit)); 

你試圖讓卡的點數和花色這並沒有真正的邏輯感。你想用這個做什麼?

+0

好的,但我應該如何能夠在Session類中實現它們?它抱怨當我刪除靜態。 :) – user1938165

+0

@ user1938165:你正在修理錯誤的東西。它並不是告訴你使用靜態,而是說你不能以靜態方式訪問非靜態字段。解決方法是不要試圖以靜態方式訪問它們。只能通過Card實例(對象)訪問它們,而不能通過Card類訪問它們。請參閱上面的編輯以回答。 –