Skip to content

Instantly share code, notes, and snippets.

Created February 18, 2013 20:39
Show Gist options
  • Save anonymous/4980502 to your computer and use it in GitHub Desktop.
Save anonymous/4980502 to your computer and use it in GitHub Desktop.
class Board
def initialize
@squares = {}
101.times do |n|
@squares[n] = n
end
@squares.delete(0)
set_active_squares
end
def set_active_squares
@squares[1] = 38
@squares[4] = 14
@squares[9] = 31
@squares[16] = 6
@squares[28] = 84
@squares[36] = 44
@squares[40] = 42
@squares[47] = 26
@squares[49] = 11
@squares[51] = 67
@squares[56] = 53
@squares[62] = 19
@squares[64] = 60
@squares[71] = 91
@squares[80] = 100
@squares[87] = 24
@squares[93] = 73
@squares[95] = 75
@squares[98] = 78
end
def check_square(position)
if position > 100
100
else
@squares[position]
end
end
end
class Game
def initialize
@board = Board.new
end
def play_round(players)
players.each do |player|
puts player.name
player.play_turn(@board)
check_for_win(player)
end
end
def check_for_win(player)
if player.position == 100
puts "#{player.name} Wins!"
Process.exit
end
end
def play_game(players)
50.times do
play_round(players)
end
end
end
class Player
attr_accessor :name, :position
def initialize(name)
@name = name
@position = 0
end
def play_turn(board)
roll = roll_dice
@position = @position += roll
puts "position after roll is: #{@position}"
@position = board.check_square(@position)
puts "position after check_square is: #{@position}"
end
def roll_dice
rand(1..6)
end
end
@game = Game.new
@players = [Player.new("mike"), Player.new("beth"), Player.new("duncan")]
@game.play_game(@players)
@bethtabler
Copy link

class Board

def initialize(number_of_players)
@squares = []
@tokens = []
100.times do
@squares << Square.new
end
set_snakes_and_ladders
number_of_players.times do
@tokens << Token.new
place_token(@tokens.last, 0)
end
end

def place_token(token, position)
token.position = position
end

def move_token(token, spaces)
token.position += spaces
if token.position > @squares.size
token.position = @squares.size
end
check_for_jump(token)
end

def check_for_win(token)
if token.position == 100
win_game(token)
end
end

def win_game(token)
puts "Congratulations, #{token.player_name}! You win!"
exit
end

def check_for_jump(token)
if @squares[token.position].destination
token.position = @squares[token.position].destination
end
end

def roll_die
rand(6) + 1
end

def set_snakes_and_ladders
@squares[7].destination = 25
@squares[21].destination = 81
# etc etc etc
end
end

class Square
attr_accessor :destination
end

class Token
attr_accessor :position, :player_name
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment