4.猜拳游戏(修改)
- 对战 人 电脑
- 出拳 出拳的:内容1 2 3
- 积累积分 {如何计分?规则 胜、负、平 各计多少分}
- 流程 n局 (循环 n 次数固定的 ,可以for
- 最后要有结果的输出 、流程每局的胜利or失败 、第几局、每一方出的是什么、等等输出
- 分析:如何使用面向对象 抽象成代码
- 分析出 人、 电脑 两个类
- 人:
- 属性:姓名、积分
- 方法:出拳:
- 电脑: 姓名、 积分
- 方法: 出拳:
- 1.初步的想法:抽取一个父类,共同的属性、方法都放进去
- 2.方法是需要子类重写的,需要两个子类:人和电脑
- 3.细节:如何重写方法:人输入、电脑是随机
- 4.流程:循环 简化:先进行一次比较,看是否能正常输出,然后再考虑循环多次的情况
bean包含BaseRole、Hand、Computer、Person类 controller包含Justice类 view包含Views类 --1.BaseRole类:抽象类,共性抽取Person类和Computer类 package bean; public abstract class BaseRole { protected String username="匿名"; protected int score; protected int hand; @Override public String toString() { return "BaseRole{" + "username='" + username + '\'' + ", score=" + score + '}'; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public abstract int hand(); } 2.Hand类定义猜拳常量 package bean; public class Hand { public static final int SCISSORS = 1; //剪刀 public static final int STONE = 2; //石头 public static final int CLOTH = 3; //布 } 3.Computer类继承抽象类BaseRole package bean; public class Computer extends BaseRole{ @Override public int hand() { //猜拳 return (int) (Math.random()*3+1); } } 4.Person类继承抽象类BaseRole package bean; import view.Views; public class Person extends BaseRole { private Views v = new Views(); public static final int PERSON_WIN = 1; public static final int PERSON_LOSE = -1; public static final int DRAW = 0; @Override public int hand() { return v.inputNum(this); } } 5.Justice类进行猜拳的逻辑评判 package controller; import bean.Computer; import bean.Hand; import bean.Person; public class Justice { public int judge(Person p, int personHand, Computer c, int computerHand) { //1 2 3 //剪刀<石头<布<剪刀 //1 0 -1 //玩家胜 0 平 -1 玩家负 switch (personHand) { case Hand .SCISSORS: switch (computerHand) { case Hand.CLOTH: return Person.PERSON_WIN; case Hand.SCISSORS: return Person.DRAW; case Hand.STONE: return Person.PERSON_LOSE; } break; case Hand.STONE: switch (computerHand) { case Hand.CLOTH: return Person.PERSON_LOSE; case Hand.SCISSORS: return Person.PERSON_WIN; case Hand.STONE: return Person.DRAW; } break; case Hand.CLOTH: switch (computerHand) { case Hand.CLOTH: return Person.DRAW; case Hand.SCISSORS: return Person.PERSON_LOSE; case Hand.STONE: return Person.PERSON_WIN; } break; } return Person.DRAW; } /** * 胜利+1,输-1,平不加 * * @param p * @param c * @param result */ public void addScore(Person p, Computer c, int result) { switch (result) { case Person.PERSON_LOSE: p.setScore(p.getScore() - 1); c.setScore(c.getScore() + 1); break; case Person.PERSON_WIN: p.setScore(p.getScore() + 1); c.setScore(c.getScore() - 1); break; default: break; } } } 6.Views类进行控制台输入输出,相当于视图层 package view; import bean.BaseRole; import bean.Computer; import bean.Hand; import bean.Person; import java.util.Scanner; public class Views { private static Scanner sc = new Scanner(System.in); public int inputNum(BaseRole role) { System.out.println("玩家:" + role.getUsername() + ",请出拳!"); return inputNum(3); } private int inputNum(int max) { System.out.println("请输入1-" + max + "之间的数字:"); String str = sc.nextLine(); int num = -1; try { num = Integer.parseInt(str); } catch (NumberFormatException e) { } if (num < 0 || num > max) { System.out.println("输入有误!"); num = inputNum(max); } return num; } public void show(BaseRole role, int hand) { String roleName = role instanceof Person ? "玩家" : "电脑"; System.out.print(roleName + ":" + role.getUsername() + "出的是:"); switch (hand) { case Hand.SCISSORS: System.out.println("剪刀"); break; case Hand.STONE: System.out.println("石头"); break; case Hand.CLOTH: System.out.println("布"); break; } } public void showResult(Person p, Computer c, int result) { switch (result) { case Person.DRAW: System.out.println("平手!"); break; case Person.PERSON_LOSE: System.out.println("电脑" + c.getUsername() + "赢了!"); break; case Person.PERSON_WIN: System.out.println("玩家:" + p.getUsername() + "赢了!"); break; } } public void welcome() { System.out.println("欢迎来到猜拳大战!准备好血洗你的对手了吗?"); } public String inputName() { System.out.println("请输入你的大名:"); return sc.nextLine(); } public int inputRound() { System.out.println("请输入你想玩的回合数:最大为:20"); return inputNum(20); } public void gameStart(Person p, Computer c, int round) { System.out.println("玩家:"+p.getUsername()+"大战电脑:"+c.getUsername()+":"+round +"局定输赢!"); } public void showFinalResult(Person p, Computer c) { System.out.println(p.getScore()+"\t"+c.getScore()); if (p.getScore()>c.getScore()){ System.out.println("恭喜玩家"+p.getUsername()+"取得了最终的胜利!"); }else if(p.getScore()<c.getScore()){ System.out.println("很遗憾,"+p.getUsername()+"你输了!"); }else{ System.out.println("双方积分相等,平局结束,再接再厉!"); } } } 7.Main类 import bean.Computer; import bean.Person; import controller.Justice; import view.Views; public class Main { static Views v = new Views(); static Justice justice = new Justice(); public static void main(String[] args) { /* Person p=new Person(); p.setUsername("张三丰"); int hand = p.hand(); Computer c=new Computer(); int hand1 = c.hand(); v.show(p,hand); v.show(c,hand1); int result = justice.judge(p, hand, c, hand1); v.showResult(p,c,result);*/ gameStart(); } private static void gameStart() { v.welcome(); String username = v.inputName(); Person p = new Person(); p.setUsername(username); Computer c = new Computer(); c.setUsername("阿尔法-狗"); int round = v.inputRound(); v.gameStart(p, c, round); for (int i = 0; i < round; i++) { int hand = p.hand(); int hand1 = c.hand(); v.show(p, hand); v.show(c, hand1); int result = justice.judge(p, hand, c, hand1); justice.addScore(p, c, result); v.showResult(p, c, result); } v.showFinalResult(p, c); } }