Author Topic: Hangman  (Read 15395 times)

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Hangman
« on: December 14, 2011, 08:38:49 PM »
Here is a hangman game I wrote for a job application programming test. The zip includes a Windows 32 and Linux 64 standalone executable with the common script source. I have attached the original spec. for the hangman game as well.

Code: [Select]
' Hangman - ScriptBasic


word[1]  = "COMPLY"
word[2]  = "THREE"
word[3]  = "VACATION"
word[4]  = "INFORMATION"
word[5]  = "TECHNOLOGY"
word[6]  = "ORLANDO"
word[7]  = "COMPUTER"
word[8]  = "ROUTER"
word[9]  = "PRINTER"
word[10] = "BUDGE"
word[11] = "SOFTWARE"
word[12] = "HARDWARE"
word[12] = "OBJECTIVE"
word[13] = "FILE"
word[14] = "EMPLOYEE"
word[15] = "SECURITY"
word[16] = "DATA"
word[17] = "REPORT"
word[18] = "PROPERTY"
word[19] = "OWNERSHIP"

hung_state[1] = """
 O

"""
hung_state[2] = """
 O
 |

"""
hung_state[3] = """
 O
\\|

"""
hung_state[4] = """
 O
\\|/

"""
hung_state[5] = """
 O
\\|/
 |

"""
hung_state[6] = """
 O
\\|/
 |
/

"""
hung_state[7] = """
 O
\\|/
 |
/ \\

"""

current_word = 0
guesses = 0

NEW_GAME:

IF current_word > UBOUND(word) THEN current_word = 0
current_word += 1
word_length = LEN(word[current_word])
partial_word = STRING(word_length, "_")

PRINTNL
PRINT "Welcome to hangman. You get seven chances to guess the mystery word.\n"
PRINTNL
temp_word = partial_word
GOSUB PRINT_OUT

AGAIN:

PRINT "Pick a letter (. to quit)--> "
LINE INPUT letter
letter = UCASE(TRIM(CHOMP(letter)))
IF letter = "." THEN
  PRINT "Good-bye!\n"
  END
END IF
IF LEN(letter) <> 1 OR ASC(letter) < 65 OR ASC(letter) > 90 THEN
  PRINT "You must enter a letter from A to Z\n"
  GOTO AGAIN
END IF

IF INSTR(selected_letters, letter) THEN
  PRINT "Sorry, you already guessed '" & letter & "'.\n"
  GOTO AGAIN
ELSE IF INSTR(word[current_word], letter) THEN
  FOR x = 1 TO word_length
    IF MID(word[current_word],x,1) = letter THEN
      partial_word = LEFT(partial_word, x-1) & letter & MID(partial_word, x+1)
    END IF
  NEXT
  IF word[current_word] = partial_word THEN
    temp_word = partial_word
    GOSUB PRINT_OUT
    PRINT "You guessed the word!\n"
    GOTO PLAY_AGAIN
  END IF  
ELSE
  guesses += 1
END IF
selected_letters &= letter

PRINT "Guessed letters: "
temp_word = selected_letters
GOSUB PRINT_OUT
IF guesses THEN PRINT hung_state[guesses]
temp_word = partial_word
GOSUB PRINT_OUT
IF guesses = 7 THEN
  PRINT "So sorry. You struck out.\n"
  PRINT "The mystery word was '",word[current_word],"'.\n"
  GOTO PLAY_AGAIN
END IF
GOTO AGAIN

PRINT_OUT:

FOR x = 1 TO LEN(temp_word)
  PRINT MID(temp_word, x, 1) & " "
NEXT
PRINTNL
PRINTNL
RETURN

PLAY_AGAIN:

PRINTNL
PRINT "New Game? <Y/N> "
LINE INPUT ng
ng = UCASE(CHOMP(ng))
IF ng = "Y" THEN
  selected_letters = ""
  guesses = 0
  GOTO NEW_GAME
END IF
END
« Last Edit: December 14, 2011, 08:47:58 PM by support »
Script BASIC Project Manager