Tic Tac Toe - Computer Science Project Report

Meeting The Brief

Investigation and Plan

I started my investigation by looking at many different board and card games. I used search engines such as DuckDuckGo and Google to find info on many board and card games. I looked at Snakes and Ladders, but the game is only luck based and there is no way for players to improve and get better at the game. I looked at Solitaire, Uno and Chess and these games would work for this project, but I think that they are too complicated for this project and take up too much time to program. I settled on doing Tic Tac Toe because it has the ability for players to get better at it and the game has a bit of probability to it too. I have looked at the rules and other digital versions of Tic Tac Toe and how they work (see references). I decided to not do graphics and have the game be rendered in the command line as I don't have the time to learn and make graphics for the game. My project will be written in Python as it is the programming language I know best, it will be rendered in the command line and it will be mostly written in a functional programming way to make the program as easy to code and modify as possible. My objectives are to make a working project that hits the below requirements:

Meeting Basic Requirements

Meeting Advanced Requirements

Design

First, I thought about all my requirements and how I will meet them. To take 3 inputs, I will have the program take three inputs: the user’s name, the user’s email and the game mode the user wants to play in. Next, I decided to make the main game in the program as a function with inputs that will decide which players are human or the computer. If a player is human, it will ask the user for an input to pick the player’s choice for the board. If the player is the computer, it will generate a random number between 1 and 9 as the player’s choice for the board. If the player's choice on the board is already occupied, a human player will be asked to input a choice for an empty spot on the board, and a computer player will generate a new number. The human player will kept being asking for a move until an empty space is chosen and the computer player will continue to generate random numbers until a number representing a empty space is generated. After each player move, the game will check the board against a list of winning game boards and it will also detet if the game turns out to be a tie. If it is a win or a tie, a message will be displayed saying if it is a tie or which player has won. If the game is over the user will be asked if they want to play again. If the user answers yes, then the main game function will be ran again with the same inputs and if the user answers no, the program will exit. When the game finishs it will record game data into a csv file including all moves of each player. I will then program a menu that allows you to pick what player's moves to use and what type of statistical analysis to do on those moves like getting the mean, mode, median, etc. The 3 inputs of email, name and game mode at the start, having the function to be able to run multiple game modes, recording csv data and using this csv data to do statistical analysis should reach my requirements.

Psuedocode and Flowchart

Psuedocode Design Flowchart

The abstraction of my program is where I didn't do graphics. This massively simplifies making the game as I don't have to learn graphic making and it keeps everything inside the command line.

I used a function based model to make my program where pieces of code that would normally be repeated can be put inside a function and then that function can be called when needed. I have designed the functions to be adaptable using function inputs, so that one function can do multiple things.

Implementation

I wrote the program using Visual Studio Code using the Visual Studio Code Python Extension Pack that provided debug tools and syntax highlighting. I started off doing the inputs that run at the start of the program. I also added than the inputs can be put in at the command line when running the command to run the program to speed up testing later. I then did the game mode menu and inputs to the main game function. Next, I made the main game function and made it be able to play each game mode based of which inputs were passed through to the main game function. I wrote most of the code from scratch as I have much experience with Python and I have certain code style I like to stick to. For things, that I wasn't completely sure on, needed a refresh on or didn't know, I looked up the required documentation, articles or answers on forums like StackOverflow and wrote my own code based of the information I found or borrowed small bits and messed with them to get used to how those bits of code worked. The system I had designed had implemented well and not much of it had changed other than adding code to made certain things work, fix certain issues or add things I forgot to plan in the design phase. My process of making the program was to look at my design and then write Python code that did what was in my design. I later on had to add code for requirements and features I forgot to plan to, but other than that everything in the system I planned in my design, made it through to the program.

One problem I had during programming was during I part I completely forgot to design, which was making the program save and record csv data and then using that data to do statistical analysis. My problem was that I saved every move a player made as a string in the csv file that might look like this: "2,4,6,3". The reason for this was that I could have every line of the csv file be one game, but also have each line include all of each player's moves. My solution to this problem was with string manipulation. What I did write a function that takes the string and take the first, third, fifth, seventh and ninth (if it exists) and put them into their own lists based of if it was the first move, second move, third move, etc.

A section of code I would like to discuss is the statistical analysis section of the program. It is the fourth option on the game mode menu after the inputs for name and email. After selecting statistical analysis, it will ask for whether to do analysis on Player 1 or 2 and then what kind of statistical analysis to do on that players moves. The options are Frequency, Mean, Median and Mode. After selecting your option for what kind of analysis to do, the program will display the stats you wanted and exit.

Testing

I have tested my project numerous times after finishing the programming of it and I tested some parts of it, while programming, which is form of testing called Unit Testing. One problem I came across while doing Integration testing (where you test parts of the system together) on the main game function and the different choices at the start of the program for the gamemodes that input into the main game function was that sometimes the tie detection would go off and say that there was tie even when there wasn't. I never figured out why this happened but after moving some code around trying to find why this happened I somehow fixed it. I did some System testing (testing of the whole program) afterwards and everything seemed to be working fine. My testing shows me that my program works successfully and doesn't have any bugs that I can detect.

