我正在開發一款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;
}
}
請發佈[SSCCE](http://sscce.org/),而不是整個項目。 –
您可能還想告訴我們您的代碼是如何行事不端的。你是否遇到過例外情況?每張卡都是一樣的嗎(我懷疑是這種情況)? –