JAVA Collection Collection of poker game instances _java

Source: Internet
Author: User
Tags addall arrays id3 set set shuffle

The root interface in the Collection hierarchy. Collection represents a set of objects, also called collection elements. Some collection allow for duplicate elements, while others are not allowed. Some of the collection are ordered, while others are unordered. JDK does not provide any direct implementation of this interface: it provides more specific sub-interfaces (such as Set and List) implementations. This interface is typically used to pass collection, and to manipulate these collection where the maximum universality is required.

Main content: Here uses the collection collection, simulates the Hong Kong movie The Big Brothers play the poker game.

1, the rules of the game: two players each hand issued two cards, to compare. Compare the hands of each player the largest number of points, size by A-2, the number of large winners. If the points are the same, then the color, size by Black (4), Red (3), plum (2), square (1), the color of the big winners.

2. Implementation steps:

Create a deck of poker A-2, Four Color black (4), Red (3), plum (2), square (1) a total of 52 cards;
Create two players including player ID and name, card information;
Shuffle and send two cards to two players each;
Compare player hand size, draw the winner;

3. Program Realization

Card class: Including the number and color of cards

Package collectiontest.games;
public class Card {
  private integer ID;//the size of the
  private integer type;//card
  
  (integer ID, integer typ e) {
    this.id = ID;
    This.type = type;
  }
  Public Integer GetId () {return
    ID;
  }
  public void SetId (Integer id) {
    this.id = ID;
  }
  Public Integer GetType () {return
    type;
  }
  public void SetType (Integer type) {
    this.type = type;
  }
  @Override public
  String toString () {return
    "card [id=" + ID + ", type=" + Type + "]";
  }  
}

Poker Poker class: Includes poker card A-2

