Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 15 files changed
  • 0 comments
  • 1 contributor
Dec 05, 2011
Kevin Liddle rename game state to rules 45acc09
Dec 07, 2011
Kevin Liddle add game, human and machine players, and illustrator. machine player …
…doesn't work yet
f2eaa2a
76 lib/board.ik
... ... @@ -1,58 +1,60 @@
1 1 Board = Origin mimic
2 2
3 3 Board initialize = method(dimension,
4   - self dimension = dimension
5   - self spaces = self initialize_spaces)
  4 + self dimension = dimension
  5 + self spaces = self initialize_spaces)
6 6
7 7 Board initialize_spaces = method(
8   - row = []
9   - board_spaces = []
10   - self dimension times(row << 0)
11   - self dimension times(board_spaces << deep_list_copy(row))
12   - board_spaces
13   - )
14   -
15   -Board deep_list_copy = method(template_list,
16   - target_list = []
17   - template_list each(elem,
18   - target_list << elem)
19   - target_list)
  8 + row = []
  9 + board_spaces = []
  10 + self dimension times(row << 0)
  11 + self dimension times(board_spaces << row mimic)
  12 + board_spaces
  13 + )
20 14
21 15 Board get_space = method(row, column,
22   - self spaces[row][column])
  16 + self spaces[row][column])
23 17
24 18 Board set_space = method(row, column, player_value,
25   - self spaces[row][column] = player_value
26   -)
  19 + self spaces[row][column] = player_value
  20 + )
27 21
28 22 Board blank? = method(
29   - blank = true
30   - dimension times(row,
31   - dimension times(column,
32   - unless(get_space(row, column) == 0,
33   - blank = false)))
34   - blank)
  23 + blank = true
  24 + dimension times(row,
  25 + dimension times(column,
  26 + unless(get_space(row, column) == 0,
  27 + blank = false)))
  28 + blank)
  29 +
  30 +Board full? = method(
  31 + full = true
  32 + dimension times(row,
  33 + dimension times(column,
  34 + if(get_space(row, column) == 0,
  35 + full = false)))
  36 + full)
35 37
36 38 Board reset! = method(
37   - self spaces = initialize_spaces)
  39 + self spaces = initialize_spaces)
38 40
39 41 Board get_row = method(row_index,
40   - self spaces[row_index])
  42 + self spaces[row_index])
41 43
42 44 Board get_column = method(column_index,
43   - column = []
44   - self dimension times(row_index,
45   - column << get_space(row_index, column_index))
46   - column)
  45 + column = []
  46 + self dimension times(row_index,
  47 + column << get_space(row_index, column_index))
  48 + column)
47 49
48 50 Board get_back_diagonal = method(
49   - diagonal = []
50   - self dimension times(index,
51   - diagonal << get_space(index, index))
52   - diagonal)
  51 + diagonal = []
  52 + self dimension times(index,
  53 + diagonal << get_space(index, index))
  54 + diagonal)
53 55
54 56 Board get_forward_diagonal = method(
55   - diagonal = []
56   - self dimension times(index,
57   - diagonal << get_space(self dimension - 1 - index, index))
58   - diagonal)
  57 + diagonal = []
  58 + self dimension times(index,
  59 + diagonal << get_space(self dimension - 1 - index, index))
  60 + diagonal)
