2016-11-08 5 views
3

Ich beginne Ruby zu lernen, und ich bin mit diesem Programm fest, ich schreibe, die das Spiel "Craps" kleinen Teil des Spiels zumindest sowieso sein soll, die Der Code wird in der zweiten While-Schleife wiederholt, auch wenn der Zufallswürfel den Bedingungen der Schleife entspricht.Ruby While-Schleife nicht zu stoppen, wenn Bedingung erfüllt ist

Dies ist die Schleife, die

while new_dice_sum != 7 or new_dice_sum != 11 or new_dice_sum != 2 or new_dice_sum != 3 or new_dice_sum != 12 or new_dice_sum != point 
        dice1 = 1 + rand(6) 
        dice2 = 1 + rand(6) 
        new_dice_sum = dice1.to_i + dice2.to_i 

Dies ist der vollständige Code des Programms hält Looping.

input = "" 
point = 0 
balance = 100 
Bet = 10 
dice1 = 0 
dice2 = 0 
dice_sum = 0 
new_dice_sum = 0 
won = 0 
lost = 0 
while input != "Q" 
    print "Please choose R to roll the dice or Q to exit the game: " 
    input = gets.chomp.upcase 
    if input == "R" 
     if balance > 10 
      dice1 = 1 + rand(6) 
      dice2 = 1 + rand(6) 
      dice_sum = dice1.to_i + dice2.to_i 
      puts "First dice is #{dice1} while the second dice is #{dice2} both are a total of #{dice_sum}" 
      if dice_sum == 7 || dice_sum == 11 
       balance = balance + Bet 
       won = won + 1 
       puts "You won, Your new balance is #{balance}" 
      elsif dice_sum == 2 || dice_sum == 3 || dice_sum == 12 
       balance = balance - Bet 
       lost = lost + 1 
       puts "You lost, Your new balance is #{balance}" 
      else 
       point = dice_sum 
       while new_dice_sum != 7 or new_dice_sum != 11 or new_dice_sum != 2 or new_dice_sum != 3 or new_dice_sum != 12 or new_dice_sum != point 
        dice1 = 1 + rand(6) 
        dice2 = 1 + rand(6) 
        new_dice_sum = dice1.to_i + dice2.to_i 
        puts "In the new roll with your point #{point}, The first dice is #{dice1} while the second dice is #{dice2} both are a total of #{new_dice_sum}" 
        if new_dice_sum == 7 || new_dice_sum == 11 
         balance = balance + Bet 
         won = won + 1 
         puts "You won, Your new balance is #{balance}" 
        elsif new_dice_sum == 2 || new_dice_sum == 3 || new_dice_sum == 12 
         balance = balance - Bet 
         lost = lost + 1 
         puts "You lost, Your new balance is #{balance}" 
        elsif new_dice_sum == point 
         balance = balance + Bet 
         won = won + 1 
         puts "Your Total dice is equal to your point you won, your new balance is #{balance}" 
        end 
       end 
      end 
     else 
      puts "Your balance is not enough to place a bet, Your balance now is #{balance}" 
     end 
    elsif input != "Q" 
    puts "#{input} is not valid choice" 
    end 
end 
puts "Your current balance is #{balance}$ and you won #{won} times and lost #{lost} times" 
exit 
+0

Wenn Sie die Schleife stoppen wollen, sollten Sie Rückkehr nennen. Und auch Ihr Code ist zu kompliziert mit verschachtelten if und Schleifen. – araratan

+0

Wenn Sie Ihre Antwort haben, schlage ich vor, den Beitrag zu schließen oder eine Antwort zu markieren. Nicht jeder liest den Kommentarabschnitt. – Rishav

Antwort

1

Weil Sie oder auf alle Parameter tun, also auch wenn es zuerst wird es passt passieren das zweite nicht übereinstimmen.

Ihre Logik ändern für while-Schleife von variable != firstcondition or variable != secondcondition ... zu !(variable == condition or variable == condition ...)

+2

Uh ... du willst wahrscheinlich '==' da drin, nicht '='. Persönlich würde ich nur empfehlen 'while! [7,11,2,3,12] .include? (Variable)' – philomory

+0

lol ja, ty, aktualisiert die Antwort, war nicht sicher, auf Ruby-Syntax sah Logik – danchik

+0

Ich hatte den Eindruck, dass, wenn eines der ** oder ** zurückgibt, die Schleife endet. – Sary

Verwandte Themen