Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 15 commits
  • 22 files changed
  • 0 comments
  • 1 contributor
5 lib/board.rb
@@ -7,9 +7,8 @@ class Board
7 7 :size,
8 8 :winning_symbol
9 9
10   - def initialize(*n)
11   - size = n[0]
12   - size ||= BOARD_SIZE
  10 + def initialize(board_size = BOARD_SIZE)
  11 + size = board_size
13 12 @grid = fresh_grid(size)
14 13 @size = size * size
15 14 end
21 lib/play_cli.rb → lib/command_line_game.rb
... ... @@ -1,14 +1,15 @@
1 1 require 'game'
2   -require 'human_player'
3   -require 'computer_player'
  2 +require 'player_factory'
4 3
5   -class PlayCli
  4 +class CommandLineGame
6 5
7   - attr_accessor :game, :ui, :player1, :player2
  6 + attr_accessor :game, :ui
  7 + attr_accessor :player_factory, :player1, :player2
8 8
9 9 def initialize(ui)
10 10 @ui = ui
11 11 @game = TTT::Game.new
  12 + @player_factory = TTT::PlayerFactory.new
12 13 end
13 14
14 15 def setup_game
@@ -43,20 +44,16 @@ def set_players
43 44
44 45 def create_player(n)
45 46 input = @ui.get_details_for_player(n)
46   - if input[:type] == :human
47   - player = TTT::HumanPlayer.new(input[:symbol])
48   - elsif input[:type] == :computer
49   - player = TTT::ComputerPlayer.new(input[:symbol])
50   - end
  47 + player = @player_factory.create(input)
51 48 set_player(n,player)
52 49 end
53   -
  50 +
54 51 def set_player(n,player)
55 52 n == 1 ? @player1 = player : @player2 = player
56 53 end
57 54
58 55 def setup_game_board
59   - @game.set_board_symbols(@player1.symbol,@player2.symbol)
  56 + @game.set_game_player_symbols(@player1.symbol,@player2.symbol)
60 57 end
61 58
62 59 def get_player_move(player)
@@ -64,7 +61,7 @@ def get_player_move(player)
64 61 @ui.get_human_move(moves_left)
65 62 else
66 63 sleep 0.5
67   - player.get_move(@game.board, player.symbol, @game.opponent)
  64 + player.get_ai_move(@game.board, player.symbol, @game.opponent_symbol)
68 65 end
69 66 end
70 67
0  lib/commandlineinterface.rb → lib/command_line_interface.rb
File renamed without changes
2  lib/computer_player.rb
@@ -13,7 +13,7 @@ def human?
13 13 false
14 14 end
15 15
16   - def get_move(board, player, opponent)
  16 + def get_ai_move(board, player, opponent)
17 17 # @ai.random_move(board)
18 18 @ai.get_minimax_move(board, player, opponent)
19 19 end
12 lib/game.rb
... ... @@ -1,6 +1,6 @@
1 1 require 'board'
2 2 require 'player_factory'
3   -require 'commandlineinterface'
  3 +require 'command_line_interface'
4 4
5 5 module TTT
6 6 class Game
@@ -13,14 +13,16 @@ def initialize
13 13 @next_player = :player1
14 14 end
15 15
16   - def set_board_symbols(symbol1,symbol2)
17   - # @board.player1_symbol = symbol1
18   - # @board.player2_symbol = symbol2
  16 + def set_game_player_symbols(symbol1,symbol2)
19 17 @player1_symbol = symbol1
20 18 @player2_symbol = symbol2
21 19 end
22 20
23   - def opponent
  21 + def next_player_symbol
  22 + @next_player == :player1 ? @player1_symbol : @player2_symbol
  23 + end
  24 +
  25 + def opponent_symbol
24 26 @next_player == :player1 ? @player2_symbol : @player1_symbol
25 27 end
26 28
118 lib/limelight_game.rb
... ... @@ -1,73 +1,69 @@
1   -require 'board'
2   -require 'player_factory'
  1 +require 'game'
  2 +require 'human_player'
  3 +require 'computer_player'
3 4
4   -module TTT
5   - class LimelightGame
6   - attr_accessor :board, :ui, :player1, :player2
  5 +class LimelightGame
  6 + attr_accessor :game, :ui
  7 + attr_accessor :player_factory, :player1, :player2
