Scissors Game Case

Source: Internet
Author: User

Scissors is made up of stone, scissors, cloth, you can choose who your opponent, here set up 3 opponents (President, Deputy, director), and then choose your punch, by using the number 1, 2, three, respectively, represents 3 states. Then we also need to add a scoring function. Here we build 4 classes, man, machine, Main method, test class.

Of course, we all know that the basic methods of human and machine are similar, because we want to get to the keyboard input content, so we want to use this scanner, this under the Util package, we directly import it. The code directly posted here:

Import Java.util.scanner;public class person{private String name;private int score;public person () {name= "sdksdk0"; score=0;} Public person (String N) {name=n;score=0;} public int showfist () {Scanner input=new Scanner (system.in); System.out.println ("\ n please punch: 1 Scissors 2 stone  3  cloth (enter the corresponding number):"); int show=input.nextint (); switch (show) {Case 1: System.out.println ("You Punch: Scissors"); Break;case 2:system.out.println ("You Punch: Stone"); Break;case 3:system.out.println ("You Punch: cloth"); break;} return show;} Public String GetName () {return name;} public void SetName (String name) {this.name=name;} public int Getscore () {return score;} public void SetScore (int score) {This.score=score;}}
Machine class, the natural need for a random number, so that can be randomly compared.
Import Java.util.random;public class android{private string name;private int score;public Android (string n) {name=n; score=0;} Public Android () {name= "Android"; score=0;} public int showfist () {random r=new random (), int show=r.nextint (3) +1;switch (show) {Case 1:system.out.println ("You Punch: Scissors") ; Break;case 2:system.out.println ("You Punch: Stone"), Break;case 3:system.out.println ("You Punch: cloth"); return show;} Public String GetName () {return name;} public void SetName (String name) {this.name=name;} public int Getscore () {return score;} public void SetScore (int score) {This.score=score;}}


The most important of the following is our Main method class.
We need to define people, machines, scoring methods.

private person person;
Private Android Android;
private int count;

Function initialization
public void initial () {
Person=new person ();
Android=new Android ();
count=0;
}

Define display results:

if (result==1) {System.out.println ("Result: Equal shares"),} else if (result==2) {System.out.println ("Result:" +person.getname () + "You won" );} else if (result==3) {System.out.println ("Result:" +person.getname () + "You Lost" +android.getname () + "won");}
In these cases, the player wins:
1 Scissors 2 stone  3  cloth

The user 1 to the machine 3, the user 2 to the machine 1, the user 3 to the Machine 2.

If the user inputs the same as the machine produced is a draw, the rest of the situation is the machine wins.

As for the calculation of the score, that's it. Android.setscore (Android.getscore () +1);

The complete code is as follows:

Import Java.util.scanner;public class referee{private person person;private Android android;private int count;// initialization function public void initial () {person=new person (); android=new android (); count=0;} Calculates the total sum of public int calcresult () {if (Person.getscore () ==android.getscore ()) {return 1;} else if (Person.getscore () > Android.getscore ()) {return 2;} Else{return 3;}} Show total results public void Showresult () {System.out.println ("======================"); System.out.println (Person.getname () + "VS" +android.getname ()); SYSTEM.OUT.PRINTLN ("Total number of battles:" +count), int result=calcresult (), if (result==1) {System.out.println ("Result: equal");} else if ( result==2) {System.out.println ("Result:" +person.getname () + "You Won");} else if (result==3) {System.out.println ("Result:" + Person.getname () + "You Lost" +android.getname () + "won");} System.out.println ("======================");} public void Startgame () {System.out.println ("======================"); System.out.println ("\n\t************************"); SYSTEM.OUT.PRINTLN ("Please select your opponent 1 President 2 VP 3 Director"); Scanner input=new Scanner (system.in); int role=input.nextint (); switch (role) {case 1:android.setname ("President"); Break;case 2:android.setname ("VP"); Break;case 3:android.setname ("director"); System.out.println ("\ n to start? [y/n] "); String con=input.next (); int Perfist;int androidfist;while (con.equals ("Y")) {//perfist = Person.showfist ();p erfist= Person.showfist (); androidfist=android.showfist (); if (perfist==androidfist) {System.out.println ("Result: Draw");} else if ( (perfist==1&&androidfist==3) | | (perfist==2&&androidfist==1) | | (perfist==3&&androidfist==2)) {System.out.println ("Result:" +person.getname () + "won!"); Person.setscore (Person.getscore () +1);} ELSE{SYSTEM.OUT.PRINTLN ("Result:" +person.getname () + "Lost"); Android.setscore (Android.getscore () +1);} count++; System.out.print ("\ n whether to start the next round (y/n)"); Con=input.next ();} Showresult ();}}
Finally, we just need a test class to do it.

public class Startgame{public static void Main (string[] args) {Referee R=new Referee (); r.initial (); R.startgame ();}}

Of course, if you want to write the test class together can be compiled normally, but it is not recommended to do so, so the refactoring of the code is not good. Use the command Javac Startgame.java to compile, and then Java Startgame run.





Scissors Game Case

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.