-
-
Save marktabler/4981192 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment