-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
46 lines (44 loc) · 1.11 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Game
def initialize(player, board)
@board = board
case player.mark
when 'X'
@player_x = player
@player_o = Player.new('O', "computer", @board)
when 'O'
@player_x = Player.new('X', "computer", @board)
@player_o = player
end
@before = @board.size - 1
@after = (((@board.size*@board.size)-2*@board.size + 2)/2)
@additional = 0
@additional += 1 if @board.size % 2 == 1
end
def play
@board.display
@before.times do
@player_x.turn
@player_o.turn
end
@after.times do
@player_x.turn
if @board.win?("X")
@player_x.win
return
end
@player_o.turn
if @board.win?("O")
@player_o.win
return
end
end
@additional.times do
@player_x.turn
if @board.win?("X")
@player_x.win
return
end
end
puts "This game is a draw."
end
end