Module: TicTacToe

Defined in:
lib/tic_tac_toe_mchliakh.rb,
lib/tic_tac_toe_mchliakh/version.rb,
lib/tic_tac_toe_mchliakh/board/line.rb,
lib/tic_tac_toe_mchliakh/board/board.rb,
lib/tic_tac_toe_mchliakh/board/lines.rb,
lib/tic_tac_toe_mchliakh/board/square.rb,
lib/tic_tac_toe_mchliakh/board/squares.rb,
lib/tic_tac_toe_mchliakh/players/player.rb,
lib/tic_tac_toe_mchliakh/players/computer.rb

Defined Under Namespace

Classes: Board, Computer, IllegalMoveError, Line, Lines, Player, Square, Squares

Constant Summary collapse

VERSION =
'0.1.6'

Class Method Summary collapse

Class Method Details

.move(square, saved_board = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tic_tac_toe_mchliakh.rb', line 10

def self.move(square, saved_board=nil)
  square = square.to_i
  saved_board.map! {|s| s.to_i if s } if saved_board

  board = Board.new(saved_board)
  board.square(square).take(1)

  if board.game_over?
    return { board: board.serialize, winner: board.winner }
  end

  computer = Computer.new(board, 0)
  computer.next_move.take(0)

  if board.game_over?
    { board: board.serialize, winner: board.winner }
  else
    { board: board.serialize }
  end
rescue IllegalMoveError => e
  { error: e.message }
end