import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; public class Guriko { public static void main(String[] args) { //プレイヤー生成 Map<String, Player> playersMap = new LinkedHashMap<String, Player>(); for (int i = 0; i< Constant.PLAYER_COUNT; i++) { Player p = new Player(); p.setName("プレイヤー" + i); playersMap.put(p.getName(), p); } //発番済みランキング int ranking = 0; String displayRanking = null; while (isRestPlayer(playersMap)) { for (Iterator<Entry<String, Player>> it = playersMap.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Player> entry = it.next(); String name = entry.getKey(); Player p = entry.getValue(); //ジャンケンの選択 p.setHand(Utils.getJanken()); } //判定(勝者選出) Map<String, Player> winners = Utils.jankenJudge(playersMap); int winHand = Constant.AIKO_ID; //歩を進める、順位付ける if (winners != null) { boolean rankAlready = false; for (Iterator<Entry<String, Player>> it = winners.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Player> entry = it.next(); String name = entry.getKey(); //勝ち手を控えておく winHand = playersMap.get(name).getHand(); if (!playersMap.get(name).isGoalFlg()) { int stepCount = playersMap.get(name).getStepCount() + Constant.JANKEN_MOVE_STEPS.get(winHand); playersMap.get(name).setStepCount(stepCount); //ゴール判定 if (Constant.MAX_STEPS <= playersMap.get(name).getStepCount()) { if (!rankAlready) { rankAlready = true; ranking++; } playersMap.get(name).setGoalFlg(true); playersMap.get(name).setRanking(ranking); } } } } //結果出力 StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); StringBuilder sb3 = new StringBuilder(); for (Iterator<Entry<String, Player>> it = playersMap.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Player> entry = it.next(); Player p = entry.getValue(); sb1 = sb1.append("|" + p.getName() + ":" + Constant.JANKEN_TITLE.get(p.getHand())); sb2 = sb2.append("|" + p.getName() + ":" + p.getStepCount()); sb3 = sb3.append("|" + p.getName() + ":" + p.getRanking() + "位"); } displayRanking = sb3.toString().replaceFirst("|", ""); System.out.println("///////////"); System.out.println("じゃんけん開始"); System.out.println(sb1.toString().replaceFirst("|", "")); System.out.println("じゃんけんの勝ち手:" + Constant.JANKEN_TITLE.get(winHand)); System.out.println(sb2.toString().replaceFirst("|", "")); System.out.println(""); } System.out.println("///////////"); System.out.println("じゃんけん結果"); System.out.println(displayRanking); } /** * 残プレイヤーを判定する * * @param プレイヤーマップ * @return true:残プレイヤーがいる、false:全員ゴール済み * * */ private static boolean isRestPlayer(Map<String, Player> playersMap) { for (Iterator<Entry<String, Player>> it = playersMap.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Player> entry = it.next(); Player p = entry.getValue(); if (!p.isGoalFlg()) { //まだゴールしていないプレイヤーがいる=処理続行のため真を返却 return true; } } //全員ゴールしているので処理終了 return false; } }
public class Player { /** プレイヤー名 */ private String name; /** じゃんけんの手 */ private int hand; /** 階数 */ private int stepCount; /** 順位 */ private int ranking; /** ゴール済み */ private boolean goalFlg; public Player() { this.hand = 0; this.stepCount = 0; this.ranking = 0; this.goalFlg = false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHand() { return hand; } public void setHand(int hand) { this.hand = hand; } public int getStepCount() { return stepCount; } public void setStepCount(int stepCount) { this.stepCount = stepCount; } public int getRanking() { return ranking; } public void setRanking(int ranking) { this.ranking = ranking; } public boolean isGoalFlg() { return goalFlg; } public void setGoalFlg(boolean goalFlg) { this.goalFlg = goalFlg; } }
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class Utils { /** * じゃんけんの手の生成 * * @return int * * */ public static int getJanken() { return (int) (Math.random() * 3) + 1; } /** * プレイヤーの勝者を判定する * * @param プレイヤーマップ * @return 勝者マップ。nullはあいこ。 * * */ public static HashMap<String, Player> jankenJudge(Map<String, Player> playersMap) { HashMap<String, Player> guuWinnerMap = new HashMap<String, Player>(); HashMap<String, Player> chokiWinnerMap = new HashMap<String, Player>(); HashMap<String, Player> parWinnerMap = new HashMap<String, Player>(); //ジャンケン毎にプレイヤー群を分ける for (Iterator<Entry<String, Player>> it = playersMap.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Player> entry = it.next(); String name = entry.getKey(); Player p = entry.getValue(); //振り分け switch (p.getHand()) { case Constant.GURIKO_ID: guuWinnerMap.put(name, p); break; case Constant.CHOKI_ID: chokiWinnerMap.put(name, p); break; case Constant.PAR_ID: parWinnerMap.put(name, p); break; } } //判定 if (guuWinnerMap.isEmpty()) { if (chokiWinnerMap.isEmpty()) { //全員パー return null; } else { if (parWinnerMap.isEmpty()) { //全員ちょき return null; } else { //ちょきとぱーなのでちょきの勝ち return chokiWinnerMap; } } } else if (chokiWinnerMap.isEmpty()) { if (parWinnerMap.isEmpty()) { //全員ぐー return null; } else { //ぱーとぐーなのでぱーの勝ち return parWinnerMap; } } else if (parWinnerMap.isEmpty()) { //ぐーとちょきなのでぐー勝ち return guuWinnerMap; } else { //ぐー、ちょき、ぱーなのであいこ return null; } } }
import java.util.HashMap; public class Constant { //参加人数 public static final int PLAYER_COUNT = 4; //階段数(頂上) public static final int MAX_STEPS = 25; public static final int AIKO_ID = 0; public static final int GURIKO_ID = 1; public static final int CHOKI_ID = 2; public static final int PAR_ID = 3; //じゃんけんの手の名称 public static final HashMap<Integer, String> JANKEN_TITLE = new HashMap<Integer, String>() { { put(AIKO_ID, "あいこ"); put(GURIKO_ID, "ぐー"); put(CHOKI_ID, "ちょき"); put(PAR_ID, "ぱー"); } }; //じゃんけんで勝った時の進める歩数 public static final HashMap<Integer, Integer> JANKEN_MOVE_STEPS = new HashMap<Integer, Integer>() { { put(AIKO_ID, 0); put(GURIKO_ID, 3); put(CHOKI_ID, 6); put(PAR_ID, 6); } }; }
最新コメント