Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
re-assign arrays
#1
I'm making a cheasy hang man game and i wanted to clean up the code...I've made arrays for the word and the blank word the size of the array depends on the length of the word...do i have to have the use input the word BEFORE i can dim the array...or can I dim the array at 0 have them input the word then redim it in a Sub? I know that was an incredibly long run-on sentence but i don't care :bounce:
Reply
#2
can we see some code please? =D
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#3
this is the current code that i tries to use the redim statement work


DECLARE SUB playhangman ()
DECLARE SUB checktries ()
DECLARE SUB showtries ()
DECLARE SUB capturekeymainloop ()
DECLARE SUB enablecountdown ()
DECLARE SUB letterhelp ()
DECLARE SUB checkalphabet ()
DECLARE SUB showalphabet ()
DECLARE SUB createalphabetarray ()
DECLARE SUB timermove ()
DECLARE SUB initiatetimer ()
DECLARE SUB checkletter ()
DECLARE SUB checkwordsolved ()
DECLARE SUB createcopywordarray ()
DECLARE SUB showsolved ()
DECLARE SUB errorcheck ()
DECLARE SUB createwordarray ()

DIM SHARED alphabet$(0)
DIM SHARED wrd$(0)
DIM SHARED subwrd$(0)


COMMON SHARED starttime!, word$, letter$, alpha$, tries, enabled, wordlength, lcheck, timerbar, timerspeed, cheat

CLS

SCREEN 13

tries = 6
enabled = -1

enablecountdown

playhangman

CLS

PRINT "THE WORD WAS "; UCASE$(word$)

IF cheat = 1 THEN
PRINT "CHEATER"
END IF

DATA a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,-,-,z,-,-

REM $STATIC
SUB capturekeymainloop

DO

currenttime! = TIMER

LOCATE 2, 1: PRINT "LETTER"

LOCATE 3, 1: PRINT "SECONDS"; INT(currenttime! - starttime!)

letter$ = INKEY$

IF letter$ = "1" THEN
letterhelp
END IF

IF letter$ = "0" THEN
letterhelp
cheat = 0
END IF

IF enabled = 0 THEN
timermove
END IF

WAIT &H3DA, 12

LOOP UNTIL letter$ <> "" OR timerbar = timerspeed


END SUB

SUB checkalphabet

FOR x = 0 TO 29
IF alphabet$(x) = letter$ THEN
alphabet$(x) = "-"
END IF

NEXT x

END SUB

SUB checkletter

r = 0

FOR x = 0 TO LEN(word$) - 1
IF letter$ = wrd$® THEN
subwrd$® = wrd$®
lcheck = 1
END IF
r = r + 1
NEXT x


END SUB

SUB checktimerou
END SUB

SUB checktries

IF lcheck = 0 OR cheat = 1 THEN
tries = tries - 1
END IF


END SUB

SUB checkwordsolved

wordlength = 0

FOR z = 0 TO LEN(word$) - 1
IF wrd$(y) = subwrd$(y) THEN
wordlength = wordlength + 1
END IF
y = y + 1
NEXT z

END SUB

SUB createalphabetarray

x = 0

FOR i = 0 TO 29
READ alpha$
alphabet$(x) = alpha$
x = x + 1
NEXT i

END SUB

DEFSNG A-Z
SUB createcopywordarray

x = 0
y = 0

FOR x = 0 TO LEN(word$) - 1
subwrd$(y) = "-"
y = y + 1
NEXT x

END SUB

SUB createwordarray

ltr$ = ""
x = 1
y = 0

FOR i = 0 TO LEN(word$) - 1
ltr$ = MID$(word$, x, 1)
x = x + 1
wrd$(y) = ltr$
y = y + 1
NEXT i

END SUB

DEFINT A-Z
SUB enablecountdown

DO

LOCATE 1, 1: PRINT "WOULD YOU LIKE INDIVIDUAL LETTER COUNT"
LOCATE 2, 1: PRINT "DOWN ENABLED?"

press$ = INKEY$

IF press$ = "n" THEN
enabled = -1
ELSEIF press$ = "y" THEN
enabled = 0
END IF

LOOP UNTIL press$ = "n" OR press$ = "y"


END SUB

SUB initiatetimer

timerbar = 20

