Class: CommandLine::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt-cli/display.rb

Class Method Summary collapse

Class Method Details

.choose_cell(choice = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# 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

.clearObject



18
19
20
# File 'lib/ttt-cli/display.rb', line 18

def self.clear
  system('clear') || system('cls')
end

.difficultyObject



22
23
24
25
26
27
28
# 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

.drawObject



63
64
65
# File 'lib/ttt-cli/display.rb', line 63

def self.draw
  puts Rainbow("\tDraw!\n").yellow
end

.game_modeObject



30
31
32
33
34
35
36
# 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

.logoObject



79
80
81
82
83
84
# File 'lib/ttt-cli/display.rb', line 79

def self.
  "
┌───────────────┐
│  Tic-Tac-Toe  │
└───────────────┘"
end

.loser(game) ⇒ Object



71
72
73
74
75
76
77
# 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_againObject



67
68
69
# File 'lib/ttt-cli/display.rb', line 67

def self.play_again
  prompt.yes?('Would you to play again?')
end


94
95
96
97
98
99
# File 'lib/ttt-cli/display.rb', line 94

def self.print_board(board)
  clear
  puts Rainbow().lawngreen
  puts board
  scoreboard(board.game)
end

.promptObject



9
10
11
# File 'lib/ttt-cli/display.rb', line 9

def self.prompt
  TTY::Prompt.new(interrupt: :exit)
end

.scoreboard(game) ⇒ Object



86
87
88
89
90
91
92
# 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]) ⇒ Object



38
39
40
41
42
43
# 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_bannerObject



13
14
15
16
# 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) ⇒ Object



55
56
57
58
59
60
61
# 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