Event-Driven Seaman

(Using GameEngine)

<<Previous | ToC | Next >>

This page re-implements the previous Seaman game as event-driven in the GameEngine, as part of a course on programming in Java (which starts here). If you have not yet programmed the text/console version of Seaman, you might want to go back and do that.

If you have not yet programmed anything in GameEngine, you might want to begin reading the tutorial on GameMaker, and then do an easier program, like Pong in the GameMaker or the Calculator. Then come back here.
 

Design

As always, we begin by thinking about what this program needs to do. This is different from what the text-based Seaman did, because the computer is relinquishing control to the (human) user, where it belongs.

We can salvage some of our work from the text-based program -- the arrays and the search loops -- but the control structure is completely replaced. The user is in control, not the computer. So let's start in English. Do you remember the TDD we used for (English) Seaman? It was something like this:

 
"Play Seaman"
  Print Instructions 
  First Player
  Repeat
    Second Player

"First Player"
  Input the word
  Hide the word

"Second Player"
  Print game State
  Input guess letter
  Scoring

"Print game State"
  Draw Stick Figure
  Print dashes (and letters)

"Scoring"
  if letter is in the word replace dash(es)
    otherwise add body part to stick figure

The top level of the new TDD is (as for all event-driven programs)
"Event-Driven Seaman"
  Initialize
  Handle Events
Most of the initialization is taken care of using the GameMaker layout tool, except we have a few arrays to set up. Everything else is handling the events, which are only letter inputs. But all the input comes into a single place in the program, so the program needs a boolean variable -- we could call it "playing" -- that is initially false while the first player enters the letters of their word, then goes true while the second player tries to guess the word.

Let's do the initialization first in English. Our Startup method initializes

Startup: initialize playing = false; nLetts = 0; wrong = 0;
...preset the word array guessword to empty; dashes to be underscore characters '_'
...hide all dashes widgets
...hide the sailor's body part widgets.
Then the only event we need to handle is accepting keystrokes:
let inlett be the input character
if playing is false then:
if inlett is < 'A' then
set playing = true
otherwise
add inlett to the guessword
add +1 to nLetts
show next dashes character
otherwise (if playing is true):
At this point it is probably useful to look at our existing English Seaman code -- you did that, didn't you? -- if you saved it, (or get it from the Done archive, then) it might be helpful to open it now in another window. Otherwise we can use mine. Otherwise here is mine (I changed some of the variable names to be more obvious):
 
Variable nerrs = 0 "Hide word from 2nd player"
Variable gottem = 0   Repeat 25
      Print " "
"Play Seaman"     Next
  Part 1   Done
  repeat  
    Part 2 "Print dashes and letters"
    next   Print gWord " (" nerrs " misses)"
  done   If sofar=0 then Stop
    If nerrs=6 then Stop
"Part 1"   Done
  Print instructions  
  Input whole word "Input letter"
  Hide word from 2nd player   Input guess,1
  done   Print "Got " guess
    Done
"Part 2"
  Print dashes and letters "Decide if input is correct" 
  Input letter   Let got = 0 
  Decide if input is correct   Let try = 0
  {Do Print Seaman}   Repeat nLetts
  done     if myWord[try]=guess then Do ItsEqual 
      Add 1 to try
"Print instructions"     Next
  Print "This is Seaman, yada, yada"   if got = 0 then Do UnEqual
  Done   {Do Print Seaman}
    Done
"Input whole word"
  Array myWord "UnEqual"
  Array gWord   Add 1 to nerrs
  Variable nLetts = 0   Done
  Print "Enter your word"  
  Repeat "ItsEqual"
    Input guess,1   Let item try of gWord = guess
    If guess < 'A' then Exit   Subtract 1 from sofar
    Add 1 to nLetts   Add 1 to got
    Let item nLetts of myWord = guess   Show the body part corresponding to got
    Let item nLetts of gWord = "_"   (let the GameEngine draw everything)
    Next   Done
  Print myWord " is " nLetts " letters. " gWord 
  Let sofar = nLetts {= unguessed}  
  Done
I colored red the stuff that is done otherwise in the GameEngine (not needed in Java), and added in green the new drawing stuff we need for GameEngine. When we turn this into Java, the four blue lines will turn into one Java for-loop (with braces).

We are assuming that dashes is an array of text widgets, one letter each, arranged in a line in your game board, and that your boat and the body parts are placed appropriately on the game board.

In the next page we will lay out the game board in GameMaker.

<<Previous | ToC | Next >>

Revised: 2022 November 18