From 3c8ba43e9c84de9dc032d2ff13876ddc80f62968 Mon Sep 17 00:00:00 2001 From: LittleboyHarry <13063376+LittleboyHarry@users.noreply.github.com> Date: Mon, 7 Sep 2020 22:00:51 +0800 Subject: [PATCH] Update RockPaperScissors.java --- DataStructures_n_Algo/RockPaperScissors.java | 122 +++++++++++++------ 1 file changed, 84 insertions(+), 38 deletions(-) diff --git a/DataStructures_n_Algo/RockPaperScissors.java b/DataStructures_n_Algo/RockPaperScissors.java index 7013290..72ae05b 100644 --- a/DataStructures_n_Algo/RockPaperScissors.java +++ b/DataStructures_n_Algo/RockPaperScissors.java @@ -2,64 +2,110 @@ import java.util.Random; -/* -Simulation of a Rock-paper-scissors game which is -analogous to some situations in counting and games. +enum Choice { + STONE("Stone"), + PAPER("Paper"), + SCISSORS("Scissors"); -The stone is beaten by paper which is beaten by scissors which are beaten by stones. + static final Random RANDOM = new Random(); + final String name; -During the game, each player counts his score of beating others without deducting -scores caused by beaten by others. + Choice(String name) { + this.name = name; + } + + /** + * 比较两者 + * + * @return 当 A 打赢了 B 返回 1,输了返回 2,否则为 0. + */ + static int compare(Choice choiceA, Choice choiceB) { + int flag = 0; + switch (choiceA) { + case PAPER: + if (choiceB == STONE) flag = 1; + if (choiceB == SCISSORS) flag = -1; + break; + case SCISSORS: + if (choiceB == PAPER) flag = 1; + if (choiceB == STONE) flag = -1; + break; + case STONE: + if (choiceB == SCISSORS) flag = 1; + if (choiceB == PAPER) flag = -1; + break; + } + return flag; + } + + public String getName() { + return name; + } + + + static Choice getRandom() { + return Choice.values()[RANDOM.nextInt(Choice.values().length)]; + } +} + +class Player { + + final Choice choice = Choice.getRandom(); + + public Choice getChoice() { + return choice; + } +} + +/** + * Simulation of a Rock-paper-scissors game which is + * analogous to some situations in counting and games. + * The stone is beaten by paper which is beaten by scissors which are beaten by stones. + * During the game, each player counts his score of beating others without deducting + * scores caused by beaten by others. */ public class RockPaperScissors { public static void main(String[] args) { - int[] players = new int[5]; - Random rdm = new Random(); - for (int i = 0; i