74 lib/game.ik
... ... @@ -0,0 +1,74 @@
  1 +use("lib/board")
  2 +use("lib/illustrator")
  3 +use("lib/rules")
  4 +use("lib/human_player")
  5 +use("lib/machine_player")
  6 +
  7 +Game = Origin mimic
  8 +
  9 +Game initialize = method(game_type,
  10 + self board = Board mimic(3)
  11 + self player1 = nil
  12 + self player2 = nil
  13 + cond(
  14 + game_type == 1, human_vs_human,
  15 + game_type == 2, machine_vs_human,
  16 + game_type == 3, human_vs_machine,
  17 + human_vs_human
  18 + )
  19 + )
  20 +
  21 +Game human_vs_human = method(
  22 + self player1 = HumanPlayer mimic(1)
  23 + self player2 = HumanPlayer mimic(-1)
  24 + )
  25 +
  26 +Game machine_vs_human = method(
  27 + self player1 = MachinePlayer mimic(1)
  28 + self player2 = HumanPlayer mimic(-1)
  29 + )
  30 +
  31 +Game human_vs_machine = method(
  32 + self player1 = HumanPlayer mimic(1)
  33 + self player2 = MachinePlayer mimic(-1)
  34 + )
  35 +
  36 +Game play = method(
  37 + until(Rules game_over?(board),
  38 + take_turn
  39 + )
  40 + display_board
  41 + game_over_message println
  42 + )
  43 +
  44 +Game take_turn = method(
  45 + display_board
  46 + move = player_by_turn get_move(board)
  47 + make_move(move[0], move[1])
  48 + )
  49 +
  50 +Game player_by_turn = method(
  51 + total_moves = 0
  52 + self board dimension times(row,
  53 + self board dimension times(column,
  54 + if(self board get_space(row, column) != 0,
  55 + total_moves++)))
  56 + if(total_moves % 2 == 0,
  57 + player1,
  58 + player2)
  59 + )
  60 +
  61 +Game make_move = method(row, column,
  62 + if(self board get_space(row, column) == 0,
  63 + self board set_space(row, column, player_by_turn marker_value)))
  64 +
  65 +Game display_board = method(
  66 + Illustrator draw(self board) print)
  67 +
  68 +Game game_over_message = method(
  69 + cond(
  70 + Rules winner(self board) == 1, "Player 1 wins!",
  71 + Rules winner(self board) == -1, "Player 2 wins!",
  72 + Rules winner(self board) == 0, "Cat's Game..."
  73 + )
  74 + )
29 lib/game_state.ik
... ... @@ -1,29 +0,0 @@
1   -GameState = Origin mimic
2   -
3   -GameState horizontal_winner? = method(board,
4   - winning_row? = false
5   - board dimension times(row_index,
6   - row = board get_row(row_index)
7   - if(winning_group?(board dimension, row),
8   - winning_row? = true))
9   - winning_row?)
10   -
11   -GameState vertical_winner? = method(board,
12   - winning_column? = false
13   - board dimension times(column_index,
14   - column = board get_column(column_index)
15   - if(winning_group?(board dimension, column),
16   - winning_column? = true))
17   - winning_column?)
18   -
19   -GameState diagonal_winner? = method(board,
20   - back_diagonal_winner?(board) or forward_diagonal_winner?(board))
21   -
22   -GameState back_diagonal_winner? = method(board,
23   - winning_group?(board dimension, board get_back_diagonal))
24   -
25   -GameState forward_diagonal_winner? = method(board,
26   - winning_group?(board dimension, board get_forward_diagonal))
27   -
28   -GameState winning_group? = method(board_dimension, group,
29   - group select(n, n == group first and n != 0) length == board_dimension)
27 lib/human_player.ik
... ... @@ -0,0 +1,27 @@
  1 +HumanPlayer = Origin mimic
  2 +
  3 +HumanPlayer initialize = method(marker_value,
  4 + self marker_value = marker_value)
  5 +
  6 +HumanPlayer get_move = method(board,
  7 + move = []
  8 + bind(
  9 + handle(Condition Error Arithmetic NotParseable, fn(c, "Please enter a real number...jerk." println))
  10 + )
  11 + ["row", "column"] each(position,
  12 + input = -1
  13 + until(valid_input?(input, board),
  14 + prompt_for_move(position, board)
  15 + input = System in read asText toRational - 1
  16 + move << input
  17 + )
  18 + )
  19 + move
  20 + )
  21 +
  22 +HumanPlayer prompt_for_move = method(position, board,
  23 + "Enter a #{position} (1-#{board dimension}): " print
  24 + )
  25 +
  26 +HumanPlayer valid_input? = method(input, board,
  27 + input >= 0 and input < board dimension)
16 lib/illustrator.ik
... ... @@ -0,0 +1,16 @@
  1 +Illustrator = Origin mimic
  2 +
  3 +Illustrator draw = method(board,
  4 + illustration = ""
  5 + board spaces each(row,
  6 + row each(space,
  7 + illustration += "[" + space_marker(space) + "]")
  8 + illustration += "\n")
  9 + illustration)
  10 +
  11 +Illustrator space_marker = method(space_value,
  12 + cond(
  13 + space_value == 1, "X",
  14 + space_value == -1, "O",
  15 + space_value == 0, " ",
  16 + "?"))