7 8
8   - def initialize(ui)
9   - @board = Board.new(3)
10   - @ui = ui
11   - end
12   -
13   - # def update_board(symbol, space)
14   - # @board.place_move(symbol, space)
15   - # if @board.has_winner
16   - # @ui.player_wins
17   - # puts "got here too"
18   - # end
19   - # end
20   -
21   - def play
22   - set_competitors
23   - @board.set_players(@player1.symbol,@player2.symbol)
24   - play_script until game_is_over
25   - end
  9 + def initialize(ui, player1, player2)
  10 + @ui = ui
  11 + @game = TTT::Game.new
  12 + @player_factory = TTT::PlayerFactory.new
  13 + create_players(player1, player2)
  14 + setup_game_board
  15 + end
26 16
27   - def play_script
28   - @ui.display_board(@board)
29   -
30   - update_board
31   - next_player_move
32   - game_over_scenario if game_is_over
33   - end
  17 + def create_players(player1,player2)
  18 + @player1 = @player_factory.create(player1)
  19 + @player2 = @player_factory.create(player2)
  20 + end
34 21
35   - def next_player_move
36   - @board.next_player == :player1 ? @player1.move(@board) : @player2.move(@board)
37   - end
  22 + def setup_game_board
  23 + @game.set_game_player_symbols(@player1.symbol,@player2.symbol)
  24 + end
38 25
39   - def game_over_scenario
40   - if @board.has_winner
41   - @ui.winning_message(@board.winning_symbol)
42   - else
43   - @ui.draw_message
44   - end
45   - @ui.display_board(@board)
46   - @board.reset_board
47   - play_script unless exit_game
48   - end
  26 + def begin
  27 + @ui.display_game_in_progress
  28 + check_for_computer_move
  29 + end
49 30
50   - def set_competitors
51   - set_player(1)
52   - set_player(2)
  31 + def update_game(n)
  32 + @game.make_move(@game.next_player_symbol,n)
  33 + @ui.display_board(@game.current_board)
  34 + if @game.is_over?
  35 + game_over_message
  36 + else
  37 + check_for_computer_move
53 38 end
  39 + end
  40 +
  41 + def next_player
  42 + @game.next_player == :player1 ? @player1 : @player2
  43 + end
54 44
55   - def set_player(n)
56   - input = @ui.get_details_for_player(n)
57   - player = PlayerFactory.create(input, @ui)
58   - n == 1 ? @player1 = player : @player2 = player
59   - end
60   -
61   - def game_is_over
62   - @board.has_winner || @board.is_draw
63   - end
  45 + def is_computer?
  46 + next_player.human? == false
  47 + end
64 48
65   - def ask_to_play_again
66   - @ui.prompt_to_play_again
  49 + def check_for_computer_move
  50 + if next_player.human? == false
  51 + move = next_player.get_ai_move(@game.board, next_player.symbol, @game.opponent_symbol)
  52 + sleep 0.3
  53 + update_game(move)
67 54 end
  55 + end
68 56
69   - def exit_game
70   - @ui.play_again == :no
71   - end
  57 + def game_over_message
  58 + if @game.winner.nil?
  59 + @ui.draw_message
  60 + else
  61 + @ui.winning_message(@game.winner)
  62 + end
72 63 end
73   -end
  64 +
  65 + def reset_game
  66 + @game.reset
  67 + end
  68 +
  69 +end
6 limelight/limelight_ui/players/__reset_button.rb
... ... @@ -0,0 +1,6 @@
  1 +module ResetButton
  2 +
  3 + def mouse_clicked(event)
  4 + scene.reset
  5 + end
  6 +end
37 limelight/limelight_ui/players/limelight_ui.rb
... ... @@ -1,11 +1,12 @@
1 1 module LimelightUi
2 2
3   - def welcome_message
4   - display_game_in_progress
  3 + def start_game(player1,player2)
  4 + production.game = LimelightGame.new(self, player1, player2)
  5 + production.game.begin
5 6 end
6 7
7   - def display_instructions
8   - update_display("Game Starting...")
  8 + def make_move(n)
  9 + production.game.update_game(n)
9 10 end
10 11
11 12 def get_details_for_player(n)
@@ -21,17 +22,6 @@ def display_board(moves)
21 22 end
22 23 end
23 24
24   - def prompt_for_next_move
25   - update_display("Game in Progress")
26   - end
27   -
28   - def get_input
29   - return production.human_move
30   - end
31   -
32   - def invalid_move_message
33   - end
34   -
35 25 def winning_message(winner)
36 26 update_display("Game Over. #{winner} Wins!")
37 27 end
@@ -49,12 +39,17 @@ def display_game_in_progress
49 39 update_display("Game in Progress")
50 40 end
51 41
52   - def prompt_to_play_again
53   - production.play_again = :no
54   - end
55   -
56   - def play_again
57   - return production.play_again
  42 + def reset
  43 + clear_board
  44 + production.game.reset_game
  45 + update_display("Press Start to Begin Game")
  46 + end
  47 +
  48 + def clear_board
  49 + (1..9).each do |n|
  50 + scene.find("square#{n}").text = ""
  51 + end
