Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active August 29, 2015 14:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rightfold/53b4add706c60bef7693 to your computer and use it in GitHub Desktop.
identification division.
program-id. hangman.
data division.
working-storage section.
01 word pic X(100).
01 word-length pic 9(3).
01 guess pic X.
01 filler.
05 guesses occurs 256 times pic 9.
05 lives pic 9 value 8.
01 filler pic X.
88 won value "1" false "0".
01 filler pic X.
88 correct value "1" false "0".
01 i pic 9(3).
procedure division.
main.
perform pick-word
* TODO: show this in debug mode only
display "word: " word
perform until won or lives = 0
perform display-lives
perform accept-guess
perform display-guesses
display " "
perform check
end-perform
if won then
display ":)"
else
display ":("
end-if
goback
.
pick-word.
* TODO: pick random word.
move "hello" to word
move 5 to word-length
.
accept-guess.
display "> " with no advancing
accept guess
move 1 to guesses(function ord(guess))
.
display-guesses.
perform varying i from 1 by 1 until i > word-length
if guesses(function ord(word(i:1))) = 1 then
display word(i:1) with no advancing
else
display "_" with no advancing
end-if
end-perform
display " "
.
display-lives.
display "lives: ", lives
.
check.
set won to true
set correct to false
perform varying i from 1 by 1 until i > word-length
if guesses(function ord(word(i:1))) <> 1 then
set won to false
end-if
if word(i:1) = guess then
set correct to true
end-if
end-perform
if not correct then
subtract 1 from lives
end-if
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment