Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 84 additions & 38 deletions DataStructures_n_Algo/RockPaperScissors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <players.length ; i++) {
var t = rdm.nextInt(3);
players[i] = t;
System.out.println("Player "+(char)(i+65)+" showed "+getType(t));
Player[] players = new Player[5];
for (int i = 0; i < players.length; i++) {
players[i] = new Player();
printPlayerWithChoice((char) (i + 65), players[i]);
}

System.out.println("----------I'm the judge--------");

printScores(getScores(players));
printScores(computeScores(players));
}

static String getType(int n){
String type =null;
switch (n){
case 0: type = "Stone";
break;
case 1: type = "Paper";
break;
case 2: type= "Scissors";
break;
private static void printScores(int[] computedScores) {
for (int i = 0; i < computedScores.length; i++) {
System.out.println("Player " + (char) (i + 65) + " got scores " + computedScores[i]);
}

return type;
}

static int[] getScores(int[] players) {

static int[] computeScores(Player[] players) {
int[] scores = new int[players.length];
for (int i = 0; i < players.length - 1; i++) {//pointing to current player
/*as for length minus 1, the last player don't need to compute his score
as it will be updated by previous players*/
for (int j = i+1; j < players.length; j++) {//pointing to his opponents
var diff = players[i] - players[j];
if (diff == 1 | diff == -2) {
scores[i]++;//current player wins
} else if (diff !=0 ) {//-1 or 2
scores[j]++;//his opponent wins
}//else neither wins
/*as for length minus 1, the last player don't need to compute his score
as it will be updated by previous players*/
for (int j = i + 1; j < players.length; j++) {//pointing to his opponents
final Choice choiceA = players[i].getChoice();
final Choice choiceB = players[j].getChoice();
switch (Choice.compare(choiceA, choiceB)) {
case 1:
scores[i]++;
break;
case -1:
scores[j]++;
break;
}
}
}
return scores;
}

static void printScores(int[] scores){
for (int i = 0; i <scores.length ; i++) {
System.out.println("Player "+(char)(i+65)+" got scores "+scores[i]);
}
static void printPlayerWithChoice(char label, Player player) {
System.out.println("Player " + label + " showed " + player.getChoice().getName());
}
}