Class: TicTacToe::GameType::Terminal
- Inherits:
-
Object
- Object
- TicTacToe::GameType::Terminal
- 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
- #computer_goes_first? ⇒ Boolean
- #display_text(text) ⇒ Object
- #get_move_from_user ⇒ Object
-
#initialize(board, io = Kernel) ⇒ Terminal
constructor
A new instance of Terminal.
- #play_again? ⇒ Boolean
- #update_board ⇒ Object
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
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_user ⇒ Object
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.}. Please try again" retry end |
#play_again? ⇒ 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_board ⇒ Object
39 40 41 |
# File 'lib/tic_tac_toe/game_types/terminal_game.rb', line 39 def update_board @io.puts @board end |