From 82a126bce36b1c3aea47fbaa017953968cd968c7 Mon Sep 17 00:00:00 2001 From: Mark Wieder Date: Mon, 10 Jan 2022 10:18:24 -0800 Subject: [PATCH 1/2] initial attempt --- 65_Nim/ruby/nim.rb | 282 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 65_Nim/ruby/nim.rb diff --git a/65_Nim/ruby/nim.rb b/65_Nim/ruby/nim.rb new file mode 100644 index 00000000..a326695f --- /dev/null +++ b/65_Nim/ruby/nim.rb @@ -0,0 +1,282 @@ +puts "NIM".center(80) +puts"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".center(80) +puts "\n\n\n" + +#210 DIM A(100),B(100,10),D(2) +$pileArray = Array.new[100] +$bArray = Array.new +$dArray = Array.new[2] +$winOption = 0 # take-last option +$numberOfPiles = 1 +$c = 0 +$e = 0 +$f = 0 +$g = 0 +$h = 0 + +def displayTheRules +puts "THE GAME IS PLAYED WITH A NUMBER OF PILES OF OBJECTS." +puts "ANY NUMBER OF OBJECTS ARE REMOVED FROM ONE PILE BY YOU AND" +puts "THE MACHINE ALTERNATELY. ON YOUR TURN, YOU MAY TAKE" +puts "ALL THE OBJECTS THAT REMAIN IN ANY PILE, BUT YOU MUST" +puts "TAKE AT LEAST ONE OBJECT, AND YOU MAY TAKE OBJECTS FROM" +puts "ONLY ONE PILE ON A SINGLE TURN. YOU MUST SPECIFY WHETHER" +puts "WINNING IS DEFINED AS TAKING OR NOT TAKING THE LAST OBJECT," +puts "THE NUMBER OF PILES IN THE GAME, AND HOW MANY OBJECTS ARE" +puts "ORIGINALLY IN EACH PILE. EACH PILE MAY CONTAIN A" +puts "DIFFERENT NUMBER OF OBJECTS." +puts "THE MACHINE WILL SHOW ITS MOVE BY LISTING EACH PILE AND THE" +puts "NUMBER OF OBJECTS REMAINING IN THE PILES AFTER EACH OF ITS" +puts "MOVES." +end + +def sub1570 + $z=0 + for i in 1..$numberOfPiles do + if $pileArray[i] != 0 then + return + end + $z=1 + return + end +end + +def playAnother + put "do you want to play another game"; + return gets.strip.ucase == "YES" +end +puts "THIS IS THE GAME OF NIM." +print "DO YOU WANT INSTRUCTIONS?" +240 +wantInstructions = gets.strip.upcase +if wantInstructions == "YES" then + displayTheRules +end +#250 IF Z$="NO" THEN 440 +#260 IF Z$="no" THEN 440 +#270 IF Z$="YES" THEN displayTheRules +#280 IF Z$="yes" THEN displayTheRules +#290 PRINT "PLEASE ANSWER YES OR NO" +#300 GOTO 240 + +def sub490 # get number of piles + print "ENTER NUMBER OF PILES:" + while $numberOfPiles < 0 && $numberOfPiles <= 100 do + $numberOfPiles = gets.strip.to_i + end +end + +def getPileSizes + puts "ENTER PILE SIZES:" + for i in 1..$numberOfPiles do + print i + while true do + $pileArray[i] = gets.strip.to_i + if $pileArray[i] < 2000 && $pileArray[i] > 0 then + break + end + end + end +end + +def sub440 # get win option + puts "" + $winOption = 0 + while $winOption != 1 && q != 2 do + puts "ENTER WIN OPTION - 1 TO TAKE LAST, 2 TO AVOID LAST" + $winOption = gets.strip.to_i + end +end + +puts "DO YOU WANT TO MOVE FIRST?"; +#630 INPUT Q9$ +moveFirst = "" +while moveFirst != "YES" && moveFirst != "NO" do + moveFirst = gets.strip.upcase + case moveFirst + when "YES" + yourMove + when "NO" + machineMove + end +end + +#640 IF Q9$="YES" THEN 1450 +#650 IF Q9$="yes" THEN 1450 +#660 IF Q9$="NO" THEN 700 +#670 IF Q9$="no" THEN 700 +#680 PRINT "PLEASE ANSWER YES OR NO." +#690 GOTO 630 + +def machineMove + if $winOption == 1 then + sub940 # take last + end + $c=0 + for i in 1..$numberOfPiles do + if $pileArray[i] != 0 then 770 + $c=$c+1 + if $c == 3 then + sub840 + end + $dArray[$c-1]=i + end + end + + if $c == 2 then + sub920 + end + if $pileArray[$dArray[0]] > 1 then + machineWins + end + machineLoses +end + +def machineLoses + puts "MACHINE LOSES" +# 810 GOTO playAnother + if playAnother then + sub440 # loop for another + end +end + +def machineWins + puts "MACHINE WINS" +# 830 GOTO playAnother + if playAnother then + sub440 # loop for another + end +end + +def sub840 + $c=0 + for i in 1..$numberOfPiles do + if $pileArray[i] > 1 then + sub940 + end + if $pileArray[i] == 0 then 890 + $c=$c+1 + end + if $c/2 != ($c/2).to_i then + machineLoses + end + sub940 # goto + end +end + +def sub920 + if $pileArray[$dArray[0]] == 1 then + machineWins + end + if $pileArray[$dArray[1]] == 1 then + machineWins + end +end + +def sub940 + for i in 1..$numberOfPiles do + e=$pileArray[i] + for j in 0..10 do + $f = $e/2 + $bArray[i][j] = 2*($f-($f.to_i)) + $e = $f.to_i + end + end +end + +#for j in 10..0 STEP -1 do +10..0.step(-1).each do|index| + $c=0 + $h=0 + for i in 1..$numberOfPiles do + if $bArray[i][index] != 0 then + $c=$c+1 + if $pileArray[i] > $h then + $h = $pileArray[i] + $g = i + end + end + end +end + +if $c/2 != ($c/2).to_i then 1190 +end +$e = rand($numberOfPiles).to_i +#if $pileArray[$e] == 0 then 1140 + +$f = rand($pileArray[$e]).to_i +$pileArray[$e] = $pileArray[$e]-$f +sub1380 +$pileArray[$g]=0 +for j in 0..10 do +$bArray[$g][index]=0 +$c=0 +for i in 1..$numberOfPiles do + if $bArray[i][index] != 0 then + $c=$c+1 + end + end +$pileArray[$g]=$pileArray[$g]+2*($c/2-($c/2)).to_i*2^j +end +if $winOption == 1 then + sub1380 +end +$c=0 +for i in 1..$numberOfPiles do +if $pileArray[i]>1 then + sub1380 +end +if $pileArray[i] != 0 then + $c=$c+1 +end +if $c/2 == ($c/2).to_i then + sub1380 +end +$pileArray[$g] == 1 -$pileArray[$g] + +def sub1380 + puts "PILE SIZE" + for i in 1..$numberOfPiles do + put i + put $pileArray[i] + end + if $winOption == 2 then # avoid take-last option + yourMove + end + sub1570 + if $z == 1 then + machineWins + end +end + +def yourMove + put "YOUR MOVE - PILE, NUMBER TO BE REMOVED" +# 1460 INPUT x,y +x = gets.strip.to_i +y = gets.strip.to_i + if x > $numberOfPiles then yourMove + if x < 1 then yourMove + if x != INT(x) then yourMove + if y > $pileArray[x] then yourMove + if y < 1 then + yourMove + end + if y != INT(y) then + yourMove + end + + $pileArray[x] = $pileArray[x]-y + sub1570 # gosub + if $z == 1 then + machineLoses + end +# 1560 GOTO 700 +end + +end +end +end +end +end + + From 71bc3b95f4ae936ef00b034a41d8dd07bb9edb15 Mon Sep 17 00:00:00 2001 From: Mark Wieder Date: Tue, 11 Jan 2022 15:12:06 -0800 Subject: [PATCH 2/2] Slot machine in ruby --- 80_Slots/ruby/slots.rb | 152 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 80_Slots/ruby/slots.rb diff --git a/80_Slots/ruby/slots.rb b/80_Slots/ruby/slots.rb new file mode 100644 index 00000000..a4c3e7ef --- /dev/null +++ b/80_Slots/ruby/slots.rb @@ -0,0 +1,152 @@ +$pot = 0 + +def greeting + puts "SLOTS".center(80) + puts "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".center(80) + puts "\n\n" + + # PRODUCED BY FRED MIRABELLE AND BOB HARPER ON JAN 29, 1973 + # IT SIMULATES THE SLOT MACHINE. + + puts "You are in the H&M Casino, in front of one of our" + puts "one-arm bandits. You can bet from $1 to $100." + puts "To pull the arm, punch the return key after making your bet." + puts "\nBet zero to end the game." +end + +def overLimit + puts "House Limit is $100" +end + +def underMinimum + puts "Minimum Bet is $1" +end + +# bells don't work on my machine. YMMV +# I'm displaying dashes between the reels + +def tenBells + 10.times do + # beep if you can + print "-" + end +end + +def fiveBells + "-----" +end + +def goodbye + puts "\n\n\n" + # end the game + exit +end + +def payUp + puts "PAY UP! PLEASE LEAVE YOUR MONEY ON THE TERMINAL." +end + +def brokeEven + puts "HEY, YOU BROKE EVEN." +end + +def collectWinnings + puts "COLLECT YOUR WINNINGS FROM THE H&M CASHIER." +end + +def win winType, bet + case winType + when "jackpot" + winMessage = "***JACKPOT***" + winnings = 101 + when "topDollar" + winMessage = "**TOP DOLLAR**" + winnings = 11 + when "doubleBar" + winMessage = "*DOUBLE BAR!!*" + winnings = 6 + when "double" + winMessage = "DOUBLE!!" + winnings = 3 + end + puts "\nYou won: " + winMessage + $pot += (winnings * bet) +end + +greeting + +#$pot = 0 +while true + reelArray = ["BAR","BELL","ORANGE","LEMON","PLUM","CHERRY"] + print "\nYOUR BET? " + # get input, remove leading and trailing whitespace, cast to integer + bet = gets.strip.to_i + + if bet == 0 then + goodbye + elsif bet > 100 then + overLimit # error if more than $100 + elsif bet < 1 then + underMinimum # have to bet at least a dollar + else + # valid bet, continue + tenBells # ding + + # assign a random value from the array to each of the three reels + reel1 = reelArray[rand(5)] + reel2 = reelArray[rand(5)] + reel3 = reelArray[rand(5)] + + # print the slot machine reels + puts "\n\n" + reel1 + fiveBells + reel2 + fiveBells + reel3 + + # see if we have a match in the first two reels + if reel1 == reel2 then + if reel2 == reel3 then + if reel3 == "BAR" then + # all three reels are "BAR" + win "jackpot", bet + else + # all three reels match but aren't "BAR" + win "topDollar", bet + end + elsif reel2 == "BAR" then + # reels 1 and 2 are both "BAR" + win "doubleBar", bet + else + # reels 1 and 2 match but aren't "BAR" + win "double", bet + end + # otherwise see if there's a match in the remaining reels + elsif reel1 == reel3 or reel2 == reel3 then + if reel3 == "BAR" then + # two reels match, both "BAR" + win "doubleBar", bet + else + # two reels match, but not "BAR" + win "double", bet + end + else + # bad news - no matches + puts "\nYou lost" + # decrement your standings by the bet amount + $pot -= bet + end + + puts "Your standings are: " + $pot.to_s + print "\nAgain? " # YES to continue + # get input, remove leading and trailing whitespace, make uppercase + again = gets.strip.upcase + if again != "Y" && again != "YES" then + # that's enough... evaluate the pot and quit + if $pot < 0 then + payUp + elsif $pot == 0 then + brokeEven + else # yay! + collectWinnings + end + goodbye + end + end +end