本文主要是介绍猜拳游戏之面向对象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
猜拳游戏
要求:用户可以选择对战的角色名称(1,刘备 2,关羽 3,张飞),也可以自由输入自己名称,在对战期间,电脑由系统随机出拳(1,剪刀 2,石头 3, 布),玩家由自己出拳,结束后可以看到自己和电脑的手势,以及得分情况,(赢一局积3分,输一局不扣分,平局各加一分)。在整局游戏结束后要求显示出胜负情况以及积分情况。
类的设计
本局游戏设置为四个类分别是玩家类、电脑类、裁判类、游戏测试类。
玩家类
属性:昵称(roleName)积分(integral)
方法:玩家出拳(printMora)
电脑类
属性:名称(roleName)积分(integral)
方法:电脑出拳(printMora)
裁判类
属性:玩家(player)电脑(computer)局数(gameNumber)
方法: 游戏开始准备(start()) 猜拳输赢判定(arbitrament()) 游戏结果公示(end())游戏过程(wholeJourney())转换手势(changeString(int mora, Object gamePlayer))
代码展示
玩家类
package com.qianfeng.moraGame;import java.util.Scanner;//玩家类
public class Player {private String roleName;//玩家姓名private int integral;//玩家积分public Player() {}public Player(String roleName, int integral) {this.roleName = roleName;this.integral = integral;}//玩家猜拳public int printMora() {Scanner input = new Scanner(System.in);int mora = input.nextInt();
// input.close();return mora;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}public int getIntegral() {return integral;}public void setIntegral(int integral) {this.integral = integral;}}
电脑类
package com.qianfeng.moraGame;import java.util.Random;//电脑类
public class Computer {private String roleName;//电脑名字private int integral;//电脑积分public Computer() {}public Computer(String roleName, int integral) {this.roleName = roleName;this.integral = integral;}//电脑猜拳public int printMora() {Random random = new Random();int mora = random.nextInt(3)+1;return mora;}public void setRoleName(String roleName) {this.roleName = roleName;}public String getRoleName() {return this.roleName;}public void setIntegral(int integral) {this.integral = integral;}public int getIntegral() {return integral;}}
裁判类
package com.qianfeng.moraGame;import java.util.Scanner;//裁判类
public class Referee {Scanner input = new Scanner(System.in);private Player player;private Computer computer;private int gameNumber;public Referee() {}public Referee(Player player, Computer computer) {this.player = player;this.computer = computer;}//游戏开始准备private void start() {System.out.println("***************************");System.out.println("* 欢迎您进入游戏 *");System.out.println("***************************");int n;do {System.out.print("请选择您对战的角色(1,刘备 2,关羽 3,张飞):");n = input.nextInt();if (n == 1 || n == 2 || n == 3) {break;} else {System.out.println("输入错误!!");}} while (true);switch (n) {case 1:computer.setRoleName("刘备");break;case 2:computer.setRoleName("关羽");break;case 3:computer.setRoleName("张飞");break;}System.out.print("请输入您的游戏昵称:");player.setRoleName(input.next());System.out.println(player.getRoleName() + " VS " + computer.getRoleName());System.out.println();}//猜拳输赢判定private void arbitrament() {int mora1;do {System.out.print("请" + player.getRoleName() + "出拳(1,剪刀 2,石头 3,布):");mora1 = player.printMora();if (mora1 == 1 || mora1 == 2 || mora1 == 3) {break;} else {System.out.println("输入错误!!");}} while (true);int mora2 = computer.printMora();System.out.println(computer.getRoleName() + "由系统出拳完毕");changeString(mora1, player);changeString(mora2, computer);if (mora1-mora2 == 1 || mora1-mora2 == -1) {System.out.println("恭喜您取得一局胜利加3积分");player.setIntegral(player.getIntegral()+3);} else if (mora1-mora2 == -1 || mora1-mora2 == 2) {System.out.println("您惜败" + computer.getRoleName() + "加3积分");computer.setIntegral(computer.getIntegral()+3);} else {System.out.println("平手各加一积分");player.setIntegral(player.getIntegral()+1);computer.setIntegral(computer.getIntegral()+1);}}//游戏结束公示private void end() {System.out.println();System.out.println("公布此次游戏的最终结果:\n结果场次为:" + gameNumber);System.out.println(player.getRoleName() + "积分为:" + player.getIntegral());System.out.println(computer.getRoleName() + "积分为:" + computer.getIntegral());if (computer.getIntegral() < player.getIntegral()) {System.out.println("恭喜您此次游戏您胜利了!!!");} else if(computer.getIntegral() > player.getIntegral()) {System.out.println("很遗憾此次游戏您惜败!!!");} else {System.out.println("平分秋色!!!");}System.out.println("游戏结束");}//游戏过程public void wholeJourney() {start();String judge;do {arbitrament();System.out.println();System.out.print("是否继续战斗(y/n):");judge = input.next();gameNumber++;} while (judge.equals("y"));end();}//转化手势private void changeString(int mora, Object gamePlayer) {String name;if(gamePlayer instanceof Player) {Player player = (Player)gamePlayer;name = player.getRoleName();} else if (gamePlayer instanceof Computer) {Computer computer = (Computer)gamePlayer;name = computer.getRoleName();} else {name = "错误昵称";}switch (mora) {case 1:System.out.println(name + "出拳为:剪刀");break;case 2:System.out.println(name + "出拳为:石头");break;case 3:System.out.println(name + "出拳为:布");break;}}public int getGameNumber() {return gameNumber;}public void setGameNumber(int gameNumber) {this.gameNumber = gameNumber;}}
游戏测试类
package com.qianfeng.moraGame;import java.util.Scanner;public class GameTest {public static void main(String[] args) {Scanner input = new Scanner(System.in);Player player = new Player();Computer computer = new Computer();Referee referee = new Referee(player,computer);String judge;do {referee.wholeJourney();System.out.print("是否继续一局新的游戏(y/n):");judge = input.next();System.out.println();//新的一局初始积分与局数referee.setGameNumber(0);player.setIntegral(0);computer.setIntegral(0);} while (judge.equals("y"));System.out.println("欢迎下次再来");input.close();}
}
这篇关于猜拳游戏之面向对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!