Java learning simulation card game

  • First put the test effect map


  • design framework


 

  • specific code implementation

  • Create the player class

 

public class Player implements Comparable<Player>{
    int id;
    String name;
    List<Card> cardList;
    Integer maxCard;
    public Player(int id, String name){
        this.id = id;
        this.name = name;
        this.cardList = new ArrayList<Card>();
    }
    public int compareTo(Player o) {
        return this.maxCard.compareTo(o.maxCard);
    }
}

 

 


 

  • Create a poker class
public class Card implements Comparable<Card>{
    Integer id;
    String value;
    public Card(Integer id, String value) {
        this.id = id;
        this.value = value;
    }
    public int compareTo(Card o) {
        return this.id.compareTo(o.id);
    }    
}

 

  • Create a game class
public class PlayCards {
    
    Console scanner;
    List<Card> cardlist;
    Map<Integer, Player> playermap;
    List<Card> shufflelist;
    public PlayCards(){
        console = new Scanner(System.in);
        cardlist = new ArrayList<Card>();
        playermap = new HashMap<Integer, Player>();
        shufflelist = new ArrayList<Card>();
    }
// Method for creating playing cards 
    public  void creatCard() {
        System.out.println( "------Create playing cards------" );
        String[] head = {"Square", "Plum", "Hearts", "Spades" };
        String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        int id = 0;
        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 4; j++) {
                String tempstr = head[j] + number[i];
                Card card = new Card(id++, tempstr);
                cardlist.add(card);
            }
        }
        System.out.println( "------The card was created successfully!------" );
        System.out.println("*********************************************");
        for (int i = 0; i < 52; i++) {
            if(i % 13 == 0) {
                System.out.println();
            }
            System.out.print(cardlist.get(i).value);
        }
        System.out.println("\n**********************************************");
        

    }
// Method for shuffling 
    public  void shuffle() {
        Random random = new Random();
        System.out.println("------开始洗牌------");
        for (int i = 52; i > 0; i--) {
            int tempNum = random.nextInt(i);
            Card tempcard = cardlist.get(tempNum);
            cardlist.remove(tempNum);
            shufflelist.add(tempcard);
            
        }
        System.out.println( "------End of shuffle!------" );
    }
// Method to create player and enter related id and name     
    public  void creatPlayer() {
        System.out.println( "------Create player------" );
        
        int i = 1;
        while(i < 3) {
            try {
                System.out.println( "Please enter the ID and name of the player " + i + "" );
                System.out.println( "Please enter an integer ID: " );
                Scanner input = new Scanner(System.in);
                Integer integer = input.nextInt();
                Player player = playermap.get(integer);
                if(player == null) {
                    System.out.println( "Please enter your name: " );
                    String name = console.next();
                    Player newPlayer = new Player(integer, name);
                    playermap.put(integer, newPlayer);
                    i++;
                }else {
                    System.out.println( "Player ID is occupied, please re-enter!" );
                     continue ;
                }
            }catch(InputMismatchException e) {
                System.out.println( "The player ID input is incorrect, please input the specified integer" );
                 continue ;
            }
        }
        Set<Entry<Integer, Player>> entry = playermap.entrySet();
        for (Entry<Integer, Player> ey: entry) {
            System.out.println( "----Welcome players: " + ey.getValue().name);
        }
        
    }
// Method for dealing cards to players 
    public  void sendCardToPyer() {
        System.out.println( "Requires two players to have less than 27 cards in hand" );
         try {
            System.out.println( "Please enter the number of cards in each player's hand: " );
            Scanner input = new Scanner(System.in);
            int cardNum = input.nextInt();
            if (cardNum > 26 || cardNum < 1) {
                sendCardToPyer();
            }else {
                System.out.println("------开始发牌------");
                int k = 0;
                Set<Entry<Integer, Player>> entry = playermap.entrySet();
                for (int i = 0; i < cardNum; i++) {
                    for (Entry<Integer, Player> ey: entry) {
                        ey.getValue().cardList.add(shufflelist.get(k++));
                        System.out.println("玩家:" + ey.getValue().name + "-拿牌");
                    }
                }
                System.out.println( "------End of the deal------" );
            }
            
        }catch(InputMismatchException e) {
            sendCardToPyer();
        }
        
    }
// The method for two players to take out the biggest hand to compare and decide the outcome and show the hand 
    public  void whoIsWinner() {
        Set<Entry<Integer, Player>> entry = playermap.entrySet();
        List<Player> playerlist = new ArrayList<Player>();
        for (Entry<Integer, Player> ey: entry) {
            Collections.sort(ey.getValue().cardList);
            int listsize = ey.getValue().cardList.size();
            ey.getValue().maxCard = ey.getValue().cardList.get(listsize -1).id ;
            playerlist.add(ey.getValue());
            System.out.println( "Player:" + ey.getValue().name + "The biggest hand is:"
            + ey.getValue().cardList.get(listsize - 1).value);
        }
        Collections.sort(playerlist);
        System.out.println("------玩家:" + playerlist.get(playerlist.size() - 1).name + "获胜!------");
        System.out.println( "Players' respective hand cards are: " );
         for (Player player: playerlist){
            System.out.print(player.name + ": ");
            for (Card card: player.cardList) {
                System.out.print(card.value + " ");
            }
            System.out.println();        
        }     
    }    
}

 


 

  • Create initial class
public class Initial {
    
    public static void main(String[] args) {
        Scanner  input = null;
        int i = 0;
        while(true) {
            System.out.println( "Press any number to start the game" );
             try {
                input = new Scanner(System.in);
                i = input.nextInt();    
                break;
            }catch(InputMismatchException e) {
                System.out.println( "Please input as required!!!" );
            }
        }
        PlayCards playcards = new PlayCards();
        playcards.creatCard();
        playcards.shuffle();
        playcards.creatPlayer();
        playcards.sendCardToPyer();
        playcards.whoIsWinner();
        System.out.println( "Welcome to the next game!!!" );
        System.exit(0);

        if (input != null) {
            input.close();
            input = null;
        }
    }
}

 


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324830749&siteId=291194637