PHYS291 project – “One left” game

Rules:

You have 32 pieces lined up in a cross (like 4 overlapping squares with 9 pieces each), with one free spot in the middle. The aim of the game is to remove all but one of the pieces. You can move a piece to a free spot by moving it over one (and only one) other piece. When you have done this you remove the piece between the origin and destination. You can move the pieces north, west, south and east directions, but not diagonally.

How to play:

You play this game by clicking on the piece you want to move, and then click on the empty spot you want to put it. The middle piece will then disappear automatically. The white spots in the corners are forbidden areas.

Statistics:

When you are done playing, click the blue button. The number of remaining pieces will then be stored in the file gamestat.txt, and you will see a histogram showing how many pieces you have left. This will happen every time you play the game (and end it by clicking the blue button), so you can follow your improvement.

Code files

Click on the link and find the attatched code files. “oneleft4.C” is the game part of the project, while “gamestat.C” is the statistics.

Oneleft4.C is divided into separate functions. “Void oneleft4()” creates the canvas and draws the gameboard. “Void flytt()” implements mouse click and makes it possible to manipulate the gameboard drawn in the first function. Here I added some if sentences so that the game is played by the rules, and it should not be possible to move pieces too far, outside of the game area etc. “Void writetofile()” stores the number of remaining pieces in the file gamestat.txt. Some of the variables are global since they are being used in all the functions. I found it useful to have such I structure in the code, since I found it to be easier to deal with when something went wrong.

In gamestat.C you will find the function “void gamestat()” which uses the data in the file gamestat.txt to represent it in a histogram. I found it practical to have this in a separate file, since it can easily be changed to be used for other purposes, and it's not essential for the game to work.