LINE (20, 100)-(timerspeed, 105), 4, BF

END SUB

SUB letterhelp
RANDOMIZE TIMER

x = 0

DO
randomletter = INT(RND * LEN(word$))
IF subwrd$(randomletter) = "-" THEN
letter$ = wrd$(randomletter)
cheat = 1
x = 1
END IF
LOOP UNTIL x = 1
END SUB

SUB playhangman

DO

CLS

INPUT "WORD:", word$

LOOP UNTIL LEN(word$) > 0

word$ = LCASE$(word$)

ERASE alphabet$
DIM alphabet$(0 TO 29)
REDIM wrd$(0 TO LEN(word$) - 1)
REDIM subwrd$(0 TO LEN(word$) - 1)

createwordarray
createcopywordarray
createalphabetarray
cheat = 0

starttime! = TIMER

CLS

DO

lcheck = 0

showsolved
showalphabet
showtries


inputl:

timerspeed = 150

IF enabled = 0 THEN
initiatetimer
END IF

capturekeymainloop

IF timebar = 190 THEN
tries = tries - 1
GOTO inputl
END IF

checkletter
checkalphabet
checktries

checkwordsolved

LOOP UNTIL (tries = 0 OR wordlength = LEN(word$))


END SUB

SUB showalphabet

z = 0
q = 6
n = 0

FOR x = 0 TO 5
FOR y = 0 TO 4
LOCATE (q), (n + 2): PRINT alphabet$(z)
z = z + 1
n = n + 2
NEXT y
q = q + 1
n = 0
NEXT x

END SUB

DEFSNG A-Z
SUB showsolved

FOR x = 0 TO LEN(word$) - 1
LOCATE 1, x + 1: PRINT UCASE$(subwrd$(y));
y = y + 1
NEXT x

END SUB

DEFINT A-Z
SUB showtries

LOCATE 4, 1: PRINT "TRIES"; tries

END SUB

SUB timermove

LINE (20, 100)-(timerbar, 105), 5, BF

timerbar = timerbar + 1

END SUB
Reply
#4
[syntax="qbasic"]DECLARE SUB playhangman ()
DECLARE SUB checktries ()
DECLARE SUB showtries ()
DECLARE SUB capturekeymainloop ()
DECLARE SUB enablecountdown ()
DECLARE SUB letterhelp ()
DECLARE SUB checkalphabet ()
DECLARE SUB showalphabet ()
DECLARE SUB createalphabetarray ()
DECLARE SUB timermove ()
DECLARE SUB initiatetimer ()
DECLARE SUB checkletter ()
DECLARE SUB checkwordsolved ()
DECLARE SUB createcopywordarray ()
DECLARE SUB showsolved ()
DECLARE SUB errorcheck ()
DECLARE SUB createwordarray ()

DIM SHARED alphabet$(0)
DIM SHARED wrd$(0)
DIM SHARED subwrd$(0)


COMMON SHARED starttime!, word$, letter$, alpha$, tries, enabled, wordlength, lcheck, timerbar, timerspeed, cheat

CLS

SCREEN 13

tries = 6
enabled = -1

enablecountdown

playhangman

CLS

PRINT "THE WORD WAS "; UCASE$(word$)

IF cheat = 1 THEN
PRINT "CHEATER"
END IF

DATA a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,-,-,z,-,-

REM $STATIC
SUB capturekeymainloop

DO

currenttime! = TIMER

LOCATE 2, 1: PRINT "LETTER"

LOCATE 3, 1: PRINT "SECONDS"; INT(currenttime! - starttime!)

letter$ = INKEY$

IF letter$ = "1" THEN
letterhelp
END IF

IF letter$ = "0" THEN
letterhelp
cheat = 0
END IF

IF enabled = 0 THEN
timermove
END IF

WAIT &H3DA, 12

LOOP UNTIL letter$ <> "" OR timerbar = timerspeed


END SUB

SUB checkalphabet

FOR x = 0 TO 29
IF alphabet$(x) = letter$ THEN
alphabet$(x) = "-"
END IF

NEXT x

END SUB

SUB checkletter

r = 0

FOR x = 0 TO LEN(word$) - 1
IF letter$ = wrd$® THEN
subwrd$® = wrd$®
lcheck = 1
END IF
r = r + 1
NEXT x


