Skip to content

Instantly share code, notes, and snippets.

@apgordon
Created March 2, 2014 16:33
Show Gist options
  • Select an option

  • Save apgordon/9309171 to your computer and use it in GitHub Desktop.

Select an option

Save apgordon/9309171 to your computer and use it in GitHub Desktop.
Using an array to re-/define possession (Text Hockey)
=begin
#First 4 numbers in the array represent skaters. Last 2 represent goalies.
#To see who has the puck, find the index in the array that has 1.
arr = [1,0,0,0,0,0]
puts "Player #{arr.find_index(1)} has the puck." ==> Player 0 has the puck.
#Faceoff to determine who has the puck. Either p0 through p4.
arr.map! {|x| x=0} #First reset all to 0. ==> [0,0,0,0,0,0]
arr[rand(4)]=1 #Then assign possession to one of the 4 players.
puts "Player #{arr.find_index(1)} has the puck."
=end
class Player
@@poss = [0,0,1,0,0,0]
def initialize (name)
@name = name
end
def poss_who
p @@poss
puts "Player #{@@poss.find_index(1)} has the puck."
end
def poss_change
@@poss.map! {|x| x=0}
@@poss[0]=1
end
end
p0 = Player.new("Andrew")
p0.poss_who
p0.poss_change
p0.poss_who
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment