Qbasicnews.com

Full Version: Can someone help me with this school program, its a sliding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to make the program check if the numbers are in order, could someone help me do this.

Code:
1 CLS
10 REM ****************************
11 REM ***** NUMBER MOVE GAME *****
12 REM ****************************
80 LET CE = 0
100 DIM G$(3, 3)
110 GOSUB 300
120 GOSUB 400
130 x = 3: y = 3: NX = 3: NY = 3
140 GOSUB 500
150 SWAP G$(y, x), G$(NY, NX): y = NY: x = NX
160 GOSUB 400
170 GOTO 140
180 REM ********** PUT NUMBERS IN ARRAY ***
300 FOR J = 1 TO 8
310 y = INT(RND * 3) + 1: x = INT(RND * 3) + 1
320 IF x = 3 AND y = 3 THEN GOTO 310
330 IF G$(y, x) <> "" THEN GOTO 310: REM 0 SPACE
335 N$ = STR$(J): IF LEN(N$) < 3 THEN N$ = N$ + " ": REM 1 SPACE
340 G$(y, x) = N$
350 NEXT J
355 G$(3, 3) = "   ": REM 3 SPACE
360 RETURN
370 REM ********* PRINT NUMBERS **********
400 FOR J = 1 TO 3
410 FOR K = 1 TO 3
420 LOCATE J * 2 + 5, K * 3 + 14: PRINT G$(J, K)
430 NEXT K
440 NEXT J
450 RETURN
460 REM ****************** GET MOVE ****************
500 K$ = INKEY$: IF K$ = "" THEN GOTO 500
510 IF K$ = CHR$(0) + CHR$(80) THEN NY = y - 1: NX = x: IF NY < 1 THEN NY = y + 1: GOTO 500
520 IF K$ = CHR$(0) + CHR$(72) THEN NY = y + 1: NX = x: IF NY > 3 THEN NY = y - 1:  GOTO 500
530 IF K$ = CHR$(0) + CHR$(77) THEN NX = x - 1: NY = y: IF NX < 1 THEN NX = x + 1:  GOTO 500
540 IF K$ = CHR$(0) + CHR$(75) THEN NX = x + 1: NY = y: IF NX > 3 THEN NX = x - 1: GOTO 500
550 IF K$ = CHR$(27) THEN END
CE = CE + 1
LOCATE 25, 8
COLOR 0, 4
PRINT "You have taken"; CE; "moves since you began."
COLOR 15
560 RETURN

600 CLS
FOR abce = 1 TO 100
LOCATE INT(RND * 50) + 1, INT(RND * 80) + 1
PRINT "YOU WON!!!!"
NEXT abce
First, let's get a few things straight.

1) Are you allowed to use ' instead of REM?
2) Are you allowed not to use line numbers?
3) Are you allowed to use SELECT CASE?
4) Are you allowed to use labels instead of line numbers?
5) Are you allowed to use SUB/FUNCTIONs?
it has a nastalgic feel to it
More questions:

Are you allowed to ignore using LET?

Are you allowed to not use GOTO?

Can you use variable names longer than 2 characters long?
Yeah, good points oracle.

And it does have a nostalgic feel to it, Diroga.

I just wish I could help. But I don't know the "rules".
FEAR,
Your Basic programming style is a little outdated, but it's fine --- don't let they guys kid you about it. It still works fine.

I don't understand the problem. You say you want to "check if the numbers are in order". You mean sequence check them, no? Are equals permitted? Which numbers are supposed to be checked? Did you expect them to be in order, and you're checking for an error?

By the way, you're using a random function (RND). In order that the numbers be truly random, you need to do a RANDOMIZE TIMER instruction at the beginniing of your program.
*****
To check if an array of numbers is in order just check if each one is less or equal to the next (or upside down).

Code:
correct%=-1
FOR i%=0 to MaxElements%-1
   IF array%(i%)>array%(i%+1) THEN correct%=0: EXIT FOR
NEXT i%
IF correct% THEN PRINT "Ordered!" ELSE PRINT "Not Ordered!"