58 52 end
  53 +
59 54
60 55 end
15 limelight/limelight_ui/players/reset_button.rb
... ... @@ -1,15 +0,0 @@
1   -module ResetButton
2   -
3   - def mouse_clicked(event)
4   - scene.update_display("Press Start to Begin Game")
5   - clear_board
6   - production.play_again = :yes
7   - end
8   -
9   - def clear_board
10   - (1..9).each do |n|
11   - scene.find("square#{n}").text = ""
12   - end
13   - end
14   -
15   -end
2  limelight/limelight_ui/players/square.rb
@@ -3,7 +3,7 @@ module Square
3 3 def mouse_clicked(event)
4 4 if available?
5 5 n = self.id.match(/\d/)[0]
6   - production.human_move = n.to_i
  6 + scene.make_move(n.to_i)
7 7 end
8 8 end
9 9
16 limelight/limelight_ui/players/start_button.rb
... ... @@ -0,0 +1,16 @@
  1 +require 'limelight_game'
  2 +
  3 +module StartButton
  4 +
  5 + def mouse_clicked(event)
  6 + load_players
  7 + scene.start_game(@player1,@player2)
  8 + end
  9 +
  10 + def load_players
  11 + @player1 = {:type => scene.find('first_player').value.downcase.to_sym,
  12 + :symbol => "X"}
  13 + @player2 = {:type => scene.find('second_player').value.downcase.to_sym,
  14 + :symbol => "O" }
  15 + end
  16 +end
31 limelight/limelight_ui/players/start_game.rb
... ... @@ -1,31 +0,0 @@
1   -require 'game'
2   -
3   -module StartGame
4   -
5   - def mouse_clicked(event)
6   - reset_human_move
7   - load_players
8   - begin_game
9   - end
10   -
11   - def reset_human_move
12   - production.human_move = nil
13   - end
14   -
15   - def load_players
16   - player1,player2 = Hash.new,Hash.new
17   - player1[:type] = scene.find('first_player').value.downcase.to_sym
18   - player1[:symbol] = "X"
19   - player2[:type] = scene.find('second_player').value.downcase.to_sym
20   - player2[:symbol] = "O"
21   - production.player_values = [player1,player2]
22   - end
23   -
24   - def begin_game
25   - production.game = TTT::Game.new(scene)
26   - Thread.new do
27   - production.game.play
28   - end
29   - end
30   -
31   -end
4 limelight/limelight_ui/props.rb
@@ -25,8 +25,8 @@
25 25 end
26 26
27 27 button_row do
28   - start_game :text => "Start", :styles => "button"
29   - reset_button :id => "reset_btn", :styles => "button", :text => "Reset"
  28 + start_button :text => "Start", :styles => "button"
  29 + reset_button :id => "reset_btn", :styles => "button", :text => "Reset", :on_mouse_clicked => "scene.reset"
30 30 end
31 31
32 32 end
2  limelight/production.rb
@@ -4,7 +4,7 @@
4 4
5 5 module Production
6 6
7   - attr_accessor :game, :player_values, :play_again, :human_move
  7 + attr_accessor :game
8 8
9 9 # # Define this method if you want the production name to be different from the default, directory name.
10 10 # def name
6 play.rb
... ... @@ -1,7 +1,7 @@
1 1 $: << File.expand_path(File.dirname(__FILE__) + "/lib")
2   -require 'play_cli'
3   -require 'commandlineinterface'
  2 +require 'command_line_game'
  3 +require 'command_line_interface'
4 4
5 5 ui = CommandLineInterface.new
6   -cli_game = PlayCli.new(ui)
  6 +cli_game = CommandLineGame.new(ui)
7 7 cli_game.setup_game
9 spec/play_cli_spec.rb → spec/command_line_game_spec.rb
... ... @@ -1,17 +1,20 @@
1   -require 'play_cli'
  1 +require 'command_line_game'
2 2 require 'human_player'
  3 +require 'player_factory'
