Package Youxi;
public class Player {
private String name;
private double score;
Public Player (String name, double score) {
Super ();
THIS.name = name;
This.score = score;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public double Getscore () {
return score;
}
public void SetScore (double score) {
This.score = score;
}
Private String SetScore () {
TODO auto-generated Method Stub
return This.setscore ();
}
Private String SetName () {
TODO auto-generated Method Stub
return This.setname ();
}
}
Package Youxi;
Import Java.util.Scanner;
public class Game {
Private player player;
Public Game (player player) {
This.player=player;
}
public void Start () {
while (true) {
System.out.println ("Start Game");
Scanner sc=new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please input scissors stone cloth or exit");
String S1=sc.next ();
if (S1.equals ("Exit")) {
Break
}
String[] arr=new string[]{"scissors", "stone", "cloth"};
int count= (int) (Math.random ());
String S2=arr[count];
int Flag=rule (S1,S2);
if (flag>0) {
System.out.println ("Robot" +arr[count]+ "~~~~~" +player.getname () + "won, plus very");
Player.setscore (Player.getscore () +10);
}
if (flag<0) {
System.out.println ("Robot" +arr[count]+ "~~~~~" +player.getname () + "lost, minus very");
Player.setscore (Player.getscore ()-10);
}
else if (flag==0) {
System.out.println ("Robot" +arr[count]+ "~~~~~" + "Tie");
}
System.out.println (Player.getname () + ":" +player.getscore ());
}
}
/**
* Rules of the game
* @return
*/
public int rule (String s1,string s2) {
if (s1.equals (S2))
{return 0;}
if (S1.equals ("scissors")) {
if (S2.equals ("stone")) {
return-1;
}
if (S2.equals ("cloth")) {
return 1;
}
}
if (S1.equals ("stone")) {
if (S2.equals ("cloth")) {
return-1;
}
if (S2.equals ("scissors")) {
return 1;
}
}
if (S1.equals ("cloth")) {
if (S2.equals ("scissors")) {
return-1;
}
if (S2.equals ("stone")) {
return 1;
}
}
return 0;
}
}
Package Youxi;
public class Test {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Player P=new player ("Chi Wei", 100);
Game G=new Game (p);
G.start ();
}
}
The Java applet for the stone Scissors cloth for beginners Reference