tttazures 0.0.4 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/lib/board.rb +49 -0
  3. data/lib/board_printer_three_dim.rb +40 -0
  4. data/lib/board_printer_two_dim.rb +32 -0
  5. data/lib/computer_ai.rb +78 -0
  6. data/lib/config.rb +78 -0
  7. data/lib/console_ui.rb +107 -0
  8. data/lib/driver.rb +3 -0
  9. data/lib/game_controller.rb +76 -0
  10. data/lib/human_player.rb +17 -0
  11. data/lib/three_dimensional_board.rb +197 -0
  12. data/lib/two_dimensional_board.rb +67 -0
  13. data/spec/board_3x3_spec.rb +108 -0
  14. data/spec/board_3x3x3_spec.rb +173 -0
  15. data/spec/board_4x4_spec.rb +95 -0
  16. data/spec/board_5x5_spec.rb +74 -0
  17. data/spec/computer_ai_3x3_board_spec.rb +62 -0
  18. data/spec/computer_ai_spec.rb +10 -0
  19. data/spec/configurer_spec.rb +140 -0
  20. data/spec/console_ui_spec.rb +115 -0
  21. data/spec/coverage/assets/0.7.1/application.css +1110 -0
  22. data/spec/coverage/assets/0.7.1/application.js +626 -0
  23. data/spec/coverage/assets/0.7.1/fancybox/blank.gif +0 -0
  24. data/spec/coverage/assets/0.7.1/fancybox/fancy_close.png +0 -0
  25. data/spec/coverage/assets/0.7.1/fancybox/fancy_loading.png +0 -0
  26. data/spec/coverage/assets/0.7.1/fancybox/fancy_nav_left.png +0 -0
  27. data/spec/coverage/assets/0.7.1/fancybox/fancy_nav_right.png +0 -0
  28. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_e.png +0 -0
  29. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_n.png +0 -0
  30. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_ne.png +0 -0
  31. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_nw.png +0 -0
  32. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_s.png +0 -0
  33. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_se.png +0 -0
  34. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_sw.png +0 -0
  35. data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_w.png +0 -0
  36. data/spec/coverage/assets/0.7.1/fancybox/fancy_title_left.png +0 -0
  37. data/spec/coverage/assets/0.7.1/fancybox/fancy_title_main.png +0 -0
  38. data/spec/coverage/assets/0.7.1/fancybox/fancy_title_over.png +0 -0
  39. data/spec/coverage/assets/0.7.1/fancybox/fancy_title_right.png +0 -0
  40. data/spec/coverage/assets/0.7.1/fancybox/fancybox-x.png +0 -0
  41. data/spec/coverage/assets/0.7.1/fancybox/fancybox-y.png +0 -0
  42. data/spec/coverage/assets/0.7.1/fancybox/fancybox.png +0 -0
  43. data/spec/coverage/assets/0.7.1/favicon_green.png +0 -0
  44. data/spec/coverage/assets/0.7.1/favicon_red.png +0 -0
  45. data/spec/coverage/assets/0.7.1/favicon_yellow.png +0 -0
  46. data/spec/coverage/assets/0.7.1/loading.gif +0 -0
  47. data/spec/coverage/assets/0.7.1/magnify.png +0 -0
  48. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  49. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  50. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  51. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  52. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  53. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  54. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  55. data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  56. data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_222222_256x240.png +0 -0
  57. data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  58. data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_454545_256x240.png +0 -0
  59. data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_888888_256x240.png +0 -0
  60. data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  61. data/spec/coverage/index.html +72 -0
  62. data/spec/game_controller_spec.rb +50 -0
  63. data/spec/human_player_spec.rb +20 -0
  64. data/spec/long_tests.rb +265 -0
  65. data/spec/spec_helper.rb +4 -0
  66. metadata +121 -3