Package collectiontest.games;
  public class Poker {private card id2;
  Private card ID3;
  Private card id4;
  Private card id5;
  Private card id6;
  Private card Id7;
  Private card Id8;
  Private card id9;
  Private card id10;
  Private card J;
  Private card Q;
  Private card K;
  
  Private card A;
    Public Poker () {}//four types: Black--4, Red--3, Mei--2, party--1 public Poker (Integer type) {This.id2 = new card (2, type);
    THIS.ID3 = new Card (3, type);
    This.id4 = new Card (4, type);
    THIS.ID5 = new Card (5, type);
    THIS.ID6 = new Card (6, type);
    This.id7 = new Card (7, type);
    This.id8 = new Card (8, type);
    THIS.ID9 = new Card (9, type);
    THIS.ID10 = new card (type); This.
    J = new Card (one, type); This.
    Q = new card (type); This.
    K = new card (type); This.
  A = new card (type);
  Public card GetId2 () {return id2;
  } public void SetId2 (card id2) {This.id2 = Id2;
  Public card GetId3 () {return ID3;
 } public void SetId3 (card id3) {this.id3 = ID3;
  Public card GetId4 () {return id4;
  } public void SetId4 (card id4) {this.id4 = Id4;
  Public card GetId5 () {return id5;
  } public void SetId5 (card id5) {this.id5 = ID5;
  Public card GetId6 () {return id6;
  } public void SetId6 (card id6) {this.id6 = Id6;
  Public card GetId7 () {return id7;
  } public void SetId7 (card id7) {This.id7 = Id7;
  Public card GetId8 () {return id8;
  } public void SetId8 (card id8) {this.id8 = Id8;
  Public card getId9 () {return id9;
  } public void SetId9 (card id9) {this.id9 = id9;
  Public card GetId10 () {return id10;
  } public void SetId10 (card id10) {this.id10 = ID10;
  Public card Getj () {return J;
  } public void Setj (card j) {j = j;
  Public card Getq () {return Q;
  } public void setq (card q) {q = q;
  Public card Getk () {return K; } public VoiD setk (card k) {k = k;
  Public card Geta () {return A;
  } public void SetA (card a) {a = A;

 }
}

Player Player class: Contains the player ID and name, the card information held

 Package collectiontest.games;
Import java.util.ArrayList;
Import java.util.List;

public class Player {
  //player ID
  private String ID;
  Player name
  private String name;
  The player is licensed
  private list<card> pokertype;
  
  Public player () {  
  } public
  player (string ID, string name, List<card> pokertype) {
    this.id = id;
   
    this.name = name;
    This.pokertype = new arraylist<> ();
  }

  Public String GetId () {return
    ID;
  }
  public void SetId (String id) {
    this.id = ID;
  }
  Public String GetName () {return
    name;
  }
  public void SetName (String name) {
    this.name = name;
  }
  Public list<card> Getpokertype () {return
    pokertype;
  }
  public void Setpokertype (list<card> pokertype) {
    this.pokertype = Pokertype;
  }  
}


   

Poker main class: including 1 poker create 2 players create 3 Shuffle 4) licensing 5) Compare the winners and losers

Package collectiontest.games;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.Collections;
Import Java.util.HashMap;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Scanner;

Import Java.util.Set; public class Gamsbegin {//create poker public set<poker> Cpoker () {System.out.println ("********** Start creating Poker * *
    ********");
    Create a pair of Poker//four types: Black--4, Red--3, Plum--2, square--1 set<poker> = new Pokers ();
    Poker[] Poker = {new Poker (1), New Poker (2), New Poker (3), New Poker (4)};
     * * The use of the Collections tool class * Collections.addall (Pokers, New Poker (1), New Poker (2), New Poker (3), New Poker (4));

    * * * * */Pokers.addall (arrays.aslist (poker));

    System.out.println ("********** Card creation Success **********");
  return pokers;
    //Create two player public map<string, player> Cplayer () {System.out.println ("********** starts creating player **********"); map<string, player> map =New hashmap<string, player> ();

    Controlling quantity Integer control = 0;
    SYSTEM.OUT.PRINTLN ("Create two players, create according to prompts");
    Scanner console = new Scanner (system.in);
      while (true) {System.out.println ("Please enter the first" + (control+1) + "Player ID:");

      String CourseID = Console.next ();
        if (IsNumeric (CourseID)) {System.out.println ("Please enter the first" + (control+1) + "player name:");

        String coursename = Console.next ();
        Player players = new player (CourseID, coursename, NULL);

        Save Data Map.put (CourseID, players);
        System.out.println ("Add cap" + (Control + 1) + "Player" + Coursename + "success");
      Quantity from Add control++;
        else {System.out.println ("* * * Please enter digital id*****");
      Continue
      } if (Control = = 2) {break;

    } System.out.println ("********** player creates success **********");
  return map; //Determine if input is numeric, character.isdigit () is Java method public boolean isnumeric (String str) {for (int i = 0; i < str. Length (); i++) {if (!
      Character.isdigit (Str.charat (i)) {return false;
  } return true; /** * Shuffle: can also generate 52 different random numbers, implementation shuffle * **/public list<card> wpoker (set<poker> pokers) {System.ou
    T.println ("********** Start Shuffle **********");
    Use List of ordered ordering, shuffle after the save order unchanged list<card> Listcard = new arraylist<> ();

    Using the unordered ordering of set set, the shuffle set<card> listset = new hashset<> () is realized.
      Save to set collection, unordered for (Poker pk:pokers) {Listset.add (Pk.getid2 ());
      Listset.add (PK.GETID3 ());
      Listset.add (Pk.getid4 ());
      Listset.add (PK.GETID5 ());
      Listset.add (Pk.getid6 ());
      Listset.add (Pk.getid7 ());
      Listset.add (Pk.getid8 ());
      Listset.add (PK.GETID9 ());
      Listset.add (PK.GETID10 ());
      Listset.add (PK.GETJ ());
      Listset.add (PK.GETQ ());
      Listset.add (PK.GETK ());
    Listset.add (Pk.geta ());
      }//Save in list collection, ordered for (card cd:listset) {Listcard.add (CD); System.out.println (CD);

    } System.out.println ("********** Shuffle successfully **********");
  return listcard; 
    }//Licensing public map<string, player> pushpoker (list<card> listcard, map<string, player> pMap) {
    
    System.out.println ("********** licensing began **********");

    Control two cards per person after the end of the int controls = 0; For (map.entry<string, player> entry:pMap.entrySet ()) {if (+ = 0) {for (int i = 0; i < 3;
        i = i + 2) {//Licensing Entry.getvalue (). Getpokertype (). Add (Listcard.get (i));
        //Update Map Object Pmap.put (Entry.getkey (), Entry.getvalue ());
      control++; else if (control = = 1) {for (int i = 1; i < 4; i = i + 2) {//Licensing Entry.getvalue (). Getpo
        Kertype (). Add (Listcard.get (i));
        //Update Map Object Pmap.put (Entry.getkey (), Entry.getvalue ());
      control++;
      } else {break;
} System.out.println ("********** successful **********");
    return PMAP;
     } public void Comparematch (map<string, player> newmap) {/* Compare winning * 1. First get the ID and color ID of the largest card in each player's hand.
     * 2. Compare two players in the hands of the largest card ID size, the big card wins.
     * 3. If the ID of two cards is equal, compare the suit ID of two cards, the color ID is bigger to win.

    * * * */list<player> players = new arraylist<> ();
    Get two players for (Map.entry<string, player> entry:newMap.entrySet ()) {Players.add (Entry.getvalue ());
    }//Player one information and licensed list<card> Playerone = players.get (0). Getpokertype ();
    Get the maximum card ID and color Integer onemaxid = Math.max (playerone.get (0). GetId (), Playerone.get (1). GetId ()); Integer Onemaxtype = (onemaxid!=playerone.get (0). GetId ())?

    Playerone.get (1). GetType (): Playerone.get (0). GetType ();
    Player II information and licensed list<card> Playertwo = Players.get (1). Getpokertype ();
    Get the maximum card ID and color Integer twomaxid = Math.max (playertwo.get (0). GetId (), Playertwo.get (1). GetId ()); Integer Twomaxtype = (twomaxid!=playertwo.get (0). GetId ())? PlayertwO.get (1). GetType (): Playertwo.get (0). GetType (); if (Onemaxid > Twomaxid) {System.out.println ("Player:" + players.get (0). GetName () + "Win!!"
    "); else if (Onemaxid = = Twomaxid) {if (Onemaxtype > Twomaxtype) {System.out. println ("Player: "+ players.get (0). GetName () +" Win!!

      "); else {System.out. println ("Player:" + players.get (1). GetName () + "Win!!"
      "); } else {System.out.println ("Player:" + players.get (1). GetName () + "Win!!"
    ");
    } System.out.println ("**********************************************");
        System.out.println ("Player:" + players.get (0). GetName () + "card is:" + showname (playerone.get (0). GetType (), 0) + "--" + showname (playerone.get (0). GetId (), 1) + "" + ShowName (Playerone.get (1). GetType (), 0) + "--" + sh
    Owname (Playerone.get (1). GetId (), 1)); System.out.println ("Player:" + players.get (1). GetName () + "cards are:" + showname (playertwo.get (0). GetType (), 0) + "--" + showname (playertwo.get (0). GetId (), 1) + "" + ShowName (Playertwo.get (1). GetType (), 0) + "-
  "+ ShowName (Playertwo.get (1). GetId (), 1));

    The name of the//display card is private string ShowName (integer i, integer type) {String str = "";
        Display Color if (type = = 0) {switch (i) {Case 1: {str = "square";
      Break
        Case 2: {str = "plum blossom";
      Break
        Case 3: {str = "red peach";
      Break
        Case 4: {str = "spades";
      Break
      } default: {break;
      }}//Display number if (type = = 1) {if (I < one) {return i.tostring ();
          else {switch (i) {case one: {str = "J";
        Break
          Case: {str = "Q";
        Break
          Case: {str = "K";
        Break
          Case: {str = "A";
        Break
}
        Default: {break;
  }}} return str;
    
    public static void Main (string[] args) {gamsbegin GB = new Gamsbegin ();

    1, create poker set<poker> pokers = Gb.cpoker ();

    2, create two players map<string, player> pMap = Gb.cplayer ();

    3, shuffle list<card> Listcard = Gb.wpoker (pokers);

    4, Licensing map<string, player> newmap = Gb.pushpoker (Listcard, PMAP);

  4, compare the victory and defeat Gb.comparematch (NEWMAP);

 }
}

Run Result:

Start creating poker **********
Poker creation Success **********
Start creating player **********
Create two players, as prompted to create
Please enter a 1th player ID:
Please enter a 1th player name:
Stephen chow
Add 1th player Stephen Chow Success
Please enter a 2nd player ID:
Please enter a 2nd player name:
Chow
Add 2nd Player Chow Yun-Fat success
Player creates successful **********
Start shuffling **********
Card [id=9, type=3]
Card [id=11, type=4]
Card [id=13, type=3]
Card [Id=8, type=3]
Card [id=5, type=2]
Card [Id=6, type=1]
Card [id=4, type=3]
Card [id=5, type=4]
Card [id=2, type=3]
Card [id=9, type=2]
Card [id=9, type=4]
Card [id=14, type=2]
Card [id=9, type=1]
Card [id=2, type=1]
Card [id=2, type=4]
Card [id=7, type=4]
Card [id=11, type=1]
Card [id=10, type=1]
Card [id=14, type=4]
Card [id=14, type=3]
Card [id=12, type=2]
Card [id=2, type=2]
Card [id=10, type=2]
Card [id=7, type=1]
Card [id=7, type=3]
Card [Id=8, type=2]
Card [id=4, type=4]
Card [id=13, type=4]
Card [id=14, type=1]
Card [id=12, type=1]
Card [id=5, type=1]
Card [Id=6, type=4]
Card [id=12, type=4]
Card [id=11, type=2]
Card [id=10, type=3]
Card [id=3, type=4]
Card [id=12, type=3]
Card [id=4, type=2]
Card [id=4, type=1]
Card [Id=6, type=2]
Card [id=5, type=3]
Card [Id=8, type=4]
Card [id=3, type=2]
Card [id=13, type=2]
Card [id=7, type=2]
Card [id=3, type=3]
Card [id=3, type=1]
Card [Id=6, type=3]
Card [Id=8, type=1]
Card [id=11, type=3]
Card [id=13, type=1]
Card [id=10, type=4]
Shuffle successful **********
Licensing starts **********
Successful Licensing **********
Player: Stephen Chow wins!!
**********************************************
Player: Stephen Chow's card is: Red peach--9 Red Peach--k
Player: Chow Yun-fat cards are: Spades--j hearts--8

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.