# Please do your best to complete this assignment on your own, and resist the temptation
# to look at someone elses solution, or to copy someone elses work.
# Students who are found to be in violation of the policies regarding academic integrity
# will be reported to the dean of students.
# You will get out of this class what you put in.
# You will only get better at coding if you challenge yourself.
# This assignment is intended to be challenging, but not insurmountable.
# Your code need not be very complicated. You will NOT be graded on coding
# efficiency, but so you have an idea, my implementation is 145 lines of code.
# These are some recommendations for tic-tac-toe:
# You are welcome to implement the game another way.
# You will be graded on whether your game works or not. Point details are in the HW assignment instructions.
# First, there are 8 ways to win. I have stored them as a list called triples.
triples <- list(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))# The `state` of the game can 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.# For example, when I start the game, I made a vector called state with 9 NAs# state# [1] NANANANANANANANANA # Let’s say ‘x’ plays in the middle spot (position 5)# the state becomes:# [1] NANANANA”x” NANANANA # When I created the game, I created the following functions:# display(state)# displays the current state of the board.# prompt_user(who, state)# prompts the user for where they want to play.# checks to see if the move is legal or not# update(state, who, pos)# updates the state of the board by putting an x or o (who) # in the designated position (pos) # computer_turn(state)# has the computer take a turn. The input is the state.# The function returns the position where the computer will play.# check_winner(state) # checks if there is a winner.# play() # the ‘wrapping’ function that lets you play a game by combining the above functions.# The display(state) function presents the board as a 3×3 grid with numbers in the positions as follows.# I used cat() with the state vector to create the board#x | 2 | 3# —+—+—#4 | o | 6# —+—+—#7 | 8 | 9# As the game progresses, the display function should output the current state of the game board. For example:# The function `update(state, who, pos)` takes the current state of the game and puts in an ‘x’ or ‘o’ in the designated position. # This function should be very simple to implement.# The `computer_turn` function reads the current board and returns where it will play next. # The `computer_turn` is able to deduce whether the computer is playing as x or as o.# The function also implements some basic strategy (wins and blocks).# The `play` function puts everything together.# The function first asks if there is one or two human players. # If there is one human player, it asks if the human will play first or second.# I’ve outlined in psuedo-code how I imagine you can set up the play function:# # play <- function(){# # 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# }# These are recommendations that I make for programming the game. # You are welcome to implement the game another way.# You will be graded on whether your game works or not. Point details are in the HW assignment instructions.
Reviews
There are no reviews yet.