@@ -0,0 +1,67 @@
1
+ require_relative "./board"
2
+
3
+ class TwoDimensionalBoard < Board
4
+
5
+ def initialize size
6
+ @board_array = Array.new(size**2,:open)
7
+ @row_length = size
8
+ end
9
+
10
+ def get_type
11
+ :two_dimensional_board
12
+ end
13
+
14
+ def check_board_status
15
+ a = [check_columns_for_win(@board_array), check_rows_for_win(@board_array),
16
+ check_top_bottom_diagonal(@board_array), check_bottom_top_diagonal(@board_array)]
17
+ a.delete(:continue_game)
18
+ if a.size > 0 then return a.pop
19
+ elsif board_is_full? then return :tie
20
+ else return :continue_game end
21
+ end
22
+
23
+ def check_rows_for_win board
24
+ for row_number in 0...@row_length
25
+ row = get_row(row_number, board).uniq
26
+ return row.pop if win_is_found(row)
27
+ end
28
+ :continue_game
29
+ end
30
+
31
+ def get_row row_number, board
32
+ start_index = row_number * @row_length
33
+ board.slice(start_index, @row_length)
34
+ end
35
+
36
+ def check_columns_for_win board
37
+ for column_number in 0...@row_length
38
+ column = get_column(column_number,board).uniq
39
+ return column.pop if win_is_found(column)
40
+ end
41
+ :continue_game
42
+ end
43
+
44
+ def get_column start_index, board
45
+ board.select.with_index {|x,i| x if (start_index-i) % @row_length == 0}
46
+ end
47
+
48
+ def check_top_bottom_diagonal board
49
+ diagonal = get_top_bottom_diagonal(board).uniq
50
+ return diagonal.pop if win_is_found(diagonal)
51
+ :continue_game
52
+ end
53
+
54
+ def get_top_bottom_diagonal board
55
+ board.select.with_index {|x,i| x if i % (@row_length+1) == 0}
56
+ end
57
+
58
+ def check_bottom_top_diagonal board
59
+ diagonal = get_bottom_top_diagonal(board).uniq
60
+ return diagonal.pop if win_is_found(diagonal)
61
+ :continue_game
62
+ end
63
+
64
+ def get_bottom_top_diagonal board
65
+ board.select.with_index{|x,i| x if i % (@row_length-1) == 0 && i != @board_array.length-1 && i != 0}
66
+ end
67
+ end
@@ -0,0 +1,108 @@
1
+ require_relative "spec_helper"
2
+ require_relative '../lib/ttt/two_dimensional_board'
3
+
4
+ describe TwoDimensionalBoard do
5
+
6
+ let(:board) {b = TwoDimensionalBoard.new 3}
7
+
8
+ describe TwoDimensionalBoard do
9
+
10
+ it "creates empty board of size 9" do
11
+ open_spots = board.get_open_spots
12
+ open_spots.size.should == 9
13
+ end
14
+ end
15
+
16
+ describe '#check_for_tie' do
17
+
18
+ it "scores a tie" do
19
+ board.check_for_tie.should == :continue_game
20
+ board.instance_variable_set(:@board_array, Array.new(9,:taken))
21
+ board.check_for_tie.should == :tie
22
+ end
23
+ end
24
+
25
+ describe '#check_row_win' do
26
+
27
+ it "scores row win" do
28
+ test_board = [:x,:x,:open, :open,:open,:open, :open,:open,:open]
29
+ board.check_rows_for_win(test_board).should == :continue_game
30
+
31
+ test_board = [:x,:open,:open, :x,:open,:open, :x,:open,:open]
32
+ board.check_rows_for_win(test_board).should ==:continue_game
33
+
34
+ test_board = [:x,:x,:x, :open,:open,:open, :open,:open,:open]
35
+ board.check_rows_for_win(test_board).should == :x
36
+
37
+ test_board = [:open,:open,:open, :o,:o,:o, :open,:open,:open]
38
+ board.check_rows_for_win(test_board).should == :o
39
+
40
+ test_board = [:open,:open,:open, :open,:open,:open, :o,:o,:o]
41
+ board.check_rows_for_win(test_board).should == :o
42
+ end
43
+ end
44
+
45
+ describe '#check_columns_for_win' do
46
+
47
+ it "scores column win" do
48
+ test_board = [:open,:open,:open, :open,:open,:open, :o,:o,:o]
49
+ board.check_columns_for_win(test_board).should == :continue_game
50
+
51
+ test_board = [:o,:open,:open, :o,:open,:open, :o,:open,:open]
52
+ board.check_columns_for_win(test_board).should == :o
53
+
54
+ test_board = [:open,:x,:open, :open,:x,:open, :open,:x,:open]
55
+ board.check_columns_for_win(test_board).should == :x
56
+ end
57
+ end
58
+
59
+ describe '#check_bottom_top_diagonal' do
60
+
61
+ it "scores bottom top diag win" do
62
+ test_board = [:x,:open,:open, :x,:open,:open, :open,:open,:open]
63
+ board.check_bottom_top_diagonal(test_board).should == :continue_game
64
+
65
+ test_board = [:open,:open,:x, :open,:x,:open, :x,:open,:open]
66
+ board.check_bottom_top_diagonal(test_board).should == :x
67
+ end
68
+ end
69
+
70
+ describe '#check_top_bottom_diagonal' do
71
+
72
+ it "scores top bottom diag win" do
73
+ test_board = [:x,:open,:open, :open,:x,:open, :open,:open,:open]
74
+ board.check_top_bottom_diagonal(test_board).should == :continue_game
75
+
76
+ test_board = [:x,:open,:open, :open,:x,:open, :open,:open,:x]
77
+ board.check_top_bottom_diagonal(test_board).should == :x
78
+ end
79
+
80
+ it "tests full board" do
81
+ test_board = [:x,:o,:x, :x,:x,:o, :o,:o,:x]
82
+ board.check_top_bottom_diagonal(test_board).should == :x
83
+
84
+ end
85
+ end
86
+
87
+ describe '#special test cases' do
88
+
89
+ it "scores win" do
90
+ test_board = [:x,:open,:open, :x,:open,:o, :x,:open,:open]
91
+ board.instance_variable_set(:@board_array,test_board)
92
+ board.check_board_status.should == :x
93
+
94
+ test_board = [:x,:x,:open, :open,:open,:o, :x,:o,:open]
95
+ board.instance_variable_set(:@board_array,test_board)
96
+ board.check_board_status.should == :continue_game
97
+
98
+ end
99
+
100
+ it "scores a win" do
101
+ test_board = [:x,:o,:x, :x,:open,:open, :o,:o,:open]
102
+ board.instance_variable_set(:@board_array,test_board)
103
+ board.check_board_status.should == :continue_game
104
+ end
105
+
106
+ end
107
+
108
+ end
@@ -0,0 +1,173 @@
1
+ require_relative "spec_helper"
2
+ require_relative '../lib/ttt/three_dimensional_board'
3
+
4
+ describe ThreeDimensionalBoard do
5
+
6
+ let(:board){b = ThreeDimensionalBoard.new 3}
7
+ let(:test_board) {b = Array.new(27, :open)}
8
+
9
+ it "creates array of size 27" do
10
+ open_spots = board.get_open_spots
11
+ open_spots.size.should == 27
12
+ end
13
+
14
+ it "scores a tie" do
15
+ board.check_for_tie.should == :continue_game
16
+ test_board.fill(:taken)
17
+ board.instance_variable_set(:@board_array,test_board)
18
+ board.check_for_tie.should == :tie
19
+ end
20
+
21
+ it "scores a row win" do
22
+ test_board[26] = :x; test_board[25] = :x; test_board[24] = :x
23
+ board.instance_variable_set(:@board_array,test_board)
24
+ board.check_rows_for_win(test_board).should == :x
25
+
26
+ test_board.fill(:open)
27
+ test_board[0] = :o; test_board[1] = :o; test_board[2] = :o
28
+ board.check_rows_for_win(test_board).should == :o
29
+
30
+ test_board.fill(:open)
31
+ test_board[18] = :x; test_board[21] = :x; test_board[24] = :x
32
+ board.check_rows_for_win(test_board) == :x
33
+
34
+ end
35
+
36
+ it "scores column win" do
37
+ test_board[20] = :x; test_board[22] = :x; test_board[26] = :x
38
+ board.check_column_win(test_board).should == :continue_game
39
+
40
+ test_board[20] = :x; test_board[23] = :x; test_board[26] = :x
41
+ board.check_column_win(test_board).should == :x
42
+
43
+ test_board.fill(:open)
44
+ test_board[9] = :o; test_board[12] = :o; test_board[15] = :o
45
+ board.check_column_win(test_board).should == :o
46
+ end
47
+
48
+ it "scores top->bot diagonal win" do
49
+ test_board[9] = :x; test_board[13] = :x
50
+ board.check_top_bottom_diagonal(test_board).should == :continue_game
51
+
52
+ test_board[9] = :x; test_board[13] = :x; test_board[17] = :x
53
+ board.check_top_bottom_diagonal(test_board).should == :x
54
+
55
+ test_board.fill(:open)
56
+ test_board[18] = :o; test_board[22] = :o; test_board[26] = :o
57
+ board.check_top_bottom_diagonal(test_board).should == :o
58
+ end
59
+
60
+ it "scores bot->top diagonal win" do
61
+ test_board[24] = :x; test_board[22] = :x
62
+ board.check_bottom_top_diagonal(test_board).should == :continue_game
63
+
64
+ test_board[20] = :x
65
+ board.check_bottom_top_diagonal(test_board).should == :x
66
+
67
+ test_board.fill(:open)
68
+ test_board[15] = :x; test_board[13] = :x
69
+ board.check_bottom_top_diagonal(test_board).should == :continue_game
70
+
71
+ test_board[11] = :x
72
+ board.check_bottom_top_diagonal(test_board).should == :x
73
+
74
+ test_board.fill(:open)
75
+ test_board[6] = :x; test_board[4] = :x; test_board[2] = :x
76
+ board.check_bottom_top_diagonal(test_board).should == :x
77
+ end
78
+
79
+ it "scores three_dim row win" do
80
+ test_board[8] = :x; test_board[17] = :x
81
+ board.check_front_to_back_row_win(test_board).should == :continue_game
82
+
83
+ test_board[26] = :x
84
+ board.check_front_to_back_row_win(test_board).should == :x
85
+
86
+ test_board.fill(:open)
87
+ test_board[4] = :o; test_board[13] = :o
88
+ board.check_front_to_back_row_win(test_board).should == :continue_game
89
+
90
+ test_board[22] = :o
91
+ board.check_front_to_back_row_win(test_board).should == :o
92
+ end
93
+
94
+ it "scores horizontal_left_right_diagonal_check" do
95
+ test_board[3] = :x; test_board[13] = :x
96
+ board.horizontal_left_right_diagonal_check(test_board).should == :continue_game
97
+
98
+ test_board[23] = :x
99
+ board.horizontal_left_right_diagonal_check(test_board).should == :x
100
+ end
101
+
102
+ it "scores horizontal_right_left_diagonal_check" do
103
+ test_board[5] = :x; test_board[13] = :x
104
+ board.horizontal_right_left_diagonal_check(test_board).should == :continue_game
105
+
106
+ test_board[21] = :x
107
+ board.horizontal_right_left_diagonal_check(test_board).should == :x
108
+ end
109
+
110
+ it "scores vertical_top_bottom_diagonal_check" do
111
+ test_board[1] = :x; test_board[13] = :x
112
+ board.vertical_top_bottom_diagonal_check(test_board).should == :continue_game
113
+
114
+ test_board[25] = :x
115
+ board.vertical_top_bottom_diagonal_check(test_board).should == :x
116
+ end
117
+
118
+ it "scores vertical_bottom_top_diagonal_check" do
119
+ test_board[7] = :x; test_board[13] = :x
120
+ board.vertical_bottom_top_diagonal_check(test_board).should == :continue_game
121
+
122
+ test_board[19] = :x
123
+ board.vertical_bottom_top_diagonal_check(test_board).should == :x
124
+ end
125
+
126
+ it "scores angled_through_center_diagonals_check" do
127
+ test_board[0] = :x; test_board[13] = :x
128
+ board.angled_through_center_diagonals_check(test_board).should == :continue_game
129
+
130
+ test_board[26] = :x
131
+ board.angled_through_center_diagonals_check(test_board).should == :x
132
+
133
+ test_board.fill(:open)
134
+
135
+ test_board[8] = :x; test_board[13] = :x
136
+ board.angled_through_center_diagonals_check(test_board).should == :continue_game
137
+
138
+ test_board[18] = :x
139
+ board.angled_through_center_diagonals_check(test_board).should == :x
140
+ end
141
+
142
+ describe '#check_board_status' do
143
+
144
+ it "returns win if there is a column win" do
145
+ board.stub(:check_column_win){:x}
146
+ board.check_board_status.should == :x
147
+ end
148
+
149
+ it "returns win if there is a row win" do
150
+ board.stub(:check_rows_for_win){:o}
151
+ board.check_board_status.should == :o
152
+ end
153
+
154
+ it "return win if there is a diag win" do
155
+ board.stub(:check_diagonal_wins){:x}
156
+ board.check_board_status.should == :x
157
+ end
158
+
159
+ it "return tie if no win and board is full" do
160
+ board.stub(:check_for_tie){:tie}
161
+ board.check_board_status.should == :tie
162
+ end
163
+ end
164
+
165
+ describe '#check_diagonal_wins' do
166
+
167
+ it "returns win if check bottom top diag win" do
168
+ board.stub(:check_top_bottom_diagonal){:x}
169
+ board.check_diagonal_wins(board).should == :x
170
+ end
171
+
172
+ end
173
+ end
@@ -0,0 +1,95 @@
1
+ require_relative "spec_helper"
2
+ require_relative "../lib/ttt/two_dimensional_board"
3
+
4
+ describe TwoDimensionalBoard do
5
+
6
+ let(:board) {b = TwoDimensionalBoard.new 4}
7
+
8
+ describe "#initialize" do
9
+
10
+ it "creates empty board of size 16" do
11
+ open_spots = board.get_open_spots
12
+ open_spots.size.should == 16
13
+ end
14
+ end
15
+
16
+ describe '#check_for_tie' do
17
+
18
+ it "scores a tie" do
19
+ board.check_for_tie.should == :continue_game
20
+ board.instance_variable_set(:@board_array, Array.new(16,:taken))
21
+ board.check_for_tie.should == :tie
22
+ end
23
+ end
24
+
25
+ describe '#check_row_win' do
26
+
27
+ it "scores row win" do
28
+ test_board = [:x,:x,:open,:open,
29
+ :open,:open,:open,:x,
30
+ :open,:open,:open,:x,
31
+ :open,:open,:open,:open]
32
+ board.check_rows_for_win(test_board).should == :continue_game
33
+
34
+ test_board = [:x,:x,:x,:open,
35
+ :open,:open,:open,:open,
36
+ :open,:open,:open,:open,
37
+ :open,:open,:open,:open]
38
+ board.check_rows_for_win(test_board).should == :continue_game
39
+
40
+ test_board = [:x,:x,:x,:x,
41
+ :open,:open,:open,:open,
42
+ :open,:open,:open,:open,
43
+ :open,:open,:open,:open]
44
+ board.check_rows_for_win(test_board).should == :x
45
+
46
+ test_board = [:open,:open,:open,:open,
47
+ :x,:x,:x,:x,
48
+ :open,:open,:open,:open,
49
+ :open,:open,:open,:open]
50
+ board.check_rows_for_win(test_board).should == :x
51
+
52
+ test_board = [:open,:open,:open,:open, :open,:open,:open,:open, :x,:x,:x,:x, :open,:open,:open,:open]
53
+ board.check_rows_for_win(test_board).should == :x
54
+ end
55
+ end
56
+
57
+ describe '#check_columns_for_win' do
58
+
59
+ it "scores column win" do
60
+ test_board = [:x,:open,:open,:open, :x,:open,:open,:open, :x,:open,:open,:open, :open,:open,:open,:open]
61
+ board.check_columns_for_win(test_board).should == :continue_game
62
+
63
+ test_board = [:x,:open,:open,:open, :x,:open,:open,:open, :x,:open,:open,:open, :x,:open,:open,:open]
64
+ board.check_columns_for_win(test_board).should == :x
65
+
66
+ test_board = [:open,:x,:open,:open, :open,:x,:open,:open, :open,:x,:open,:open, :open,:x,:open,:open]
67
+ board.check_columns_for_win(test_board).should == :x
68
+ end
69
+ end
70
+
71
+ describe '#check_bottom_top_diagonal' do
72
+
73
+ it "scores bottom top diag win" do
74
+ test_board = [:x,:open,:open,:open,
75
+ :x,:open,:open,:open,
76
+ :open,:open,:open,:open,
77
+ :open,:open,:open,:open]
78
+ board.check_bottom_top_diagonal(test_board).should == :continue_game
79
+
80
+ test_board = [:open,:open,:open,:x, :open,:open,:x,:open, :open,:x,:open,:open, :x,:open,:open,:open]
81
+ board.check_bottom_top_diagonal(test_board).should == :x
82
+ end
83
+ end
84
+
85
+ describe '#check_top_bottom_diagonal' do
86
+
87
+ it "scores top bottom diag win" do
88
+ test_board = [:x,:open,:open,:open, :open,:x,:open,:open, :open,:open,:x,:open, :open,:open,:open,:open]
89
+ board.check_top_bottom_diagonal(test_board).should == :continue_game
90
+
91
+ test_board = [:x,:open,:open,:open, :open,:x,:open,:open, :open,:open,:x,:open, :open,:open,:open,:x]
92
+ board.check_top_bottom_diagonal(test_board).should == :x
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,74 @@
1
+ require_relative "spec_helper"
2
+ require_relative "../lib/ttt/two_dimensional_board"
3
+
4
+ describe TwoDimensionalBoard do
5
+
6
+ let(:board) {b = TwoDimensionalBoard.new 5}
7
+
8
+ describe "#initialize" do
9
+
10
+ it "creates empty board of size :continue_game5" do
11
+ open_spots = board.get_open_spots
12
+ open_spots.size.should == 25
13
+ end
14
+ end
15
+
16
+ describe '#check_for_tie' do
17
+
18
+ it "scores a tie" do
19
+ board.check_for_tie.should == :continue_game
20
+ board.instance_variable_set(:@board_array, Array.new(25,:taken))
21
+ board.check_for_tie.should == :tie
22
+ end
23
+ end
24
+
25
+ describe '#check_row_win' do
26
+
27
+ it "scores row win" do
28
+ test_board = [:x,:x,:x,:x,:open, :open,:open,:open,:x,:open, :open,:open,:open,:x,:open, :open,:open,:open,:open,:x, :open,:open,:open,:open,:open]
29
+ board.check_rows_for_win(test_board).should == :continue_game
30
+
31
+ test_board = [:x,:x,:x,:open,:open, :open,:open,:x,:x,:open, :open,:open,:open,:x,:open, :open,:x,:open,:open,:x, :open,:open,:open,:open,:open]
32
+ board.check_rows_for_win(test_board).should == :continue_game
33
+
34
+ test_board = [:x,:x,:x,:x,:x, :open,:open,:x,:x,:open, :open,:open,:open,:x,:open, :open,:x,:open,:open,:x, :open,:open,:open,:open,:open]
35
+ board.check_rows_for_win(test_board).should == :x
36
+ end
37
+ end
38
+
39
+ describe '#check_columns_for_win' do
40
+
41
+ it "scores column win" do
42
+ test_board = [:x,:open,:open,:open,:open, :x,:open,:open,:open,:open, :x,:open,:open,:open,:open, :open,:open,:open,:open,:open, :open,:open,:open,:open,:open]
43
+ board.check_columns_for_win(test_board).should == :continue_game
44
+
45
+ test_board = [:x,:open,:open,:open,:open, :x,:open,:open,:open,:open, :x,:open,:open,:open,:open, :x,:open,:open,:open,:open, :x,:open,:open,:open,:open]
46
+ board.check_columns_for_win(test_board).should == :x
47
+
48
+ test_board = [:open,:x,:open,:open,:open, :open,:x,:open,:open,:open, :open,:x,:open,:open,:open, :open,:x,:open,:open,:open, :open,:x,:open,:open,:open]
49
+ board.check_columns_for_win(test_board).should == :x
50
+ end
51
+ end
52
+
53
+ describe '#check_bottom_top_diagonal' do
54
+
55
+ it "scores bottom top diag win" do
56
+ test_board = [:open,:x,:open,:open,:open, :open,:x,:open,:open,:open, :open,:x,:open,:open,:open, :open,:x,:open,:open,:open, :open,:x,:open,:open,:open]
57
+ board.check_bottom_top_diagonal(test_board).should == :continue_game
58
+
59
+ test_board = [:open,:open,:open,:open,:x, :open,:open,:open,:x,:open, :open,:open,:x,:open,:open, :open,:x,:open,:open,:open, :x,:open,:open,:open,:open]
60
+ board.check_bottom_top_diagonal(test_board).should == :x
61
+ end
62
+ end
63
+
64
+ describe '#check_top_bottom_diagonal' do
65
+
66
+ it "scores top bottom diag win" do
67
+ test_board = [:x,:open,:open,:open,:open, :open,:x,:open,:open,:open, :open,:open,:x,:open,:open, :open,:open,:open,:x,:open, :open,:open,:open,:open,:open]
68
+ board.check_top_bottom_diagonal(test_board).should == :continue_game
69
+
70
+ test_board = [:x,:open,:open,:open,:open, :open,:x,:open,:open,:open, :open,:open,:x,:open,:open, :open,:open,:open,:x,:open, :open,:open,:open,:open,:x]
71
+ board.check_top_bottom_diagonal(test_board).should == :x
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,62 @@
1
+ require_relative "spec_helper"
2
+ require_relative '../lib/ttt/two_dimensional_board'
3
+ require_relative '../lib/ttt/computer_ai'
4
+
5
+ describe ComputerAI do
6
+
7
+ let(:board) {b = TwoDimensionalBoard.new 3}
8
+ let(:ai) {ai = ComputerAI.new(:o, 100)}
9
+
10
+ describe '#make_move' do
11
+
12
+ it "takes win if available" do
13
+ test_board = [:x,:o,:x,
14
+ :open,:o,:open,
15
+ :open,:open,:x]
16
+ board.instance_variable_set(:@board_array,test_board)
17
+ ai.make_move(board).should == 7
18
+
19
+ test_board = [:x,:open,:x,
20
+ :o,:o,:open,
21
+ :open,:open,:x]
22
+ board.instance_variable_set(:@board_array,test_board)
23
+ ai.make_move(board).should == 5
24
+
25
+ test_board = [:x,:open,:x,
26
+ :open,:o,:o,
27
+ :x,:open,:open]
28
+ board.instance_variable_set(:@board_array,test_board)
29
+ ai.make_move(board).should == 3
30
+
31
+ test_board = [:o,:x,:x,
32
+ :x,:o,:open,
33
+ :open,:open,:open]
34
+ board.instance_variable_set(:@board_array,test_board)
35
+ ai.make_move(board).should == 8
36
+ end
37
+
38
+ it "takes win" do
39
+ test_board = [:x,:o,:x,
40
+ :x,:open,:open,
41
+ :open,:o,:open]
42
+ board.instance_variable_set(:@board_array,test_board)
43
+ ai.make_move(board).should == 4
44
+
45
+ end
46
+
47
+
48
+ it "defends against immediate loss" do
49
+ test_board = [:x,:open,:open,
50
+ :open,:open,:o,
51
+ :x,:open,:open]
52
+ board.instance_variable_set(:@board_array,test_board)
53
+ ai.make_move(board).should == 3
54
+
55
+ test_board = [:x,:open,:x,
56
+ :open,:open,:open,
57
+ :open,:open,:open]
58
+ board.instance_variable_set(:@board_array,test_board)
59
+ ai.make_move(board).should == 1
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,10 @@
1
+ require_relative "spec_helper"
2
+ require_relative '../lib/ttt/computer_ai'
3
+ require_relative "../lib/ttt/two_dimensional_board"
4
+
5
+ describe ComputerAI do
6
+
7
+ let(:ai) {nai = ComputerAI.new;}
8
+ let(:board) {b = TwoDimensionalBoard.new 3}
9
+ end
10
+