In: Computer Science
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11, where s is the board size. Before the game starts the program will prompt for the board size. Note: the winning conditions are the same as the original Tic-tac-toe game in that you need to fill the entire row/column/diagonal to win.
Here are a few sample runs. The output is a bit different so that we can handle two-digit coordinates consistently. We expect (but you won’t lose any marks if you don’t) that you make extensive use of compound data types and string library. Note that our solution has less than 50 lines of code. You may assume that a user always enters a valid move.
You may use any functions/operators for this question.
Sample runs:
Size--> 6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 1 0 X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 13 0 X 2 3 4 5 6 7 8 9 10 11 12 O 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 2 0 X X 3 4 5 6 7 8 9 10 11 12 O 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 14 0 X X 3 4 5 6 7 8 9 10 11 12 O O 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 3 0 X X X 4 5 6 7 8 9 10 11 12 O O 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 15 0 X X X 4 5 6 7 8 9 10 11 12 O O O 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 4 0 X X X X 5 6 7 8 9 10 11 12 O O O 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 16 0 X X X X 5 6 7 8 9 10 11 12 O O O O 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 5 0 X X X X X 6 7 8 9 10 11 12 O O O O 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 17 0 X X X X X 6 7 8 9 10 11 12 O O O O O 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 6 0 X X X X X X 7 8 9 10 11 12 O O O O O 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 18 0 X X X X X X 7 8 9 10 11 12 O O O O O O 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 0 X X X X X X X 7 8 9 10 11 12 O O O O O O 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Winner: X
Size--> 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 X--> 0 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 O--> 72 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O 73 74 75 76 77 78 79 80 X--> 1 X X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O 73 74 75 76 77 78 79 80 O--> 73 X X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 80 X--> 8 X X 2 3 4 5 6 7 X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 80 O--> 80 X X 2 3 4 5 6 7 X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 O X--> 7 X X 2 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 O O--> 79 X X 2 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 O O X--> 2 X X X 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 O O O--> 74 X X X 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O 75 76 77 78 O O X--> 3 X X X X 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O 75 76 77 78 O O O--> 75 X X X X 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O 76 77 78 O O X--> 4 X X X X X 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O 76 77 78 O O O--> 76 X X X X X 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O 77 78 O O X--> 5 X X X X X X 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O 77 78 O O O--> 77 X X X X X X 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O O 78 O O X--> 15 X X X X X X 6 X X 9 10 11 12 13 14 X 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O O 78 O O O--> 78 X X X X X X 6 X X 9 10 11 12 13 14 X 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O O O O O Winner: O
Size--> 6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 0 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 1 X O 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 2 X O X 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 3 X O X O 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 4 X O X O X 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 5 X O X O X O 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 12 X O X O X O 6 7 8 9 10 11 X 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 13 X O X O X O 6 7 8 9 10 11 X O 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 14 X O X O X O 6 7 8 9 10 11 X O X 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 15 X O X O X O 6 7 8 9 10 11 X O X O 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 16 X O X O X O 6 7 8 9 10 11 X O X O X 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 17 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 24 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X 25 26 27 28 29 30 31 32 33 34 35 O--> 25 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O 26 27 28 29 30 31 32 33 34 35 X--> 26 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X 27 28 29 30 31 32 33 34 35 O--> 27 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X O 28 29 30 31 32 33 34 35 X--> 28 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X O X 29 30 31 32 33 34 35 O--> 29 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 11 X O X O X O 6 7 8 9 10 X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 O--> 10 X O X O X O 6 7 8 9 O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 9 X O X O X O 6 7 8 X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 O--> 8 X O X O X O 6 7 O X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 7 X O X O X O 6 X O X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 O--> 6 X O X O X O O X O X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 23 X O X O X O O X O X O X X O X O X O 18 19 20 21 22 X X O X O X O 30 31 32 33 34 35 O--> 22 X O X O X O O X O X O X X O X O X O 18 19 20 21 O X X O X O X O 30 31 32 33 34 35 X--> 21 X O X O X O O X O X O X X O X O X O 18 19 20 X O X X O X O X O 30 31 32 33 34 35 O--> 20 X O X O X O O X O X O X X O X O X O 18 19 O X O X X O X O X O 30 31 32 33 34 35 X--> 19 X O X O X O O X O X O X X O X O X O 18 X O X O X X O X O X O 30 31 32 33 34 35 O--> 18 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O 30 31 32 33 34 35 X--> 30 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X 31 32 33 34 35 O--> 31 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O 32 33 34 35 X--> 32 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X 33 34 35 O--> 33 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O 34 35 X--> 34 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O X 35 O--> 35 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O X O Winner: None
Size--> 3 0 1 2 3 4 5 6 7 8 X--> 0 X 1 2 3 4 5 6 7 8 O--> 1 X O 2 3 4 5 6 7 8 X--> 2 X O X 3 4 5 6 7 8 O--> 3 X O X O 4 5 6 7 8 X--> 4 X O X O X 5 6 7 8 O--> 5 X O X O X O 6 7 8 X--> 6 X O X O X O X 7 8 Winner: X
def printBoard(plane): #function to print out the current board
for w in range(len(plane)):
for e in range(len(plane)):
if plane[w][e]== 1:
print('{:>3}'.format('X'), end="")
elif plane[w][e] == -1:
print('{:>3}'.format('O'), end="")
elif plane[w][e] == 0:
print('{:>3d}'.format((len(plane)*w+e)), end="")
print("")
def placer(plane, choice, side): #function to place user's choice on the field
splitSide1, splitSide2 = choice[0], choice[1]
if side == "X":
sider=1
elif side == "O":
sider=-1
plane[splitSide1][splitSide2] = sider
return plane
def listSum(numList): #Function to add elements of a list together
if len(numList) == 1:
return numList[0]
else:
return numList[0] + listSum(numList[1:])
def checkWinner(plane, side): #check for a winner in 3 different ways:
#check for row win by adding values to see if they're equal to the length of the playing plane
tempVar=[]
for g in range(len(plane)):
if listSum(plane[g]) == len(plane) or listSum(plane[g]) == -int(len(plane)):
print("Winner:", side)
exit()
#check for column win by adding values:
for t in range(len(plane)):
for u in range(len(plane)):
tempVar.append(plane[u][t])
if listSum(tempVar)== len(plane) or listSum(tempVar)== -int(len(plane)):
print("Winner:", side)
exit()
else:
tempVar=[]
tempVar=[]
#check for diagonally downward wins
for a in range(len(plane)):
tempVar.append(plane[a][a])
if listSum(tempVar)== len(plane) or listSum(tempVar)== -int(len(plane)):
print("Winner:", side)
exit()
#check for diagonally upward wins
tempVar=[]
for s in range(len(plane)):
tempVar.append(plane[-s-1][s])
if listSum(tempVar)== len(plane) or listSum(tempVar)== -int(len(plane)):
print("Winner:", side)
exit()
def choiceTrans(choice, plane): #"translate" human input into more easily computable values
findHelp=0
for p in range(len(plane)):
for i in range(len(plane)):
if findHelp == choice:
return p,i
findHelp+=1
def endOfGame(plane): #check whether all positions are taken up
counter=0
for x in range(len(plane)):
for y in range(len(plane)):
if plane[x][y] == 0:
counter=counter
else:
counter+=1
if counter == (len(plane) * len(plane)):
print("Winner: None")
exit()
# Driver code
def main():
side="X"
print("Size -->", end="")
size=int(input())
plane=[]
for g in range(size): #create initial (empty) 2-Dimensional array with user-chosen size
plane.append([0]*size)
printBoard(plane)
while True:
if side=="X":
print("X--> ", end="")
if side=="O":
print("O--> ", end="")
choice=int(input())
choice=choiceTrans(choice, plane) #choice "translates" user input into computeable values
plane=placer(plane, choice, side) #Places user's choice on the array with a 1 for X and a -1 for O
printBoard(plane) #Prints (now modified) array to screen, replacing empty locations with a corresponding number
checkWinner(plane, side) #Finds the winner based on whether the absolute value of the sum of a line is equal to the length of the array
if side == "X":
side="O"
elif side=="O":
side="X"
endOfGame(plane) #Checks whether the field is full. Only executes after checkWinner() in order to make sure when the field is full, no one has won yet
main()