Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Last active August 29, 2015 13:58
Show Gist options
  • Save davidpaulhunt/9963339 to your computer and use it in GitHub Desktop.
Save davidpaulhunt/9963339 to your computer and use it in GitHub Desktop.
# create racetrack
class RaceTrack
attr_reader :horses_racing, :length, :names, :now_playing, :winner
def initialize
@length = 75
@horses_racing = []
#let_them_race
end
def greet_the_user
add_horses
puts "Hello! Welcome to the Race Track.\nWe have 2 awesome horses for you to race against, #{@horse1.name} and #{@horse2.name} \n"
horses_racing.each do |a|
a.print_me
end
choose_a_name
end
def choose_a_name
puts "What do you want to call your horse?"
@users_horse_name = gets.chomp.capitalize
@users_horse = Horse.new(@users_horse_name)
@users_horse.make_user
@horses_racing << @users_horse
@users_horse_name # for testing only
end
def name_the_ai_horses
@names = ["Mark Sanchez", "Tim Tebow", "Joe Namath", "Mike Vick", "Chad Pennington", "Greg McElroy"]
@name = @names.sample
@names.delete_at(@names.index(@name))
@name
end
def add_horses
@horse1 = Horse.new(name_the_ai_horses)
@horse2 = Horse.new(name_the_ai_horses)
@horses_racing << @horse1
@horses_racing << @horse2
end
def start_race
puts "This is your horse #{@users_horse_name}"
#@users_horse.print_me
puts "When you're ready, press the enter key. Then, keep pressing the enter key to make your horse go!"
gets
@now_playing = true
end
def show_racetrack
system "clear"
horses_racing.each do |a|
a.print_me
end
end
def print_last_scene
system "clear"
@horses_racing.each do |a|
if a.position >= @length
a.print_me_winning
else
a.print_me
end
end
end
def get_user_command
gets
@horses_racing.each do |a|
a.run
end
end
def race
until @now_playing == false
show_racetrack
get_user_command
check_horse_position
#give_a_carrot
end
end
def check_horse_position
@horses_racing.each do |a|
if a.position >= @length
@winner = a.name
print_last_scene
#race_over
end
end
end
def race_over
puts "Wow! What an exciting race!"
puts "#{@winner} won that one."
race_again
end
def race_again
puts "Would you like to race again? Yes or No"
play_again = gets.chomp.downcase
if play_again == "yes"
RaceTrack.new
elsif play_again == "no"
quit
else
puts "Sorry, didn't understand that one."
race_again
end
end
def quit
puts "Thanks for playing!"
exit
end
def let_them_race
greet_the_user
start_race
race
# race_over
end
def give_a_carrot
carrot = rand(1..20)
horse = rand(3)
if carrot == rand(1..20) && @horses_racing[horse].position < 35
@horses_racing[horse].has_carrot = true
end
end
end
class Horse
attr_accessor :position, :name, :length, :is_user, :has_carrot, :my_carrot
def initialize(name)
@name = name
@position = 0
@is_user = false
@visual = ".-.^"
@has_carrot = false
@my_carrot = Carrot.new
end
def make_user
@is_user = true
@visual = ".-.+"
end
def run
@position += rand(1..2)
end
def make_the_horses_go
gets
@horses_racing.each do |a|
a.run
end
end
# # describe Race do
# it 'should get user input' do
# race = Race.new
# race.stubs(:gets).and_returns("Sugar lollypop")
# race.gets_user_input
# race.user_input.should eq("Sugar lollypop")
# end
# end
def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end
def print_me
carrot_check
end
def print_without_carrot
if @is_user == true
print " " * @position + red("#{@visual}") + " " * (75 - @position) + "\|\n"
else
print " " * @position + green("#{@visual}") + " " * (75 - @position) + "\|\n"
end
end
def carrot_check
if @has_carrot == true && @position < @my_carrot.position
print_with_carrot
elsif
print_without_carrot
end
end
def print_with_carrot
if @is_user == true
print " " * @position + red("#{@visual}") + " " * (@my_carrot.position - @position) + "~" + " " * (74 - @my_carrot.position) + "\|\n"
else
print " " * @position + green("#{@visual}") + " " * (@my_carrot.position - @position) + "~" + " " * (74 - @my_carrot.position) + "\|\n"
end
end
def print_me_winning
if @is_user == true
print " " * @position + red("#{@visual}") + "\n"
else
print " " * @position + green("#{@visual}") + "\n"
end
end
def get_carrot
if @position == @my_carrot.position - 1 && has_carrot == true
@has_carrot = false
eat_carrot
end
end
def eat_carrot
@position += @my_carrot.boost
@my_carrot.expire
end
# def lose_carrot
# if @position >
# end
# end
end
class Carrot
attr_accessor :boost, :position, :visual
def initialize
@position = rand(20..35)
@boost = 5
@visual = "~"
end
end
RaceTrack.new
require 'rspec'
require './racetrack.rb'
system 'clear'
## Test the racetrack
describe RaceTrack do
let (:race) { RaceTrack.new }
it 'should exist' do
race.should_not eq(nil)
end
it 'should generate two horses' do
race.add_horses.should_not eq(nil)
end
it 'should not be empty' do
race.add_horses
race.horses_racing.should_not be_empty
end
it 'should evaluate to a name' do
race.name_the_ai_horses.should_not eq(nil)
end
it 'should create a new horse and push to horses_racing' do
temp = race.horses_racing.length
race.stub(:gets).and_return("Bob")
race.choose_a_name.should eq("Bob")
race.horses_racing.length.should_not eq(temp)
end
it 'should set now playing to true' do
race.start_race
race.now_playing.should eq(true)
end
it 'should make the horses run' do
race.add_horses
race.stub(:gets).and_return("Bob")
race.choose_a_name
temp = race.horses_racing[0].position
race.stub(:gets).and_return("")
race.get_user_command
race.horses_racing[2].position.should > 0
end
it 'should choose a winner' do
race.add_horses
race.horses_racing[0].position = 75
race.check_horse_position
race.winner.should_not eq(nil)
end
it 'should check for new game' do
race.stub(:gets).and_return("yes")
race.race_again.should_not eq(nil)
end
end
describe Horse do
let (:race) { RaceTrack.new }
it 'should cause the horses to run' do
race.add_horses
race.choose_a_name
temp = race.horses_racing[0].position
race.horses_racing[0].run
race.horses_racing[0].position.should_not eq(temp)
end
end
@switzersc
Copy link

You don't need to call .initialize on .new

Your racetrack class does a lot of things that are not the responsibility of the race track. like colorizing a horse and making a horse go. a racetrack does not move a horse. the horse needs to move on its own. it should also probably handle its own coloring.

slow down on this and go back to thinking about objects. You essentially have one long process inside this racetrack object. i dont see any object interaction, and i dont see responsibilities of objects clearly defined and separated into their respective classes. does a racetrack actually check horse position? probably not.

@davidpaulhunt
Copy link
Author

for sure you're totally right. i'm just trying to go with the whole make it work, make it better strategy, with git being my record for revisions.

i promise it won't be so horrible come monday :/

@switzersc
Copy link

Your spec for the describe Horse block is actually testing the behavior and expectations of the racetrack -- this is even evident of the spec message "it should cause the horses to run." if you're testing a horse, then an appropriate spec would be "it should run", and then you test if the horse's position can change through the appropriate method.

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