56 lib/machine_player.ik
... ... @@ -0,0 +1,56 @@
  1 +use("lib/board")
  2 +
  3 +MachinePlayer = Origin mimic
  4 +
  5 +MachinePlayer initialize = method(marker_value,
  6 + self marker_value = marker_value
  7 + )
  8 +
  9 +MachinePlayer get_move = method(board,
  10 + if(board get_space(middle_of_board(board), middle_of_board(board)) == 0,
  11 + [middle_of_board(board), middle_of_board(board)],
  12 + minimax(board, marker_value, 1) keys first
  13 + )
  14 + )
  15 +; DOES NOT WORK YET. need to compare weights based on player value
  16 +MachinePlayer minimax = method(board, turn, depth,
  17 + best_move = {}
  18 + board dimension times(row,
  19 + board dimension times(column,
  20 + if(board get_space(row, column) == 0,
  21 + board set_space(row, column, turn)
  22 + if(Rules game_over?(board),
  23 + if(best_move == {} or better_than_best?(inverse(Rules winner(board) * (depth + 1)), best_move[best_move keys first]),
  24 + best_move = { [row, column] => inverse( Rules winner(board) * (depth + 1) ) }
  25 + )
  26 + ,
  27 + next_level_best_move = minimax(board, turn negation, depth + 1)
  28 + next_move = { [row, column] => next_level_best_move[next_level_best_move keys first] }
  29 + if( best_move == {} or better_than_best?(next_move[next_move keys first], best_move[best_move keys first]),
  30 + best_move = next_move
  31 + )
  32 + )
  33 + board set_space(row, column, 0)
  34 + )
  35 + )
  36 + )
  37 + best_move
  38 + )
  39 +
  40 +MachinePlayer better_than_best? = method(score, best_move_score,
  41 + if(marker_value > 0,
  42 + score > best_move_score,
  43 + score < best_move_score
  44 + )
  45 + )
  46 +
  47 +MachinePlayer middle_of_board = method(board,
  48 + (board dimension - (board dimension % 2)) / 2 ; Ioke doesn't have Integer divsion or rounding. performing (3/2) will yield a "Ratio" of 3/2, which you can't do much with
  49 + )
  50 +
  51 +MachinePlayer inverse = method(value,
  52 + if(value != 0,
  53 + 1 / value,
  54 + 0
  55 + )
  56 + )
3  lib/play_game.ik
... ... @@ -0,0 +1,3 @@
  1 +use("lib/game")
  2 +
  3 +Game mimic(3) play
45 lib/rules.ik
... ... @@ -0,0 +1,45 @@
  1 +Rules = Origin mimic
  2 +
  3 +Rules winner_of_group = method(board_dimension, group,
  4 + common_group = group select(n, n == group first and n != 0)
  5 + if(common_group length == board_dimension,
  6 + common_group first,
  7 + 0
  8 + ))
  9 +
  10 +Rules horizontal_winner = method(board,
  11 + winning_marker = 0
  12 + board dimension times(row_index,
  13 + row = board get_row(row_index)
  14 + if(winner_of_group(board dimension, row) != 0,
  15 + winning_marker = winner_of_group(board dimension, row)))
  16 + winning_marker)
  17 +
  18 +Rules vertical_winner = method(board,
  19 + winning_marker = 0
  20 + board dimension times(column_index,
  21 + column = board get_column(column_index)
  22 + if(winner_of_group(board dimension, column) != 0,
  23 + winning_marker = winner_of_group(board dimension, column)))
  24 + winning_marker)
  25 +
  26 +Rules diagonal_winner = method(board,
  27 + back_diagonal_winner(board) | forward_diagonal_winner(board))
  28 +
  29 +Rules back_diagonal_winner = method(board,
  30 + winner_of_group(board dimension, board get_back_diagonal))
  31 +
  32 +Rules forward_diagonal_winner = method(board,
  33 + winner_of_group(board dimension, board get_forward_diagonal))
  34 +
  35 +Rules winner = method(board,
  36 + horizontal_winner(board) | vertical_winner(board) | diagonal_winner(board))
  37 +
  38 +Rules winner? = method(board,
  39 + winner(board) != 0)
  40 +
  41 +Rules cats_game? = method(board,
  42 + board full? and winner?(board) not)
  43 +
  44 +Rules game_over? = method(board,
  45 + cats_game?(board) or winner?(board))
