カードゲームのハートに近いJavaカードゲームを開発しています。私は多くの構造を持っていますが、今は行き詰まっていて、これを印刷することができません。プレイヤーの数はさまざまで、各プレイヤーに5枚のカードを配る必要があります。各プレイヤーがどのカードを受け取ったかを示すためです。'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;
}
}