Skip to content

Instantly share code, notes, and snippets.

@jlink
Created September 29, 2022 12:55
Show Gist options
  • Save jlink/74f013418ee03424ee5367add17df3d5 to your computer and use it in GitHub Desktop.
Save jlink/74f013418ee03424ee5367add17df3d5 to your computer and use it in GitHub Desktop.
Hacked implementation for running to RPS bots agains each other.
package rps;
import java.util.*;
import java.util.concurrent.*;
import static rps.RockPaperScissors.Move.*;
public class RockPaperScissors {
enum Move {
ROCK, PAPER, SCISSORS, TOO_SLOW
}
enum Player {
A, B
}
static ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
public static void main(String[] args) throws InterruptedException {
int numberOfWins = 10000;
int winsTeamA = 0;
int winsTeamB = 0;
while (winsTeamA < numberOfWins && winsTeamB < numberOfWins) {
System.out.println("Ready...");
System.out.println("Steady...");
System.out.println("Go!");
Map<Player, Move> moves = play();
Move moveA = moves.get(Player.A);
teamAMoves.add(moveA);
Move moveB = moves.get(Player.B);
teamBMoves.add(moveB);
System.out.println("Team A: " + moveA);
System.out.println("Team B: " + moveB);
if (moveA == moveB) {
System.out.println("Draw");
} else if (moveB == TOO_SLOW) {
System.out.println("Team A wins");
winsTeamA++;
} else if (moveA == ROCK && moveB == SCISSORS) {
System.out.println("Team A wins");
winsTeamA++;
} else if (moveA == PAPER && moveB == ROCK) {
System.out.println("Team A wins");
winsTeamA++;
} else if (moveA == SCISSORS && moveB == PAPER) {
System.out.println("Team A wins");
winsTeamA++;
} else {
System.out.println("Team B wins");
winsTeamB++;
}
System.out.printf("Standings = %s : %s %n%n", winsTeamA, winsTeamB);
}
if (winsTeamA == numberOfWins) {
System.out.println("*** TEAM A WINS THE MATCH! ***");
} else {
System.out.println("*** TEAM B WINS THE MATCH! ***");
}
System.out.println("Team A moves: " + teamAMoves);
System.out.println("Team B moves: " + teamBMoves);
executor.shutdown();
}
private static Map<Player, Move> play() {
ScheduledFuture<Move> moveA = executor.schedule(RockPaperScissors::teamANext, 0, TimeUnit.SECONDS);
Move moveTeamA;
try {
moveTeamA = moveA.get(1000, TimeUnit.MILLISECONDS);
if (moveTeamA == null) {
moveTeamA = TOO_SLOW;
}
} catch (Exception e) {
moveTeamA = TOO_SLOW;
}
ScheduledFuture<Move> moveB = executor.schedule(RockPaperScissors::teamBNext, 0, TimeUnit.SECONDS);
Move moveTeamB;
try {
moveTeamB = moveB.get(1000, TimeUnit.MILLISECONDS);
if (moveTeamB == null) {
moveTeamB = TOO_SLOW;
}
} catch (Exception e) {
moveTeamB = TOO_SLOW;
}
return Map.of(Player.A, moveTeamA, Player.B, moveTeamB);
}
static List<Move> teamAMoves = new ArrayList<>();
static List<Move> teamBMoves = new ArrayList<>();
// Return one of ROCK, PAPER, SCISSORS within 1000 ms, otherwise you lose
static Move teamANext() {
Random random = new Random();
List<Move> randomsMoves = new ArrayList<>(List.of(ROCK, PAPER, SCISSORS));
for (Move teamBMove : teamBMoves) {
if (teamBMove == ROCK)
randomsMoves.add(PAPER);
if (teamBMove == PAPER)
randomsMoves.add(SCISSORS);
if (teamBMove == SCISSORS)
randomsMoves.add(ROCK);
}
List<Move> moves = new ArrayList<>(randomsMoves);
Collections.shuffle(moves, random);
return moves.get(0);
}
// Return one of ROCK, PAPER, SCISSORS within 1000 ms, otherwise you lose
static Move teamBNext() {
if (teamAMoves.isEmpty()) {
return PAPER;
}
return teamAMoves.get(teamAMoves.size() - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment