Created
June 10, 2013 03:48
-
-
Save ayoformayo/5746424 to your computer and use it in GitHub Desktop.
This is the revamped edition of ruby Racer.
This file contains 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 Die | |
def initialize(sides = 6) | |
@sides = sides | |
end | |
# Remember: rand(N) randomly returns one of N consecutive integers, starting at 0 | |
# So rand(N) returns a random integer in (0..N-1) | |
# And 1 + rand(N) returns a random integer in (1..N) | |
# See: http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-rand | |
def roll | |
1 + rand(@sides) | |
end | |
end | |
# Use "reputs" to print over a previously printed line, | |
# assuming the cursor is positioned appropriately. | |
def reputs(str = '') | |
puts "\e[0K" + str | |
end | |
# Clear the screen | |
def clear_screen! | |
print "\e[2J" | |
end | |
# Moves cursor to the top left of the terminal | |
def move_to_home! | |
print "\e[H" | |
end | |
# Flushes the STDOUT buffer. | |
# By default STDOUT is only flushed when it encounters a newline (\n) character | |
def flush! | |
$stdout.flush | |
end |
This file contains 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
require_relative 'racer_utils' | |
class RubyRacer | |
attr_reader :players, :length | |
def initialize(players, length = 30) | |
@players = players | |
@length = length | |
@player_position = Hash[players.map{|player| [player,0]}] | |
@board = Hash.new | |
@dice = Die.new | |
@winner = nil | |
end | |
# Returns +true+ if one of the players has reached | |
# the finish line, +false+ otherwise | |
def finished? | |
@finished | |
end | |
# Returns the winner if there is one, +nil+ otherwise | |
def winner(player = @player) | |
if @player_position[player] == @length - 1 | |
@finished = true | |
@winner = player | |
else | |
@winner | |
end | |
end | |
# Rolls the dice and advances +player+ accordingly | |
def advance_player!(player) | |
@player_position[player] += @dice.roll | |
if @player_position[player] > @length - 1 | |
@player_position[player] = @length -1 | |
clear_screen! | |
print_board | |
end | |
winner(player) | |
end | |
# Prints the current game board | |
# The board should have the same dimensions each time | |
# and you should use the "reputs" helper to print over | |
# the previous board | |
def print_board | |
@players.each do |player| | |
print " |" * @player_position[player] | |
reputs(player + "|" + " |" * (@length - @player_position[player] - 1)) | |
end | |
end | |
end | |
players = ['a', 'b'] | |
game = RubyRacer.new(players) | |
# This clears the screen, so the fun can begin | |
clear_screen! | |
until game.finished? | |
players.each do |player| | |
players.each do |player| | |
@player = player | |
move_to_home! | |
game.print_board | |
game.advance_player!(@player) | |
sleep(0.2) | |
end | |
end | |
end | |
# The game is over, so we need to print the "winning" board | |
puts "Player '#{game.winner}' has won!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment