ConnectFourLaz

Game by Alex Karet Y13 Age ~17

Introduction

Connect4 simulations are suited to testing a programmer's ingenuity and resolve. You might be content to display the moves of two opponents and check for the first vertical, horizontal, or diagonal line of four similarly coloured counters. Alex implements this as well as the far more difficult player versus AI, for which he uses the Minimax algorithm. Not only this, but he has developed a version with a scoring system (and the ability to log for debugging purposes during development).

Alex developed the program using Delphi 2007, and wrote the code with possibilities for future expansion in mind. We made slight changes to provide you with a Lazarus version in order to be accessible to all. We formatted each unit with the CTRL+D key combination, and removed some unnecessary files, variables and parameters to make the code easier to follow. We also make available in the Download section the executable of the scoring version together with its code in a separate zip file.

Game in progress

Game in progress (Lucida Console size 18 font)

Technical Features

The program illustrates the use of:

  • appropriate comments;
  • an enumerated type;
  • a set constant for the range of the stepper variable in a for loop;
  • classes and objects;
  • a dynamic array;
  • own functions with several parameters to make them versatile;
  • many inbuilt routines such as Assign, Reset, Close, Val, SetLength, ClrScr, TextColor, StrToInt, FloatToStr, Pos and RightStr.
  • validation of menu item selection;
  • file handling and string manipulation.

Download

Download the executable and source of the main version in ConnectFourLaz.zip and/or a working version with scoring enabled in ConnectFourLazScoring.zip. To run a version, unzip to a suitable folder and double click on the .exe file. (Supplied text files such as settings.txt and StartMenu.txt must be in the in same folder as the executable. It helps to increase the font and height of the console as described here. To select a menu item, type 1 or 2 then press enter. To choose a column, just type a number and do not press enter. (In certain situations there may be an appreciable delay before the response). Let us know if you beat the AI! The source files are included so that you can experiment with the code. There is a commented-out line added belatedly to the placing unit (on the next page but one) that you might like to uncomment to make the game easier to play:
writeln('Last Play was: ', Guess);

The Code

Follow the numbered links to see the code. Alex suggests that you might look through the code to work out why in certain situations the AI will prefer to delay winning.

Programming - a skill for life!

BlendingEllipses, Hangman and ConnectFourLaz by Alex Karet