MCPcopy Create free account
hub / github.com/aws-samples/eb-java-scorekeep / move

Method move

src/main/java/scorekeep/TicTacToe.java:41–71  ·  view source on GitHub ↗
(String oldState, String moveText)

Source from the content-addressed store, hash-verified

39 * it against known win states to check for victory.
40 */
41 public static String move(String oldState, String moveText) {
42 // current state in char[]
43 char[] oldchar = oldState.toCharArray();
44 // move in char[]
45 char[] movchar = moveText.toCharArray();
46 // validate move and update state
47 if ( movchar[0] == oldchar[0] ) {
48 oldchar[Character.getNumericValue(movchar[1])] = movchar[0];
49 if ( movchar[0] == 'X' ) {
50 oldchar[0] = 'O';
51 } else if ( movchar[0] == 'O') {
52 oldchar[0] = 'X';
53 }
54 } else {
55 logger.error("Not your turn");
56 }
57 // convert state to integer
58 int stateInt = toInt(oldchar, movchar[0]);
59 logger.info("state int: " + stateInt);
60 // check for victory
61 boolean win = checkWin(stateInt);
62 if ( win ) {
63 if ( movchar[0] == 'X') {
64 oldchar[0] = 'A';
65 } else {
66 oldchar[0] = 'B';
67 }
68 }
69 String newState = new String(oldchar);
70 return newState;
71 }
72
73 /* Convert a string game state to an integer by treating it as a binary
74 * number where spaces occupied by the player are '1' and all other spaces

Callers 1

gameplayTestMethod · 0.95

Calls 2

toIntMethod · 0.95
checkWinMethod · 0.95

Tested by 1

gameplayTestMethod · 0.76