Class: TictactoeJ8th::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, player1, player2) ⇒ Game

Returns a new instance of Game.



6
7
8
9
10
11
12
# File 'lib/tictactoe_j8th/game.rb', line 6

def initialize(board, player1, player2)
  @board = board
  @player1 = player1
  @player2 = player2

  @playerup = @player1
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



4
5
6
# File 'lib/tictactoe_j8th/game.rb', line 4

def board
  @board
end

Instance Method Details

#game_over?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/tictactoe_j8th/game.rb', line 14

def game_over?
  return true if @board.full? or not winner.nil?
  false
end

#playObject



37
38
39
40
# File 'lib/tictactoe_j8th/game.rb', line 37

def play
  turn
  play unless game_over?
end

#player_upObject



42
43
44
# File 'lib/tictactoe_j8th/game.rb', line 42

def player_up
  
end

#turn(spot = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/tictactoe_j8th/game.rb', line 27

def turn(spot = nil)
  if spot.nil?
    spot = @playerup.move(@board)
  else
    @board.place(@playerup.token, spot)
  end
  @playerup = @playerup == @player1 ? @player2 : @player1
  spot
end

#winnerObject



19
20
21
22
23
24
25
# File 'lib/tictactoe_j8th/game.rb', line 19

def winner
  @board.lines.each do |line|
    return @player1.token if line.values.all? { |v| v == @player1.token }
    return @player2.token if line.values.all? { |v| v == @player2.token }
  end
  nil
end