Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 6.57 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. `DarkBASIC Pro - Exam 1
  2.    `Guess the word
  3.  
  4.    `For some reason I was unable I was having trouble with passing encoded and guess around in the variable.
  5.    `Maybe the array was not dimmed globally
  6.    `For now, here's my sequencial ugly code
  7.  
  8.    SET WINDOW ON
  9.    set window size 800,600
  10.  
  11.    `Declarations
  12.  
  13.    dim pool() as string
  14.    global answer as string = "Terminator"
  15.    global encoded as string
  16.    guess as string
  17.    global lives as integer
  18.    play = 1
  19.    playAgain as string
  20.    match as string
  21.    errorMsg as string
  22.  
  23.    `Label to go back to if we want to start again
  24.  
  25.    start:
  26.  
  27.    `Initialise the program by resetting all variables and re-encoding the word
  28.  
  29.    startProgram(answer)
  30.  
  31.  
  32. `While the user is not bored of the game yet
  33.  
  34. WHILE play = 1
  35.  
  36.    `Play the main DO/LOOP of the game
  37.    DO
  38.  
  39.      `Print the user interface consiting of the encoded word and the number of lives + any error messages
  40.  
  41.       cls
  42.       Print "Welcome to 'Guess the Word'"
  43.       print
  44.       Print encoded
  45.       print
  46.       Print "You have ", lives, " lives left!"
  47.       SET TEXT TO BOLD
  48.       print
  49.       print errorMsg
  50.       PRINT
  51.       SET TEXT TO NORMAL
  52.       Print "Please guess a letter: "
  53.       input guess
  54.  
  55.  
  56.       `Resets the error checking flags before we go through and start error checking
  57.  
  58.       error = 0
  59.       errorMsg = ""
  60.  
  61.  
  62.       `This checks to make sure the player has entered a guess
  63.       if len(guess) = 0
  64.  
  65.          `Set off the error flag and write the error message
  66.  
  67.          error = 1
  68.          errorMsg = "Please enter a guess!"
  69.  
  70.       endif
  71.  
  72.       `This checks to see that the user has inputted a letter and not a number. I would love a isChar() here.
  73.  
  74.       if val(guess) > 0
  75.  
  76.          `Set off the error flag and write and error message
  77.  
  78.          error = 1
  79.          errorMsg = "Please enter a letter - not a number"
  80.  
  81.       endif
  82.  
  83.       `This checks to see if the user has not already inputted that guess into the array
  84.  
  85.       for c = 0 to array count(pool(0))
  86.          if upper$(mid$(guess, 1)) = upper$(pool(c))
  87.  
  88.  
  89.             error = 1
  90.             errorMsg = "You've already guessed that letter!"
  91.  
  92.          endif
  93.       next c
  94.  
  95.  
  96.       `If we've got to this point and we have no errors - continue with the checking of the guess
  97.       if error = 0
  98.  
  99.          `Insert the guess into the array
  100.  
  101.           array insert at bottom pool(0)
  102.           pool() = mid$(guess,1)
  103.  
  104.           `Guilty until proven innocent I say - remove a life for each time you enter a guess
  105.           `Don't worry! You might get it back later.
  106.  
  107.             dec lives
  108.  
  109.          `Search each entry in the guess pool
  110.          for a = 0 to array count(pool(0))
  111.  
  112.                `and search each letter in the answer
  113.                for b = 1 to len(answer)                           `
  114.  
  115.                               `if the current letter has not been found
  116.                               if mid$(encoded,b) = "-"
  117.  
  118.  
  119.                                  `and it matches an entry in the array
  120.  
  121.                                  if upper$(mid$(pool(a),1)) = upper$(mid$(answer,b))
  122.  
  123.                                  `tear apart the string where the match was found, insert the new character and
  124.                                  `glue everything back together again
  125.  
  126.                                     encoded = left$(encoded, b-1) + mid$(answer,b) + right$(encoded, len(encoded)- b)
  127.  
  128.                                  `We found a match so we get a life back - but only 1!
  129.                                     match = "true"
  130.  
  131.  
  132.  
  133.                                  endif
  134.  
  135.                               endif
  136.  
  137.  
  138.                next b
  139.  
  140.             next a
  141.  
  142.  
  143.       endif
  144.  
  145.       `If we found a match through all of that, we get a life back and reset the match check
  146.          if match = "true"
  147.             inc lives
  148.             match = "false"
  149.          endif
  150.  
  151.  
  152.  
  153.       `This checks to see if the player has run out lives and tells them they fail miserably and asked them to try again
  154.        if lives <= 0
  155.                print
  156.                SET TEXT TO BOLD
  157.                print "---You Lose---"                             ` FAIL!
  158.                SET TEXT TO NORMAL
  159.                print "The correct word was ", answer
  160.                   input "Would you like to play again? Y/N?", playAgain
  161.                      if playAgain = "n" OR playAgain = "N"        `Are you bored of this game yet?
  162.                         play = 0
  163.                         exit
  164.                      else                                         ` Assuming all other answers would have been yes
  165.                         gosub start
  166.                      endif
  167.       endif
  168.  
  169.  
  170.       `Check to see if the player is awesome and the encoded word matches the answer
  171.        if upper$(encoded) = upper$(answer)
  172.                print
  173.                SET TEXT TO BOLD
  174.                print "***You Win***"                              ` YAY!!!
  175.                SET TEXT TO NORMAL
  176.                print "The correct word was ", answer
  177.                   input "Would you like to play again? Y/N?", playAgain
  178.                      if playAgain = "n" OR playAgain = "N"       ` Again, I'm suprised you're not bored of this game yet
  179.                         play = 0                                 ` if you are, hit N and it'll all go away
  180.                         exit
  181.                      else                                        ` Assuming all other answers would have been yes
  182.                         gosub start
  183.                      endif
  184.  
  185.  
  186.  
  187.        endif
  188.  
  189.  
  190.  
  191.    LOOP                                                       ` Look its the end of the main DO/LOOP
  192.  
  193.    exit                                                              `unload me
  194.  
  195. ENDWHILE
  196.  
  197.    end
  198.  
  199.  
  200.  
  201.  
  202.  
  203.    function startProgram(answer as string)
  204.       encoded = ""                                          ` reset encoded before re-encoding
  205.       lives = 10                                            ` How many chances do you need?
  206.       empty array pool(0)                                   ` empty all the previous answers
  207.  
  208.  
  209.       `This for/next loop takes each of the characters in the answer and creates a hyphen
  210.       `If it encounters a space, it creates a space
  211.       `... should there multiple movies in other revisions of the game
  212.  
  213.       for char = 1 TO len(answer) STEP 1                    `
  214.          if mid$(answer, char) = " "
  215.             encoded = encoded + " "
  216.  
  217.          else
  218.             encoded = encoded + "-"
  219.  
  220.          endif
  221.       next char
  222.  
  223.  
  224.    endfunction
  225.  
  226.  
  227.  
  228. `===============
  229. `END OF PROGRAM
  230. `===============