110 spec/board_spec.ik
@@ -3,62 +3,70 @@ use("lib/board")
3 3
4 4 describe("Board",
5 5
6   - it("can be initialized",
7   - board = Board mimic(3)
8   - board dimension should == 3
9   - )
  6 + before(board = Board mimic(3))
10 7
11   - it("initializes board with values of 0",
12   - board = Board mimic(3)
13   - 3 times(row,
14   - 3 times(column,
15   - board get_space(row, column) should == 0))
16   - )
  8 + it("can be initialized",
  9 + board dimension should == 3
  10 + )
17 11
18   - it("can set board spaces",
19   - board = Board mimic(3)
20   - board get_space(0,0) should == 0
21   - board set_space(0,0,1)
22   - board get_space(0,0) should == 1
23   - board get_space(1,0) should == 0
24   - )
  12 + it("initializes board with values of 0",
  13 + 3 times(row,
  14 + 3 times(column,
  15 + board get_space(row, column) should == 0))
  16 + )
25 17
26   - it("resets the board",
27   - board = Board mimic(3)
28   - board blank? should be true
29   - board set_space(0,0,1)
30   - board blank? should be false
31   - board reset!
32   - board blank? should be true
33   - )
  18 + it("can set board spaces",
  19 + board get_space(0,0) should == 0
  20 + board set_space(0,0,1)
  21 + board get_space(0,0) should == 1
  22 + board get_space(1,0) should == 0
  23 + )
34 24
35   - it("grabs a given row",
36   - board = Board mimic(3)
37   - board set_space(0,0,1)
38   - board set_space(0,1,-1)
39   - board get_row(0) should == [1, -1, 0]
40   - )
  25 + it("resets the board",
  26 + board blank? should be true
  27 + board set_space(0,0,1)
  28 + board blank? should be false
  29 + board reset!
  30 + board blank? should be true
  31 + )
41 32
42   - it("grabs a given column",
43   - board = Board mimic(3)
44   - board set_space(0,0,1)
45   - board set_space(1,0,-1)
46   - board get_column(0) should == [1, -1, 0]
47   - )
  33 + it("grabs a given row",
  34 + board set_space(0,0,1)
  35 + board set_space(0,1,-1)
  36 + board get_row(0) should == [1, -1, 0]
  37 + )
48 38
49   - it("grabs a back diagonal",
50   - board = Board mimic(3)
51   - board set_space(0,0,1)
52   - board set_space(1,1,-1)
53   - board get_back_diagonal should == [1, -1, 0]
54   - )
  39 + it("grabs a given column",
  40 + board set_space(0,0,1)
  41 + board set_space(1,0,-1)
  42 + board get_column(0) should == [1, -1, 0]
  43 + )
  44 +
  45 + it("grabs a back diagonal",
  46 + board set_space(0,0,1)
  47 + board set_space(1,1,-1)
  48 + board get_back_diagonal should == [1, -1, 0]
  49 + )
  50 +
  51 + it("grabs a forward diagonal",
  52 + board set_space(0,2,1)
  53 + board set_space(1,1,-1)
  54 + board set_space(2,0,-1)
  55 + board get_back_diagonal should == [0, -1, 0]
  56 + board get_forward_diagonal should == [-1, -1, 1]
  57 + )
55 58
56   - it("grabs a forward diagonal",
57   - board = Board mimic(3)
58   - board set_space(0,2,1)
59   - board set_space(1,1,-1)
60   - board set_space(2,0,-1)
61   - board get_back_diagonal should == [0, -1, 0]
62   - board get_forward_diagonal should == [-1, -1, 1]
  59 + it("detects a full board",
  60 + board full? should be false
  61 + board set_space(0,0,1)
  62 + board set_space(0,1,-1)
  63 + board set_space(0,2,-1)
  64 + board set_space(1,0,1)
  65 + board set_space(1,1,-1)
  66 + board set_space(1,2,-1)
  67 + board set_space(2,0,1)
  68 + board set_space(2,1,-1)
  69 + board set_space(2,2,-1)
  70 + board full? should be true
  71 + )
63 72 )
64   -)
76 spec/game_spec.ik
... ... @@ -0,0 +1,76 @@
  1 +use("ispec")
  2 +use("lib/game")
  3 +
  4 +describe("Game",
  5 +
  6 + before(game = Game mimic(1))
  7 +
  8 + it("initializes a game with a board",
  9 + game board should not be nil
  10 + )
  11 +
  12 + it("initializes a human vs. human game with game type 1",
  13 + game player1 kind should == "HumanPlayer"
  14 + )
  15 +
  16 + it("draws the board when game is started",
  17 + Game should receive display_board
  18 + game player1 should receive get_move(game board) andReturn([2,1])
  19 + game take_turn
  20 + )
  21 +
  22 + it("knows whose turn it is",
  23 + game player_by_turn should == game player1
  24 + game board set_space(0,0,1)
  25 + game player_by_turn should == game player2
  26 + )
  27 +
  28 + it("makes a move",
  29 + game make_move(2, 1)
  30 + game board get_space(2, 1) should == 1
  31 + )
  32 +
  33 + it("creates a game over message for player 1 win",
  34 + game board set_space(0,0,1)
  35 + game board set_space(0,1,1)
  36 + game board set_space(0,2,1)
  37 + game game_over_message should == "Player 1 wins!"
  38 + )
  39 +
  40 + it("creates a game over message for player 2 win",
  41 + game board set_space(0,0,-1)
  42 + game board set_space(0,1,-1)
  43 + game board set_space(0,2,-1)
  44 + game game_over_message should == "Player 2 wins!"
  45 + )
  46 +
  47 + it("creates a game over message for a Cat's game",
  48 + game board set_space(0,0,1)
  49 + game board set_space(0,1,1)
  50 + game board set_space(0,2,-1)
  51 + game board set_space(1,0,-1)
  52 + game board set_space(1,1,-1)
  53 + game board set_space(1,2,1)
  54 + game board set_space(2,0,1)
  55 + game board set_space(2,1,-1)
  56 + game board set_space(2,2,1)
  57 + game game_over_message should == "Cat's Game..."
  58 + )
  59 +
  60 + it("creates 2 human players for game type 1",
  61 + game player1 kind should == "HumanPlayer"
  62 + game player2 kind should == "HumanPlayer"
  63 + )
  64 +
  65 + it("creates 1 machine player and 1 human player for game type 2",
  66 + game1 = Game mimic(2)
  67 + game1 player1 kind should == "MachinePlayer"
  68 + game1 player2 kind should == "HumanPlayer"
  69 + )
  70 +
  71 + it("creates 1 human player and 1 machine player for game type 3",
  72 + game2 = Game mimic(3)
  73 + game2 player1 kind should == "HumanPlayer"
  74 + game2 player2 kind should == "MachinePlayer"
  75 + )
  76 + )
