Counter Problem
Author |
Message |
kaotickid
|
Posted: Thu Apr 24, 2008 8:19 pm Post subject: Counter Problem |
|
|
Well I have Compsci project and I've decided to make a game. The game involves multiplication and the user must choose which answer is the right one. However, whenever I generate the numbers and where to place the numbers on a grid, unless the answer is placed on the 9th box it tells me it is out of range. I've tried changing up the code but to no avail. If anyone can suggest anything that will solve my problem I thank you ahead of time. Here is my code so far, the problem is within the loop in the for loop:
Turing: |
setscreen ("graphics:1024;768")
var number1, number2, answer, timecounter, placement, score : int := 0
var life : int := 3
var font1 : int
font1 := Font.New ("trebuchet ms:30")
var numberwherex : array 1 .. 9 of int := init (141, 482, 823, 141, 482, 823, 141, 482, 823)
var numberwherey : array 1 .. 9 of int := init (118, 118, 118, 384, 384, 384, 640, 640, 640)
var number1fake : array 1 .. 8 of int
var number2fake : array 1 .. 8 of int
var answerfake : array 1 .. 8 of int
loop
randint (number1, 1, 50) %Generates a number to multiply
randint (number2, 1, 50) %Generates a number to multiply
answer := number1 * number2 %Multiplies the two numbers for an answer
for i : 1 .. 8
randint (number1fake (i ), 1, 50) %Generates 8 fake numbers to multiply
randint (number2fake (i ), 1, 50) %Generates 8 fake numbers to multiply
end for
for i : 1 .. 8
answerfake (i ) := number1fake (i ) * number2fake (i ) %Multiplies fake numbers for 8 fake answers
end for
timecounter := 15 %Restarts timer
randint (placement, 1, 9) %Randomizes where the answer is placed on the grid
loop
drawfillbox (1, 1, maxx, maxy, black)
Draw.ThickLine (341, 1, 341, 752, 5, brightblue) %Draws first vertical line
Draw.ThickLine (683, 1, 683, 752, 5, brightblue) %Draws second vertical line
Draw.ThickLine (1, 512, maxx, 512, 5, brightblue) %Draws first horizontal line
Draw.ThickLine (1, 256, maxx, 256, 5, brightblue) %Draws second horizontal line
Text.Locate (1, 1)
put " Time: ", timecounter %Puts up timer
timecounter := timecounter - 1 %Counts down
Text.Locate (1, 59)
put number1, " * ", number2, " = ?" %Puts up the question
drawbox (1, 753, 1023, 767, black) %Draws the box
var x : int := 0
for i : 1 .. 9
if i = placement then
Font.Draw (intstr (answer ), numberwherex (placement ), numberwherey (placement ), font1, brightred)
elsif i + placement > 9 then
x := x + 1
Font.Draw (intstr (answerfake (i )), numberwherex (x ), numberwherey (x ), font1, brightred)
else
Font.Draw (intstr (answerfake (i )), numberwherex (i + placement ), numberwherey (i + placement ), font1, brightred)
end if
end for
View.Update
delay (1000)
end loop
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
gitoxa
|
Posted: Thu Apr 24, 2008 11:45 pm Post subject: Re: Counter Problem |
|
|
code: | for i : 1 .. 9
if i = placement then
Font.Draw (intstr (answer), numberwherex (i), numberwherey (i), font1, brightred)
elsif i > placement then
x := x + 1
Font.Draw (intstr (answerfake (x)), numberwherex (i), numberwherey (i), font1, brightred)
else
x := x + 1
Font.Draw (intstr (answerfake (x)), numberwherex (i), numberwherey (i), font1, brightred)
end if
end for |
You were making the loop a lot more complicated than it was. |
|
|
|
|
|
kaotickid
|
Posted: Fri Apr 25, 2008 7:59 pm Post subject: Re: Counter Problem |
|
|
Thanks gitoxa But I don't want to completely leech, can you mind explaining the code? If not it's okay Thanks again ! |
|
|
|
|
|
gitoxa
|
Posted: Sat Apr 26, 2008 9:54 am Post subject: Re: Counter Problem |
|
|
Your old code
code: | for i : 1 .. 9
if i = placement then
Font.Draw (intstr (answer), numberwherex (placement), numberwherey (placement), font1, brightred)
elsif i + placement > 9 then
x := x + 1
Font.Draw (intstr (answerfake (i)), numberwherex (x), numberwherey (x), font1, brightred)
else
Font.Draw (intstr (answerfake (i)), numberwherex (i + placement), numberwherey (i + placement), font1, brightred)
end if
end for |
The easiest way to see what your problem is, is to do just pick a random number for "placement" and run through your loop. Lets start with 1.
~~~~~~~~
First run through the for loop, i = 1
Since placement is 1, the real answer gets placed into the first sqaure.
i = 2
answerfake(i) is placed in the (i + placement) square. i + placement = 1 + 2 = 3
There's two problems here. One, your first fake answer was the second in the array; you skipped the first.
Two, you completly skipped square 2.
i=3
answerfake(i) is placed in the (i + placement) square. i + placement = 1 + 3 = 4
As you can see, this trend will continue until i + placement > 9, which in this case happens to be when i = 9
i=9
x = x + 1 = 0 + 1 = 1
answerfake(i) is placed in the x-th box. Two problems, x = 1, so you're going to be putting it in the 1st sqaure, which already happens to have a number in it.
Secondly, you only have 8 fake answers, you're trying to use the 9th.
~~~~~~~~~~~
To fix the answerfake problem, there was two possible solutions. You could either just create 9 fake answers, and leave it as i, or use x to keep track of how many fake numbers you've placed already. I did the latter.
The next part I fixed was your elsif in the if statement. I'm not really sure where you got "i + placement" from, but what happens when placement is 8. It'll put the frist number in the first square, then it'll put the 2nd number in the xth (1) square, again. (looking at the loop again, my elsif and else do the exact same thing, oops.)
Then came where you were placing the numbers. Using x as a counter to keep track of where to put the numbers isn't a good idea. Not when your for loop is keeping track of all nine squares in the first place. Just use i, it'll hit all nine square, and only hit each one once.
numberwherex (i+placement) i + anything will eventually result in a number greater then 9, which will result in it trying to read from a non-existant element in the variable. It also doesn't make any sense.
~~~~~~~~~~~~
All in all, the rest of the program was well put together. Good luck with the rest of it. |
|
|
|
|
|
|
|