Action Input(s) Expected Output(s) Actual Output(s) Test Result Test Data Used Comments
Accept and validate username Any name that is 6 or more characters long Accepts a valid name and moves on Accepted a variety of names containing various characters that were 6 or more characters long and moved on Pass “John,” “Mary”, “(^*$”, “c00lj0e78”, “أَمِيْن” and “明美” It also accepted names with symbols, numbers and even non-English characters
Accept and validate email Any input that contains the “@” character Accepts a valid email and moves on Accepted a variety of strings, if the string contained an “@” and moved on Pass “Jim@yahoo.com”, “coolbeans@gmail.com”, “jim@yahoo” and “beans_hotmail.com” It also accepted emails that weren’t real or had a proper domain e.g., it accepted jim@yahoo, while it should be jim@yahoo.com
Accept and validate game mode The numbers 1 to 5 Launches game mode that the number the user inputted corresponds with Accepts the numbers 1 to 5 and launches the game mode associated Pass 1, 2, 3, 4, 5, 6, 0, “one”, “two”, “zero”, “four”, “three”, “five”, “Singleplayer”, “singleplayer” and “multiplayer” It didn’t accept the words “one”, “two”, “three”, “four” or “five” and it didn’t accept the names of the game modes
Main game Player's move After each player move it shows board or if a player won If a player has won, it displays that that player has won and if no player has own it shows board and then accepts the next player’s move and this repeats until someone wins Pass 3, 2, 5, 4, 1, 9, 0, “nine”, “ten”, “Four” and “Foot” It accepted any number from 1 to 9, but not 0 as there is no square labeled 0. It also didn’t accept the strings “nine”, “ten” or “Four”

Evaluation

My initial design was a little barebones and could had more work. I didn't design and left out some crucial parts that were apart of certain requirements and completely forgot to plan some requirements. However, my entire design has made it through to the final project without much modification. I did have to add in bits that weren't in the design for the parts I didn't plan out. While making my plan I feel I had a clear idea of what the Python code would be and the way it was going to work out.

My initial objectives of making a functional tic tac toe game, having 3 inputs, having multiple game modes, saving and storing data in a csv file, doing statistical analysis on that csv data and writing it in Python I feel like I have achieved. Some of these objectives I had forgotten to plan, but was able to add in after programming the parts I did design. Any issue I ran into that would have stopped me from reaching those goals I was effectively able to fix or work around.

References

  1. DuckDuckGo - Search Engine used to find out information, references and resources
  2. Google - Search Engine used to find out information, references and resources
  3. Games I Considered Doing

  4. Snakes and Ladders - This game is only luck based and there is no way for the players to get better
  5. Solitaire (Patience) - This game I found to be too complicated and/or time consuming to program for this project
  6. Chess - This game I found to be too complicated and/or time consuming to program for this project
  7. Uno - This game I found to be too complicated and/or time consuming to program for this project
  8. Game I Picked (Tic Tac Toe)

  9. Rules of Tic Tac Toe and Online Game Version of Tic Tac Toe - Rules on how Tic Tac Toe works and an online version to get used to how the game works
  10. Online Tic Tac Toe Game on Google - Another online version to get used to how the game works
  11. Tools Used to Make Artefact

  12. Visual Studio Code - The IDE I used to program the artefact
  13. Visual Studio Code Python Extension Pack - Extension pack I used to help with programming by adding colouring and debugging for python to Visual Studio Code
  14. Links Used to Refresh My Memory, Solve Problems I Ran into and Learn About How to Use Something

  15. W3Schools: Python Functions
  16. StackOverflow: How to break out of a while loop in python
  17. DuckDuckgo Images: tic tac toe ascii
  18. GeeksforGeeks: Decale an empty list in python
  19. W3Schools: Python Lists
  20. StackOverflow: How to check if the string is empty
  21. StackOverflow: Greather than or equal vs equal or greater than in python
  22. TutorialsPoint: Python command line arguments
  23. GeeksforGeeks: Find length of a string in python - 4 ways
  24. StackOverflow: Check if string contains specific character
  25. StackOverflow: Checking if sys argvx is defined
  26. W3Schools: For loops in python
  27. PythonGuides: Python check if the variable is an integer
  28. StackOverflow: Check if string is in a list
  29. RealPython: Convert python string to integer
  30. GeeksforGeeks: Convert string to integer in python
  31. W3Schools: ref string isdigit
  32. StackOverflow: How can I randomly select an item from a list
  33. GeeksforGeeks: Python replace substring in list of strings
  34. PythonCentral: How to generate a random number in python
  35. StackOverflow: How do you check in python whether a string contains only numbers
  36. GeeksforGeeks: Python check if two lists are identical
  37. W3Schools: Python booleans
  38. StackOverflow: How to loop back to the beginning of a program python
  39. GeeksforGeeks: Python check if a list exists in given list of lists
  40. PythonforBeginners: List of lists in python
  41. DuckDuckGo Images: tic tac toe all possible wins
  42. PythonExamples: Python if and
  43. W3Schools: Continue function
  44. W3Schools: Python write to file
  45. W3Schools: Python append to list
  46. StackOverflow: How to convert list to string
  47. Official Python Documentation: Statistics Module
  48. GeeksforGeeks: Python read csv columns into list
  49. StackOverflow: How to read specific column index from csv using pandas
  50. StackOverflow: How do I check whether a file exists without exceptions
  51. DuckDuckGo Images: Columns vs rows
  52. StackOverflow: How to take only first four elements of every string from a list
  53. StackOverflow: If list index exists do x
  54. RealPython: Python Rounding
  55. StackOverflow: How do I check if a list is empty
  56. StackOverflow: How to count the frequency of the elements in an unordered list
  57. GeeksforGeeks: Python ways to remove n characters from start of given string

Summary Word Count

Section Word Count
1. Meeting the Brief 0 (Video)
2. Investigation and Plan 422
3. Design 459
4. Implementation 518
5. Testing 441
6. Evaluation 387
Total: 2227