Commits

Arkadi Shishlov committed 684aedf Draft

TicTacToe

  • Participants

Comments (0)

Files changed (8)

session1/src/TicTacToe.groovy

+class TicTacToe {
+
+    def N = 3
+    def X = 'X'
+    def Y = 'Y'
+    def battlefield = [:]
+    def lastMark = 'Y'
+
+    def winner(who) {
+        false
+    }
+
+    def gameEnded() {
+        winner(X) || winner(Y) || draw() || false
+    }
+
+
+    private def badCoords(List coords) {
+        coords.any { coord ->
+            coord <= 0 || coord > N
+        }
+    }
+
+    def place(x, y) {
+        def coords = [x, y]
+        if (battlefield.containsKey(coords) || badCoords(coords))
+            return false
+        battlefield[coords] = lastMark = lastMark == X ? Y : X
+        true
+    }
+}

session1/src/TicTacToeTest.groovy

+import org.junit.Test
+
+@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)
+class TicTacToeTest extends GroovyTestCase {
+
+    TicTacToe t
+
+    @org.junit.Before
+    void init() {
+        t = new TicTacToe()
+    }
+
+    @Test
+    void testGameNotEnded() {
+        assert t.gameEnded() == false
+    }
+
+    @Test
+    void testDraw() {
+        t.N.times { x ->
+            t.N.times { y ->
+                t.place(x, y)
+            }
+        }
+        assert t.gameEnded() == "draw"
+    }
+
+    @Test
+    void testPlaceMark() {
+        t.place(2, 1)
+        assert t.battlefield == [[2, 1]: t.X]
+    }
+
+    @Test
+    void testPlaceInvalidMark() {
+        assert t.place(2, 1)
+        assert !t.place(2, 1)
+        assert !t.place(20, 1)
+        assert t.place(1, 2)
+    }
+
+}

session2/src/EngineTicTacToe.groovy

+class EngineTicTacToe {
+
+    int N = 3
+    int[][] field = new int[N][N]
+    int X = 1
+    int O = -1
+    int player = 1
+
+    boolean isGameOver() {
+        for (int i = 0; i < 3; i++) {
+            for (int j = 0; j < 3; j++) {
+                if (field[i][j] == 0) {
+                    return false
+                }
+            }
+        }
+        return true
+    }
+
+    void setPlayer(player) {
+        if (player == X) {
+            this.player = O
+        } else {
+            this.player = X
+        }
+    }
+
+    boolean turn(x, y) {
+        --y
+        --x
+        if (field[x][y] == 0) {
+            field[x][y] = player
+            setPlayer()
+            return true
+        } else {
+            return false
+        }
+
+    }
+
+    char winner() {
+
+    }
+}

session2/src/TestTicTacToe.groovy

+class TestTicTacToe extends GroovyTestCase {
+
+    void testIsGameOver() {
+        def t = new EngineTicTacToe()
+        assert !t.isGameOver()
+    }
+
+    void testWinner() {
+        def t = new EngineTicTacToe()
+        t.turn(1, 1) // X
+        t.turn(1, 2) // O
+        t.turn(2, 1) // X
+        t.turn(1, 3) // O
+        t.turn(3, 1) // X
+        assert t.isGameOver()
+        assert t.winner() == 'X'
+    }
+
+    void testTurnValid() {
+        def t = new EngineTicTacToe()
+        assert t.turn(1, 1)
+        assert !t.turn(1, 1)
+    }
+
+}

session3/src/Test.groovy

+class Test extends GroovyTestCase {
+    def a = 'a'
+    def b = 'b'
+    def c = 'c'
+
+    void testIsEmptyBoardEmpty() {
+        def board = [:]
+        assert board.size() == 0
+    }
+
+    void testIsFullBoardFull() {
+        def board = [:]
+        board[['a', 'a']] = 'X'
+        board[['a', 'b']] = 'O'
+        board[['a', 'c']] = 'X'
+        board[['b', 'a']] = 'O'
+        board[['b', 'b']] = 'X'
+        board[['b', 'c']] = 'O'
+        board[['c', 'a']] = 'X'
+        board[['c', 'b']] = 'O'
+        board[['c', 'c']] = 'X'
+        assert board.size() == 9
+    }
+
+    void testWinner() {
+        def board = [:]
+        board[['a', 'a']] = 'X'
+        board[['a', 'b']] = 'X'
+        board[['a', 'c']] = 'X'
+        board[['c', 'b']] = 'O'
+        assert 'X' == winner(board)
+
+        def board2 = [:]
+        board2[['a', 'a']] = 'X'
+        board2[['b', 'a']] = 'X'
+        board2[['c', 'a']] = 'X'
+        board2[['c', 'b']] = 'O'
+        assert 'X' == winner(board2)
+
+        def board3 = [:]
+        board3[['a', 'a']] = 'X'
+        board3[['b', 'b']] = 'X'
+        board3[['c', 'c']] = 'X'
+        board3[['c', 'b']] = 'O'
+        assert 'X' == winner(board3)
+
+        def board4 = [:]
+        board4[['c', 'a']] = 'X'
+        board4[['b', 'b']] = 'X'
+        board4[['a', 'c']] = 'X'
+        board4[['c', 'b']] = 'O'
+        assert 'X' == winner(board4)
+    }
+
+    def winner(Map board) {
+        winner(board, 'X') || winner(board, 'O') || board.size() == 9 ? "Draw" : false
+    }
+
+    def winner(Map board, String who) {
+        def cells = [a, b, c]
+        cells.any { x ->
+            cells.every { y ->
+                board[[x, y]] == who || board[[y, x]] == who
+            }
+        } ||
+        cells.every { c ->
+            board[[c, c]] == who
+        } ||
+        [[c, a], [b, b], [a, c]].every { coords ->
+            board[coords] == who
+        }
+    }
+}

session4/src/TicTacToe.groovy

+class TicTacToe extends GroovyTestCase {
+    def realBoard = new TicTacToeBoard()
+    def who = 'X'
+    def n = 3
+
+    def board() {
+        realBoard.board
+    }
+
+    def turn(x, y, z) {
+        if (!realBoard.turn([x, y, z], who))
+            return false
+        who = who == 'X' ? 'O' : 'X'
+        true
+    }
+
+    def winner() {
+        winner('X') ? 'X' : winner('O') ? 'O' : draw() ?: false
+
+    }
+
+    def winner(who) {
+        def n = 3
+        (0..<n).any { x ->
+            winner(n, who, x)
+        }
+    }
+
+    def winner(n, who, x) {
+        (0..<n).any { y ->
+            winner(n, who, x, y) || winner(n, who, y, x)
+        }
+    }
+
+    def winner(n, who, x, y) {
+        (0..<n).any { z ->
+            board()[[x, y, z]] == who || board()[[x, z, y]] == who
+        }
+    }
+
+    def draw() {
+        board().size() == n * n * n
+    }
+}

session4/src/TicTacToeBoard.groovy

+class TicTacToeBoard {
+    def board = [:]
+
+    def turn(coords, who) {
+        if (board.containsKey(coords))
+            return false
+        board[coords] = who
+        true
+    }
+}

session4/src/TicTacToeTest.groovy

+class TicTacToeTest extends GroovyTestCase {
+
+    void testSimpleTurn() {
+        def t = new TicTacToe()
+        assert t.turn(1, 1, 1)
+        assert t.board() == [[1, 1, 1]: 'X']
+        assert !t.turn(1, 1, 1)
+        assert t.board() == [[1, 1, 1]: 'X']
+    }
+
+    void testWinner() {
+        def t = new TicTacToe()
+        t.turn(1, 1, 1)
+        t.turn(2, 1, 1)
+        t.turn(1, 2, 1)
+        t.turn(2, 2, 1)
+        t.turn(1, 3, 1)
+        assert 'X' == t.winner()
+    }
+}