tic_tac_toe_nhu 0.0.4 → 0.0.5

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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/bin/tic_tac_toe +2 -2
  3. data/coverage/.last_run.json +1 -1
  4. data/coverage/.resultset.json +51 -136
  5. data/lib/tic_tac_toe/game.rb +44 -23
  6. data/lib/tic_tac_toe/game_state.rb +35 -0
  7. data/lib/tic_tac_toe/game_state_factory.rb +56 -0
  8. data/lib/tic_tac_toe/player.rb +2 -2
  9. data/lib/tic_tac_toe/player_factory.rb +3 -3
  10. data/lib/tic_tac_toe/rules.rb +1 -1
  11. data/lib/tic_tac_toe/strategy/console_user.rb +1 -1
  12. data/lib/tic_tac_toe/strategy/minimax.rb +18 -16
  13. data/lib/tic_tac_toe/ui/console.rb +3 -3
  14. data/lib/tic_tac_toe/values.rb +10 -0
  15. data/spec/integration/tictactoe/unbeatable_computer_spec.rb +3 -3
  16. data/spec/mocks/game_state.rb +18 -0
  17. data/spec/mocks/game_state_factory.rb +14 -0
  18. data/spec/mocks/player.rb +16 -0
  19. data/spec/mocks/player_factory.rb +3 -2
  20. data/spec/mocks/strategy/dynamic.rb +1 -1
  21. data/spec/tic_tac_toe/game_spec.rb +66 -61
  22. data/spec/tic_tac_toe/game_state_factory_spec.rb +63 -0
  23. data/spec/tic_tac_toe/game_state_spec.rb +73 -0
  24. data/spec/tic_tac_toe/player_factory_spec.rb +5 -9
  25. data/spec/tic_tac_toe/player_spec.rb +17 -16
  26. data/spec/tic_tac_toe/strategy/console_user_spec.rb +1 -1
  27. data/spec/tic_tac_toe/strategy/minimax_spec.rb +65 -64
  28. data/spec/tic_tac_toe/ui/console_spec.rb +1 -1
  29. data/spec/tic_tac_toe/values_spec.rb +17 -0
  30. data/tic_tac_toe_nhu.gemspec +1 -1
  31. metadata +10 -8
  32. data/lib/tic_tac_toe/game_factory.rb +0 -51
  33. data/lib/tic_tac_toe/main.rb +0 -54
  34. data/spec/integration/tictactoe/tic_tac_toe.rb +0 -4
  35. data/spec/mocks/game_factory.rb +0 -14
  36. data/spec/tic_tac_toe/game_factory_spec.rb +0 -78
  37. data/spec/tic_tac_toe/main_spec.rb +0 -95
  38. data/spec/tic_tac_toe/player_factory_mock.rb +0 -10
@@ -0,0 +1,17 @@
1
+ require 'tic_tac_toe/values'
2
+
3
+ describe "Values" do
4
+ VALUES = ["X", "O"]
5
+
6
+ it "has X and O values" do
7
+ TicTacToe::VALUES.should == VALUES
8
+ end
9
+
10
+ it "gets opponent for X" do
11
+ TicTacToe::Values.opponent(VALUES[0]).should == VALUES[1]
12
+ end
13
+
14
+ it "gets opponent for O" do
15
+ TicTacToe::Values.opponent(VALUES[1]).should == VALUES[0]
16
+ end
17
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'tic_tac_toe_nhu'
3
- s.version = '0.0.4'
3
+ s.version = '0.0.5'
4
4
  s.date = '2013-06-11'
5
5
  s.summary = "Tic Tac Toe"
6
6
  s.description = "A simple tic tac toe game with AI"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tic_tac_toe_nhu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nhu Nguyen
@@ -32,27 +32,28 @@ files:
32
32
  - features/tictactoe_submit_move.feature
33
33
  - lib/tic_tac_toe/board.rb
34
34
  - lib/tic_tac_toe/game.rb
