我正在开发一款Java卡游戏,它接近纸牌游戏 hearts 。我有很多结构,但现在我卡住了,我无法打印出来。它有不同数量的球员和 - 我需要它为每个玩家发出五张牌 - 显示每位玩家收到的牌。 我相信'Session.dealHands'方法存在问题。希望你们能帮忙。
会话类
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 :(得分: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;
}
这没有任何意义,因为没有Card会有自己的套装和等级字段,因为它们将是类的属性而不是实例。将它们全部设为 非静态 实例字段和方法。
事实上,除了main方法之外,上面代码中的任何内容都不应该是静态的。
修改强>
此外,这需要改变:
player[i].hand.add(new Card(Card.rank, Card.suit));
您正试图获得 类 卡的等级和适合度,这在逻辑上并不合理。你想用这个来实现什么?