Programming Coursework Project

My programming module in my first semester requires me to write a programme to play the game Noughts and Crosses.

The games requires:

  • Displaying the grid which updates after each turn
  • A check for when the game is complete (won or drawn)
  • Option to play again after the game has finished
  • Option to play against the computer or against another human player
  • Artificial intelligence for how the computer takes its turn instead of just making random moves
  • Ability to save the game mid-way through for completion later
  • Documentation, instructions to the user, comments in my programme to explain the code, and use of docstrings.

When first starting this project, I began by planning out how the game of Noughts and Crosses is played normally, and broke down into stages the different parts, and functions required for the game. From this I could think of it from a programming point of view and set out what processes and functions I will need to develop.

To do this I first put comments for each section and sub-section of my code and ordered them in the order I thought they would need to be in. I like to do this when starting a project because it helps to set out what I am going to have to do; instead of just diving straight into coding, I feel that good planning helps a lot. Obviously I cannot plan everything and when I started coding I soon found that I had to rearrange the order, or maybe add in more sections than I initially thought were needed, but this is a normal process when programming and I still found that it helped a lot to set out the foundations of my programme. This process also allows me to set out the requirements for the game and allows me to meet the minimum requirements for the game before moving onto other sections.

One way to help me plan and organise this project was using a flow chart to illustrate the structure of my programme. Below is a copy of the final version, but this was updated and edited throughout the development process until I got it to a stage I was happy with:

Assignment Flow Chart PNG

One of the main features the game must include is for the game’s grid to be updated after every move and displayed to the player. For this I could have just printed out the grid after each turn, but instead I chose to use Python’s turtle function. Turtle is a feature of Python which allows you to draw lines and shapes in a window, so is an ideal representation of when you draw the noughts and crosses if you were playing the game on paper. This also meant that I didn’t have to keep showing the grid multiple times, as the grid would just update and is always in the same window. This is a feature of my game I was really pleased with, and I am glad I made sure to include this as it makes it look higher quality and more professional; as well as being easier for the user to see what is going on. This was something which helped improve my game, so I didn’t include this until the basic functionality of the game was done first, and then I could focus on using turtle and making a graphical version of the game.

To check if the game had been completed (won or drawn), I used a global list variable to represent the grid squares. This list is what is updated throughout the game and is the key part of the programme. Every time the player makes a move, a function is called to check this list. This checks to see whether there is a winning scenario depending on the values in the list, or if the list is full with no winning scenario then it is a draw.

These features I have so far discussed are all for when playing versus another human player; however, one of the main aspects of the game is playing against the computer. After I created a multiplayer game that I was happy with, I started developing a version where you play against the computer. To include this, the game now includes an option for the user to choose if they want to play multiplayer or versus the computer.

When playing versus the computer, the computer cannot just be making its turn at random and so there needs to be some sort of artificial intelligence in the decision making of how the computer picks its turn. I have created a separate blog post detailing how this was implemented here.

One of the last features I included in my game was the option to save the game and complete it later. To do this I used Python’s pickle feature to save and load to a text file. I saved the grid squares list to the file and from reading this the game could load up to the same point it was at and then continue the game. One problem for this was that because I was using turtle, it meant adding a loop to redraw all of the already existing symbols again, but after this is done the game can continue as normal. One issue I had was implementing the save/load feature for the game versus the computer. I was saving the list to the file and there was no way of loading this data to find out which game mode had been selected. Therefore, I decided to create a global variable for the game mode that was selected and pickle this variable as well so that it could load this and know which type of game was saved.

Throughout my programme I have made sure that it is documented using comments and docstrings. This has meant it is really easy to see what is going on through my programme and when errors have occurred it meant that I could easily identify what went wrong. For all of my functions I have made docstrings which sets out what each function does step by step, and which function call or output is returned from the function. Also for some areas in the code it has been useful to have short descriptions in comments of what the variables, if statements, and loops are actually doing.

Overall I am very happy with my programme and think I have achieved what I set out to do. However, I feel that I could have gone one step further with my programme to improve it. I was considering have a feature where the user can point and click on the grid squares to make their move, instead of entering a number corresponding to the grid square. This would have been a great feature to have because it would have meant not needing to explain to the user which number represent which grid square. However, this would have taken more time than what was needed and doesn’t offer any further functionality to the game, so I decided to leave this out.

Leave a comment