Game by Alex Karet Y13 Age ~17
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 (Lucida Console size 18 font)
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.
writeln('Last Play was: ', Guess);
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.