3 4 require 'mocks/mock_game'
4 5 require 'mocks/mock_ui'
5 6 require 'mocks/mock_board'
6 7 require 'mocks/mock_human'
7 8 require 'mocks/mock_computer'
8 9
9   -describe PlayCli do
  10 +describe CommandLineGame do
10 11 before(:each) do
11 12 @ui = FakeUI.new
12   - @cli_runner = PlayCli.new(@ui)
  13 + @cli_runner = CommandLineGame.new(@ui)
13 14 @game = FakeGame.new
14 15 @cli_runner.game = @game
  16 + @player_factory = TTT::PlayerFactory.new
  17 + @cli_runner.player_factory = @player_factory
15 18 @ui.player_details = [{type: :human, symbol: "X"},
16 19 {type: :computer, symbol: "O"}]
17 20 end
2  spec/computer_player_spec.rb
@@ -18,7 +18,7 @@ module TTT
18 18
19 19 it "should get a move from the Ai" do
20 20 @ai.move = :valid_move
21   - @computer.get_move(@board, :player, :opponent).should == :valid_move
  21 + @computer.get_ai_move(@board, :player, :opponent).should == :valid_move
22 22 end
23 23 end
24 24 end
4 spec/game_spec.rb
@@ -11,7 +11,7 @@ module TTT
11 11 @game = Game.new
12 12 @board = FakeBoard.new
13 13 @game.board = @board
14   - @game.set_board_symbols("X","O")
  14 + @game.set_game_player_symbols("X","O")
15 15 end
16 16
17 17 it "should set the player symbols" do
@@ -21,7 +21,7 @@ module TTT
21 21
22 22 it "should return the opponent's symbol" do
23 23 @game.next_player = :player1
24   - @game.opponent.should == "O"
  24 + @game.opponent_symbol.should == "O"
25 25 end
26 26
27 27 describe "a game" do
27 spec/limelight_game_spec.rb
... ... @@ -1,11 +1,28 @@
1 1 require 'limelight_game'
  2 +require 'human_player'
  3 +require 'player_factory'
  4 +require 'mocks/mock_game'
  5 +require 'mocks/mock_ui'
  6 +require 'mocks/mock_board'
  7 +require 'mocks/mock_human'
  8 +require 'mocks/mock_computer'
2 9
3 10 module TTT
4 11 describe LimelightGame do
5   - it "should have a board" do
6   - ui = :some_ui
7   - @game = LimelightGame.new(ui)
8   - @game.board.should_not be_nil
  12 + before(:each) do
  13 + @ui = FakeUI.new
  14 + @game = FakeGame.new
  15 + @player_factory = TTT::PlayerFactory.new
  16 + @player1 = {type: :human, symbol: "X"}
  17 + @player2 = {type: :computer, symbol: "O"}
  18 + @limelight_game = LimelightGame.new(@ui,@player1,@player2)
  19 + @limelight_game.game = @game
  20 + @limelight_game.player_factory = @player_factory
  21 + end
  22 +
  23 + it "should set up the players" do
  24 + @limelight_game.player1.class.should == TTT::HumanPlayer
  25 + @limelight_game.player2.class.should == TTT::ComputerPlayer
9 26 end
10 27 end
11   -end
  28 +end
2  spec/mocks/mock_computer.rb
@@ -3,7 +3,7 @@ class MockComputer
3 3 attr_accessor :ai, :symbol
4 4 attr_accessor :received_move_request
5 5
6   - def get_move(board, player, opponent)
  6 + def get_ai_move(board, player, opponent)
7 7 @received_move_request = true
8 8 end
9 9
4 spec/mocks/mock_game.rb
@@ -19,7 +19,7 @@ def create_board(size)
19 19 @created_board = true
20 20 end
21 21
22   - def set_board_symbols(symbol1,symbol2)
  22 + def set_game_player_symbols(symbol1,symbol2)
23 23 @board_player1_symbol = symbol1
24 24 @board_player2_symbol = symbol2
25 25 end
@@ -29,7 +29,7 @@ def current_board
29 29 return @board
30 30 end
31 31
32   - def opponent
  32 + def opponent_symbol
33 33
34 34 end
35 35
2  spec/mocks/mock_human.rb
@@ -16,7 +16,7 @@ def human?
16 16 true
17 17 end
18 18
19   - def get_move(board)
  19 + def get_ai_move(board)
20 20 @received_move_request = true
21 21 return @move
22 22 end

No commit comments for this range

Something went wrong with that request. Please try again.