Hangman

by Alex Karet: Y12 Age ~16

Introduction

Alex is a self-taught programmer who is picking up Pascal quickly and is already teaching it to younger students. Not content with a basic implementation of hangman, he takes the trouble to display a scaffold and hanging man with ASCII characters. He also allows you to select word lists of varying difficulty. (Alex downloaded the longest word list from mieliestronk.com then selectively deleted items to produce the other lists. You might like to view these text files to see the wide range of targets, including some cities and a few phrases such as runofthemill and ragstoriches). Furthermore, Alex has developed a scoring system to encourage users to continue to play.

This zip file contains the source code files, the executable and the three word lists.

The following screenshot shows the gallows in action, and one further wrong guess will result in a loss. Can you guess the remaining letters?

Execution?

Execution?

Immediately after executing Hangman for the first time, set the height of the console window as follows.
  1. Click on the icon at the top left of the console window.
  2. Click on Properties.
  3. Select the Layout tab.
  4. Set the height of the screen buffer and of the screen to 40.
  5. Click on OK.
  6. Close the console and run Hangman again to use the new settings.

See the main program below then follow the links for the code of the units.

The Program

program Hangman;

{$APPTYPE CONSOLE}
{
    Copyright (c) 2014 Alex Karet

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ 

}
(*
Units are used although not necessary in order to allow for easy adding of tracking code. This
will then be used as a way of creating an AI to guess words although not implemented yet
*)
uses
  SysUtils, StrUtils, crt, Draw, Variables, Words,  Background;
  //calling in crt in order to use the clear screen function.

begin
  play := 'yes';
  Getwords; //imports the words
  run := 0;
  repeat
    //Game plays until user loses and quits
    ClearGuess;
    Inc(runNumber);
    Inc(run);
    ClearGuess;
    WordSelector;
    wrong := 0;
    DrawGuess(' ', True);
    Count := 0;
    AssignLetter;
    Writeln('');
    repeat
      Drawhangman;
      GetGuess;
      Clrscr;
      DrawGuess(Guess, False);
      Guessing[Count] := Guess;
      AssignLetter;
      Writeln(' ');
      WorkOutScore;
      Inc(Count);
      if Wrong = 9 then
        begin
          writeln('You Lose! The Word was: ' + word);
          WorkOutScore;
          break;
        end;
    until LettersLeft = 0;
    if LettersLeft = 0 then
      begin
        Writeln('Well done you win!!');
        WorkOutScore;
      end
    else
      begin
        Inc(WrongRuns);
      end;
    ClearGuess;
    writeln('play again, type "no" to quit');
    readln(play);
  until play = 'no';  //checks whether to run again
end.

Programming - a skill for life!

BlendingEllipses, Hangman and ConnectFourLaz by Alex Karet