⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blackjackdealer.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        //        out.print(numOfBetsIn);
        
        while (all_players.hasMoreElements())
            {
                Player curr_player = (Player)(all_players.nextElement());
                if (curr_player.getBet() != 0)
                    {
                        out.print(ProtocolConstants.FIELD_DELIMITER);
                        out.print(curr_player.getId());
                        out.print(',');
                        out.print(curr_player.getBet());
                    }
            }
        out.println("");
    }
    
    private void setStatusReplyToGameStarted()
    {
        statusReply.append(ProtocolConstants.R_GAME_STARTED);
    }
    
    private void sendStatus(PrintStream out, Player player)
    {
        int player_id = 0;
        
        try     // in case player asks for status after game was reset
            {
                player_id = player.getId();
            } 
        catch (java.lang.NullPointerException e)
            {
                log("player asked for status after game ended");
            }
        if (gameStatus == NOT_STARTED || 
            (gameStatus == ENDED && numOfRepliesSent == numOfPlayers))  // waiting to start next game
            {
                sendNoChange(out, player_id);
                return;
            }
        if (gameStatus == BETTING)
            {
                sendBettingStatus(out);
                return;
            }
        // in all the other cases we must count how many players 
        // received the status reply
        if (numOfRepliesSent != numOfPlayers) 
            {
                if (playerReceivedReply[player_id] == false)
                    {
                        sendStatusReply(out, player_id);
                        if (gameStatus == ENDED && 
                            numOfRepliesSent == numOfPlayers &&   // last player received status ENDED
                            nextGameWithSamePlayers == true)
                            {
                                startNewGameWithSamePlayers();
                            }
                    }
                else
                    sendNoChange(out, player_id);
                return;
            }
        // all recipients received the latest status, move to a new one
        
        // was a card dealt while players were informed that turn has changed?
        if (cardDealtReply != null) 
            {
                gameStatus = lastDealOutcome;
                zeroRepliesInfo();
                statusReply.append(cardDealtReply.toString());
                cardDealtReply = null;
                sendStatusReply(out, player_id);
                return;
            }
        zeroRepliesInfo();
        changeStatus(); // move to the next state of the game
        // if we reached here all players received the previous status 
        // and now it can change
        switch (gameStatus)
            {
            case BETTING:   // reached only if the a new game started with the same players
                sendBettingStatus(out);
                return;
            case AWAITING_PLAYER:
                sendNoChange(out, player_id);
                return;
                // in all other cases send reply in sendStatusReply()
            case PLAYING:
                dealInitialHands();
                break;
            case DEALER_BLACKJACK:
            case NEW_TURN:
                statusReply.append(ProtocolConstants.R_TURN);
                statusReply.append(ProtocolConstants.FIELD_DELIMITER);
                statusReply.append(currentPlayer.getId());
                break;
            case BLACKJACK:
                statusReply.append(ProtocolConstants.R_BLACKJACK);
                statusReply.append(ProtocolConstants.FIELD_DELIMITER);
                statusReply.append(currentPlayer.getId());
                break;
            case ENDED:
                statusReply.append(ProtocolConstants.R_END);
                statusReply.append(ProtocolConstants.FIELD_DELIMITER);
                statusReply.append(calculateGameOutcome());
                break;
            }
        sendStatusReply(out, player_id);
    }
    
    
    private void sendBettingStatus(PrintStream out)
    {
        out.print(ProtocolConstants.R_WAITING_BETS);
        out.print(ProtocolConstants.FIELD_DELIMITER);
        out.print(numOfBetsIn);
        out.print(ProtocolConstants.FIELD_DELIMITER);
        out.println(numOfPlayers);
    }
    
    private String calculateGameOutcome()
    {
        Enumeration all_players = players.elements();
        StringBuffer outcome_str = new StringBuffer();
        int dealerCardsValue = dealer.getCardsValue();
        
        outcome_str = new StringBuffer();
        // if player is busted it will be discovered when the last card was
        // drawn, and the outcome will already be set
        for (int i=0; i<numOfPlayers; i++)
            {
                Player player = (Player)(all_players.nextElement());
                int playerCardsValue;
                int outcome = player.getOutcome();
            
                if (outcome == PLAYER_HOLD || outcome == NO_OUTCOME)
                    {
                        playerCardsValue = player.getCardsValue();
                        if (dealerCardsValue == playerCardsValue)
                            player.setOutcome(DRAW);
                        else if (playerCardsValue < dealerCardsValue &&
                                 dealerCardsValue <= 21)
                            player.setOutcome(LOST);
                        else
                            player.setOutcome(WON);
                    }
                outcome_str.append(player.getId());
                outcome_str.append(',');
                outcome_str.append(player.getOutcome());
                if (i < numOfPlayers-1)
                    outcome_str.append(ProtocolConstants.FIELD_DELIMITER);
            }
        return outcome_str.toString();
    }
    
    private void sendStatusReply(PrintStream out, int player_id)
    {
        // servlet log
        if (statusReply != null)
            log("game status = " + gameStatus + 
                " player " + player_id + 
                " status reply = " + statusReply.toString());
        else 
            log("game status = " + gameStatus + 
                " player " + player_id);
        
        playerReceivedReply[player_id] = true;
        numOfRepliesSent++;
        out.println(statusReply);
    }
    
    private void sendNoChange(PrintStream out, int player_id)
    {
        out.print(ProtocolConstants.R_NO_CHANGE);
        out.print(ProtocolConstants.FIELD_DELIMITER);
        out.println(player_id);
    }
    
    private void zeroRepliesInfo()
    {
        numOfRepliesSent = 0;
        statusReply = new StringBuffer();
        for (int i=0; i<=numOfPlayers; i++)
            {
                playerReceivedReply[i] = false;
            }
    }
    
    private void changeStatus()
    {
        log("status changed from " + gameStatus);
        switch (gameStatus)
            {
            case NEW_GAME:
                gameStatus = BETTING;
                break;
            case START_PLAYING:     // all players know we started the game, now cards can be dealt
                gameStatus = PLAYING;
                break;
            case NEW_TURN:      // fall through - we only reach here when all players know about the new turn or
            case CARD_DEALT:    // the card that has been dealt, but the player still hasn't sent his next move
                if (currentPlayer.getId() != ProtocolConstants.DEALER)  // current player cannot be null
                    {
                        gameStatus = AWAITING_PLAYER;
                        break;
                    }
                // else card was dealt to dealer - fall through to continue dealer's game
            case PLAYING:       // fall through - if cards were dealt find out if player has blackjack
            case BUST:          // fall through
            case HOLD:          // fall through
            case BLACKJACK:     // fall through
                // move on to next player's turn
                if (numOfPlayersLeft > 0)
                    {
                        int next_player_id;
                        if (currentPlayer == null)
                            next_player_id = 1;
                        else
                            {
                                next_player_id = currentPlayer.getId()+1;
                            }
                        currentPlayer = findPlayer(next_player_id);
                        // blackjack is discovered
                        if (currentPlayer.getCardsValue() == 21)
                            {
                                currentPlayer.setOutcome(WON);  // YG - is this the place?
                                numOfPlayersLeft--;
                                gameStatus = BLACKJACK;
                            }
                        else 
                            {
                                gameStatus = NEW_TURN;
                                log("new turn: player "+currentPlayer.getId());
                            }
                    }
                else    // dealer should play now
                    {
                        if (currentPlayer.getId() != ProtocolConstants.DEALER)  // we haven't moved to the dealer yet
                            {
                                currentPlayer = dealer;
                                gameStatus = NEW_TURN;
                            }
                        else
                            {
                                if (numOfPlayersHolding > 0)    // not all players are busted or have blackjack
                                    {
                                        dealerPlays();
                                    } 
                                else
                                    {
                                        gameStatus = ENDED;
                                    }
                            }
                    }
                break;
            case DEALER_BLACKJACK:
                if (currentPlayer == null)  // a NEW_TURN message hasn't been sent yet
                    {
                        currentPlayer = dealer;
                    }
                else    // message has been sent, now end game
                    {
                        gameStatus = ENDED;
                    }
                break;
            }
        log(" to " + gameStatus);
    }
    
    private void dealerPlays()
    {
        int dealerCardsValue = dealer.getCardsValue();
        if (dealerCardsValue < 17)
            {
                log("deal card to the dealer");
                dealCardAtPlayersRequest();
                log("dealer cards value is now " + dealer.getCardsValue());
                // change of status to CARD_DEALT will occur when the cardDealtReply is seen
            }
        else
            {
                gameStatus = ENDED;
            }
    }
    
    private void dealCardAtPlayersRequest()
    {
        Card card = dealCard(currentPlayer);
        int playerCardsValue = currentPlayer.getCardsValue();
        cardDealtReply = new StringBuffer();                   
        
        if (playerCardsValue > 21)
            {
                currentPlayer.setOutcome(LOST);
                numOfPlayersLeft--;
                cardDealtReply.append(ProtocolConstants.R_BUST);
                lastDealOutcome = BUST;
            }
        else
            {
                cardDealtReply.append(ProtocolConstants.R_CARD);
                lastDealOutcome = CARD_DEALT;
            }
        cardDealtReply.append(ProtocolConstants.FIELD_DELIMITER);
        cardDealtReply.append(currentPlayer.getId());
        cardDealtReply.append(ProtocolConstants.FIELD_DELIMITER);
        cardDealtReply.append(card.toString());
        cardDealtReply.append(ProtocolConstants.FIELD_DELIMITER);
        cardDealtReply.append(playerCardsValue);            
    }
    
    private void resetGame()
    {
        deck = new Deck();
        deck.shuffle();
        dealer = new Player(ProtocolConstants.DEALER);
        players = new Vector();
        currentPlayer = null;
        numOfPlayers = 0;
        numOfPlayersHolding = 0;
        numOfBetsIn = 0;
        gameStatus = NOT_STARTED;
        // keep track of who was informed of a status change
        statusReply = null;
        cardDealtReply = null;
        numOfRepliesSent = 0;
        nextGameWithSamePlayers = false;
    }
    
    private void startNewGameWithSamePlayers()
    {
        deck = new Deck();
        deck.shuffle();
        dealer = new Player(ProtocolConstants.DEALER);
        currentPlayer = null;
        numOfPlayersHolding = 0;
        numOfBetsIn = 0;
        gameStatus = NEW_GAME;
        cardDealtReply = null;
        // keep track of who was informed of a status change
        zeroRepliesInfo();
        resetPlayers();
        setStatusReplyToNewGame();
        nextGameWithSamePlayers = false;
    }
    
    private void resetPlayers() // used for new game with the same players
    {
        Enumeration all_players = players.elements();
        while (all_players.hasMoreElements())
            {
                Player curr_player = (Player)(all_players.nextElement());
                curr_player.reset();
            }
    }
    
    private void setStatusReplyToNewGame()
    {
        statusReply = new StringBuffer();
        statusReply.append(ProtocolConstants.R_NEW_GAME);
    }

    private void log(String msg) 
    {
        System.out.println(msg);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -