Class: OXO::Application

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

Constant Summary collapse

ERRORCODE =
{ general: 1, usage: 2 }.freeze

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



99
100
101
102
103
104
105
106
107
# File 'lib/oxo.rb', line 99

def initialize
  begin
    options = Optionparser.parse!(ARGV)
  rescue => e
    usage_fail(e.message)
  end

  @delay = options[:delay]
end

Instance Method Details

#runObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/oxo.rb', line 109

def run
  board = Board.new

  case beginning_side
  when :quit
    return
  when :human
    players = [Human.new(:X), Computer.new(:O, delay: @delay)]
    puts board
  when :computer
    players = [Computer.new(:X, delay: @delay), Human.new(:O)]
  end

  game_over = false

  until game_over
    player = players.first
    move = player.make_move(board)

    break  if move == :quit

    board.place(player.color, move[0], move[1])

    puts board
    puts "Move of #{player.name} was: #{(move[0] - 1) * 3 + move[1]}"

    if board.win?(player.color)
      puts "#{player.name} wins!"
      game_over = true
    elsif board.full?
      puts "It's a draw."
      game_over = true
    end

    players.rotate!
  end
end