Class: UI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#boardObject

Returns the value of attribute board.



2
3
4
# File 'lib/ui.rb', line 2

def board
  @board
end

#ioObject

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 bad_cell_message(cell)
  io.print "#{cell} is not a valid cell ID!\n"
end

#difficulty_level_messageObject



26
27
28
# File 'lib/ui.rb', line 26

def difficulty_level_message
  io.print "Select computer difficulty level: Enter 'easy' or 'hard.'\n"
end

#display_boardObject



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_messageObject



112
113
114
115
116
# File 'lib/ui.rb', line 112

def early_exit_message
  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 first_move_message(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 invalid_input_message(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_assigned_message(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 next_move_message(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 player_type_message(marker)
  io.print "For player " + "'#{marker}'," + " enter 'human' or 'computer.'\n"
end


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


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


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_levelObject



13
14
15
16
# File 'lib/ui.rb', line 13

def request_difficulty_level
  difficulty_level_message
  io.gets.chomp.downcase
end

#request_human_moveObject



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)
  player_type_message(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 taken_cell_message(cell)
  io.print "#{cell} has already been taken!\n"
end

#tie_game_messageObject



100
101
102
# File 'lib/ui.rb', line 100

def tie_game_message
  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_assigned_message(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 winning_game_message(player)
  io.print "GAME OVER! Player '#{player.marker}' wins!\n"
end