48 spec/game_state_spec.ik
... ... @@ -1,48 +0,0 @@
1   -use("ispec")
2   -use("lib/game_state")
3   -use("lib/board")
4   -
5   -describe("GameState",
6   -
7   - it("detects a horizontal win",
8   - board = Board mimic(3)
9   - board dimension times(column,
10   - board reset!
11   - GameState horizontal_winner?(board) should be false
12   - board set_space(column,0,1)
13   - board set_space(column,1,1)
14   - board set_space(column,2,1)
15   - GameState horizontal_winner?(board) should be true
16   - )
17   - )
18   -
19   - it("detects a vertical win",
20   - board = Board mimic(3)
21   - board dimension times(row,
22   - board reset!
23   - GameState vertical_winner?(board) should be false
24   - board set_space(0,row,-1)
25   - board set_space(1,row,-1)
26   - board set_space(2,row,-1)
27   - GameState vertical_winner?(board) should be true
28   - )
29   - )
30   -
31   - it("detects a back diagonal win",
32   - board = Board mimic(3)
33   - GameState diagonal_winner?(board) should be false
34   - board set_space(0,0,-1)
35   - board set_space(1,1,-1)
36   - board set_space(2,2,-1)
37   - GameState diagonal_winner?(board) should be true
38   - )
39   -
40   - it("detects a back diagonal win",
41   - board = Board mimic(3)
42   - GameState diagonal_winner?(board) should be false
43   - board set_space(2,0,-1)
44   - board set_space(1,1,-1)
45   - board set_space(0,2,-1)
46   - GameState diagonal_winner?(board) should be true
47   - )
48   -)
29 spec/human_player_spec.ik
... ... @@ -0,0 +1,29 @@
  1 +use("ispec")
  2 +use("lib/human_player")
  3 +
  4 +describe("HumanPlayer",
  5 +
  6 + before(
  7 + player = HumanPlayer mimic(1)
  8 + board = Board mimic(3)
  9 + )
  10 +
  11 + it("can be initialized with a marker value",
  12 + player marker_value should == 1
  13 + )
  14 +
  15 + it("gets input from user for a move",
  16 + player should receive prompt_for_move("row", board)
  17 + player should receive prompt_for_move("column", board)
  18 + System in should receive read andReturn("2") twice
  19 + player get_move(board) should == [1, 1]
  20 + )
  21 +
  22 + it("knows what moves are valid",
  23 + player valid_input?(2, board) should be true
  24 + player valid_input?(1, board) should be true
  25 + player valid_input?(0, board) should be true
  26 + player valid_input?(3, board) should be false
  27 + player valid_input?(-1, board) should be false
  28 + )
  29 + )
