Monolithic

posted in Event-driven engine for project Hangman
Published July 25, 2021
Advertisement

We'll start the game engine with some simple games like hangman, memory, mine finder, multiply, match3, … One important characteristic is that they don't need any type of collision detection, no need of high FPS, and they are basically event driven. The user takes an action and the game reacts to that action/event.

I don't have a predefined architecture with all the details, we will start with a monolithic game and we will start to refactor it.

The first game is “hangman”, very simple:

  1. Game initialization
    1. select random word
    2. set the number of attempts
  2. Game loop
    1. User input
    2. Logic check
    3. Win/Lose check
    4. Output
  3. Game close

Our main dependencies are the input/output system, in the first attempt they will be the console (System.in(Scanner) and System.out) later we will decouple that to allow any input/output system.

At the moment, we will use a classic game loop, no events at the moment, all tightly couple :(

  • Monolithic code
package manager;

import java.util.Scanner;

import hangman.Play;

public class Monolithic {

	public static void main(String[] args) {

		// 1. Game initialization
		
		System.out.println("HANGMAN. New Play.");	
		int attempts = 7;
		Play hangman = new Play(attempts);		
		String word = hangman.GetCurrentRevealedWord('_');		
		System.out.println(">>> " + word + " <<<"); 
		
		
		// 2. Game loop
		boolean success = false;
		Scanner in = new Scanner(System.in);
		while (attempts > 0 && !success) {
			
			// 2.1 User input
			char mchar = in.nextLine().charAt(0);
			
			// 2.2 Logic check
			boolean result = hangman.Try(mchar);
			
			// 2.3 Win/Lose check
			success = hangman.isWordCompleatedRevealed();
			
			// 2.4 Output
			if ( true == result ) {
				word = hangman.GetCurrentRevealedWord('_');
				System.out.println(">>> " + word + " <<<");
				if ( success ) {
					System.out.println("YOU WIN");
				}
			} else {
				attempts--;
				if ( attempts > 0 ) {
					System.out.println("try again. " + attempts + " left.");
				} else {					
					System.out.println("You lose. It was " + hangman.GetSecretWord());
				}
			}
			


		}
		in.close();
		
		// 3. Game close
		
	}
}
  • Play 1: win
HANGMAN. New Play.
>>> _ _ _ _ _ _ _ _ _ _  <<<
e
>>> _ _ _ _ _ _ _ e e _  <<<
a
>>> _ a _ _ _ _ _ e e _  <<<
t
>>> _ a _ _ _ _ t e e _  <<<
s
>>> _ a _ _ _ s t e e _  <<<
b
try again. 6 left.
c
try again. 5 left.
h
try again. 4 left.
p
try again. 3 left.
n
>>> _ a n _ _ s t e e n  <<<
o
>>> _ a n _ o s t e e n  <<<
m
>>> m a n _ o s t e e n  <<<
g
>>> m a n g o s t e e n  <<<
YOU WIN
  • Play 2: lose
HANGMAN. New Play.
>>> _ _ _ _ _ _ _ _ _ _  <<<
e
>>> _ _ e _ e _ _ _ _ e  <<<
a
try again. 6 left.
i
>>> _ _ e _ e _ _ i _ e  <<<
p
try again. 5 left.
s
try again. 4 left.
r
try again. 3 left.
y
try again. 2 left.
c
>>> c _ e _ e _ _ i _ e  <<<
l
>>> c l e _ e _ _ i _ e  <<<
m
>>> c l e m e _ _ i _ e  <<<
q
try again. 1 left.
z
You lose. It was clementine

Note: I will use Java.

0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement