HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Hangman in COBOL

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
hangmancobolstackoverflow

Problem

Whenever I learn a new (or an old :)) programming language, I implement Hangman in it. This time it's with COBOL.

identification division.
program-id. hangman.

data division.
working-storage section.
01 state.
    05 word pic A(100).
    05 word-length pic 9(3).
    05 guess pic A.
    05 guesses occurs 256 times pic 9.
    05 done pic 9.
01 i pic 9(3).

procedure division.
* TODO: pick random word from word list
    move "hello" to word
    move 5 to word-length

* TODO: show this in debug mode only
    display "word: " word

    perform until done = 1
        accept guess
        move 1 to guesses(function ord(guess))
        move 1 to done
        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
                move 0 to done
                display "_" with no advancing
            end-if
        end-perform
        display " "
    end-perform.


I'm of course terrible at COBOL, and I wonder whether the data types I used for the variables are the correct ones and whether there are better ones to use instead. I'm also worried about the hardcoded size for word. I'm using the latest GnuCOBOL.

Solution

This a very good first COBOL program. I hope you enjoy learning it. The only advice I can only offer is mostly stylistic.


I wonder whether the data types I used for the variables are the correct ones

The types are chosen are perfectly fine. In "real" code, you would proably declare numbers with a COMP clause. This means they're stored as 16/32/64-bit numbers, instead of the usual character arrays. You might also use PIC X instead of PIC A because no one uses PIC A for some reason.


I'm also worried about the hardcoded size for word.

Strings of fixed length are normal for COBOL. In fact, until COBOL 2014, strings could only have a fixed length. And, as no compiler these days targets the standard, strings will remain of fixed length for years to come.

Comments on code

In the data division:

  • almost all clauses are indented to the right side of the screen, normally column 40, for neatness



  • variables are defined in alphabetical order to make them easier to find.



  • perhaps delete state as it's not used and as it's not a very informative grouping.



Following this, your data division would become:

data division.
       working-storage section.
       01 done                          pic 9.
       01 guess                         pic A.
       01 guesses                       occurs 256 times pic 9.
       01 i                             pic 9(3).
       01 word                          pic A(100).
       01 word-length                   pic 9(3).


In the procedure division:

-
To only display word in debug mode, you can use the >>define and >>if directives:

>>define debug-mode

   >>if debug-mode is defined
       display "word: " word
   >>end-if


-
Instead of using perform ... until done = 1 you can use an 88-level. This is COBOL's version of a boolean variable.

working-storage section.
   01  done-flag                     pic X.
       88  done                      value "1" false "0".
  *> ...
       perform until done
           *>  ...
           set done to true
           *> ...
               else
                   set done to false
  *> (and similarly for guesses)


-
it's common style to precede indices and reference modification with a space; e.g.:

if guesses (function ord(word (i:1))) = 1 then


-
common style also dictates that the final, mandatory full stop goes on a line on its own:

end-perform
       .


Finally, if you're new to COBOL, you may prefer to use fixed-format code which lets you put code in the first 7 columns and past column 72. Either compile the file with -free or preface your code with

>>source format is free

Code Snippets

data division.
       working-storage section.
       01 done                          pic 9.
       01 guess                         pic A.
       01 guesses                       occurs 256 times pic 9.
       01 i                             pic 9(3).
       01 word                          pic A(100).
       01 word-length                   pic 9(3).
>>define debug-mode

   >>if debug-mode is defined
       display "word: " word
   >>end-if
working-storage section.
   01  done-flag                     pic X.
       88  done                      value "1" false "0".
  *> ...
       perform until done
           *>  ...
           set done to true
           *> ...
               else
                   set done to false
  *> (and similarly for guesses)
if guesses (function ord(word (i:1))) = 1 then
end-perform
       .

Context

StackExchange Code Review Q#87491, answer score: 6

Revisions (0)

No revisions yet.