31 spec/illustrator_spec.ik
... ... @@ -0,0 +1,31 @@
  1 +use("ispec")
  2 +use("lib/illustrator")
  3 +
  4 +describe("Illustrator",
  5 +
  6 + before(board = Board mimic(3))
  7 +
  8 + it("can draw a blank board",
  9 + Illustrator draw(board) should == "[ ][ ][ ]\n[ ][ ][ ]\n[ ][ ][ ]\n"
  10 + )
  11 +
  12 + it("draws an X for player value 1",
  13 + board set_space(0,0,1)
  14 + Illustrator draw(board) should == "[X][ ][ ]\n[ ][ ][ ]\n[ ][ ][ ]\n"
  15 + )
  16 +
  17 + it("draws an O for player value -1",
  18 + board set_space(0,0,-1)
  19 + Illustrator draw(board) should == "[O][ ][ ]\n[ ][ ][ ]\n[ ][ ][ ]\n"
  20 + )
  21 +
  22 + it("draws a partially filled in board",
  23 + board set_space(0,0,-1)
  24 + board set_space(1,0,1)
  25 + board set_space(2,1,-1)
  26 + board set_space(2,2,1)
  27 + board set_space(0,1,-1)
  28 + Illustrator draw(board) should == "[O][O][ ]\n[X][ ][ ]\n[ ][O][X]\n"
  29 + )
  30 +
  31 + )
79 spec/machine_player_spec.ik
... ... @@ -0,0 +1,79 @@
  1 +use("ispec")
  2 +use("lib/machine_player")
  3 +
  4 +describe("MachinePlayer",
  5 +
  6 + before(
  7 + player = MachinePlayer mimic(1)
  8 + board = Board mimic(3)
  9 + )
  10 +
  11 + it("can be initialized with a marker value",
  12 + player marker_value should == 1
  13 + )
  14 +
  15 + it("calculates the inverse of a number",
  16 + MachinePlayer inverse(4) should == 1/4
  17 + MachinePlayer inverse(-4) should == -1/4
  18 + MachinePlayer inverse(0) should == 0
  19 + )
  20 +
  21 + it("compares scores based on the player's marker value",
  22 + player better_than_best?(1/3, -1/8) should be true
  23 + MachinePlayer mimic(-1) better_than_best?(1/3, -1/8) should be false
  24 + )
  25 +
  26 + it("returns the index of the middle of the board",
  27 + MachinePlayer middle_of_board(board) should == 1
  28 + MachinePlayer middle_of_board(Board mimic(10)) should == 5
  29 + MachinePlayer middle_of_board(Board mimic(17)) should == 8
  30 + )
  31 +
  32 + ; it("will pick the middle if available",
  33 + ; player get_move(board) should == [1,1]
  34 + ; )
  35 +
  36 + ; it("will win if it can",
  37 + ; board set_space(0,0,1)
  38 + ; board set_space(0,1,-1)
  39 + ; board set_space(1,1,1)
  40 + ; board set_space(0,2,-1)
  41 + ; player get_move(board) should == [2,2]
  42 + ; )
  43 +
  44 + it("blocks when it should",
  45 + board set_space(0,0,-1)
  46 + board set_space(1,1,1)
  47 + board set_space(1,0,-1)
  48 + player get_move(board) should == [2,0]
  49 + )
  50 +
  51 + ; it("*********************",
  52 + ; board set_space(0,0,-1)
  53 + ; board set_space(1,1,1)
  54 + ; board set_space(1,0,-1)
  55 + ; player get_move(board) should == [2,0]
  56 + ; )
  57 +
  58 + ; it("wins an easy one",
  59 + ; board set_space(0,0,-1)
  60 + ; board set_space(0,1,1)
  61 + ; board set_space(0,2,-1)
  62 + ; board set_space(1,0,-1)
  63 + ; board set_space(1,1,1)
  64 + ; board set_space(1,2,-1)
  65 + ; board set_space(2,0,1)
  66 + ; player get_move(board) should == [2,1]
  67 + ; )
  68 +
  69 + ; it("blocks an easy one",
  70 + ; board set_space(0,0,1)
  71 + ; board set_space(0,1,-1)
  72 + ; board set_space(0,2,1)
  73 + ; board set_space(1,0,-1)
  74 + ; board set_space(1,1,-1)
  75 + ; board set_space(2,0,-1)
  76 + ; board set_space(2,1,1)
  77 + ; player get_move(board) should == [1,2]
  78 + ; )
  79 + )
