import java.util.Scanner;
public class Game {
private Scanner scan = new Scanner(System.in);
private String rule[] = {"","剪刀","石头","布"};
private String role[] = {"","刘备","孙权","曹操"};
private Computer computer;
private User user;
private int roundCount;
private Game(){
roundCount = 0;
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void start(){
computer = new Computer();
System.out.println("- - 欢迎进入游戏世界 - -");
System.out.println("** ******************");
System.out.println("** 猜拳,开始 **");
System.out.println("** ******************");
System.out.println();
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.print("请选择角色(1:刘备 2.孙权 3.曹操)");
int userRole = scan.nextInt();
if(userRole >=1 && userRole <=3){
user = new User(role[userRole]);
System.out.println();
System.out.print("要开始吗?(y/n) ");
String start = scan.next();
if(start.equals("y")){
round();
}
else if(start.equals("n")){
}
}
}
private void round(){
roundCount ++;
System.out.println();
System.out.print("请出拳:1.剪刀 2.石头 3.布 (输入相应数字): ");
int userRule = user.round();
if(userRule >= 1 && userRule <= 3){
int computerRule = computer.round();
judge(userRule,computerRule);
System.out.println();
System.out.print("是否开始下一轮(y/n): ");
String next = scan.next();
if(next.equals("y")){
round();
}
else if(next.equals("n")){
end();
}
}
}
private void judge(int ur,int cr){
System.out.println("你出拳:"+ rule[ur]);
System.out.println("电脑出拳:"+ rule[cr]);
if(ur == cr){
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}
else if((ur == 1 && cr== 3)||
(ur == 2 && cr == 1)||
(ur == 3 && cr == 2)){
System.out.println("结果:你赢了!");
user.win();
}
else{
System.out.println("结果:你输了!");
computer.win();
}
}
private void end(){
System.out.println("- -------------------------------------");
System.out.println(user.getName() + "VS" + computer.getName());
System.out.println("对战次数:" + roundCount);
if(computer.getWin() == user.getWin()){
System.out.println("结果:打成平手,下次再和你一分高下!");
}
else if(computer.getWin() > user.getWin()){
System.out.println("结果:你输了!电脑赢了" + computer.getWin()+"次!");
}
else{
System.out.println("结果:你赢了!你赢了" + user.getWin()+"次!");
}
System.out.println("- -------------------------------------");
}
class Computer{
private int win;
private String name;
public Computer(){
win = 0;
name = "匿名";
}
public int round(){
return (int)(System.currentTimeMillis() % 3) + 1;//随机返回1、2、3
}
public String getName(){
return name;
}
public void win(){
win ++;
}
public int getWin(){
return win;
}
}
class User{
private int win;
private String name = "";
public User(String name){
this.name = name;
win = 0;
}
public int round(){
return scan.nextInt();
}
public String getName(){
return name;
}
public void win(){
win ++;
}
public int getWin(){
return win;
}
}
}
刚刚写好的,应该满足你的题目要求。赢了和输了的提示信息题目里没有是我自己随便写的。