Class: UI
- Inherits:
-
Object
- Object
- UI
- Defined in:
- lib/ui.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#io ⇒ Object
Returns the value of attribute io.
Instance Method Summary collapse
- #bad_cell_message(cell) ⇒ Object
- #difficulty_level_message ⇒ Object
- #display_board ⇒ Object
- #early_exit_message ⇒ Object
- #first_move_message(player) ⇒ Object
-
#initialize(board) ⇒ UI
constructor
A new instance of UI.
- #invalid_input_message(input) ⇒ Object
- #level_assigned_message(level) ⇒ Object
- #next_move_message(player) ⇒ Object
- #player_type_message(marker) ⇒ Object
- #print_board_numbers ⇒ Object
- #print_board_rows ⇒ Object
- #print_divider ⇒ Object
- #request_difficulty_level ⇒ Object
- #request_human_move ⇒ Object
- #request_player_type(marker) ⇒ Object
- #show_marker(cell) ⇒ Object
- #show_row(letter, cells) ⇒ Object
- #standardize(input) ⇒ Object
- #taken_cell_message(cell) ⇒ Object
- #tie_game_message ⇒ Object
- #type_assigned_message(type, marker) ⇒ Object
- #winning_game_message(player) ⇒ Object
Constructor Details
#initialize(board) ⇒ UI
Returns a new instance of UI.
3 4 5 6 |
# File 'lib/ui.rb', line 3 def initialize(board) @board = board @io = Kernel end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
2 3 4 |
# File 'lib/ui.rb', line 2 def board @board end |
#io ⇒ Object
Returns the value of attribute io.
2 3 4 |
# File 'lib/ui.rb', line 2 def io @io end |
Instance Method Details
#bad_cell_message(cell) ⇒ Object
108 109 110 |
# File 'lib/ui.rb', line 108 def (cell) io.print "#{cell} is not a valid cell ID!\n" end |
#difficulty_level_message ⇒ Object
26 27 28 |
# File 'lib/ui.rb', line 26 def io.print "Select computer difficulty level: Enter 'easy' or 'hard.'\n" end |
#display_board ⇒ Object
81 82 83 84 85 |
# File 'lib/ui.rb', line 81 def display_board print_board_numbers print_board_rows print_board_numbers end |
#early_exit_message ⇒ Object
112 113 114 115 116 |
# File 'lib/ui.rb', line 112 def io.print "\nExiting Tic-Tac-Toe..." io.print "...\n" io.print "Goodbye!\n\n" end |
#first_move_message(player) ⇒ Object
87 88 89 90 |
# File 'lib/ui.rb', line 87 def (player) io.print "\n\n************ New Game ************\n" io.print "Player '#{player.marker}' goes first.\n" end |
#invalid_input_message(input) ⇒ Object
34 35 36 |
# File 'lib/ui.rb', line 34 def (input) io.print " #{input} is not a valid option.\n" end |
#level_assigned_message(level) ⇒ Object
30 31 32 |
# File 'lib/ui.rb', line 30 def (level) io.print "You selected difficulty level #{level.upcase}.\n" end |
#next_move_message(player) ⇒ Object
92 93 94 |
# File 'lib/ui.rb', line 92 def (player) io.print "Player '#{player.marker}': Enter open cell ID.\n" end |
#player_type_message(marker) ⇒ Object
38 39 40 |
# File 'lib/ui.rb', line 38 def (marker) io.print "For player " + "'#{marker}'," + " enter 'human' or 'computer.'\n" end |
#print_board_numbers ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/ui.rb', line 46 def print_board_numbers num = 1 io.print " " board.num_of_rows.times do io.print "--#{num}-- " num += 1 end io.print "\n" end |
#print_board_rows ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/ui.rb', line 62 def print_board_rows alpha = 'A' board.all_rows.each do |row| show_row(alpha, row) alpha = alpha.next end end |
#print_divider ⇒ Object
56 57 58 59 60 |
# File 'lib/ui.rb', line 56 def print_divider io.print " " board.num_of_rows.times { io.print "------" } io.print "\n" end |
#request_difficulty_level ⇒ Object
13 14 15 16 |
# File 'lib/ui.rb', line 13 def request_difficulty_level io.gets.chomp.downcase end |
#request_human_move ⇒ Object
18 19 20 |
# File 'lib/ui.rb', line 18 def request_human_move standardize(io.gets.chomp) end |
#request_player_type(marker) ⇒ Object
8 9 10 11 |
# File 'lib/ui.rb', line 8 def request_player_type(marker) (marker) io.gets.chomp.downcase end |
#show_marker(cell) ⇒ Object
77 78 79 |
# File 'lib/ui.rb', line 77 def show_marker(cell) board.all_cells[cell].nil? ? ' ' : board.all_cells[cell] end |
#show_row(letter, cells) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/ui.rb', line 70 def show_row(letter, cells) io.print "#{letter}" cells.each { |cell| io.print " | " + show_marker(cell) } io.print " | #{letter}\n" print_divider end |
#standardize(input) ⇒ Object
22 23 24 |
# File 'lib/ui.rb', line 22 def standardize(input) input.split('').sort.join('').upcase end |
#taken_cell_message(cell) ⇒ Object
104 105 106 |
# File 'lib/ui.rb', line 104 def (cell) io.print "#{cell} has already been taken!\n" end |
#tie_game_message ⇒ Object
100 101 102 |
# File 'lib/ui.rb', line 100 def io.print "GAME OVER! It's a tie!\n" end |
#type_assigned_message(type, marker) ⇒ Object
42 43 44 |
# File 'lib/ui.rb', line 42 def (type, marker) io.print "Player " + "'#{marker}' " + "is #{type}.\n" end |
#winning_game_message(player) ⇒ Object
96 97 98 |
# File 'lib/ui.rb', line 96 def (player) io.print "GAME OVER! Player '#{player.marker}' wins!\n" end |