mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Merge pull request #64 from rjnienaber/add_33_dice_in_ruby
Add games in ruby and touch up some others
This commit is contained in:
@@ -11,9 +11,7 @@ end
|
|||||||
def get_facets
|
def get_facets
|
||||||
while true
|
while true
|
||||||
number = gets.chomp
|
number = gets.chomp
|
||||||
if /^\d+$/.match(number)
|
return number.to_i if /^\d+$/.match(number)
|
||||||
return number.to_i
|
|
||||||
end
|
|
||||||
puts "!NUMBER EXPECTED - RETRY INPUT LINE"
|
puts "!NUMBER EXPECTED - RETRY INPUT LINE"
|
||||||
print "? "
|
print "? "
|
||||||
end
|
end
|
||||||
@@ -42,4 +40,6 @@ def main
|
|||||||
draw_diamonds(lines)
|
draw_diamonds(lines)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
trap "SIGINT" do puts; exit 130 end
|
||||||
|
|
||||||
main
|
main
|
||||||
51
33 Dice/ruby/dice.rb
Normal file
51
33 Dice/ruby/dice.rb
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
def intro
|
||||||
|
puts " DICE
|
||||||
|
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
THIS PROGRAM SIMULATES THE ROLLING OF A
|
||||||
|
PAIR OF DICE.
|
||||||
|
YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO
|
||||||
|
'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE
|
||||||
|
A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000.
|
||||||
|
|
||||||
|
"
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_rolls
|
||||||
|
while true
|
||||||
|
number = gets.chomp
|
||||||
|
return number.to_i if /^\d+$/.match(number)
|
||||||
|
puts "!NUMBER EXPECTED - RETRY INPUT LINE"
|
||||||
|
print "? "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dice_roll = rand(6) + 1 # ruby 3, woot!
|
||||||
|
|
||||||
|
def print_rolls(rolls)
|
||||||
|
values = Array.new(11, 0)
|
||||||
|
(1..rolls).each { values[dice_roll + dice_roll - 2] += 1 }
|
||||||
|
puts "\nTOTAL SPOTS NUMBER OF TIMES"
|
||||||
|
(0..10).each { |k| puts " %-2d %-2d" % [k + 2, values[k]] }
|
||||||
|
end
|
||||||
|
|
||||||
|
def main
|
||||||
|
intro
|
||||||
|
loop do
|
||||||
|
print "HOW MANY ROLLS? "
|
||||||
|
rolls = get_rolls
|
||||||
|
|
||||||
|
print_rolls(rolls)
|
||||||
|
|
||||||
|
print "\n\nTRY AGAIN? "
|
||||||
|
option = (gets || '').chomp.upcase
|
||||||
|
break unless option == 'YES'
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
trap "SIGINT" do puts; exit 130 end
|
||||||
|
|
||||||
|
main
|
||||||
15
78 Sine Wave/ruby/sinewave.rb
Normal file
15
78 Sine Wave/ruby/sinewave.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
def intro
|
||||||
|
puts " SINE WAVE
|
||||||
|
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
def main
|
||||||
|
intro
|
||||||
|
(0..40).step(0.25).each do |t|
|
||||||
|
a = (26 + 25 * Math.sin(t)).to_i
|
||||||
|
text = (t % 0.5) == 0 ? "CREATIVE" : "COMPUTING"
|
||||||
|
puts " " * a + text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
main
|
||||||
@@ -9,8 +9,8 @@ import math
|
|||||||
def equation(input):
|
def equation(input):
|
||||||
return 30 * math.exp(-input * input / 100)
|
return 30 * math.exp(-input * input / 100)
|
||||||
|
|
||||||
print(" " * 32 + "3D PLOT\n")
|
print(" " * 32 + "3D PLOT")
|
||||||
print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
|
print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n")
|
||||||
|
|
||||||
for x in range (-300, 315, 15):
|
for x in range (-300, 315, 15):
|
||||||
x1 = x / 10
|
x1 = x / 10
|
||||||
@@ -23,4 +23,4 @@ for x in range (-300, 315, 15):
|
|||||||
if z > l:
|
if z > l:
|
||||||
l = z
|
l = z
|
||||||
yPlot[z] = "*"
|
yPlot[z] = "*"
|
||||||
print("".join(yPlot) + "\n")
|
print("".join(yPlot))
|
||||||
28
87 3-D Plot/ruby/3dplot.rb
Normal file
28
87 3-D Plot/ruby/3dplot.rb
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
def intro
|
||||||
|
puts " 3D PLOT
|
||||||
|
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
def fna(z) = 30 * Math.exp(-z * z / 100)
|
||||||
|
|
||||||
|
def render
|
||||||
|
(-30..30).step(1.5).each do |x|
|
||||||
|
l = 0
|
||||||
|
y1 = 5 * (Math.sqrt(900 - x * x) / 5).to_i
|
||||||
|
y_plot = " " * 80
|
||||||
|
(y1..-y1).step(-5).each do |y|
|
||||||
|
z = (25 + fna(Math.sqrt(x * x + y * y)) - 0.7 * y).to_i
|
||||||
|
next if z <= l
|
||||||
|
l = z
|
||||||
|
y_plot[z] = '*'
|
||||||
|
end
|
||||||
|
puts y_plot
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def main
|
||||||
|
intro
|
||||||
|
render
|
||||||
|
end
|
||||||
|
|
||||||
|
main
|
||||||
@@ -27,9 +27,9 @@ def main
|
|||||||
intro
|
intro
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
car_speed = (Random.rand * 25 + 40).to_i
|
car_speed = rand(25) + 40
|
||||||
car_time = (Random.rand * 15 + 5).to_i
|
car_time = rand(15) + 5
|
||||||
train_speed = (Random.rand * 19 + 20).to_i
|
train_speed = rand(19) + 20
|
||||||
|
|
||||||
print " A CAR TRAVELING #{car_speed} MPH CAN MAKE A CERTAIN TRIP IN
|
print " A CAR TRAVELING #{car_speed} MPH CAN MAKE A CERTAIN TRIP IN
|
||||||
#{car_time} HOURS LESS THAN A TRAIN TRAVELING AT #{train_speed} MPH.
|
#{car_time} HOURS LESS THAN A TRAIN TRAVELING AT #{train_speed} MPH.
|
||||||
|
|||||||
Reference in New Issue
Block a user