How to make a loop that loops a specific number of times ?
Author |
Message |
sdn121019
|
Posted: Sat Apr 18, 2020 3:48 pm Post subject: How to make a loop that loops a specific number of times ? |
|
|
Hello, this is my first post.
I have a school project where I have to make a rock paper scissors assignment that lets the player play the game a specific number of times by entering how many points they want to play to against a computer.
Currently the program runs perfectly fine but when the player plays the game the game just keeps looping forever.
Pls help..
Here is the code I currently have:
var randNum : int
var myNum : int
var playagain : char
var playername : string
var score : int
randNum := Rand.Int (1, 3)
const ROCK : int := 1
const PAPER : int := 2
const SCISSORS : int := 3
put "Hello Player! What's your name?"
get playername
put "Welcome ", playername, "! How many points do you want to play to?"
get score
delay (100)
put "Loading game..."
delay (3500)
cls
loop
put "Choose:"
put ""
put " 1. ROCK"
put " 2. PAPER"
put " 3. SCISSORS"
get myNum
if myNum = 1 then
put "YOU CHOSE ROCK"
elsif myNum = 2 then
put "YOU CHOSE PAPER"
elsif myNum = 3 then
put "YOU CHOSE SCISSORS"
end if
if randNum = 1 then
put "COMPUTER CHOSE ROCK"
elsif randNum = 2 then
put "COMPUTER CHOSE PAPER"
elsif randNum = 3 then
put "COMPUTER CHOSE SCISSORS"
end if
if myNum = 1 and randNum = 1 then
put "YOU TIED!"
end if
if myNum = 1 and randNum = 2 then
put "YOU LOSE!"
end if
if myNum = 1 and randNum = 3 then
put "YOU WIN!"
end if
if myNum = 2 and randNum = 1 then
put "YOU WIN!"
end if
if myNum = 2 and randNum = 2 then
put "YOU TIED!"
end if
if myNum = 2 and randNum = 3 then
put "YOU LOSE!"
end if
if myNum = 3 and randNum = 1 then
put "YOU LOSE!"
end if
if myNum = 3 and randNum = 2 then
put "YOU WIN!"
end if
if myNum = 3 and randNum = 3 then
put "YOU TIED!"
end if
end loop |
|
|
|
|
|
Sponsor Sponsor
|
|
|
scholarlytutor
|
Posted: Thu May 28, 2020 11:17 pm Post subject: RE:How to make a loop that loops a specific number of times ? |
|
|
Sorry that no one got to this earlier. What your loop needs is a counter. Just before your loop starts, write:
var counter : int := 0
Second, after you write loop, you need an exit statement.
exit when counter = score
(personally I would use another name for the variable 'score', such as num_of_games)
Finally, just before you write end loop, you will type:
counter += 1
Now your program will keep track of how many times it has looped. When the counter matches the game limit the user entered, the loop will exit.
I know you've likely finished this by now but you or any one else with a similar question can use this for future reference. |
|
|
|
|
|
|
|