Class: CLIGame

Inherits:
Game
  • Object
show all
Defined in:
lib/ruby_ttt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Game

#advance_game, #current_player, #game_status_check, #total_markers, #verify_move

Constructor Details

#initialize(settings) ⇒ CLIGame

Returns a new instance of CLIGame.



55
56
57
58
# File 'lib/ruby_ttt.rb', line 55

def initialize(settings)
  super
  @ui = CLIUI.new
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



54
55
56
# File 'lib/ruby_ttt.rb', line 54

def board
  @board
end

#player_first_moveObject

Returns the value of attribute player_first_move.



54
55
56
# File 'lib/ruby_ttt.rb', line 54

def player_first_move
  @player_first_move
end

#player_oneObject

Returns the value of attribute player_one.



54
55
56
# File 'lib/ruby_ttt.rb', line 54

def player_one
  @player_one
end

#player_twoObject

Returns the value of attribute player_two.



54
55
56
# File 'lib/ruby_ttt.rb', line 54

def player_two
  @player_two
end

#uiObject

Returns the value of attribute ui.



54
55
56
# File 'lib/ruby_ttt.rb', line 54

def ui
  @ui
end

Instance Method Details

#exit_gameObject



92
93
94
95
# File 'lib/ruby_ttt.rb', line 92

def exit_game
  board.display_board
  ui.io.exit
end

#get_next_moveObject



78
79
80
81
82
83
84
85
86
# File 'lib/ruby_ttt.rb', line 78

def get_next_move
  if current_player.is_a?(HumanPlayer)
    move = ui.request_human_move
    verify_move(move) ? advance_game : invalid_move(move)
  else
    current_player.make_move(board)
    advance_game
  end
end

#invalid_move(cell) ⇒ Object



88
89
90
# File 'lib/ruby_ttt.rb', line 88

def invalid_move(cell)
  board.valid_cell?(cell) ? ui.taken_cell_message(cell) : ui.bad_cell_message(cell)
end

#play!Object



69
70
71
72
73
74
75
76
# File 'lib/ruby_ttt.rb', line 69

def play!
  ui.first_move_message
  until board.game_over?
    board.display_board
    get_next_move
  end
  exit_game
end

#start_game!Object



60
61
62
63
64
65
66
67
# File 'lib/ruby_ttt.rb', line 60

def start_game!
  begin
    play!
  rescue Interrupt
    ui.early_exit_message
    exit
  end
end