Skip to content

Commit

Permalink
Added a count for the players pieces so that winning text knows whos …
Browse files Browse the repository at this point in the history
…won.
  • Loading branch information
Sarah Tattersall committed Nov 6, 2011
1 parent cac529f commit 731d90c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
1 change: 0 additions & 1 deletion board.rb
Expand Up @@ -70,7 +70,6 @@ def get_flippable_pieces(x, y, x_offset, y_offset, p)
return []
end

#TODO: this
def in_bounds?(x, y)
return valid_row?(x) && valid_row?(y)
end
Expand Down
4 changes: 3 additions & 1 deletion cell.rb
Expand Up @@ -15,15 +15,17 @@ def inspect
end

def flip(p)
@owner.decrement_count
@owner = p
@owner.increment_count
end

def is_occupied?
return @owner != nil
end

def owned_by?(p)
return @owner == p
return @owner == p
end

def set_value(v)
Expand Down
38 changes: 34 additions & 4 deletions game.rb
Expand Up @@ -3,10 +3,24 @@
require './ai_player.rb'

class Game
def initialize
@board = Board.new
@players = [HumanPlayer.new(Player::BLACK),
AIPlayer.new(Player::WHITE)]
def initialize
@board = Board.new
@players = []
begin
puts "Please enter the number of human players you wish to play (1 or 2)"
count = (gets).to_i
if count == 0 || count > 2
puts "Not a valid input. Please try again"
next
end
end while count == 0 || count > 2

@players << HumanPlayer.new(Player::BLACK)
if count == 1:
@players << AIPlayer.new(Player::WHITE)
else
@players << HumanPlayer(Player::BLACK)
end
place_initial_pieces
end

Expand All @@ -23,6 +37,22 @@ def play
end
player = 1 - player
end
if draw?
puts "No more moves! It's a draw!"
else
puts "No more moves! Player #{get_winner} won!"
end
end

def draw?
return @players[0].get_count == @players[1].get_count
end

def get_winner
if @players[0].get_count > @players[1].get_count
return @players[0]
end
return @players[1]
end

def place_initial_pieces
Expand Down
15 changes: 15 additions & 0 deletions player.rb
Expand Up @@ -4,8 +4,22 @@ class Player

def initialize(c)
@color = c
@count = 0
end

def increment_count
@count += 1
end

def decrement_count
@count -= 1
end

def get_count
return @count
end


def can_move?(board)
for i in (0..(board.size-1))
for j in (0..(board.size-1))
Expand All @@ -26,4 +40,5 @@ def symbol
def to_s
return symbol
end

end

0 comments on commit 731d90c

Please sign in to comment.