Software Engineering Practice Job 2--Stud games (Java) Practice Report

Source: Internet
Author: User
Tags shuffle terminates

First, the topic Introduction:

1. Create a deck of poker 7------K added to the collection object
2. Shuffle the Cards
3, define the players to participate in the game, through the keyboard input, limit the number of people 2-5
4, the number of people to comply with the requirements continue to execute, not eligible to exit
5, to the player licensing, each person issued five, on the player's card sort

GitHub Link Address:

Https://github.com/GY1/test/blob/master/ShowHand (%e6%a2%ad%e5%93%88%e6%b8%b8%e6%88%8f)

Stud Game Source code:

Package com.langsin.CollectionsFramework;

Import java.util.ArrayList;
Import Java.util.Comparator;
Import Java.util.LinkedHashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Random;
Import Java.util.Scanner;
Import Java.util.TreeSet;

public class Showhand {

Private arraylist<string> pukelist = new arraylist<string> ();
Private map<string, treeset<string>> Map = new linkedhashmap<string, treeset<string>> ();
Private map<string, double> scoremap = new linkedhashmap<string, double> ();
Private Boolean flag = true;

1. Create Poker 7-k
private void Createpuke () {
String[] point = {"2", "3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q", "K"};
String[] Type = {"Spades", "Hearts", "Plum", "block"};
for (int i = 0; i < type.length; i++) {
for (int j = 0; J < Point.length; J + +) {
This.pukeList.add (Type[i] + point[j]);
}
}
}

2. Shuffle
private void Sortedpuke () {
Random rand = new Random ();
for (int i = 0; i <; i++) {
int index1 = Rand.nextint (This.pukeList.size ());
int index2 = Rand.nextint (This.pukeList.size ());
String puke1 = This.pukeList.get (index1);
String puke2 = This.pukeList.get (INDEX2);
This.pukeList.set (index1, puke2);
This.pukeList.set (Index2, puke1);
}

}

3. Create the person who participates in the game
private void Createplayer () {
System.out.println ("Please enter the name of the player participating in the game, separated by a space: (no more than 8 people, less than 3 people, or the program terminates!") )");
Scanner scan = new Scanner (system.in);
String players = Scan.nextline ();
string[] Nums = Players.split ("");
if (Nums.length < 3 | | nums.length > 9) {
System.out.println ("The number of players participating in the game does not meet the requirements, the program terminates!") ");
This.flag = false;
System.exit (0);//(Program termination)
}
Comparator<string> comp = new comparator<string> () {

public int Compare (string str1, String str2) {

int point1 = GetPoint (str1.substring (2));
int point2 = GetPoint (str2.substring (2));
if (Point1 < Point2) {
return-1;
} else if (Point1 > Point2) {
return 1;
} else {
int type1 = GetType (str1.substring (0, 2));
int type2 = GetType (str2.substring (0, 2));
if (Type1 < type2) {
return-1;
} else {
return 1;
}
}

}
};
for (int i = 0; i < nums.length; i++) {
Map.put (Nums[i], new treeset<string> (comp));
}

}

private int GetPoint (String point) {
if ("J". Equals (point)) {
return 11;
} else if ("Q". Equals (point)) {
return 12;
} else if ("K". Equals (point)) {
return 13;
} else {
Return Integer.parseint (point);
}
}

private int GetType (String type) {
if ("Spades". Equals (type)) {
return 4;
} else if ("Red Peach". Equals (type)) {
return 3;
} else if ("Plum". Equals (type)) {
return 2;
} else {
return 1;
}
}

4. Licensing the player
private void Showpuke () {
for (int i = 0; i < 5; i++) {
For (String Key:map.keySet ()) {
String puke = this.pukeList.remove (0);
Map.get (Key). Add (puke);
}
}

For (String Key:map.keySet ()) {
System.out.println ("Player name:" + key + ":" + map.get (key));
}
}

