- `DarkBASIC Pro - Exam 1
- `Guess the word
- `For some reason I was unable I was having trouble with passing encoded and guess around in the variable.
- `Maybe the array was not dimmed globally
- `For now, here's my sequencial ugly code
- SET WINDOW ON
- set window size 800,600
- `Declarations
- dim pool() as string
- global answer as string = "Terminator"
- global encoded as string
- guess as string
- global lives as integer
- play = 1
- playAgain as string
- match as string
- errorMsg as string
- `Label to go back to if we want to start again
- start:
- `Initialise the program by resetting all variables and re-encoding the word
- startProgram(answer)
- `While the user is not bored of the game yet
- WHILE play = 1
- `Play the main DO/LOOP of the game
- DO
- `Print the user interface consiting of the encoded word and the number of lives + any error messages
- cls
- Print "Welcome to 'Guess the Word'"
- print
- Print encoded
- print
- Print "You have ", lives, " lives left!"
- SET TEXT TO BOLD
- print
- print errorMsg
- PRINT
- SET TEXT TO NORMAL
- Print "Please guess a letter: "
- input guess
- `Resets the error checking flags before we go through and start error checking
- error = 0
- errorMsg = ""
- `This checks to make sure the player has entered a guess
- if len(guess) = 0
- `Set off the error flag and write the error message
- error = 1
- errorMsg = "Please enter a guess!"
- endif
- `This checks to see that the user has inputted a letter and not a number. I would love a isChar() here.
- if val(guess) > 0
- `Set off the error flag and write and error message
- error = 1
- errorMsg = "Please enter a letter - not a number"
- endif
- `This checks to see if the user has not already inputted that guess into the array
- for c = 0 to array count(pool(0))
- if upper$(mid$(guess, 1)) = upper$(pool(c))
- error = 1
- errorMsg = "You've already guessed that letter!"
- endif
- next c
- `If we've got to this point and we have no errors - continue with the checking of the guess
- if error = 0
- `Insert the guess into the array
- array insert at bottom pool(0)
- pool() = mid$(guess,1)
- `Guilty until proven innocent I say - remove a life for each time you enter a guess
- `Don't worry! You might get it back later.
- dec lives
- `Search each entry in the guess pool
- for a = 0 to array count(pool(0))
- `and search each letter in the answer
- for b = 1 to len(answer) `
- `if the current letter has not been found
- if mid$(encoded,b) = "-"
- `and it matches an entry in the array
- if upper$(mid$(pool(a),1)) = upper$(mid$(answer,b))
- `tear apart the string where the match was found, insert the new character and
- `glue everything back together again
- encoded = left$(encoded, b-1) + mid$(answer,b) + right$(encoded, len(encoded)- b)
- `We found a match so we get a life back - but only 1!
- match = "true"
- endif
- endif
- next b
- next a
- endif
- `If we found a match through all of that, we get a life back and reset the match check
- if match = "true"
- inc lives
- match = "false"
- endif
- `This checks to see if the player has run out lives and tells them they fail miserably and asked them to try again
- if lives <= 0
- print
- SET TEXT TO BOLD
- print "---You Lose---" ` FAIL!
- SET TEXT TO NORMAL
- print "The correct word was ", answer
- input "Would you like to play again? Y/N?", playAgain
- if playAgain = "n" OR playAgain = "N" `Are you bored of this game yet?
- play = 0
- exit
- else ` Assuming all other answers would have been yes
- gosub start
- endif
- endif
- `Check to see if the player is awesome and the encoded word matches the answer
- if upper$(encoded) = upper$(answer)
- print
- SET TEXT TO BOLD
- print "***You Win***" ` YAY!!!
- SET TEXT TO NORMAL
- print "The correct word was ", answer
- input "Would you like to play again? Y/N?", playAgain
- if playAgain = "n" OR playAgain = "N" ` Again, I'm suprised you're not bored of this game yet
- play = 0 ` if you are, hit N and it'll all go away
- exit
- else ` Assuming all other answers would have been yes
- gosub start
- endif
- endif
- LOOP ` Look its the end of the main DO/LOOP
- exit `unload me
- ENDWHILE
- end
- function startProgram(answer as string)
- encoded = "" ` reset encoded before re-encoding
- lives = 10 ` How many chances do you need?
- empty array pool(0) ` empty all the previous answers
- `This for/next loop takes each of the characters in the answer and creates a hyphen
- `If it encounters a space, it creates a space
- `... should there multiple movies in other revisions of the game
- for char = 1 TO len(answer) STEP 1 `
- if mid$(answer, char) = " "
- encoded = encoded + " "
- else
- encoded = encoded + "-"
- endif
- next char
- endfunction
- `===============
- `END OF PROGRAM
- `===============