END SUB

SUB checktimerou
END SUB

SUB checktries

IF lcheck = 0 OR cheat = 1 THEN
tries = tries - 1
END IF


END SUB

SUB checkwordsolved

wordlength = 0

FOR z = 0 TO LEN(word$) - 1
IF wrd$(y) = subwrd$(y) THEN
wordlength = wordlength + 1
END IF
y = y + 1
NEXT z

END SUB

SUB createalphabetarray

x = 0

FOR i = 0 TO 29
READ alpha$
alphabet$(x) = alpha$
x = x + 1
NEXT i

END SUB

DEFSNG A-Z
SUB createcopywordarray

x = 0
y = 0

FOR x = 0 TO LEN(word$) - 1
subwrd$(y) = "-"
y = y + 1
NEXT x

END SUB

SUB createwordarray

ltr$ = ""
x = 1
y = 0

FOR i = 0 TO LEN(word$) - 1
ltr$ = MID$(word$, x, 1)
x = x + 1
wrd$(y) = ltr$
y = y + 1
NEXT i

END SUB

DEFINT A-Z
SUB enablecountdown

DO

LOCATE 1, 1: PRINT "WOULD YOU LIKE INDIVIDUAL LETTER COUNT"
LOCATE 2, 1: PRINT "DOWN ENABLED?"

press$ = INKEY$

IF press$ = "n" THEN
enabled = -1
ELSEIF press$ = "y" THEN
enabled = 0
END IF

LOOP UNTIL press$ = "n" OR press$ = "y"


END SUB

SUB initiatetimer

timerbar = 20

LINE (20, 100)-(timerspeed, 105), 4, BF

END SUB

SUB letterhelp
RANDOMIZE TIMER

x = 0

DO
randomletter = INT(RND * LEN(word$))
IF subwrd$(randomletter) = "-" THEN
letter$ = wrd$(randomletter)
cheat = 1
x = 1
END IF
LOOP UNTIL x = 1
END SUB

SUB playhangman

DO

CLS

INPUT "WORD:", word$

LOOP UNTIL LEN(word$) > 0

word$ = LCASE$(word$)

ERASE alphabet$
DIM alphabet$(0 TO 29)
REDIM wrd$(0 TO LEN(word$) - 1)
REDIM subwrd$(0 TO LEN(word$) - 1)

createwordarray
createcopywordarray
createalphabetarray
cheat = 0

starttime! = TIMER

CLS

DO

lcheck = 0

showsolved
showalphabet
showtries


inputl:

timerspeed = 150

IF enabled = 0 THEN
initiatetimer
END IF

capturekeymainloop

IF timebar = 190 THEN
tries = tries - 1
GOTO inputl
END IF

checkletter
checkalphabet
checktries

checkwordsolved

LOOP UNTIL (tries = 0 OR wordlength = LEN(word$))


END SUB

SUB showalphabet

z = 0
q = 6
n = 0

FOR x = 0 TO 5
FOR y = 0 TO 4
LOCATE (q), (n + 2): PRINT alphabet$(z)
z = z + 1
n = n + 2
NEXT y
q = q + 1
n = 0
NEXT x

END SUB

DEFSNG A-Z
SUB showsolved

FOR x = 0 TO LEN(word$) - 1
LOCATE 1, x + 1: PRINT UCASE$(subwrd$(y));
y = y + 1
NEXT x

END SUB

DEFINT A-Z
SUB showtries

LOCATE 4, 1: PRINT "TRIES"; tries

END SUB

SUB timermove

LINE (20, 100)-(timerbar, 105), 5, BF

timerbar = timerbar + 1

END SUB
[/syntax]
Reply
#5
Ohh...sorry. Well anyway I found the problem I didn't include

'$dynamic

I have no idea what it means or does...but whatever.

But I do have another question...does anybody have an idea how to include some graphics...not the line(x,y)-(x,y) stuff but the real background, foreground animated stuff. Thanx much!
Reply
#6
You can use an array to store an image with GET,
Then you can display it using PUT.

Both of 'em are in QB help and there are all kinds of examples laying around on the 'net. Wink

Here's a link to one tutorial...
http://www.qbasicnews.com/tutorials.php?...=view&id=6
Reply
#7
What about a tutorial about loading graphics from an external file?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)