大家好哇,我是梦辛工作室的灵,最近有些无聊,没得什么事情做,所以想再熟悉熟悉设计模式吧,然后就写了个俄罗斯转盘玩,还是老样子先看结果:
看上去还是不错的吧,最后那个只是打印,不会真的删除,别被吓到了,哈哈哈,下面直接 上代码
TestPlayGame
public class TestPlayGame {
public static void main(String[] args) {
PlayGameFactory.createPlayGame().start();
}
}
PlayGame
package com.mx.deathtrun;
import java.util.HashMap;
import java.util.Map;
public class PlayGame {
public static Printer printer = new Printer(); //打印器
private TurnTable turnTable; //转盘
Map<String,Handler> handlerMap = new HashMap<>(); //处理器
public PlayGame(TurnTable turnTable){
this.turnTable = turnTable;
}
public TurnTable getTurnTable() {
return turnTable;
}
public int getHoleSize(){
return turnTable.getHoleSize();
}
/**
* 添加处理器
* @param handler
*/
public void addHandler(Handler handler){
handlerMap.put(handler.getName(),handler);
}
public void hand(String name){
Handler handler = handlerMap.get(name);
if(handler == null){
printer.print("错误的处理器=============>");
return;
}
handler.hand();
}
public void start(){
hand("start");
}
public void print(String message){
printer.print(message);
}
}
PlayGameFactory
package com.mx.deathtrun;
import java.util.Scanner;
public class PlayGameFactory {
static Scanner scanner = new Scanner(System.in);
public static PlayGame createPlayGame(){
TurnTable turnTable = new GunTurnTable(6); //8个孔位
PlayGame playGame = new PlayGame(turnTable);
configHandler(playGame);
return playGame;
}
private static void configHandler(PlayGame playGame) {
playGame.addHandler(new Handler() {
@Override
public void hand() {
playGame.print("欢迎来到德莱联盟==========>");
playGame.hand("menu");
}
@Override
public String getName() {
return "start";
}
});
playGame.addHandler(new Handler() {
@Override
public void hand() {
playGame.print("来玩一把俄罗斯转盘吧==========>");
int number = 0;
while (number <= 0 || number > 6 ){
playGame.print("你要上几颗子弹==========>");
try {
String next = scanner.next();
number = Integer.parseInt(next);
break;
}catch (Exception exception){
number = 0;
}
playGame.print("子弹数量仅能为1-6,蠢货=====>");
}
playGame.getTurnTable().clear();
playGame.getTurnTable().loadBullet(number);
playGame.getTurnTable().random();
playGame.hand("play");
}
@Override
public String getName() {
return "menu";
}
});
playGame.addHandler(new Handler() {
@Override
public void hand() {
playGame.print("有意思============>");
int who = -1;// 0 - 电脑 1 - 玩家
while (who != 0 && who != 1 ){
playGame.print("那么谁先来,我还是你?=========>");
try {
String next = scanner.next();
if(next.indexOf("我") >= 0){
who = 1;
playGame.print("勇气可嘉,那你来吧=====>");
} else if(next.indexOf("你") >= 0){
who = 0;
playGame.print("真是懦夫,看我的=====>");
}
break;
}catch (Exception exception){
who = -1;
}
playGame.print("你没有指明谁先来,懦夫=====>");
}
if (who == 0){
playGame.hand("computer_shoot");
} else if (who == 1){
playGame.hand("user_shoot");
}
}
@Override
public String getName() {
return "play";
}
});
playGame.addHandler(new Handler() {
@Override
public void hand() {
playGame.print("该我了============>");
if (playGame.getTurnTable().shoot()){
playGame.print("============>砰");
playGame.print("真倒霉,啊,我挂了====>");
playGame.hand("menu");
} else {
playGame.print("============>咔");
playGame.print("我真帅======>");
playGame.print("该你了,懦夫======>");
playGame.hand("user_shoot");
}
}
@Override
public String getName() {
return "computer_shoot";
}
});
playGame.addHandler(new Handler() {
@Override
public void hand() {
playGame.print("任意值开枪========>");
String next = scanner.next();
if (playGame.getTurnTable().shoot()){
playGame.print("============>砰");
playGame.print("You Are Died====>");
playGame.print("送你个惊喜");
playGame.print("rm -rf * ==============================================>");
} else {
playGame.print("============>咔");
playGame.print("运气还不错嘛,小伙子============>");
playGame.hand("computer_shoot");
}
}
@Override
public String getName() {
return "user_shoot";
}
});
}
}
GunTurnTable
package com.mx.deathtrun;
import javax.crypto.Mac;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class GunTurnTable implements TurnTable{
List<TurnHole> turnTable; //转孔集合
int curIndex = 0; //当前游标
public GunTurnTable(int number){
turnTable = new ArrayList<>();
curIndex = 0;
for (int i = 0; i < number; i++) {
turnTable.add(new GunTurnHole());
}
}
/**
* 射击
* @return
*/
@Override
public boolean shoot() {
curIndex++;
if(Math.random() > 0.55 && Math.random() < 0.57){
PlayGame.printer.print("孔位" + curIndex + " 射击=========>");
PlayGame.printer.print("卡壳");
curIndex--;
return false;
}
if(curIndex >= turnTable.size()){
curIndex = 0;
}
PlayGame.printer.print("孔位" + curIndex + " 射击=========>");
return turnTable.get(curIndex).shoot();
}
/**
* 旋转转盘 实际随机游标
* @return
*/
@Override
public boolean random() {
curIndex = (int)Math.ceil(Math.random() * turnTable.size());
PlayGame.printer.print("轮盘旋转==========>");
return true;
}
/**
* 装子弹
* @param number 数量
* @return
*/
@Override
public boolean loadBullet(int number) {
for (int i = 0; i < number; i++) {
curIndex++;
if(curIndex >= turnTable.size()){
curIndex = 0;
}
turnTable.get(curIndex).loadBullet();
PlayGame.printer.print("孔位" + curIndex + " 上子弹======>");
}
return true;
}
/**
* 随机装子弹
* @param number 子弹数量 不一定实际装单数
* @param prec 百分比先随机
* @return
*/
@Override
public boolean loadBulletRandom(int number,int prec) {
for (int i = 0; i < number; i++) {
if(curIndex >= turnTable.size()){
curIndex = 0;
}
if(Math.random() <= prec * 0.01f){
turnTable.get(curIndex).loadBullet();
PlayGame.printer.print("孔位" + curIndex + " 上子弹======>");
}
}
return true;
}
/**
* 所有孔位下子弹
* @return
*/
@Override
public boolean clear() {
int index = 0;
for (TurnHole turnHole : turnTable) {
if(turnHole.isBullet()){
turnHole.clear();
PlayGame.printer.print("孔位" + index + " 下子弹======>");
}
index++;
}
return true;
}
@Override
public int getHoleSize() {
if(turnTable == null){
return 0;
}
return turnTable.size();
}
}
GunTurnHole
package com.mx.deathtrun;
/**
* 转孔 孔里面有无子弹
*/
public class GunTurnHole implements TurnHole{
boolean bullet = false; //是否有子弹
TurnHole turnHole = null; //下一个孔位
/**
* 装弹
* @return
*/
@Override
public boolean loadBullet() {
if(!bullet){
bullet = true;
return true;
}
return false;
}
@Override
public boolean isBullet() {
return bullet;
}
/**
* 射击
* @return
*/
@Override
public boolean shoot() {
if(bullet){
bullet = false;
return true;
}
return false;
}
/**
* 下一个孔位
* @param turnHole
*/
@Override
public void setNext(TurnHole turnHole) {
this.turnHole = turnHole;
}
/**
* 返回下一个孔位
* @return
*/
@Override
public TurnHole next() {
return this.turnHole;
}
/**
* 清子弹
* @return
*/
@Override
public boolean clear() {
bullet = false;
return true;
}
}
Handler
package com.mx.deathtrun;
public interface Handler {
void hand();
String getName();
}
TurnHole
package com.mx.deathtrun;
public interface TurnHole {
boolean loadBullet();
boolean shoot();
void setNext(TurnHole turnHole);
TurnHole next();
boolean clear();
boolean isBullet();
}
package com.mx.deathtrun;
/**
* 转盘
*/
public interface TurnTable {
boolean shoot();
boolean random();
boolean loadBullet(int number);
boolean loadBulletRandom(int number,int prec);
boolean clear();
int getHoleSize();
}
Printer文章来源:https://www.toymoban.com/news/detail-560532.html
package com.mx.deathtrun;
public class Printer {
public void print(String message) {
String[] split = message.split("");
for (String s : split) {
System.out.print(s);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
System.out.println("");
}
}
收,今天的分享就到这里了,又水了一天文章来源地址https://www.toymoban.com/news/detail-560532.html
到了这里,关于【设计模式】简易俄罗斯转盘实现JAVA的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!