简单游戏:剪刀石头布;math.random()*3没有作用,电脑会一直出剪刀.
检查不出来哪里有问题...
package daygama1;
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.getScore());
String[] arr=new String[]{"剪刀","石头","布"};
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("提示:请输入剪刀,石头,布.其中之一.退出请输入'exit'");
String s1=sc.next(); //玩家输入
if(!filter(s1)){
System.out.println("输入有误!重新输入");
continue;
}
if(s1.equals("exit")){
break;
}
int count=(int)Math.random()*3;//0-2
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);
}
if(flag<0){
System.out.println(player.getName()+"失败了!");
player.setScore(player.getScore()-10);
}
if(flag==0){
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;
}
}
/*
* 游戏规则如下
*/
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;
}
}