Monday, August 23, 2021

Hexapawn–An Interesting Programming Project

I'm cleaning up and finding interesting things. Well, actually, interesting books. One find is called "A Collection of Programming Problems and Techniques." Copyright 1972  So obviously, continued cleaning up is on pause while I code up solutions to some of the problems.

One of the problems in the book is Hexapawn (Hexapawn on Wikipedia) I remember this being a sort of challenge program when I was an undergraduate student. I didn’t take it on back then and forgot about it until running into it the other day.

I decided to take this on. I think it will be a good project for students. As did my old professor.

Hexapawn is played on a grid, often a 3 by 3 grid, with “pawns” on the top and bottom rows. Pawns, as in Chess, can move one square forward or diagonally right or diagonally left to capture pieces belonging to the other player.

 

A player wins by advancing their pawn to the opposite side of the grid or by preventing the other team from making a legal move.

It’s a simple game to play but takes some work to program.

One of the issues is that there are some tricky edge cases to be aware of. While pawns in the center column need to check for three possible moves, pawns in the left and right columns only have two possible moves. That can be a lot of special casing. It gets even worse if one uses larger boards. Trying to check for a move to the left for the left most pawn will create an error in many cases.

Years ago I ran into a boundary condition like this and a member of my team made a suggestion that I have used many times since. She suggested boxes outside the visible grid that, along with not showing, were marked as illegal moves. This means that one can check for straight, left, and right moves for all pawns without getting a subscript out of range error.

An other project I have used this technique is with the lights out game. In that game there are four squares that have to be checked in interior squares, three for edge squares, and only two for corner squares.

Using an outline of invisible squares outside the main set of squares allows the same code to be used for all squares.

I wrote a bit about the Lights Out project some years ago at The Complex Question of Complexity in Programming by the way.

I’m not quite done with my Hexapawn project but I am really understanding how it makes for a good programming project. One can set student requirements low or high. One can assign a two player game or ask students to write an artificial intelligence to play against a computer. It’s a game that lends itself to creating a heuristic program (think machine learning) for more advanced students. Low floor, high ceiling.

No comments:

Post a Comment