Learn Java from scratch (small game stone scissors cloth)

Source: Internet
Author: User

Game.java

Package Com.java;
Import Java.util.Scanner;
public class Game {
Private player player;
Public Game (player player) {
This.player=player;
}
public void Start () {
System.out.println ("Start Game");
System.out.println (Player.getname () + "Enter the game, the initial points are:" +player.getscore ());
String[] arr=new string[]{"scissors", "stone", "cloth"};
Scanner sc=new Scanner (system.in);
while (true) {
SYSTEM.OUT.PRINTLN ("Please input scissors, stone, cloth one of them");
String S1=sc.next ();//Player input
if (!filter (S1)) {
SYSTEM.OUT.PRINTLN ("The command you entered is incorrect, please re-enter");
Continue
}
if (S1.equals ("Exit")) {
Break
}
int count= (int) (Math.random ());
String S2=arr[count];
int Flag=rule (S1,S2);
System.out.println ("You Out is:" +s1+ ", the system Out is:" +S2);
if (flag>0) {
System.out.println ("Congratulations" +player.getname () + "win, reward 10 points");
Player.setscore (Player.getscore () +10);
}else if (flag<0) {
System.out.println ("Sorry" +player.getname () + "lost, minus 10 points");
Player.setscore (Player.getscore ()-10);
}else{
System.out.println ("flattened out");
}
SYSTEM.OUT.PRINTLN (player);
}
System.out.println (Player.getname () + "exit game, Current points are:" +player.getscore ());
}
public boolean filter (String s) {
if ("Scissors". Equals (s) | | " Stone ". Equals (s)
||" Cloth ". Equals (s) | |" Exit ". Equals (s)) {
return true;}
else{return false;
}
}
/**
* 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 ("scissors")) {
return 1;
}
if (S2.equals ("cloth")) {
return-1;
}
}
if (S1.equals ("cloth")) {
if (S2.equals ("scissors")) {
return-1;
}
if (S2.equals ("stone")) {
return 1;
}
return 0;

}
return 0;
}
}

Player.java

Package Com.java;

public class Player {

Private String name;//player name
private int score;//Integral

Public Player (String name, int score) {
Super ();
THIS.name = name;
This.score = score;
}
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;
}
Public String toString () {

return This.getname () + ":" +this.getscore ();
}

}

Test.java

Package Com.java;

public class Test {
public static void Main (string[] args) {
Player P=new player ("King * *", 100);
Game G=new Game (p);
G.start ();
}
}
This little game and I wrote the last to guess the core idea of the game is the use of math.random, this can be said to be its deepening application ... You can try it too!

Learn Java from scratch (small game stone scissors cloth)

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.