## Write a program that will play tic-tac-toe using R Studio [worth 45 pts] In this exercise, you will write a series of functions that will allow you to play tic-tac-toe in R. I'm assuming you know how to play tic-tac-toe, but in case you don't, have a friend teach you. It's very easy to learn. Also check out: https://en.wikipedia.org/wiki/Tic-tac-toe In the game you program, X always goes first. O goes second. Your program should provide the option to accommodate one or two human players. If there is one human player, the computer will be the opponent. The `state` of the game should be stored as a character vector of length 9. I used NA for spots that were unplayed, and entered "x" and "o" as the game progressed. You will need to create at least the following four functions. You can choose to create additional functions if you please. ```c display(state) # displays the current state of the board. [5pts] update(state, who, pos) # updates the state of the board by putting an x or o (who) # in the designated position (pos) [10 pts] computer_turn(state) # has the computer take a turn. The input is the state. # The function returns the position where the computer will play. [10 pts] check_winner(state) # checks if there is a winner. [10pts] play() # the 'wrapping' function that lets you play a game by combining the above functions. [10pts] ``` Your `display(state)` function should present the board as a 3x3 grid with numbers in the positions as follows. ```c 1 | 2 | 3 ---+---+--- 4 | 5 | 6 ---+---+--- 7 | 8 | 9 ``` As the game progresses, the display function should output the current state of the game board. For example: ```c x | 2 | 3 ---+---+--- 4 | o | 6 ---+---+--- 7 | 8 | 9 ``` The function `update(state, who, pos)` takes the current state of the game and puts in an 'x' or 'o' in the designated position. It should check to see if the spot is already taken. This function should be very simple to implement. The `computer_turn` function will read the current board and return where it will play next. The `computer_turn` should be able to deduce whether the computer is playing as x or as o. The function should also implement some basic strategy. The computer's turn does not have to be optimal play, but you must implement at least the following logic: 1) if the computer can win by playing a spot, it must play in that spot and win 2) if the human can win by playing a spot, the computer must play in that spot to block. (If the human can win in two ways, then just pick one way to block). If neither a win nor a block can be achieved in the next move, then I leave it to you as to where the computer should play next. You can attempt to implement an optimal strategy, or you can have it play sub-optimally. You do not have to program perfect gameplay. The `play` function puts everything together. It should first ask if there is one or two human players. If there is one human player, it should ask if the human will play first or second. I've outlined in psuedo-code how I imagine you can set up the play function: ```c play # determine game conditons: 1 or 2 players. If computer plays, is it player 1 or 2. # initialize game board # while( no winner ){ # x's turn display() # display board # x chooses where to play. prompt user or computer_turn() update() # update board check_winner() # if x wins - quit loop # o's turn display() # display board # o chooses where to play. prompt user or computer_turn() update() # update board check_winner() # if o wins - quit loop } # display final board state and who the winner is } ``` Hint: I strongly recommend getting the game to work for two human players first. Worry about programming the 'AI' after that. Hint: There are 8 ways to win in tic-tac-toe. I've gone ahead and created a list of these 'triples' for you. ```{r} triples c(1,2,3), c(4,5,6), c(7,8,9), c(1,4,7), c(2,5,8), c(3,6,9), c(1,5,9), c(3,5,7) ) ``` As you program the game, you'll want to check the triples to see if a player has won. I think they will also come in handy as you try to determine where the computer should play. You are not required to do so, but I wrote a "check_winner" function to see if there was a win.