-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
58 lines (48 loc) · 1.1 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
47
48
49
50
51
52
53
54
55
56
57
58
require 'player'
require 'board'
require 'piece'
class Game
NUM_PLAYERS = 2
attr_accessor :pieces, :board, :state
def initialize
@player0 = Player.new(0)
@player1 = Player.new(1)
@player0.opponent = @player1
@player1.opponent = @player0
@board = Board.new(NUM_PLAYERS)
@pieces = []
@state = :setup
@active_player = self.player0
# Place stones on the board in starting positions
1.upto(self.board.width) do |col|
BlackStone.new(self.player0, self.board[col, 2])
BlackStone.new(self.player1, self.board[col, 6])
end
RedStone.new(self.player0, self.board[2, 1])
RedStone.new(self.player0, self.board[6, 1])
RedStone.new(self.player1, self.board[2, 7])
RedStone.new(self.player1, self.board[6, 7])
end
def win(player)
@state = player == @player0 ? :win_player0 : :win_player1
end
def player(num)
if num == 0
@player0
elsif num == 1
@player1
else
nil
end
end
def player0
player(0)
end
def player1
player(1)
end
# Convenience method
def move(c1, r1, c2, r2)
self.board[c1, r1].piece.move(self.board[c2, r2])
end
end