87 spec/rules_spec.ik
... ... @@ -0,0 +1,87 @@
  1 +use("ispec")
  2 +use("lib/rules")
  3 +use("lib/board")
  4 +
  5 +describe("Rules",
  6 +
  7 + before(board = Board mimic(3))
  8 +
  9 + it("detects a horizontal win",
  10 + board dimension times(column,
  11 + board reset!
  12 + Rules winner?(board) should be false
  13 + board set_space(column,0,1)
  14 + board set_space(column,1,1)
  15 + board set_space(column,2,1)
  16 + Rules winner?(board) should be true
  17 + Rules winner(board) should == 1
  18 + )
  19 + )
  20 +
  21 + it("detects a vertical win",
  22 + board dimension times(row,
  23 + board reset!
  24 + Rules winner?(board) should be false
  25 + board set_space(0,row,-1)
  26 + board set_space(1,row,-1)
  27 + board set_space(2,row,-1)
  28 + Rules winner?(board) should be true
  29 + Rules winner(board) should == -1
  30 + )
  31 + )
  32 +
  33 + it("detects a back diagonal win",
  34 + Rules winner?(board) should be false
  35 + board set_space(0,0,-1)
  36 + board set_space(1,1,-1)
  37 + board set_space(2,2,-1)
  38 + Rules winner?(board) should be true
  39 + Rules winner(board) should == -1
  40 + )
  41 +
  42 + it("detects a back diagonal win",
  43 + Rules winner?(board) should be false
  44 + board set_space(2,0,-1)
  45 + board set_space(1,1,-1)
  46 + board set_space(0,2,-1)
  47 + Rules winner?(board) should be true
  48 + Rules winner(board) should == -1
  49 + )
  50 +
  51 + it("detects a cat's game",
  52 + Rules cats_game?(board) should be false
  53 + board set_space(0,0,1)
  54 + board set_space(0,1,1)
  55 + board set_space(0,2,-1)
  56 + board set_space(1,0,-1)
  57 + board set_space(1,1,-1)
  58 + board set_space(1,2,1)
  59 + board set_space(2,0,1)
  60 + board set_space(2,1,-1)
  61 + board set_space(2,2,1)
  62 + Rules cats_game?(board) should be true
  63 + Rules winner(board) should == 0
  64 + )
  65 +
  66 + it("detects a game over by cats game or a winner",
  67 + Rules game_over?(board) should be false
  68 + board set_space(0,0,1)
  69 + board set_space(0,1,1)
  70 + board set_space(0,2,-1)
  71 + board set_space(1,0,-1)
  72 + board set_space(1,1,-1)
  73 + board set_space(1,2,1)
  74 + board set_space(2,0,1)
  75 + board set_space(2,1,-1)
  76 + board set_space(2,2,1)
  77 + Rules game_over?(board) should be true
  78 + Rules winner(board) should == 0
  79 + board reset!
  80 + Rules game_over?(board) should be false
  81 + board set_space(0,0,1)
  82 + board set_space(0,1,1)
  83 + board set_space(0,2,1)
  84 + Rules game_over?(board) should be true
  85 + Rules winner(board) should == 1
  86 + )
  87 + )

No commit comments for this range

Something went wrong with that request. Please try again.