io.github.houbin217jz.tech14.w31.PokerGame.java Source code

Java tutorial

Introduction

Here is the source code for io.github.houbin217jz.tech14.w31.PokerGame.java

Source

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.houbin217jz.tech14.w31;

import io.github.houbin217jz.tech14.w31.handler.ProbabilityHandler;
import io.github.houbin217jz.tech14.w31.model.Card;
import io.github.houbin217jz.tech14.w31.model.PokerPlayer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

import org.apache.commons.io.input.CloseShieldInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ?
 * 
 * @author HOU Bin
 * 
 */
public class PokerGame {

    private static final Logger logger = LoggerFactory.getLogger(PokerGame.class);

    /**
     * 
     */
    public void play() {

        // 
        System.out.println("???????????(135)");
        System.out.println("0??????");

        // ???
        PokerPlayer pokerPlayer = PokerGameContainer.createPokerPlayer();

        // 5??
        try {
            pokerPlayer.draw5cards();
        } catch (NoEnoughCardsException e) {
            // ??????
            logger.error("?????", e);
        }

        // ??
        printCards(pokerPlayer.getCardsInHand());

        // ????
        List<Integer> changeNumbers = inputChangeCardNumbers(pokerPlayer);

        try {
            pokerPlayer.changeCards(changeNumbers);
        } catch (NoEnoughCardsException e) {
            // ??????
            System.out.println("????");
        }

        // ????
        printCards(pokerPlayer.getCardsInHand());

        // ?
        ProbabilityHandler handler = PokerGameContainer.createProbabilityHandler();
        String probability = handler.handleRequest(pokerPlayer.getCardsInHand());

        // ?
        printProbability(probability);
    }

    /**
     * ??????
     * 
     * @param probability
     */
    private void printProbability(String probability) {
        System.out.println("? " + probability + " ???");
    }

    /**
     * ?????????
     * 
     * @return ???????
     */
    private List<Integer> inputChangeCardNumbers(PokerPlayer pokerPlayer) {
        List<Integer> result = new ArrayList<>();

        try (Scanner scanner = new Scanner(new CloseShieldInputStream(System.in))) {
            /*
             * System.in?close???????????????eclipse???
             * CloseShieldInputStream???
             */

            while (result.size() <= 1) { // ???????????

                String inputString = scanner.nextLine();
                // 0???????
                if ("0".equals(inputString)) {
                    return Arrays.asList(0);
                }

                // ?
                String errorMsg = validInput(inputString, pokerPlayer.getCardsInHand());

                if (errorMsg != null && !"".equals(errorMsg)) {
                    // ???
                    System.out.println(errorMsg);
                    continue;
                } else {
                    // ????
                    for (char c : inputString.toCharArray()) {
                        result.add(Integer.parseInt("" + c));
                    }
                    break;
                }
            }
        }
        return result;
    }

    /**
     * ?
     * 
     * @param inputString ?????????
     * @param list ??
     * @return ??????
     */
    private String validInput(String inputString, List<Card> list) {
        // ?
        Pattern pattern = Pattern.compile("\\d+");
        if (!pattern.matcher(inputString).matches()) {
            return "?????";
        }

        // ????
        List<Integer> numbers = new ArrayList<>();
        for (char c : inputString.toCharArray()) {
            int number = Integer.valueOf("" + c);
            if (number < 1 || number > list.size()) {
                return number + "?????";
            } else {
                if (numbers.contains(number)) {
                    return "??????";
                } else {
                    numbers.add(number);
                }
            }
        }
        return "";
    }

    /**
     * ????
     * 
     * @param cards
     *            
     */
    private void printCards(List<Card> cards) {
        for (int i = 0; i < cards.size(); i++) {
            System.out.println((i + 1) + ":" + cards.get(i));
        }
    }
}