Class: TTT::Interface::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/interface/cli.rb,
lib/ttt/interface/cli/views.rb,
lib/ttt/interface/cli/players.rb

Defined Under Namespace

Classes: ComputerPlayer, HumanPlayer, Player, Views

Constant Summary collapse

X =
'X'
O =
'O'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.



14
15
16
17
18
19
# File 'lib/ttt/interface/cli.rb', line 14

def initialize(options={})
  self.filein  = options.fetch :filein,  $stdin
  self.fileout = options.fetch :fileout, $stdout
  self.fileerr = options.fetch :fileerr, $stderr
  self.turn    = 0
end

Instance Attribute Details

#fileerrObject

Returns the value of attribute fileerr.



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

def fileerr
  @fileerr
end

#fileinObject

Returns the value of attribute filein.



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

def filein
  @filein
end

#fileoutObject

Returns the value of attribute fileout.



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

def fileout
  @fileout
end

#gameObject

Returns the value of attribute game.



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

def game
  @game
end

#player1Object

Returns the value of attribute player1.



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

def player1
  @player1
end

#player2Object

Returns the value of attribute player2.



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

def player2
  @player2
end

#turnObject

Returns the value of attribute turn.



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

def turn
  @turn
end

Instance Method Details

#boardObject



90
91
92
# File 'lib/ttt/interface/cli.rb', line 90

def board
  game.board
end

#create_gameObject



53
54
55
# File 'lib/ttt/interface/cli.rb', line 53

def create_game
  self.game = Game.new
end

#create_player(letter, position) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/ttt/interface/cli.rb', line 65

def create_player(letter, position)
  type = prompt "#{letter} will go #{position}, would you like #{letter} to be a human or a computer? (h/c) ", :validate => /^[hc]$/i
  if type =~ /c/i
    ComputerPlayer.new game, self, letter
  else
    HumanPlayer.new game, self, letter
  end
end

#create_player1Object



57
58
59
# File 'lib/ttt/interface/cli.rb', line 57

def create_player1
  self.player1 = create_player X, 'first'
end

#create_player2Object



61
62
63
# File 'lib/ttt/interface/cli.rb', line 61

def create_player2
  self.player2 = create_player O, 'second'
end

#current_playerObject



49
50
51
# File 'lib/ttt/interface/cli.rb', line 49

def current_player
  turn.even? ? player1 : player2
end

#display_boardObject



86
87
88
# File 'lib/ttt/interface/cli.rb', line 86

def display_board
  Views.new(self).display_board
end

#display_resultsObject



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

def display_results
  display_board
  if game.tie?
    puts "The game ended in a tie."
  else
    puts "Player #{game.winner} won the game."
  end
  puts "Play again soon :)"
end

#playObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ttt/interface/cli.rb', line 21

def play
  fileout.puts "Welcome to Tic Tac Toe"
  fileout.flush
  create_game
  create_player1
  create_player2
  until game.over?
    display_board          
    take_current_turn
  end
  display_results
end

#prompt(message, validation = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ttt/interface/cli.rb', line 74

def prompt(message, validation={})
  validation[:validate] ||= //
  fileout.print message
  input = filein.gets
  until input =~ validation[:validate]
    fileout.puts "Invalid, input."
    fileout.print message
    input = filein.gets
  end
  input
end

#take_current_turnObject



44
45
46
47
# File 'lib/ttt/interface/cli.rb', line 44

def take_current_turn
  current_player.take_turn
  self.turn += 1
end