Class: TicTacToe::GameType::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/tic_tac_toe/game_types/terminal_game.rb

Overview

Terminal GameType Interacts with the user through the terminal Uses puts for output, and gets for input

Defined Under Namespace

Classes: IllegalMove

Instance Method Summary collapse

Constructor Details

#initialize(board, io = Kernel) ⇒ Terminal

Returns a new instance of Terminal.



11
12
13
14
# File 'lib/tic_tac_toe/game_types/terminal_game.rb', line 11

def initialize(board, io=Kernel)
  @board = board
  @io = io
end

Instance Method Details

#computer_goes_first?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/tic_tac_toe/game_types/terminal_game.rb', line 16

def computer_goes_first?
  input = get_input("Would you like to play first or second? (f/s)")

  return true unless input =~ /^(f|first)$/i
  false
end

#display_text(text) ⇒ Object



43
44
45
# File 'lib/tic_tac_toe/game_types/terminal_game.rb', line 43

def display_text(text)
  @io.puts text
end

#get_move_from_userObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/tic_tac_toe/game_types/terminal_game.rb', line 23

def get_move_from_user
  cords = get_cords_from_user

  raise IllegalMove.new("That cell is already taken") unless empty_cell?(cords)

  cords
rescue IllegalMove => error
  display_text "Illegal Move: #{error.message}. Please try again"
  retry
end

#play_again?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/tic_tac_toe/game_types/terminal_game.rb', line 34

def play_again?
  input = get_input "Play again? (y/n)"
  input =~ /^(y|yes)$/i
end

#update_boardObject



39
40
41
# File 'lib/tic_tac_toe/game_types/terminal_game.rb', line 39

def update_board
  @io.puts @board
end