用Java编译“三国群英传”游戏(本质石头剪刀布)
package com.domain;
//玩家类
public abstract class Player {
//玩家有哪些属性呢?
protected String name; //昵称
protected int score; //得分
//注意,出拳类类型是固定,是每一个玩家都有的,所以把设计为类的静态属性。方便我们去调用。
public static String[] handType = {"石头","剪刀","布"};
public Player() {
}
public Player(String name) {
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;
}
//玩家有哪些方法
//1.玩家的出拳方法
public abstract String showHand();
}
package com.logic;
//游戏的业务逻辑类
//判断每轮出拳的胜负
//判断游戏是否结束
//显示游戏的结果
import com.domain.Player;
public class GameLogic {
//电脑玩家
private Player computerPlayer;
//人类玩家
private Player humanPlayer;
public GameLogic() {
}
public GameLogic(Player computerPlayer, Player humanPlayer) {
this.computerPlayer = computerPlayer;
this.humanPlayer = humanPlayer;
}
public Player getComputerPlayer() {
return computerPlayer;
}
public void setComputerPlayer(Player computerPlayer) {
this.computerPlayer = computerPlayer;
}
public Player getHumanPlayer() {
return humanPlayer;
}
public void setHumanPlayer(Player humanPlayer) {
this.humanPlayer = humanPlayer;
}
//判断每轮PK胜负的方法
public void pk(){
//怎么判断呢?
//1.获得各自的出拳
String computerHand = computerPlayer.showHand();
String humanHand = humanPlayer.showHand();
System.out.println(this.getHumanPlayer().getName() + "的出拳是:" + humanHand);
System.out.println(this.getComputerPlayer().getName() + "的出拳是:" + computerHand);
if(computerHand.equals(humanHand)){ //说明打了平手
System.out.println("诶呦,不分胜负咱们继续大战五百回合!");
}else if(humanHand.equals("石头")){
//电脑的出拳只可能是剪刀或者布。
if(computerHand.equals("剪刀")){
//给人类玩家的分数加1
this.getHumanPlayer().setScore(this.getHumanPlayer().getScore()+1);
System.out.println("小样看你还嚣张不。");
}else{
this.getComputerPlayer().setScore(this.getComputerPlayer().getScore()+1);
System.out.println("你不过就是运气好点罢了。");
}
}else if(humanHand.equals("剪刀")){
if(computerHand.equals("布")){
//给人类玩家的分数加1
this.getHumanPlayer().setScore(this.getHumanPlayer().getScore()+1);
System.out.println("小样看你还嚣张不。");
}else{
this.getComputerPlayer().setScore(this.getComputerPlayer().getScore()+1);
System.out.println("你不过就是运气好点罢了。");
}
}else if(humanHand.equals("布")){
if(computerHand.equals("石头")){
//给人类玩家的分数加1
this.getHumanPlayer().setScore(this.getHumanPlayer().getScore()+1);
System.out.println("小样看你还嚣张不。");
}else{
this.getComputerPlayer().setScore(this.getComputerPlayer().getScore()+1);
System.out.println("你不过就是运气好点罢了。");
}
}
//显示每轮的分数
showRoundResult();
//判断游戏是否结束
if(isGameOver()){
//显示游戏结果。。。
showGameResult();
}
}
//显示每轮PK后的分数
public void showRoundResult(){
System.out.println("************当前比分是**********");
System.out.println(this.getHumanPlayer().getName()+" "+this.getHumanPlayer().getScore() + ":" + this.getComputerPlayer().getScore()+" "+this.getComputerPlayer().getName());
}
//判断游戏是否结束
//规定游戏是五局三胜制,如果有任何一方分数大于等于3分则游戏结束。
public boolean isGameOver(){
if(this.getComputerPlayer().getScore()>=3||this.getHumanPlayer().getScore()>=3){
return true;
}
return false;
}
//显示游戏结果
public void showGameResult(){
if(this.getComputerPlayer().getScore()>=3){
//说明电脑玩家赢了...
System.out.println("君子报仇十年不晚,咱们后会有期....");
}else{
System.out.println("别羡慕哥,哥只是个传奇....");
}
}
}
package com.domain;
import java.util.Random;
public class ComputerPlayer extends Player{
public ComputerPlayer() {
}
public ComputerPlayer(String name) {
super(name);
}
//实现出拳方法
/*电脑玩家每次都是随机出拳。*/
@Override
public String showHand() {
/*
* 生成一个0-2之间的随机数
* */
return Player.handType[new Random().nextInt(3)];
}
}
public class HumanPlayer extends Player {
public HumanPlayer() {
}
public HumanPlayer(String name) {
super(name);
}
/*
* 实现出拳方法,人类自己选择要出的拳
* */
@Override
public String showHand() {
try {
int choice;//接收用户的输入
Scanner sc = new Scanner(System.in);
System.out.println("请选择您要出的拳:");
System.out.println("1.石头 2.剪刀 3.布");
choice = sc.nextInt(); //当然这里也有可能会抛出异常,最好捕获异常。
//最好判断一下,如果用户输入的数字范围不是在1-3,提示错误
if (choice > 3 || choice < 1) {
System.out.println("输入选项非法!");
return null;
}
return Player.handType[--choice];
}catch(Exception ex){
System.out.println("程序发生意外错误!");
return null;
}
}
}
package com.app;
//游戏的界面类
//1显示游戏的主界面
//2实现菜单选项功能
//3 实现用户的录入import com.domain.ComputerPlayer;
import com.domain.HumanPlayer;
import com.domain.Player;
import com.logic.GameLogic;import java.util.Scanner;
public class GuessGame {
public static Scanner sc = new Scanner(System.in);
public static String[] computerPlayerNames ={“曹操”,“刘备”,“孙权”};
public static int choice =-1; //用户的输入选择。
public static Player computerPlayer; //电脑玩家对象
public static Player humanPlayer; //人类玩家对象
//显示游戏的主界面
public static void showMenu(){
System.out.println(“欢迎进入三国群英传**”);
System.out.println(“选择您要挑战的角色:”);
System.out.println(“1.曹操”);
System.out.println(“2.刘备”);
System.out.println(“3.孙权”);
System.out.println(“请选择:”);
choice = sc.nextInt();
}
public static void main(String[] args) {
showMenu();
computerPlayer = new ComputerPlayer(GuessGame.computerPlayerNames[--choice]);
System.out.println("请输入您的昵称:");
String name = sc.next();
humanPlayer = new HumanPlayer(name);
//这样的话,我们的人类玩家和电脑玩家都创建好了。生成一个游戏业务逻辑类。
GameLogic gameLogic = new GameLogic(computerPlayer,humanPlayer);
//开始pk
while(!gameLogic.isGameOver()){
gameLogic.pk();
}
}
}