tttazures 0.0.4 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/board.rb +49 -0
- data/lib/board_printer_three_dim.rb +40 -0
- data/lib/board_printer_two_dim.rb +32 -0
- data/lib/computer_ai.rb +78 -0
- data/lib/config.rb +78 -0
- data/lib/console_ui.rb +107 -0
- data/lib/driver.rb +3 -0
- data/lib/game_controller.rb +76 -0
- data/lib/human_player.rb +17 -0
- data/lib/three_dimensional_board.rb +197 -0
- data/lib/two_dimensional_board.rb +67 -0
- data/spec/board_3x3_spec.rb +108 -0
- data/spec/board_3x3x3_spec.rb +173 -0
- data/spec/board_4x4_spec.rb +95 -0
- data/spec/board_5x5_spec.rb +74 -0
- data/spec/computer_ai_3x3_board_spec.rb +62 -0
- data/spec/computer_ai_spec.rb +10 -0
- data/spec/configurer_spec.rb +140 -0
- data/spec/console_ui_spec.rb +115 -0
- data/spec/coverage/assets/0.7.1/application.css +1110 -0
- data/spec/coverage/assets/0.7.1/application.js +626 -0
- data/spec/coverage/assets/0.7.1/fancybox/blank.gif +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_close.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_loading.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_nav_left.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_nav_right.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_e.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_n.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_ne.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_nw.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_s.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_se.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_sw.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_w.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_left.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_main.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_over.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_right.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancybox-x.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancybox-y.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancybox.png +0 -0
- data/spec/coverage/assets/0.7.1/favicon_green.png +0 -0
- data/spec/coverage/assets/0.7.1/favicon_red.png +0 -0
- data/spec/coverage/assets/0.7.1/favicon_yellow.png +0 -0
- data/spec/coverage/assets/0.7.1/loading.gif +0 -0
- data/spec/coverage/assets/0.7.1/magnify.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/spec/coverage/index.html +72 -0
- data/spec/game_controller_spec.rb +50 -0
- data/spec/human_player_spec.rb +20 -0
- data/spec/long_tests.rb +265 -0
- data/spec/spec_helper.rb +4 -0
- metadata +121 -3
@@ -0,0 +1,140 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
require_relative "../lib/ttt/config"
|
3
|
+
require_relative "../lib/ttt/human_player"
|
4
|
+
require_relative "../lib/ttt/computer_ai"
|
5
|
+
require_relative "../lib/ttt/console_ui"
|
6
|
+
require_relative "../lib/ttt/board_printer_two_dim"
|
7
|
+
require_relative "../lib/ttt/board_printer_three_dim"
|
8
|
+
|
9
|
+
describe Configurer do
|
10
|
+
|
11
|
+
|
12
|
+
let(:console){con = ConsoleUI.new}
|
13
|
+
let(:config){c = Configurer.new console}
|
14
|
+
|
15
|
+
describe '#get_players' do
|
16
|
+
|
17
|
+
it "return two human objects if 0 chosen" do
|
18
|
+
console.stub(:query_user_for_game_type){0}
|
19
|
+
result = config.get_players
|
20
|
+
result[:player1].should be_an_instance_of HumanPlayer
|
21
|
+
result[:player2].should be_an_instance_of HumanPlayer
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns human as p1, computer as p2, if 1 is chosen" do
|
25
|
+
console.stub(:query_user_for_game_type){1}
|
26
|
+
config.stub(:get_ai_choice_from_user){2}
|
27
|
+
result = config.get_players
|
28
|
+
result[:player1].should be_an_instance_of HumanPlayer
|
29
|
+
result[:player2].should be_an_instance_of ComputerAI
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns computer as p1, human as p2, if 2 is chosen" do
|
33
|
+
console.stub(:query_user_for_game_type){2}
|
34
|
+
config.stub(:get_ai_choice_from_user){2}
|
35
|
+
result = config.get_players
|
36
|
+
result[:player1].should be_an_instance_of ComputerAI
|
37
|
+
result[:player2].should be_an_instance_of HumanPlayer
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns two computer players if 3 is chosen" do
|
41
|
+
console.stub(:query_user_for_game_type){3}
|
42
|
+
config.stub(:get_ai_choice_from_user){2}
|
43
|
+
config.stub(:get_ai_choice_from_user){2}
|
44
|
+
result = config.get_players
|
45
|
+
result[:player1].should be_an_instance_of ComputerAI
|
46
|
+
result[:player2].should be_an_instance_of ComputerAI
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#set_ai_difficulty' do
|
52
|
+
|
53
|
+
it "returns easy ai if 0 is chosen" do
|
54
|
+
config.stub(:get_ai_choice_from_user){0}
|
55
|
+
result = config.set_ai_difficulty(:x)
|
56
|
+
result.threshold.should == 10
|
57
|
+
end
|
58
|
+
|
59
|
+
it "returns medium ai if 1 is chosen" do
|
60
|
+
config.stub(:get_ai_choice_from_user){1}
|
61
|
+
result = config.set_ai_difficulty(:x)
|
62
|
+
result.threshold.should == 50
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns unbeatable ai if 2 is chosen" do
|
66
|
+
config.stub(:get_ai_choice_from_user){2}
|
67
|
+
result = config.set_ai_difficulty(:x)
|
68
|
+
result.threshold.should == 100
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#get_board' do
|
73
|
+
|
74
|
+
it "gets a 3x3 board when input is 0" do
|
75
|
+
config.stub(:get_board_choice){0}
|
76
|
+
result = config.get_board
|
77
|
+
result.should be_an_instance_of TwoDimensionalBoard
|
78
|
+
result.row_length.should == 3
|
79
|
+
end
|
80
|
+
|
81
|
+
it "gets a 4x4 board when input is 1" do
|
82
|
+
config.stub(:get_board_choice){1}
|
83
|
+
result = config.get_board
|
84
|
+
result.should be_an_instance_of TwoDimensionalBoard
|
85
|
+
result.row_length.should == 4
|
86
|
+
end
|
87
|
+
|
88
|
+
it "gets a 5x5 board when input is 2" do
|
89
|
+
config.stub(:get_board_choice){2}
|
90
|
+
result = config.get_board
|
91
|
+
result.should be_an_instance_of TwoDimensionalBoard
|
92
|
+
result.row_length.should == 5
|
93
|
+
end
|
94
|
+
|
95
|
+
it "gets a 3x3x3 board when input is 3" do
|
96
|
+
config.stub(:get_board_choice){3}
|
97
|
+
result = config.get_board
|
98
|
+
result.should be_an_instance_of ThreeDimensionalBoard
|
99
|
+
result.row_length.should == 3
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe '#get_board_choice' do
|
104
|
+
|
105
|
+
it "returns valid board choice" do
|
106
|
+
console.stub(:query_user_for_board_type){1}
|
107
|
+
config.get_board_choice.should == 1
|
108
|
+
|
109
|
+
console.stub(:query_user_for_board_type){5}
|
110
|
+
console.stub(:query_user_for_board_type){2}
|
111
|
+
config.get_board_choice.should == 2
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe '#get_ai_choice_from_user' do
|
116
|
+
it "returns valid ai choice" do
|
117
|
+
console.stub(:query_user_for_ai_difficulty){1}
|
118
|
+
config.get_ai_choice_from_user(:x).should == 1
|
119
|
+
|
120
|
+
console.stub(:query_user_for_ai_difficulty){5}
|
121
|
+
console.stub(:query_user_for_ai_difficulty){2}
|
122
|
+
config.get_ai_choice_from_user(:x).should == 2
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe '#get_board_printer' do
|
127
|
+
it "returns 2d board printer for 2d board" do
|
128
|
+
board = TwoDimensionalBoard.new(3)
|
129
|
+
result = config.get_board_printer(board)
|
130
|
+
result.should be_an_instance_of BoardPrinterTwoDim
|
131
|
+
end
|
132
|
+
|
133
|
+
it "returns 3d board printer for 3d board" do
|
134
|
+
board = ThreeDimensionalBoard.new(3)
|
135
|
+
result = config.get_board_printer(board)
|
136
|
+
result.should be_an_instance_of BoardPrinterThreeDim
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
require './lib/ttt/console_ui'
|
3
|
+
|
4
|
+
describe ConsoleUI do
|
5
|
+
|
6
|
+
let(:io) {io = StringIO.new}
|
7
|
+
let(:console) {c = ConsoleUI.new io}
|
8
|
+
|
9
|
+
it "creates new ui console" do
|
10
|
+
console.should be_an_instance_of ConsoleUI
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#receive_user_choice' do
|
14
|
+
|
15
|
+
it "receives user choice and sanitizes input" do
|
16
|
+
console.stub(:gets){"1"}
|
17
|
+
console.receive_user_choice.should == "1"
|
18
|
+
|
19
|
+
console.stub(:gets){"Y"}
|
20
|
+
console.receive_user_choice.should == "y"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#query_user_for_choice' do
|
25
|
+
player1 = 1
|
26
|
+
player2 = -1
|
27
|
+
|
28
|
+
it "receives input from user once at a time" do
|
29
|
+
player = 1
|
30
|
+
console.stub(:receive_user_choice) {1}
|
31
|
+
console.query_user_for_choice(player).should eql(1)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "queries for user input - player1" do
|
35
|
+
player = 1
|
36
|
+
console.stub(:receive_user_choice) {1}
|
37
|
+
console.query_user_for_choice(player)
|
38
|
+
io.string.should match "User 1: Choose"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "queries for user input - player2" do
|
42
|
+
|
43
|
+
player = -1
|
44
|
+
console.stub(:receive_user_choice) {1}
|
45
|
+
console.query_user_for_choice(player)
|
46
|
+
io.string.should match "User -1: Choose"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#query_user_for_choice' do
|
51
|
+
|
52
|
+
it "asks user for new game" do
|
53
|
+
console.stub(:receive_user_choice) {1}
|
54
|
+
console.query_user_for_new_game
|
55
|
+
io.string.should match "Would you like to start another game?"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns true if user inputs 'y'" do
|
59
|
+
console.stub(:receive_user_choice) {"y"}
|
60
|
+
console.query_user_for_new_game.should == true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#respond_to_choice' do
|
65
|
+
|
66
|
+
it "tells user that invalid choice is invalid" do
|
67
|
+
choice = 1
|
68
|
+
console.respond_to_choice(false, choice)
|
69
|
+
io.string.should match "Whoops! That choice wasn't valid"
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
it "tells user if choice was valid (choice = 1)" do
|
74
|
+
choice = 1
|
75
|
+
console.respond_to_choice(true, choice)
|
76
|
+
io.string.should match "You Chose: 1"
|
77
|
+
end
|
78
|
+
|
79
|
+
it "tells user if choice was valid (choice = 9)" do
|
80
|
+
choice = 9
|
81
|
+
console.respond_to_choice(true, choice)
|
82
|
+
io.string.should match "You Chose: 9"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe '#present_game_result' do
|
87
|
+
|
88
|
+
it "presents tie text to user if result is tie" do
|
89
|
+
console.present_game_result(:tie)
|
90
|
+
io.string.should match "Game Over: Tie"
|
91
|
+
end
|
92
|
+
|
93
|
+
it "sends player 1 win to console" do
|
94
|
+
player = 1
|
95
|
+
console.present_game_result(:x)
|
96
|
+
io.string.should match "Player x Wins"
|
97
|
+
end
|
98
|
+
|
99
|
+
it "sends player -1 win to console" do
|
100
|
+
player = -1
|
101
|
+
console.present_game_result(:o)
|
102
|
+
io.string.should match "Player o Wins"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe '#query_user_for_ai_difficulty' do
|
107
|
+
|
108
|
+
it "should print difficulty options" do
|
109
|
+
console.print_ai_difficulty_options(:x)
|
110
|
+
io.string.should match "Choose 0: AI is easy"
|
111
|
+
io.string.should match "Choose 1: AI is medium"
|
112
|
+
io.string.should match "Choose 2: AI is unbeatable"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|