Class: CLI::CLIPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output) ⇒ CLIPresenter

Returns a new instance of CLIPresenter.



4
5
6
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 4

def initialize(input, output)
  self.io = CLIO.new(input, output)
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



3
4
5
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 3

def io
  @io
end

Instance Method Details

#ai_difficulty_prompt(ai_options) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 128

def ai_difficulty_prompt(ai_options)
  clear
  output "\nChoose a difficulty level for the computer:"
  ai_options.inject(1) do |index, difficulty|
    output "#{index}. #{difficulty.to_s.gsub(/TTT::AI/, '')}"
    index += 1
  end
end

#board_selection_prompt(boards) ⇒ Object



110
111
112
113
114
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 110

def board_selection_prompt(boards)
  clear
  output "\nPlease select which board you'd like to play:\n"
  display_options_with_index(boards)
end

#clearObject



122
123
124
125
126
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 122

def clear
  system('clear')
  #output "\e[2J"
  #output "\e[0;0H"
end

#command_listObject



24
25
26
27
28
29
30
31
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 24

def command_list
  output "\nPlease select an option from the following list:"
  padding
  output "1. Play TTT!\n\n"
  output "2. Load game\n\n"
  output "3. Quit\n\n"
  padding
end

#display_options_with_index(options) ⇒ Object



116
117
118
119
120
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 116

def display_options_with_index(options)
  options.each_with_index do |option, index|
    output "#{index + 1}: #{option}\n\n"
  end
end

#draw_promptObject



183
184
185
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 183

def draw_prompt
  output "\nIt's a draw!"
end

#errorObject



175
176
177
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 175

def error
  output "\nI'm sorry, I didn't understand. Please try again."
end

#four_by_four_move_promptObject



207
208
209
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 207

def four_by_four_move_prompt
  output("\n  0  |  1  |  2  |  3  \n-----------------------\n  4  |  5  |  6  |  7  \n-----------------------\n  8  |  9  | 10  | 11  \n-----------------------\n 12  | 13  | 14  | 15   ")
end

#inputObject



16
17
18
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 16

def input
  io.input
end


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 33

def menu
  welcome_prompt
  command_list
  selection = input.chomp
  if valid_menu?(selection)
    selection
  else
    error
    menu
  end
end

#no_gamesObject



63
64
65
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 63

def no_games
  output "No games were found."
end

#output(args) ⇒ Object



8
9
10
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 8

def output(args)
  io.output args
end

#output_3d(board) ⇒ Object



195
196
197
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 195

def output_3d(board)
  output("\n  #{board[0]}  |  #{board[1]}  |  #{board[2]}       #{board[9]}  |  #{board[10]}  |  #{board[11]}       #{board[18]}  |  #{board[19]}  |  #{board[20]}   \n-----------------   -----------------   -----------------\n  #{board[3]}  |  #{board[4]}  |  #{board[5]}       #{board[12]}  |  #{board[13]}  |  #{board[14]}       #{board[21]}  |  #{board[22]}  |  #{board[23]}   \n-----------------   -----------------   -----------------\n  #{board[6]}  |  #{board[7]}  |  #{board[8]}       #{board[15]}  |  #{board[16]}  |  #{board[17]}       #{board[24]}  |  #{board[25]}  |  #{board[26]}  ")
end

#output_board(board) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 149

def output_board(board)
  output "\n"
  case board.length
  when 9
    output_three_by_three(board)
  when 16
    output_four_by_four(board)
  when 27
    output_3d(board)
  end
end

#output_four_by_four(board) ⇒ Object



191
192
193
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 191

def output_four_by_four(board)
  output("\n  #{board[0]}  |  #{board[1]}  |  #{board[2]}  |  #{board[3]}  \n-----------------------\n  #{board[4]}  |  #{board[5]}  |  #{board[6]}  |  #{board[7]}  \n-----------------------\n  #{board[8]}  |  #{board[9]}  |  #{board[10]}  |  #{board[11]}  \n-----------------------\n  #{board[12]}  |  #{board[13]}  |  #{board[14]}  |  #{board[15]}   ")
end

#output_help(board) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 161

def output_help(board)
  clear
  output "\nMove options:"
  case board.length
  when 9
   regular_move_prompt
  when 16
   four_by_four_move_prompt
  when 27
    three_d_prompt
  end
  output "\n"
end

#output_move_history(moves) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 84

def output_move_history(moves)
  output "\n"
  output "Move History:".rjust(10)
  move_num = "#"
  move_side = "side"
  move_square = "square"
  output "#{move_num} #{move_side.rjust(7)} #{move_square.rjust(7)}"
  moves.each_with_index do |move, n|
    output "#{n}. #{(move.side).to_s.rjust(5)}  #{(move.move).to_s.rjust(5)}"
  end
end

#output_three_by_three(board) ⇒ Object



187
188
189
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 187

def output_three_by_three(board)
  output("\n  #{board[0]}  |  #{board[1]}  |  #{board[2]}  \n-----------------\n  #{board[3]}  |  #{board[4]}  |  #{board[5]}  \n-----------------\n  #{board[6]}  |  #{board[7]}  |  #{board[8]}  ")
end

#paddingObject



12
13
14
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 12

def padding
  output "\n"
end

#play_again_msgObject



141
142
143
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 141

def play_again_msg
  output "\nPlay again (y or n)?"
end

#player_prompt(board, history, player) ⇒ Object



77
78
79
80
81
82
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 77

def player_prompt(board, history, player)
  output_help(board)
  output_move_history(history)
  output_board(board)
  output "\n#{player.prompt}"
end

#player_type_prompt(num, players) ⇒ Object



67
68
69
70
71
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 67

def player_type_prompt(num, players)
  output "\nPlease select an option for Player #{num}:\n\n"
  display_options_with_index(players)
  input
end

#post_game_msgObject



137
138
139
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 137

def post_game_msg
  output "\nWould you like to review the game history (y or n)?"
end

#process_draw(board, history) ⇒ Object



103
104
105
106
107
108
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 103

def process_draw(board, history)
  clear
  output_move_history(history)
  output_board(board)
  draw_prompt
end

#process_game_list(list) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 49

def process_game_list(list)
  clear
  puts "Please choose one of the following games:\n\n"
  list.each do |key|
    output key
  end
  output "\n"
  input.chomp
end

#process_winner(board, history, player) ⇒ Object



96
97
98
99
100
101
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 96

def process_winner(board, history, player)
  clear
  output_move_history(history)
  output_board(board)
  winner_prompt(player)
end

#regular_move_promptObject



203
204
205
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 203

def regular_move_prompt
  output("\n  0  |  1  |  2  \n-----------------\n  3  |  4  |  5  \n-----------------\n  6  |  7  |  8  ")
end

#select_game_promptObject



73
74
75
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 73

def select_game_prompt
  output "\nPlease choose one of the following games:\n\n"
end

#sort_list(list) ⇒ Object



59
60
61
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 59

def sort_list(list)
  list.map(&:to_i).sort.map(&:to_s)
end

#three_d_promptObject



199
200
201
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 199

def three_d_prompt
  output("\n  0  |  1  |  2       9  |  10 |  11      18 |  19 |  20  \n-----------------   -----------------   -----------------\n  3  |  4  |  5       12 |  13 |  14      21 |  22 |  23 \n-----------------   -----------------   -----------------\n  6  |  7  |  8       15 |  16 |  17      24 |  25 |  26 ")
end

#valid_menu?(selection) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 45

def valid_menu?(selection)
  selection.to_i >= 1 && selection.to_i <= 3
end

#walk_msgObject



145
146
147
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 145

def walk_msg
  output "\nEnter -1 to go back, 0 for main menu, or 1 to go forward"
end

#welcome_promptObject



20
21
22
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 20

def welcome_prompt
  output "Welcome to TTT!"
end

#winner_prompt(winner) ⇒ Object



179
180
181
# File 'lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb', line 179

def winner_prompt(winner)
  output "\n#{winner} is the winner!"
end