MCPcopy Create free account
hub / github.com/careercup/ctci / Automator

Class Automator

java/Chapter 8/Question8_8/Automator.java:8–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6
7/* A helper class to automate this game. This is just used for testing purposes. */
8public class Automator {
9 private Player[] players;
10 private Player lastPlayer = null;
11 public ArrayList<Location> remainingMoves = new ArrayList<Location>();
12 private static Automator instance;
13
14 private Automator() {
15 for (int i = 0; i < 10; i++) {
16 for (int j = 0; j < 10; j++) {
17 Location loc = new Location(i, j);
18 remainingMoves.add(loc);
19 }
20 }
21 }
22
23 public static Automator getInstance() {
24 if (instance == null) {
25 instance = new Automator();
26 }
27 return instance;
28 }
29
30 public void initialize(Player[] ps) {
31 players = ps;
32 lastPlayer = players[1];
33 }
34
35 public void shuffle() {
36 for (int i = 0; i < remainingMoves.size(); i++) {
37 int t = AssortedMethods.randomIntInRange(i, remainingMoves.size() - 1);
38 Location other = remainingMoves.get(t);
39 Location current = remainingMoves.get(i);
40 remainingMoves.set(t, current);
41 remainingMoves.set(i, other);
42 }
43 }
44
45 public void removeLocation(int r, int c) {
46 for (int i = 0; i < remainingMoves.size(); i++) {
47 Location loc = remainingMoves.get(i);
48 if (loc.isSameAs(r, c)) {
49 remainingMoves.remove(i);
50 }
51 }
52 }
53
54 public Location getLocation(int index) {
55 return remainingMoves.get(index);
56 }
57
58 public boolean playRandom() {
59 Board board = Game.getInstance().getBoard();
60 shuffle();
61 lastPlayer = lastPlayer == players[0] ? players[1] : players[0];
62 String color = lastPlayer.getColor().toString();
63 for (int i = 0; i < remainingMoves.size(); i++) {
64 Location loc = remainingMoves.get(i);
65 boolean success = lastPlayer.playPiece(loc.getRow(), loc.getColumn());

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected