Why doesn't my code work?
Author |
Message |
Ldomi
|
Posted: Thu Dec 28, 2006 1:55 pm Post subject: Why doesn't my code work? |
|
|
Everytime i press the "Play Game" button, it won't allow me to input a number, instead, a new window pops up. Can someone help me?
code: |
%The Guessing Game
%
%Ms Dyke
%December 14,2006
%This program allows users to guess a random number chosen by the computer from 10 to 65. It will tell the users if the number they guess is too high, too low, or correct, however, they are only allowed 3 tries.
%Set screen up
import GUI
setscreen ("nocursor")
setscreen ("noecho")
%Declaration Section
%(Variables here are global)
%Purpose of variable number is for user to input a number of their choice. Purpose of variable randomNum is to store randomNum as an integer in the proc randNum
var number, randomNum : int
%Varaible try is used to keep track of number of guesses remaining
var try := 0
var mainWin := Window.Open ("postion:300;300,graphics:400;400")
var playBtn, quitBtn : int := 0
%Procedures:
%Procedure Title Purpose: Proc title already contains a title so programmer only has to type the name of the proc to input a title on a screen.
procedure title
cls
locate (1, 32)
put "The Guessing Game"
put " "
end title
%Procedure pauseProgram Purpose: Proc pauseProgram pauses output screen and allows user to proceed once they choose to type a key.
procedure pauseProgram
% Var reply is a local variable. It is used to get any keyboard character that is a length of one for the getch command.
var reply : string (1)
put " "
put "Press any key to continue..."
getch (reply)
end pauseProgram
%Procedure Introduction Purpose: Proc introduction displays the introduction of the program so programmer would not have to type the introduction over and over again.
procedure introduction
title
locate (3, 1)
put "See if you can guess a random number between 10 and 65 chosen by the computer! You only have 3 tries!"
pauseProgram
% var playBtn : int := GUI.CreateButtonFull (10, 100, 0, "Play Game", userInput, 0, '^F', false)
end introduction
proc drawWindow2
var winID2 := Window.Open ("postion :200;200,graphics:200;100")
locate (2, 1)
put "Invalid input! Only enter integers between 10 and 65!" ..
locate (24, 1)
put "Press any key to go back to the main window."
loop
exit when hasch
end loop
Window.Close (winID2)
end drawWindow2
%Procedure goodBye Purpose: Proc goodBye is used to display who the program was written by and any words from the programmer. It is used when the user exits.
proc goodBye
title
locate (12, 23)
put "Program Written By: Lucille Domingo"
locate (13, 30)
put "Thanks for playing!"
delay (3000)
Window.Close (mainWin)
end goodBye
%Procedure randNum Purpose: Proc randNum chooses random number per game
proc randNum
randint (randomNum, 10, 65)
end randNum
%Procedure userInput Purpose: Asks the user what number they think is the random nuber chosen by the computer or if they would like to exit the program.
proc userInput
title
locate (3, 1)
put "To exit, enter -500."
locate (5, 1)
put " "
locate (5, 1)
put "Please enter an integer number between 10 and 65: " ..
get number
if number = -500 then
elsif number < 10 or number > 65 then
drawWindow2
userInput
else
end if
end userInput
proc mainMenu
title
var playBtn : int := GUI.CreateButtonFull (200, 100, 0, "Play Game", userInput, 0, '^F', false)
var quitBtn : int := GUI.CreateButtonFull (350, 100, 0, "Quit", GUI.Quit, 0, '^G', false)
loop
exit when GUI.ProcessEvent
end loop
end mainMenu
%Procedure display Purpose: Shows if the user's guess is correct, higher than random number, or lower than random number and tells user how much tries he/she has left.
proc display
title
try := try + 1
if number = randomNum then
locate (3, 1)
put "You guessed: ", number
put "You are correct!"
put "You are on try: ", try
elsif number < randomNum then
put "You guessed: ", number
put "Your guess is too low!"
put "You are on try: ", try
else
put "You guessed: ", number
put "Your guess is too high!"
put "You are on try: ", try
end if
if try = 3 then
locate (6, 1)
put "The random number chosen by the computer is: ", randomNum
end if
pauseProgram
end display
%Main Program
introduction
randNum
mainMenu
loop %Loop is used to keep asking user for a user input until user decides or has to exit program. When they exit the loop, it proceeds to proc goodBye screen.
exit when GUI.ProcessEvent
userInput
exit when number = -500
display
exit when number = randomNum or try = 3
end loop
goodBye
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Fri Dec 29, 2006 9:28 am Post subject: (No subject) |
|
|
Holy! There is such a thing as over commenting you know. Commenting bits of code that should not need commenting (ie procedure title: tells me pretty much exactly what it might do) should not be commented. Take a quick look at the Style Guidelines in the Turing Walkthrough for some pointers.
Now on to your problem. I think it has something to do with the way you are handling the GUI. Try getting rid of the exit when GUI.ProcessEvent in your main loop (where you control everything like userInput, etc.). I have a feeling that Turing's GUI is screwing up yet again and is somehow reading true after you press a button and when you get to your main loop, exiting because of the GUI.ProcessEvent and going straight to the goodBye procedure. Try that and post back here.
Oh, and take a good look at the other tutorials in the Turing Walkthrough as well, it is a valuable source of information to have. |
|
|
|
|
![](images/spacer.gif) |
Ldomi
|
Posted: Sat Dec 30, 2006 4:03 pm Post subject: (No subject) |
|
|
Freakman wrote: Holy! There is such a thing as over commenting you know. Commenting bits of code that should not need commenting (ie procedure title: tells me pretty much exactly what it might do) should not be commented. Take a quick look at the Style Guidelines in the Turing Walkthrough for some pointers.
Now on to your problem. I think it has something to do with the way you are handling the GUI. Try getting rid of the exit when GUI.ProcessEvent in your main loop (where you control everything like userInput, etc.). I have a feeling that Turing's GUI is screwing up yet again and is somehow reading true after you press a button and when you get to your main loop, exiting because of the GUI.ProcessEvent and going straight to the goodBye procedure. Try that and post back here.
Oh, and take a good look at the other tutorials in the Turing Walkthrough as well, it is a valuable source of information to have.
I just tried it, but it doesn't seem work. Thank you very much though |
|
|
|
|
![](images/spacer.gif) |
Prince Pwn
|
Posted: Sun Dec 31, 2006 1:37 pm Post subject: (No subject) |
|
|
This is where your new window opens and asks for an answer:
code: |
proc drawWindow2
var winID2 := Window.Open ("postion :200;200,graphics:200;100")
locate (2, 1)
put "Invalid input! Only enter integers between 10 and 65!" ..
locate (24, 1)
put "Press any key to go back to the main window."
loop
exit when hasch
end loop
Window.Close (winID2)
end drawWindow2
proc userInput
title
locate (3, 1)
put "To exit, enter -500."
locate (5, 1)
put " "
locate (5, 1)
put "Please enter an integer number between 10 and 65: " ..
get number
if number = -500 then
elsif number < 10 or number > 65 then
drawWindow2
userInput
else
end if
end userInput
proc mainMenu
title
var playBtn : int := GUI.CreateButtonFull (200, 100, 0, "Play Game", userInput, 0, '^F', false)
var quitBtn : int := GUI.CreateButtonFull (350, 100, 0, "Quit", GUI.Quit, 0, '^G', false)
loop
exit when GUI.ProcessEvent
end loop
end mainMenu
|
When you click the Play Button it goes to userInput, then
code: |
elsif number < 10 or number > 65 then
drawWindow2
userInput
|
Your mistake is probably somewhere within the above code. |
|
|
|
|
![](images/spacer.gif) |
blade360
|
Posted: Wed Jan 03, 2007 5:31 pm Post subject: (No subject) |
|
|
ya i agree |
|
|
|
|
![](images/spacer.gif) |
|
|