For this assignment, you will design and implement the Model component of a tic-tac-toe app. The View and Controller components are provided for you in the TicTacToe project. You should first download this project and familiarize yourself with the provided code. Then, you will implement a struct named TicTacToeGame
, which has the following components:
private(set)
field named board
that represents the tic-tac-toe board. It should be a 2-dimensional array of strings, with each entry in the board represented by either " ", "X", or "O".game.place(piece: "X", inRow: 1, andColumn:2)
. The method should return true if it was able to place a piece at that location (i.e., the spot was not previously taken), otherwise false.game.place(piece: "O")
. The method should return true if it was able to place a piece (i.e., the board was not already full), otherwise false. For 80% credit, this method may randomly select any open spot on the board and place the piece there. For full credit, it should prioritize a spot that leads to an immediate win, followed by a spot that avoids an immediate loss. If neither is possible, it should pick an open spot at random. game.status()
. The possible status strings are "X wins!", "O wins!", "It's a draw.", and "Play on...".
You should first implement and test your TicTacToeGame
struct in a playground. This will allow you to try out each component of the struct independently, without the complications of the View and Controller. For debugging purposes, you will need to write a temporary method for the struct that displays the board contents. Include statements that create a tic-tac-toe game, call the different methods, and display the results.
Once you have thoroughly tested your TicTacToeGame
struct in a playground, add it to the downloaded project. That is, add a new Swift file to the project and copy-paste your struct definition from the playground to this file. Test the complete project to make sure it behaves as described. You may then remove the temporary board display method and any other debugging code you added to the struct.
Once you have a working app, add at least one significant feature to its workings. Possible ideas include a welcome screen with author information and rules, a popover that provides help (for beginners), storing and displaying the player's win/loss record, etc. Be creative!
NOTE: you should turn in both the playground and the completed project.