[Optional Extension] Assignment 4 - Tic Tac Toe
Due: Monday, February 17, 2020, at 1:50pm
This assignment is completely optional. If you receive at least 80% of the points, you will gain one extra late day. If you receive at least 50% of the points (but not quite 80%), you will gain half of an extra late day.
You may work alone or with a partner, but you must type up the code yourself. You may also discuss the assignment at a high level with other students. You should list any student with whom you discussed the assignment, and the manner of discussion (high-level, partner, etc.) in a readme.txt file that you include with your submission.
You should submit your assignment as a .zip file on Moodle.
Parts of this assignment:
Getting started
This assignment comes with modified starter code. You can copy-paste it into a window in IDLE – just save it as tictactoeExtension.py
.
Problem 1: Playing a more general game
For problem 1, you should copy your functions from Assignment 4 into the new starter code. Make sure not to overwrite any of the provided functions! The changes to the starter code allow the game to be more general, not just 3x3.
You should modify all of the functions you implemented in Assignment 4 to remove any assumption that the board is 3x3. Instead, your game should support an n
xn
board.
(Hint: you can check the size of boardState
to figure out what n
is.)
In the test code at the bottom, you can choose a value of n
. It is initially set to 3, so your code from Assignment 4 should behave the same once you’ve copied it over.
if __name__ == "__main__":
n = 3 # try replacing with 4 or 5
# Problem 1
testDrawPlayerMarker(n)
# Problem 2
testPrintBoardState(n)
# Problem 3
testPlacingValidMarkers(n)
# Problem 4
playGame(n)
Once you’ve got that working, try out other values of n
, like 4 or 5. Make sure your game works for any value of n
that is 3 or higher.
Problem 2: Adding a win condition
For this problem, you will add a new win condition. In addition to winning if a player has an entire n
-cell row, column, or diagonal, a player can win if they have an entire (m
-1)x(m
-1) square, where m=ceil(n/2)
. So, for a 3x3 or 4x4 game, a player can win if they get a 2x2 square anywhere on the board.
You should implement the function didPlayerWinWithSquare
. The player must have all (n-1)^2 pieces within the square for it to count as a victory.
def didPlayerWinWithSquare(boardState, player):
"""
Returns a Boolean indicating whether the player
won the game due to an entire square of side-length ceil(n/2)-1.
boardState: a nested list containing the board state
player: 1 or 2
returns: a Boolean (True if the player has an (m-1)x(m-1) square,
where m=ceil(n/2), and False otherwise)
"""
# TODO: Optional problem 2
return False # replace with your code
What you should submit
You should submit a single .zip file on Moodle. It should contain the following files:
readme.txt
(collaboration statement listing collaborators and form of collaboration)tictactoeExtension.py
(both problems)