Calculate player score, get winning player
private void Getscorewinner () {
For (String Key:map.keySet ()) {
treeset<string> set = Map.get (key);
Scoremap.put (Key, This.score (set));
}
List<string> List1=this.getwinner (SCOREMAP);
System.out.println ("Congratulations" + list1.get (0) + "win the final title!!! "+" Championship score for: "+ list1.get (1));
Map<string, double> Scoremap1=this.getscore (SCOREMAP);

List<string> List2=this.getwinner1 (SCOREMAP1);
System.out.println ("Congratulations" + list2.get (0) + "Get runner-up!! The "+" Runner score is: "+ list2.get (1));
Map<string, double> Scoremap2=this.getscore (SCOREMAP1);

List<string> List3=this.getwinner1 (SCOREMAP2);
System.out.println ("Congratulations" + list3.get (0) + "Get the third place!" "+" The third runner-up score is: "+ list3.get (1));

}


Private list<string> Getwinner (map<string, double> scoremap) {

List<string> list=new arraylist<string> ();
String winner = null;
Double end = 0;
For (map.entry<string, double> entry:this.scoreMap.entrySet ()) {
System.out.println ("Player name:" + entry.getkey () + "+" player gets the score: "
+ Entry.getvalue ());
if (End < Entry.getvalue ()) {
End = Entry.getvalue ();
Winner = Entry.getkey ();
}
}
List.add (winner);
List.add (string.valueof (end));
return list;
}

Private list<string> GetWinner1 (map<string, double> scoremap) {

List<string> list=new arraylist<string> ();
String winner = null;
Double end = 0;
For (map.entry<string, double> entry:this.scoreMap.entrySet ()) {
if (End < Entry.getvalue ()) {
End = Entry.getvalue ();
Winner = Entry.getkey ();
}
}
List.add (winner);
List.add (string.valueof (end));
return list;
}

Private map<string, double> getscore (map<string, double> scoremap) {


String winner = null;
Double end = 0;
For (map.entry<string, double> entry:this.scoreMap.entrySet ()) {
if (End < Entry.getvalue ()) {
End = Entry.getvalue ();
Winner = Entry.getkey ();
}
}
Scoremap.remove (winner);
return scoremap;
}


Private double score (treeset<string> set) {

Boolean flag = true;
string[] Pukes = Set.toarray (New string[set.size ());
for (int i = 0; i < pukes.length-1; i++) {
int point1 = This.getpoint (pukes[i].substring (2));
int point2 = This.getpoint (Pukes[i + 1].substring (2));
if (point1-point2! =-1) {
Flag = false;
Break
}
}

if (flag) {
Return 5.0 + this.getpoint (pukes[4].substring (2)) * 0.01;
}

int point1 = This.getpoint (pukes[0].substring (2));
int point2 = This.getpoint (pukes[1].substring (2));
int point3 = This.getpoint (pukes[2].substring (2));
int point4 = This.getpoint (pukes[3].substring (2));
int point5 = This.getpoint (pukes[4].substring (2));
Four identical cards.
if (point1 = = Point4 | | point2 = = POINT5) {
Return 4.0 + this.getpoint (pukes[2].substring (2)) * 0.01;
}
Three cards of the same card
if (point1 = = Point3 | | point2 = POINT4 | | point3 = POINT5) {
Return 3.0 + this.getpoint (pukes[2].substring (2)) * 0.01;
}
Two pairs of two identical cards
if (point1 = = Point2 && Point3 = = point4) {
Return 2.0 + this.getpoint (pukes[2].substring (2)) * 0.01+ This.getpoint (pukes[0].substring (2)) * 0.0001+ This.getpoint ( Pukes[4].substring (2)) * 0.000001;
}
if (point1 = = Point2 && point5 = = point4) {
Return 2.0 + this.getpoint (pukes[3].substring (2)) * 0.01+ This.getpoint (pukes[0].substring (2)) * 0.0001+ This.getpoint ( Pukes[2].substring (2)) * 0.000001;
}
if (Point3 = = Point2 && point5 = = point4) {
Return 2.0 + this.getpoint (pukes[3].substring (2)) * 0.01+ This.getpoint (pukes[1].substring (2)) * 0.0001+ This.getpoint ( Pukes[0].substring (2)) * 0.000001;
}
Two identical cards
if (point1 = = Point2) {
Return 1.0+this.getpoint (pukes[0].substring (2)) *0.01+this.getpoint (pukes[4].substring (2)) *0.0001+this.getpoint ( Pukes[3].substring (2)) *0.000001+this.getpoint (pukes[2].substring (2)) *0.00000001;
}
if (Point2 = = Point3) {
Return 1.0+this.getpoint (pukes[1].substring (2)) *0.01+this.getpoint (pukes[4].substring (2)) *0.0001+this.getpoint ( Pukes[3].substring (2)) *0.000001+this.getpoint (pukes[0].substring (2)) *0.00000001;
}
if (Point3 = = point4) {
Return 1.0+this.getpoint (pukes[2].substring (2)) *0.01+this.getpoint (pukes[4].substring (2)) *0.0001+this.getpoint ( Pukes[1].substring (2)) *0.000001+this.getpoint (pukes[0].substring (2)) *0.00000001;
}
if (point4 = = point5) {
Return 1.0+this.getpoint (pukes[3].substring (2)) *0.01+this.getpoint (pukes[2].substring (2)) *0.0001+this.getpoint ( Pukes[1].substring (2)) *0.000001+this.getpoint (pukes[0].substring (2)) *0.00000001;
}

Return This.getpoint (pukes[4].substring (2)) *0.01+this.getpoint (pukes[3].substring (2)) *0.0001+this.getpoint ( Pukes[2].substring (2)) *0.000001+this.getpoint (pukes[1].substring (2)) *0.00000001+this.getpoint (Pukes[0]. SUBSTRING (2)) *0.0000000001;
}


public void init () {

while (true) {
This.createpuke ();
This.sortedpuke ();
This.createplayer ();
if (flag) {
This.showpuke ();
This.getscorewinner ();
System.out.println ("Do you want to continue with the game?" y/n ");
Scanner scan = new Scanner (system.in);
String idea = Scan.nextline (). toUpperCase ();
if (Idea.equals ("Y")) {
Pukelist.clear ();
Map.clear ();
This.init ();
} else {
System.out.println ("Bye, see you next time!") ");
System.exit (0);
}
}
}
}


public static void Main (string[] args) {
New Showhand (). Init ();
}
}

Questions and solutions, experience:

Through this practice more proficient in Java applications, especially the use of Set framework, set of disorder, non-repeatable, list of the order, repeatability, map of the key-value, the respective characteristics of more profound understanding. Improved personal ability!

Software Engineering Practice Job 2--Stud games (Java) Practice Report

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.