13th case of human-machine scissors

Source: Internet
Author: User

public class Computer {
String name = "Computer";//Name
int score = 0;//integral
public int showfist () {
Generate random numbers
int show = (int) ((Math.random () *10)%3+1); Generate a random number to indicate a computer punch
Output punch result and return
Switch (show) {
Case 1:
System.out.println (name+ "Punch: Scissors");
Break
Case 2:
System.out.println (name+ "punch: Stone");
Break
Case 3:
System.out.println (name+ "punch: cloth");
Break
}

return show;
}

public class Person {
String name = "Anonymous"; Name
int score = 0; Integral

public int showfist () {
Receive User's Choice
Scanner input = new Scanner (system.in);
System.out.print ("\ n please punch (1. Scissors 2. Stone 3. Cloth):");
int show = Input.nextint ();
Output punch result, and return
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;

}

Import Java.util.Scanner;

public class Game {
person person; User

Computer computer;//man-machine
int count;//PvP Times

public void inital () {
person = new person ();
Computer = new computer ();
Count = 0;
}
Start the game
public void Startgame () {
SYSTEM.OUT.PRINTLN ("-------------welcome into Scissors Game---------------");
System.out.println ();
System.out.println ("******************************************");
SYSTEM.OUT.PRINTLN ("* * scissors, start * *");
System.out.println ("******************************************");
System.out.println ();
System.out.println ("Punching rule: 1. Scissors 2. Stone 3. Cloth");
Scanner input = new Scanner (system.in);
String exit= "n";//Exit system
do {
Inital ();//initialization
Select opponent
System.out.print ("Choose Your Opponent" (1. Liu Bei 2. Sun Quan 3. Cao): ");
int role = Input.nextint ();
Switch (role) {
Case 1:
Computer.name= "Liu Bei";
SYSTEM.OUT.PRINTLN ("You have chosen" + computer.name+ "PvP");
Break
Case 2:
Computer.name= "Sun Quan";
SYSTEM.OUT.PRINTLN ("You have Chosen" +computer.name+ "PvP");
Break
Case 3:
SYSTEM.OUT.PRINTLN ("You have Chosen" +computer.name+ "PvP");
Break
Default
SYSTEM.OUT.PRINTLN ("Input Error! ");
Break
}

Enter the name of the game player
System.out.print ("Please enter your name:");
Person.name=input.next ();
System.out.println (person.name+ "VS" +computer.name+ "PVP \ n");
Start the game
System.out.print ("Do you want to start?" ( y/n) ");
Start a game
String start = Input.next ();
int perfist;//the user to punch
int comfist;//computer out fist
while (Start.equals ("Y")) {
Punch out
Perfist = Person.showfist ();
Comfist = Computer.showfist ();
Judging the winner
if ((perfist==1&&comfist==1) | | | (perfist==2&&comfist==2) | | (perfist==3&&comfist==3)) {
SYSTEM.OUT.PRINTLN ("Result: Draw, Bad!\n");//Tie
}else if ((perfist==2&&comfist==1) | | (perfist==3&&comfist==2) | | (perfist==1&&comfist==3)) {
SYSTEM.OUT.PRINTLN ("The result says: ^_^, you lose, really stupid!\n");//Computer wins
computer.score++;
}else {
SYSTEM.OUT.PRINTLN ("Result: Congratulations, you won the!\n");//Victory
person.score++;
}
count++;
System.out.print ("Whether to proceed to the next round (y/n)");
Start = Input.next ();
}
Show results
Showresult ();
System.out.print ("\ nyou want to start the next game?" ( y/n): ");
Exit = Input.next ();
System.out.println ();

} while (!exit.equals ("n"));

SYSTEM.OUT.PRINTLN ("System exit");
}
private void Showresult () {
Show PvP Times
System.out.println ("------------------------------");
System.out.println (computer.name+ "VS" +person.name);
System.out.println ("PvP Times:" +count);
Show Final Score
System.out.println ("\ n name \ t score");
System.out.println (person.name+ "\ t" +person.score);
System.out.println (computer.name+ "\ t" +computer.score+ "\ n");

Show PVP Results
int result = 3;
if (result==1) {
SYSTEM.OUT.PRINTLN ("Result: a tie, a real loser!");
}else if (result==2) {
SYSTEM.OUT.PRINTLN ("Result: Congratulations, you won!");
}else
{
SYSTEM.OUT.PRINTLN ("Result: Oh, stupid, next refueling ah!"); /Computer wins
}
System.out.println ("--------------------------");
}
Calculate Game Results
private int Calcresult () {
TODO auto-generated Method Stub
if (Person.score = = Computer.score) {
Return 1;//Battle Flat
}else if (Person.score > Computer.score) {
Return 2;//game player wins
}else {
Return 3;//defeat
}
}


}

public class Test {
public static void Main (string[] args) {
TODO auto-generated Method Stub

Game game = new Game ();

Game.inital ();

Game.startgame ();
}

}

}


}

13th case of human-machine scissors

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.