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("开始游戏");
System.out.println(player.getName()+"进入游戏,初始积分为:"+player.getScore());
String[] arr=new String[]{"剪刀","石头","布"};
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("请输入剪刀、石头、布其中个之一");
String s1=sc.next();//玩家输入
if(!filter(s1)){
System.out.println("您输入的命令有误,请重新输入");
continue;
}
if(s1.equals("exit")){
break;
}
int count=(int)(Math.random()*3);
String s2=arr[count];
int flag=rule(s1,s2);
System.out.println("您出的是:"+s1+",系统出的是:"+s2);
if(flag>0){
System.out.println("恭喜"+player.getName()+"赢了,奖励10积分");
player.setScore(player.getScore()+10);
}else if(flag<0){
System.out.println("对不起"+player.getName()+"输了,扣除10积分");
player.setScore(player.getScore()-10);
}else{
System.out.println("打平了");
}
System.out.println(player);
}
System.out.println(player.getName()+"退出游戏,当前积分为:"+player.getScore());
}
public boolean filter(String s){
if("剪刀".equals(s)||"石头".equals(s)
||"布".equals(s)||"exit".equals(s)){
return true;}
else{ return false;
}
}
/**
* 游戏规则
* @return
*/
public int rule(String s1,String s2){
if(s1.equals(s2)){
return 0;
}
if(s1.equals("剪刀")){
if(s2.equals("石头")){
return -1;
}
if(s2.equals("布")){
return 1;
}
}
if(s1.equals("石头")){
if(s2.equals("剪刀")){
return 1;
}
if(s2.equals("布")){
return -1;
}
}
if(s1.equals("布")){
if(s2.equals("剪刀")){
return -1;
}
if(s2.equals("石头")){
return 1;
}
return 0;
}
return 0;
}
}
Player.java
package com.java;
public class Player {
private String name;//玩家姓名
private int score;//积分
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("王**",100);
Game g=new Game(p);
g.start();
}
}
这个小游戏和我上个写到的猜数字游戏核心思想一样都是用到了Math.random,这个可以说是其的加深应用。。。你们也可以试试看!
博主在分享个免费翻墙软件