我正在为APCS类制作Java多人二十一点游戏。我需要能够将玩家和发牌者都存储在arraylist中。实例化arraylist的正确方法是什么?这是问题所在的类。
package BlackJack;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import static java.lang.Character.*;
import static java.lang.System.*;
import javax.swing.JPanel;
import java.util.ArrayList;
import BlackJack.Card;
import BlackJack.BlackJackCard;
import BlackJack.Deck;
import BlackJack.AbstractPlayer;
import BlackJack.Player;
import BlackJack.Dealer;
public class TheMultiPlayerGame extends JPanel implements KeyListener {
ArrayList<Object> Ai = new ArrayList<Object>(4);
private Dealer dealer = new Dealer();
private Player[] playa = new Player[3];
private Player player;
private boolean start;
private boolean hit;
private boolean finish;
private boolean show;
private Font font;
int playerTotal = 0;
int dealerTotal = 0;
int playerWinTotal = 0;
int dealerWinTotal = 0;
String j = "";
String p = "";
public TheMultiPlayerGame() {
setBackground(Color.WHITE);
font = new Font("TAHOMA", Font.BOLD, 12);
this.addKeyListener(this);
dealer = new Dealer();
Ai.set(1, playa[0]);
Ai.set(2, playa[1]);
Ai.set(3, playa[2]);
Ai.set(0, dealer);
}
public void paintComponent(Graphics window) {
super.paintComponent(window);
window.setColor(Color.blue);
window.setFont(font);
window.drawString("BlackJack GUI", 25, 50);
window.drawString("PRESS B to start/restart the game.", 25, 100);
window.drawString("PRESS H to hit - PLAYER", 325, 100);
window.drawString("PRESS F to finish(DEALER hits).", 25, 130);
window.drawString("PRESS X to see the results.", 325, 130);
window.drawString(j, 10, 530);
window.drawString(p, 10, 90);
window.drawString("Player WinCount = " + playerWinTotal, 600, 100);
window.drawString("Dealer WinCount = " + dealerWinTotal, 600, 130);
window.drawString("Dealer Score = " + dealerTotal, 600, 200);
window.drawString("Player Score = " + playerTotal, 600, 250);
Ai.set(1, playa[0]);
Ai.set(2, playa[1]);
Ai.set(3, playa[2]);
Ai.set(0, dealer);
if (start == true) {
//reset the dealer and player hands
dealer = new Dealer();
player = new Player();
dealer.shuffle();
Ai.set(0, dealer);
System.out.println(Ai.get(0).numCardsLeftInDeck());
//add two cards to dealer and player
for (int x = 1; x < Ai.size(); x++) {
Ai.get(x).addCardToHand(Ai.get(0).deal());
Ai.get(x).addCardToHand(Ai.get(0).deal());
}
playerTotal = player.getHandValue();
dealerTotal = dealer.getHandValue();
repaint();
start = false;
}
else if (hit == true) {
if (playerTotal < 21) {
player.addCardToHand(dealer.deal());
playerTotal = player.getHandValue();
repaint();
}
hit = false;
}
else if (finish == true) {
dealer.addCardToHand(dealer.deal());
dealerTotal = dealer.getHandValue();
finish = false;
show = true;
}
if (show == true) {
//determine who won the game
dealerTotal = dealer.getHandValue();
playerTotal = player.getHandValue();
if (playerTotal > 21 && dealerTotal <= 21) {
j = "\nDealer wins - Player busted! " + playerTotal + " : " + dealerTotal;
dealerWinTotal++;
}
else if (playerTotal <= 21 && dealerTotal > 21) {
j = "\nPlayer wins - Dealer busted! " + playerTotal + " : " + dealerTotal;
playerWinTotal++;
}
else if (playerTotal > 21 && dealerTotal > 21) {
j = "Both players bust!";
}
else if (playerTotal < dealerTotal) {
j = "\nDealer has bigger hand value! " + playerTotal + " : " + dealerTotal;
dealerWinTotal++;
}
else {
j = "\nPlayer has bigger hand value! " + playerTotal + " : " + dealerTotal;
playerWinTotal++;
}
repaint();
show = false;
}
window.drawString("DEALER ", 50, 190);
dealer.drawHand(window, 0, 190);
window.drawString("PLAYER ", 50, 365);
player.drawHand(window, 0, 365);
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'b' || e.getKeyChar() == 'B') {
start = true;
repaint();
}
else if (e.getKeyChar() == 'h' || e.getKeyChar() == 'H') {
hit = true;
repaint();
}
else if (e.getKeyChar() == 'f' || e.getKeyChar() == 'F') {
finish = true;
repaint();
}
else if (e.getKeyChar() == 'x' || e.getKeyChar() == 'X') {
show = true;
repaint();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
}
最佳答案
您当前的初始化无效。
您可以将其更改为:
Ai.add(dealer);
Ai.add(playa[0]);
Ai.add(playa[1]);
Ai.add(playa[2]);
但是,如果发牌者和玩家没有实现公共接口或共享公共超类(对象除外),那么将它们放入该ArrayList并不重要。