If it finds that any element is bigger than the following, then the array is not ordered.
[fear1234] What's "%" at the end? Our teacher hasn't TAUGHT us that yet! My teacher also won't let me use ELSE, or EXIT FOR! WE haven't LEARNED those yet! And we have to use LET!! [/fear1234]
I am allowed to use whatever i want this is one of the last things i have to do with this program, the only other thing i have to do is make each number a different color and have them stay that color while playing the sliding puzzle.
heh, you fixed the underlines.

Here's what I made. It's not 100% aesthetically pleasing, but...

Code:
' ****************************
' ***** Number move game *****
' ****************************

move.amount% = 0
ymax% = 3
xmax% = 3
max.zeros% = 1
DIM board%(1 TO xmax%, 1 TO ymax%)
RANDOMIZE TIMER

CLS

GOSUB put.numbers.in.array

DO
GOSUB print.numbers
GOSUB get.move
SWAP board%(curx%, cury%), board%(nx%, ny%)
cury% = ny%
curx% = nx%
GOSUB print.numbers
GOSUB check.for.win
IF game.won% THEN GOSUB win: EXIT DO
LOOP

SYSTEM

put.numbers.in.array:
FOR y% = 1 TO xmax%
FOR x% = 1 TO ymax%
i% = i% + 1
board%(x%, y%) = i%
NEXT x%, y%

'FOR x% = 1 TO xmax%
'FOR y% = 1 TO ymax%
'x2% = INT(RND * xmax%) + 1
'y2% = INT(RND * ymax%) + 1
'SWAP board%(x%, y%), board%(x2%, y2%)
'NEXT y%, x%

curx% = xmax%
cury% = ymax%
n% = xmax% * ymax%
n2& = n%
n2& = n2& * n%
FOR i& = 1 TO n2&
SELECT CASE INT(RND * 4)
CASE 0: IF curx% < xmax% THEN SWAP board%(curx%, cury%), board%(curx% + 1, cury%): curx% = curx% + 1
CASE 1: IF curx% > 1 THEN SWAP board%(curx%, cury%), board%(curx% - 1, cury%): curx% = curx% - 1
CASE 2: IF cury% < ymax% THEN SWAP board%(curx%, cury%), board%(curx%, cury% + 1): cury% = cury% + 1
CASE 3: IF cury% > 1 THEN SWAP board%(curx%, cury%), board%(curx%, cury% - 1): cury% = cury% - 1
END SELECT
NEXT i&

n% = xmax% * ymax%
FOR x% = 1 TO xmax%
FOR y% = 1 TO ymax%
IF board%(x%, y%) = n% THEN curx% = x%: cury% = y%: nx% = x%: ny% = y%
NEXT y%, x%
RETURN

print.numbers:
FOR x% = 1 TO xmax%
FOR y% = 1 TO ymax%
LOCATE y% * 2 + 5, x% * (1 + max.zeros%) + 14
IF board%(x%, y%) = n% THEN
COLOR , 4: PRINT STRING$(max.zeros%, " "): COLOR , 0
ELSE
n$ = LTRIM$(STR$(board%(x%, y%)))
len.n% = LEN(n$)
PRINT STRING$(max.zeros% - len.n%, "0") + n$
END IF
NEXT y%, x%
RETURN

get.move:
5 i$ = INKEY$: IF i$ = "" THEN GOTO 5
SELECT CASE i$
CASE CHR$(0) + CHR$(80): IF cury% > 1 THEN ny% = cury% - 1
CASE CHR$(0) + CHR$(75): IF curx% < xmax% THEN nx% = curx% + 1
CASE CHR$(0) + CHR$(77): IF curx% > 1 THEN nx% = curx% - 1
CASE CHR$(0) + CHR$(72): IF cury% < ymax% THEN ny% = cury% + 1
CASE CHR$(27): SYSTEM
END SELECT
move.amount% = move.amount% + 1
LOCATE 25, 8: PRINT "You have taken"; move.amount%; "moves since you began.";
RETURN

check.for.win:
i% = 0
FOR y% = 1 TO xmax%
FOR x% = 1 TO ymax%
i% = i% + 1
IF board%(x%, y%) <> i% THEN RETURN
NEXT x%, y%
game.won% = 1
RETURN

win:
CLS
FOR i% = 1 TO 100
LOCATE INT(RND * 20) + 1, INT(RND * 80) + 1: COLOR INT(RND * 15) + 1: PRINT " YOU WON!!!! "
NEXT i%
RETURN