[ create a new paste ] login | about

Link: http://codepad.org/ljKa0M6X    [ raw code | fork | 1 comment ]

joshua_cheek - Ruby, pasted on Oct 29:
#initialize variables / constants
print "How many dice do you wish to play with? "
NUMBER_OF_DICE  =  gets.to_i
FAIL_VALUE      =  1
score , turns , rolls  = 0 , 0 , Array.new(NUMBER_OF_DICE)

dice = <<-DICE.split("\n").map{ |line| line.scan /.{7}/ }.transpose
+-----++-----++-----++-----++-----++-----+
|     ||*    ||*    ||*   *||*   *||*   *|
|  *  ||     ||  *  ||     ||  *  ||*   *|
|     ||    *||    *||*   *||*   *||*   *|
+-----++-----++-----++-----++-----++-----+
DICE

dice.unshift nil #put nil in slot 0, causing all dice to align on their index (ie die 1 in index 1)


#play the game
loop do

  #roll and display dice
  rolls.map!{ rand(6).next }
  dice.values_at(*rolls).transpose.each{ |line| puts line.join('   ') }
  
  #calculate and display results of turn
  turns += 1
  if rolls.include? FAIL_VALUE
    puts "You rolled a #{FAIL_VALUE}. You lose!"
    break
  end
  puts "Turn #{turns} Score is: #{ score += rolls.inject(0){|s,c|s+c} }"

  #check for next turn
  print "\nWould you like to Roll or Pass? "
  break if gets =~ /pass/i

end

#post game readout
puts "Turns taken: #{turns}" , "Final Score is: #{score}" , 'Bye!'


Create a new paste based on this one


Comments:
posted by joshua_cheek on Nov 15
My solution to some dice game someone had as homework that they posted about on the Ruby mailing list.
reply