面向对象的综合作业:随机数的应用-猜拳游戏:石头、剪刀、布
分析:1 两个对象(人person和机器compter),抽象出公共的部分(Player类,包含玩家姓名属性、积分属性、出拳的抽象方法),让人和机器实现它。
2 定义一个Game游戏类,包含PK的方法和显示最终结果的方法,
3 定义一个测试类,进行游戏,直到玩家选择退出游戏才结束对战
第1局开始
请输入1石头 2剪刀 3布
-->用户选择
-->输出结果
小白出拳:石头
电脑出拳:布
结果:电脑赢了
提示是否继续,继续请输入Y?【若继续】:
第2局开始
请输入1石头 2剪刀 3布
-->用户选择
-->输出结果 小白出石头 电脑出布 电脑赢了
【若不继续】:输出积分:小白积分:1 ,电脑积分:0
package ppt10lang包.猜拳游戏;
public abstract class Player {
private String name;//用户名
private int score;//积分
public Player() {
}
public Player(String name) {
super();
this.name = name;
}
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 abstract int showFist();
}
package ppt10lang包.猜拳游戏;
import java.util.Scanner;
public class Person extends Player{
public Person(String pname){
super(pname);
}
@Override
public int showFist() {
System.out.println("请用户输入1石头 2剪刀 3布");
Scanner input = new Scanner(System.in);
int fist = input.nextInt();
return fist;
}
}
package ppt10lang包.猜拳游戏;
import java.util.Random;
/**
* 电脑出拳
* @author Administrator
*
*/
public class Computer extends Player{
public Computer(String cname){
super(cname);
}
@Override
public int showFist() {
//随机数 1-3
Random random = new Random();
int fist = random.nextInt(3) +1;
return fist;
}
}
package ppt10lang包.猜拳游戏;
public class Game {
private Person p;
private Computer c;
// private int i = 1;
public Game(Person p,Computer c){
this.p = p;
this.c = c;
}
/**
* pk
* 判断当前局的输赢结果
*/
public void pk(){
//提升部分:五局
for (int i = 1; i <= 5; i++) {
System.out.println("第" + i +"局");
//获取玩家出拳的数字
int pfist = p.showFist();
//获取电脑出拳的数字
int cfist = c.showFist();
//获取玩家出拳的具体
String pValue = getFistValue(pfist);
//获取电脑出拳的具体
String cValue = getFistValue(cfist);
//比较规则
if ((pfist == 1 && cfist == 2) ||(pfist == 2 && cfist == 3) || (pfist == 3 && cfist == 1) ) {
System.out.println(p.getName() + "玩家出拳:" + pValue);
System.out.println(c.getName() + "电脑出拳:" + cValue);
System.out.println("恭喜你:" + p.getName() );
//用户积分 +1
p.setScore( p.getScore() + 1 );
}else if (pfist == cfist) {
System.out.println(p.getName() + "玩家出拳:" + pValue);
System.out.println(c.getName() + "电脑出拳:" + cValue);
System.out.println("平局" );
}else{
System.out.println(p.getName() + "玩家出拳:" + pValue);
System.out.println(c.getName() + "电脑出拳:" + cValue);
System.out.println("你输了" + ",电脑:" + c.getName() + "赢了");
c.setScore( c.getScore() + 1 );
}
}
// i++;
// 提升完之后:调用显示结果的方法
showResult();
}
//获取猜拳对应的中文
public String getFistValue(int fist){
String fistValue = "";
switch (fist) {
case 1:
fistValue = "石头";
break;
case 2:
fistValue = "剪刀";
break;
case 3:
fistValue = "布";
break;
default:
break;
}
return fistValue;
}
//最终结果
public void showResult() {
System.out.println("最终结果:");
System.out.println(p.getName() +"最终积分: " + p.getScore());
System.out.println(c.getName() +"最终积分: " + c.getScore());
}
}
package ppt10lang包.猜拳游戏;
import java.util.Scanner;
public class TestGame {
public static void main(String[] args) {
// 玩家对象
Person p = new Person("小白");
// 电脑对象
Computer c = new Computer("电脑CC");
Game game = new Game(p, c);
// game.pk();
Scanner input = new Scanner(System.in);
while (true) {
game.pk();
System.out.println("是否继续,继续Y");
String str = input.next();
//忽略大小写
if (!"Y".equalsIgnoreCase(str)) {
break;
}
}
//未提升之前:调用显示结果
// game.showResult();
}
}