首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何正确地实例化一个可以同时容纳玩家和发牌者的数组列表?

我如何正确地实例化一个可以同时容纳玩家和发牌者的数组列表?
EN

Stack Overflow用户
提问于 2015-03-30 02:01:50
回答 1查看 290关注 0票数 0

我正在为我的APCS班级用Java制作一个多人二十一点游戏。我需要能够在一个数组列表中存储玩家和发牌者。实例化数组列表的正确方法是什么?下面是问题所在的类。

代码语言:javascript
复制
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) {
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-03-30 02:03:46

您当前的初始化将不起作用。

您可以将其更改为:

代码语言:javascript
复制
 Ai.add(dealer);
 Ai.add(playa[0]);
 Ai.add(playa[1]);
 Ai.add(playa[2]);

然而,如果交易者和玩家没有实现一个公共的接口或者共享一个公共的超类(除了Object之外),我看不出把它们放在那个ArrayList中有什么意义。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29332773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档