Interactive Tic Tac Toe Stefan Bennie Botha Thesis presented in fulfilment of the requirements for the degree of Honours of Computer Science at the University of the Western Cape Supervisor: Mehrdad Ghaziasgar Co-supervisor: Reg Dodds April 2016
ii
Declaration I, Stefan Bennie Botha, declare that this thesis Interactive Tic Tac Toe is my own work, that it has not been submitted before for any degree or assessment at any other university, and that all the sources I have used or quoted have been indicated and acknowledged by means of complete references. Signature:........................ Date:........................ Stefan Bennie Botha. iii
iv
Abstract The game tic tac toe had an early variant which began in the first century in the Roman Empire. During that time it was called Terni Lapilli where the players only had three pieces and had to move around the empty spaces to play. The actual game of tic tac toe could be traced back to ancient Egypt. The game was also known during that time as Three Men s Morris. The first reference to Noughts and crosses was made in 1864 in a British novel called Can You Forgive Her. For many years the game was referred to as noughts and crosses but was changed in the 20th century by the United States to tic tac toe. This paper will consist of a user requirements document as well as a requirements analysis document. The pupose of this paper is to create a program by which the user can play a game of tic-tac-toe using a sheet of paper as well a pen. The program will pick up the changes on the sheet of paper and superimpose its move on the screen showing the user which move was made. Various requirements will be handled in this paper with the help of one image to explain effectively what is happening. This project is designed purely for the sake of interest sake and thus has no real need at this current point. v
vi
Key words Support Vector Machine Visual Programming Graphical User Interface Requirements Analysis Document User Requirements Document vii
viii
Acknowledgment I would like to thank my supervisor Mr. G Ghazi as well as my co-supervisor Mr. R Dodds for their assistance with designing my project. Their help and support was really appreciated immensely and I would not be where I am today without their help. ix
x
Contents Declaration................................. Abstract................................... Key words.................................. Acknowledgment.............................. iii v vii ix List of Figures............................... xiii Glossary................................... xv 1. User Requirements Document................... 1 1.1 Introduction............................. 1 1.2 User s view of the problem..................... 1 1.2.1 Requirements expected from program............... 2 1.2.2 Requirements not expected from the solution.......... 2 2. Requirements Analysis Document................. 5 2.1 Introduction............................. 5 2.1.1 Designer s perspective....................... 5 2.1.2 Existing solutions.......................... 6 2.1.2.1 First Solution............................ 6 2.1.2.2 Second Solution........................... 7 2.1.2.3 Third Solution........................... 7 2.1.3 Best solution............................ 8 2.1.4 Testing............................... 8 3. Conclusion............................. 11 Bibliography................................ 12 xi
xii
List of Figures 1.1 Interactive Tic Tac Toe Representation.............. 3 xiii
xiv
Glossary SVM Support Vector Machine. RAD Requirements Analysis Document. URD User Requirements Document. CSV Comma separated variables. OpenCV Library used in Python for image processing. RASAC Random Sample Consensus. Custom Library designed and used in research paper. SURF Speeded Up Robust Features. Custom created method used in research paper. xv
xvi
Chapter 1 User Requirements Document 1.1 Introduction The user requirements document (URD) serves as a description of the user. It explains how the user sees the project by explaining it in a non- technical form. The URD will cover topics such as: the user s view of the problem, what is expected in the software solution and what is not expected. The first topic that will be discussed will be the user s view of the problem. This section will explain what the user wants to be implemented. Topics such as the requirements expected from the project will be discussed as well as what is not required. The URD will serve as the main source of information regarding how to build/construct the project to meet the exact specifications of the user. 1.2 User s view of the problem A interactive tic-tac-toe program is needed where the player will be able to play against the computer by using a piece of paper and a web camera. The camera will be mounted facing the sheet of paper at all times in order to prevent confusing the program by introducing different angles. The player draws a grid on the paper which will serve as the game board for the current match. When the player makes their move on the paper, the program will pick up this change on the board and make its move by superimposing a image representing its move towards the user on the screen. The game will end when one of the players is considered a winner or when it is considered as a draw when all entries are filled with symbols. 1
2 1.2.1 Requirements expected from program This section will explain in bullet form a few expected requirements regarding what the program should do.the requirements regarding this project/program will be listed below: This game has to be able to run on most modern computers and also on any Windows or Linux system supporting the Python environment. The game should be user friendly. The game should be easy to set up. The external peripherals should not be expensive. It should be flexible and be able to work on any similar web cam to the one currently in use. 1.2.2 Requirements not expected from the solution This section will explain in bullet form a few expected requirements regarding what the program should not do.what is not expected from the solution will follow below: The program should not be able to detect any other game on paper that is not related to tic-tac-toe. The game should not be slow and tedious to play. The program should not be complex to use. The game should never crash at any moment. The program is not expected to track games with the camera at a angle to the paper. The program is not expected to work under poor light conditions.
3 The figure below explains visually how the game will be played. Starting from the left side the computer will display the tic-tac-toe grid on the screen. Following the arrow the web cam will proceed to pick up the grid or any changes made withing the grid. The next arrow represents the move the player made on the sheet of paper. Lastly the change is picked up by the program and its move is made by superimposing a image representing their symbol on the screen. This cycle is repeating until a winner emerges or a draw occurs. Figure 1.1: Interactive Tic Tac Toe Representation
4
Chapter 2 Requirements Analysis Document 2.1 Introduction The requirement analysis document (RAD) explains how the program will work in more detail. In this document there will be various sections and subsections explaining what this program will consist of. This document will consist of a designers perspective, the identifying of existing solutions, deciding the best current solution and various ways to test the solution. The designer s will explain the requirements needed regarding how this program will be implemented. 2.1.1 Designer s perspective This section will discuss what needs to be done from the designer s perspective. This section also consists of possible attributes which will be considered when designing the program. The program will consist of a web camera picking up a drawn tic tac toe board on a clean piece of paper. The program should detect the board on the sheet of paper and notify the player that the game has begun. The game will continue to be played until all the white space inside the board is filled by a symbol or when three identical symbols are detected in a vertical, horizontal or diagonal line. The following bullet points represented below will provide more detail concerning this project. A mounted hi-definition web camera will be used,facing the sheet of paper, to ensure that a clear picture is provided to the user. A normal desktop computer will be used in order to create this program. 5
6 This program/game will be played strictly indoors to limit any changes in light affecting the program. The program will be written in the Python programming language using the OpenCV library. The OpenCV library will handle the image processing needed by this project. The board and the symbols will be converted into grayscale to eliminate the factor of which colours are used for the symbol or board. A Support Vector Machine (SVM) will be used to train the program on how to recognize different symbols as well as picking up the grid. The information given above demands the player to play in a controlled environment to ensure that the program will not crash or exit unexpectedly. 2.1.2 Existing solutions This section will cover different possible solutions which could be implemented in order to solve the problem. In each subsection, a different solution will be described in high-level which could provide some oversight as to what needs to be done. 2.1.2.1 First Solution A possible solution was created by Maguire and Saltzman (Joe Maguire, 2014) where by they implemented this project successfully by doing the following. They started by breaking down their algorithm into three parts: Acquiring the homography,in other words the board; detecting if the player moved and updating the projected board. To acquire the board they processes the current frame using the Speeded Up Robust Features (SURF) algorithm. They wanted to take advantage of the fact that the board consisted of four lines initially, but it turned out to be picking up too much noise as the camera was moving further away from the board. The SURF algorithm worked exceptionally well since it was able to pick up key points, which are the edges of the board very well when a marker was used to draw the board. To discard false key points they used the Random
7 Sample Consensus (RASAC) feature. In order to pick up the symbols they used a OpenCV function called matchtemplate which was used after they took the nine subsections of the board, each corresponding where the player could potentially draw a symbol. To update the board they created an artificial intelligence agent called MinMax which detects any movement on the board between 5 10 frames per second. This method turned out to be robust since the camera had to mounted close to the board but it still did the job. In other words template matching was used to locate the grid effectively. 2.1.2.2 Second Solution Another solution could be to read in images instead of a real-time feed from the web camera. The pictures would then eliminate the factor of the player s hand coming into play. The program can be considered as much simpler to create and also eliminates a fair amount of problems that can occur. The picture can then be converted into grayscale which eliminates the variety of colours the player might use. After the conversion, a histogram can be used to detect any non-white pixels on the board which could potentially be the lines of the board. The use of morphological operations can also be applied to reduce the amount of noise found in the images. This solution will be programmed in Python using the OpenCV library. A artificial intelligence agent will need to be created which will play against the user. The program will also require some training to detect what each symbol as well as what the board will look like. A decision will then be made by the program whether the game has started according to what player has drawn on the page. 2.1.2.3 Third Solution This solution consists of using real-time Haar-cascades and other morphological operations. All tic tac toe boards have similar features which means by using Haar-cascades the same Haar-features will be used to detect every
8 board. By using morphological operation, the image can be cleaned up by removing any noise captured by the camera. This will, however demand more performance in order to be executed. The reason this would require more performance would be that thousands of Haar-Cascades will be applied to every real-time image. Other sections of this solution will be considered similar to the second solution mentioned above meaning that this solution will also require some training to be done as well as an artificial intelligence agent to be created. This solution will also be created in Python since it is an flexible and easy to understand programming language. 2.1.3 Best solution This section will discuss which solution is considered to be the most effective and easiest to implement. This section will also discuss some reasons as to why this solution is considered. All the above solutions are very promising to yield results, but the second solution seems like an effective solution to implement. The reason why this solution was chosen was not because it is easier to implement but it serves as an good starting point to serve a basic program towards the user. This solution, however could evolve in the future to meet all of the requirements set out by the user which allows this to serve as a prototype in the meantime. The fact that the image is converted into grayscale would also make the computations easier to calculate since the array elements for each picture is considerably less. Another reason why the second solution was chosen was because the Haar-Cascade is used for detecting faces which is not needed since this project does not detect faces but rather tic tac toe boards. 2.1.4 Testing This section will touch briefly on how one can test this program to make sure it is working effectively. The steps to testing will be listed in bullet form and will follow below. Compile the code and check if any errors occur.
9 Test if the board and symbols are recognized by the program. Test the web cam by taking example pictures and displaying them back to the screen. Test the tic tac toe algorithm. The above steps are general testing steps to ensure that the code is in working order and that it is ready to run as the program.
10
Chapter 3 Conclusion This tic tac toe project seems quite the interesting and challenging project to accomplish. By designing a RAD, to explaining logically how to meet the user s requirements mentioned in the URD, will indeed be helpful during project implementation. By discovering various solutions it shows that this project is in fact possible if not easy to create. The use of image processing, by using OpenCV, and its morphological operations will serve as a starting point to begin this project. 11
12
Bibliography Joe Maguire, D. S. (2014). Augmented reality tic-tac-toe. Technical report, Department of Electrical Engineering. 13