35
- - lib/tic_tac_toe/game_factory.rb
36
- - lib/tic_tac_toe/main.rb
35
+ - lib/tic_tac_toe/game_state.rb
36
+ - lib/tic_tac_toe/game_state_factory.rb
37
37
  - lib/tic_tac_toe/player.rb
38
38
  - lib/tic_tac_toe/player_factory.rb
39
39
  - lib/tic_tac_toe/rules.rb
40
40
  - lib/tic_tac_toe/strategy/console_user.rb
41
41
  - lib/tic_tac_toe/strategy/minimax.rb
42
42
  - lib/tic_tac_toe/ui/console.rb
43
- - spec/integration/tictactoe/tic_tac_toe.rb
43
+ - lib/tic_tac_toe/values.rb
44
44
  - spec/integration/tictactoe/unbeatable_computer_spec.rb
45
45
  - spec/mocks/game.rb
46
- - spec/mocks/game_factory.rb
46
+ - spec/mocks/game_state.rb
47
+ - spec/mocks/game_state_factory.rb
48
+ - spec/mocks/player.rb
47
49
  - spec/mocks/player_factory.rb
48
50
  - spec/mocks/rules.rb
49
51
  - spec/mocks/strategy/dynamic.rb
50
52
  - spec/mocks/ui/console.rb
51
53
  - spec/tic_tac_toe/board_spec.rb
52
- - spec/tic_tac_toe/game_factory_spec.rb
53
54
  - spec/tic_tac_toe/game_spec.rb
54
- - spec/tic_tac_toe/main_spec.rb
55
- - spec/tic_tac_toe/player_factory_mock.rb
55
+ - spec/tic_tac_toe/game_state_factory_spec.rb
56
+ - spec/tic_tac_toe/game_state_spec.rb
56
57
  - spec/tic_tac_toe/player_factory_spec.rb
57
58
  - spec/tic_tac_toe/player_spec.rb
58
59
  - spec/tic_tac_toe/rules_spec.rb
@@ -60,6 +61,7 @@ files:
60
61
  - spec/tic_tac_toe/strategy/console_user_spec.rb
61
62
  - spec/tic_tac_toe/strategy/minimax_spec.rb
62
63
  - spec/tic_tac_toe/ui/console_spec.rb
64
+ - spec/tic_tac_toe/values_spec.rb
63
65
  - tic_tac_toe_nhu.gemspec
64
66
  homepage: http://rubygems.org/gems/tic_tac_toe_nhu
65
67
  licenses: []
