`
ynwetaiahf
  • 浏览: 6170 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

剪刀石头布游戏

阅读更多
//用户类
package 游戏;

import java.util.Scanner;

public class Person {
String name = "匿名";
int score;

public int showHand() {
System.out.print("请出拳: 1.剪刀 2.石头 3.布");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
while (num < 1 || num > 3) {
System.out.println("你输入的不合法,请重新输入");
num = input.nextInt();
}

switch (num) {
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
}

return num;
}
}


//机器类
package 游戏;

public class Computer {
String name = "匿名";
int score;
int random;// 注意一定要定义为成员变量

public int showHand() {
random = (int) (Math.random() * 3) + 1;
switch (random) {
case 1:
System.out.println("电脑出拳:剪刀");

break;
case 2:
System.out.println("电脑出拳:石头");

break;
case 3:
System.out.println("电脑出拳:布");

break;
}
return random;
}
}


//游戏类
package 游戏;

import java.util.Scanner;

public class Game {
Computer c;
Person p;
int count;

public void initial() {
c = new Computer();
p = new Person();

int choice;
Scanner input = new Scanner(System.in);
System.out
.println("---------------------------------欢 迎 进 入 游 戏 世 界---------------------------------\n\n\n");
System.out.println("\t\t\t---------------------------------");
System.out.println("\t\t\t----  猜 拳\t\t开始   ----");
System.out.println("\t\t\t---------------------------------\n\n");
System.out.println("出拳规则: 1.剪刀 2.石头 3.布");
System.out.println("请选择角色(1:刘备  2:孙权  3:曹操:)");
choice = input.nextInt();
while (choice < 1 || choice > 3) {
System.out.println("你输入的不合法,请重新输入");
choice = input.nextInt();
}
switch (choice) {
case 1:
p.name = "刘备";
break;
case 2:
p.name = "孙权";
break;
case 3:
p.name = "曹操";
break;

}
p.score = 0;
c.score = 0;
count = 0;

}

public void start() {
int pNum, cNum;
String answer;
System.out.print("要开始游戏吗?(y/n)");
Scanner input = new Scanner(System.in);
answer = input.next();
while (answer.equals("y")) {
pNum = p.showHand();
cNum = c.showHand();// 一定要把返回的值赋值给变量,否则出错
if (pNum == 1 && cNum == 3 || pNum == 2 && cNum == 1 || pNum == 3
&& cNum == 2) {
System.out.println("结果:你赢了,恭喜恭喜……");
p.score++;
} else if (pNum == cNum) {
System.out.println("结果:真衰,平局了,秋后再找你算帐");
p.score++;
c.score++;
} else {
System.out.println("结果:^_^  笨笨,今天你发挥不好哟");
c.score++;
}
count++;

System.out.println("是否要继续游戏?(y/n)");
answer = input.next();

}
showResult();
}

public void showResult() {
System.out
.println("----------------------------------------------------------------");
System.out.println(p.name + "\tVS\t" + c.name);
System.out.println("对战次数:" + count);
// 计算结果是哪个赢,并给出提示
if (p.score > c.score) {
System.out.println("结果:此次通关,你大获全胜");
} else if (p.score < c.score) {
System.out.println("结果:此次通关,你全军毁灭");
} else {
System.out.println("结果:此次通关,两败俱伤(平局)");
}
System.out
.println("----------------------------------------------------------------");
}

}
//测试类
package 游戏;

public class TestGame {

/**
* @测试类
*/
public static void main(String[] args) {
Game g = new Game();
g.initial();//首先要游戏初始化
g.start();
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics