class CommandLine::Display

Public Class Methods

choose_cell(choice = nil) click to toggle source
# File lib/ttt-cli/display.rb, line 45
def self.choose_cell(choice = nil)
  moves = Array(1..9).map(&:to_s)
  while !moves.include?(choice) && choice != 'exit'
    print Rainbow('What is your move? (1-9): ').lawngreen
    choice = STDIN.gets.strip
  end
  exit if choice == 'exit'
  choice.to_i - 1
end
clear() click to toggle source
# File lib/ttt-cli/display.rb, line 18
def self.clear
  system('clear') || system('cls')
end
difficulty() click to toggle source
# File lib/ttt-cli/display.rb, line 22
def self.difficulty
  levels = %w[easy medium hard]
  prompt.select(
    'Select a difficulty level:',
    levels, symbols: { marker: '>' }
  )
end
draw() click to toggle source
# File lib/ttt-cli/display.rb, line 63
def self.draw
  puts Rainbow("\tDraw!\n").yellow
end
game_mode() click to toggle source
# File lib/ttt-cli/display.rb, line 30
def self.game_mode
  modes = %w[singleplayer hotseat observer]
  prompt.select(
    'Choose game mode:',
    modes, symbols: { marker: '>' }
  )
end
loser(game) click to toggle source
# File lib/ttt-cli/display.rb, line 71
def self.loser(game)
  if game.game_mode == :singleplayer
    puts Rainbow("\tYou Lose!\n").red
  else
    winner(game)
  end
end
play_again() click to toggle source
# File lib/ttt-cli/display.rb, line 67
def self.play_again
  prompt.yes?('Would you to play again?')
end
print_board(board) click to toggle source
prompt() click to toggle source
# File lib/ttt-cli/display.rb, line 9
def self.prompt
  TTY::Prompt.new(interrupt: :exit)
end
scoreboard(game) click to toggle source
# File lib/ttt-cli/display.rb, line 86
def self.scoreboard(game)
  table = TTY::Table.new [game.first_player.name, '  Tie  ', game.second_player.name],
                         [[game.wins, game.draws, game.losses]]
  scoreboard = table.render :unicode, alignment: [:center]
  puts Rainbow(scoreboard).lawngreen
  puts
end
user_token(tokens = %w[X O]) click to toggle source
# File lib/ttt-cli/display.rb, line 38
def self.user_token(tokens = %w[X O])
  prompt.select(
    'Do you want to be X or O?',
    tokens, symbols: { marker: '>' }
  )
end
welcome_banner() click to toggle source
# File lib/ttt-cli/display.rb, line 13
def self.welcome_banner
  clear
  puts Rainbow("Welcome to Tic Tac Toe!\n").lawngreen
end
winner(game) click to toggle source
# File lib/ttt-cli/display.rb, line 55
def self.winner(game)
  if game.game_mode == :singleplayer
    puts Rainbow("\tYou Win!\n").deepskyblue
  else
    puts Rainbow("#{game.current_player.name} has won the game!\n").deepskyblue
  end
end