@@ -1,51 +0,0 @@
1
- require 'tic_tac_toe/game'
2
- require 'tic_tac_toe/board'
3
- require 'tic_tac_toe/player_factory'
4
-
5
- module TicTacToe
6
- class GameFactory
7
- def initialize(player_factory = TicTacToe::PlayerFactory.new)
8
- @player_factory = player_factory
9
- end
10
-
11
- def types
12
- [[:human, :computer], [:computer, :human], [:human, :human], [:computer, :computer]]
13
- end
14
-
15
- def create(type_index, board)
16
- case type_index
17
- when 1
18
- human_computer_game(board)
19
- when 2
20
- computer_human_game(board)
21
- when 3
22
- human_human_game(board)
23
- when 4
24
- computer_computer_game(board)
25
- else
26
- raise ArgumentError, "Type does not exist. Please select a number corresponding to the game type."
27
- end
28
- end
29
-
30
- private
31
- def computer_human_game(board)
32
- create_game(board, @player_factory.computer(board), @player_factory.human)
33
- end
34
-
35
- def human_computer_game(board)
36
- create_game(board, @player_factory.human, @player_factory.computer(board))
37
- end
38
-
39
- def human_human_game(board)
40
- create_game(board, @player_factory.human, @player_factory.human("Friend", "O"))
41
- end
42
-
43
- def computer_computer_game(board)
44
- create_game(board, @player_factory.computer(board, "X", "O"), @player_factory.computer(board, "O", "X"))
45
- end
46
-
47
- def create_game(board, player1, player2)
48
- TicTacToe::Game.new(board, player1, player2)
49
- end
50
- end
51
- end
@@ -1,54 +0,0 @@
1
- require 'tic_tac_toe/ui/console'
2
- require 'tic_tac_toe/game_factory'
3
- require 'tic_tac_toe/board'
4
-
5
- module TicTacToe
6
- class Main
7
- attr_writer :rules
8
-
9
- def initialize(ui = TicTacToe::Console.new, game_factory = TicTacToe::GameFactory.new)
10
- @ui = ui
11
- @board = TicTacToe::Board.new
12
- @game_factory = game_factory
13
- end
14
-
15
- def start
16
- @ui.display_welcome_message
17
- @game = create_game
18
- play until @game.over?
19
- @ui.display_board(@board)
20
- display_result
21
- end
22
-
23
- private
24
- def create_game
25
- game_type = @ui.game_type
26
- begin
27
- @game_factory.create(game_type, @board)
28
- rescue ArgumentError
29
- create_game
30
- end
31
- end
32
-
33
- def play
34
- @ui.display_board(@board)
35
- player = @game.current_player
36
- @ui.display_player_turn(player)
37
- begin
38
- @game.make_move
39
- rescue MoveNotAvailableError
40
- @ui.display_square_not_available
41
- end
42
- end
43
-
44
- def display_result
45
- winner = @game.winner
46
- if winner
47
- @ui.display_winner(winner)
48
- else
49
- @ui.display_tied_game
50
- end
51
- end
52
-
53
- end
54
- end
@@ -1,4 +0,0 @@
1
- describe "all situations" do
2
-
3
-
4
- end
@@ -1,14 +0,0 @@
1
- require 'surrogate/rspec'
2
-
3
- class MockGameFactory
4
- Surrogate.endow(self)
5
- define(:initialize) {|player_factory|}
6
- define :types
7
- define(:create) {|type, board|}
8
- end
9
-
10
- describe TicTacToe::GameFactory do
11
- it "checks game factory" do
12
- MockGameFactory.should be_substitutable_for(TicTacToe::GameFactory)
13
- end
14
- end
@@ -1,78 +0,0 @@
1
- require 'tic_tac_toe/spec_helper'
2
- require 'tic_tac_toe/game_factory'
3
- require 'tic_tac_toe/player_factory'
4
- require 'mocks/player_factory'
5
-
6
- describe TicTacToe::GameFactory do
7
-
8
- before(:each) do
9
- @board = "board"
10
- @player_factory = MockPlayerFactory.new
11
- @game_factory = TicTacToe::GameFactory.new(@player_factory)
12
- end
13
-
14
- it "ensures MockPlayerFactory has the same interface as PlayerFactory" do
15
- MockPlayerFactory.should be_substitutable_for(TicTacToe::PlayerFactory)
16
- end
17
-
18
- it "returns 4 types of games" do
19
- @game_factory.types.size.should == 4
20
- end
21
-
22
- it "sets the board in the game" do
23
- game = @game_factory.create(1, @board)
24
- game.board.should == @board
25
- end
26
-
27
- it "returns human vs computer game" do
28
- human = "human"
29
- computer = "computer"
30
- @player_factory.will_have_human human
31
- @player_factory.will_have_computer computer
32
-
33
- game = @game_factory.create(1, @board)
34
- game.current_player.should == human
35
-
36
- @player_factory.was asked_for :computer
37
- @player_factory.was asked_for :human
38
- end
39
-
40
- it "returns computer vs human game" do
41
- human = "human"
42
- computer = "computer"
43
- @player_factory.will_have_human human
44
- @player_factory.will_have_computer computer
45
-
46
- game = @game_factory.create(2, @board)
47
- game.current_player.should == computer
48
-
49
- @player_factory.was asked_for :computer
50
- @player_factory.was asked_for :human
51
- end
52
-
53
- it "returns user vs user game" do
54
- human1 = "mary"
55
- human2 = "alice"
56
- @player_factory.will_have_human human1, human2
57
-
58
- game = @game_factory.create(3, @board)
59
- game.current_player.should == human1
60
-
61
- @player_factory.was asked_for(:human).times(2)
62
- end
63
-
64
- it "returns computer vs computer game" do
65
- computer1 = "computer1"
66
- computer2 = "computer2"
67
- @player_factory.will_have_computer computer2, computer1
68
-
69
- game = @game_factory.create(4, @board)
70
- game.current_player.should == computer2
71
-
72
- @player_factory.was asked_for(:computer).times(2)
73
- end
74
-
75
- it "raises an error when the game type doesn't exist" do
76
- lambda{@game_factory.create(5, @board)}.should raise_error(ArgumentError)
77
- end
78
- end
@@ -1,95 +0,0 @@
1
- require 'tic_tac_toe/spec_helper'
2
- require 'tic_tac_toe/main'
3
- require 'tic_tac_toe/player'
4
- require 'tic_tac_toe/game'
5
- require 'tic_tac_toe/rules'
6
- require 'tic_tac_toe/board'
7
- require 'mocks/game'
8
- require 'mocks/game_factory'
9
- require 'mocks/ui/console'
10
- require 'mocks/strategy/dynamic'
11
- require 'mocks/rules'
12
-
13
- describe TicTacToe::Main do
14
- before(:each) do
15
- @ui = MockConsole.factory
16
- @todd = TicTacToe::Player.new("Todd", "X", MockDynamicStrategy.new([1,2,3]))
17
- @game = MockGame.factory current_player: @todd, winner: @todd
18
- @game_factory = MockGameFactory.factory create: @game
19
- @controller = TicTacToe::Main.new(@ui, @game_factory)
20
- end
21
-
22
- describe "starts game" do
23
- it "displays welcome message" do
24
- @controller.start
25
- @ui.was told_to(:display_welcome_message)
26
- end
27
-
28
- it "asked ui to get game type" do
29
- @controller.start
30
- @ui.was asked_for(:game_type)
31
- end
32
-
33
- it "creates a game based on user input" do
34
- @controller.start
35
- @game_factory.was told_to(:create)
36
- end
37
-
38
- it "asks ui for game type again if the input is incorrect" do
39
- @game_factory.will_create ArgumentError.new, @game
40
- @controller.start
41
- end
42
- end
43
-
44
- describe "play game" do
45
- before(:each) do
46
- @game.will_over? false, true
47
- end
48
-
49
- it "display board" do
50
- @controller.start
51
- @ui.was told_to(:display_board)
52
- end
53
-
54
- it "displays whose turn it is" do
55
- @controller.start
56
- @ui.was told_to(:display_player_turn).with(@todd)
57
- end
58
-
59
- it "tells game to make a move" do
60
- @game.will_over? false, true
61
- @controller.start
62
- @game.was told_to(:make_move)
63
- end
64
-
65
- it "asks for game move until game is over" do
66
- @game.will_over? false, false, false, true
67
- @controller.start
68
- @game.was told_to(:make_move).times(3)
69
- end
70
-
71
- it "displays message when square is not available" do
72
- @game.will_make_move TicTacToe::MoveNotAvailableError.new
73
- @controller.start
74
- @ui.was told_to(:display_square_not_available)
75
- end
76
- end
77
-
78
- describe "end game" do
79
- it "displays the board when the game is over" do
80
- @controller.start
81
- @ui.was told_to(:display_board)
82
- end
83
-
84
- it "notifies user when player wins" do
85
- @controller.start
86
- @ui.was told_to(:display_winner).with(@todd)
87
- end
88
-
89
- it "notifies user when it's a tied game" do
90
- @game.will_have_winner nil
91
- @controller.start
92
- @ui.was told_to(:display_tied_game)
93
- end
94
- end
95
- end
@@ -1,10 +0,0 @@
1
- class PlayerFactoryMock
2
-
3
- def human(name = "User", value = "X")
4
- "Human"
5
- end
6
-
7
- def computer(board, value = "O", opponent_value = "X")
8
- "Computer"
9
- end
10
- end