Question

In: Computer Science

Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11,...

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

Solutions

Expert Solution

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()

Related Solutions

The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Stewart invented “Max” a board game similar to “Tic Tac Toe.” In May 2017, Stewart began...
Stewart invented “Max” a board game similar to “Tic Tac Toe.” In May 2017, Stewart began negotiating with Big Board, Inc., to license “Max” for distribution outside the United States. On June 11, 2017, the parties met and orally discussed terms. As compensation, Big Board promised to pay Stewart the amount due from another board game he had developed for Big Board two years ago. On June 26, 2017, Lisa, a Big Board employee, sent Stewart an email titled “Max...
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
Assume that the human player makes the first move against the computer in a game of Tic-Tac-Toe,
Assume that the human player makes the first move against the computer in a game of Tic-Tac-Toe, which has a 3 x 3 grid. Write a MATLAB function that lets the computer respond to that move. The function’s input argument should be the cell location of the human player’s move. The function’s output should be the cell location of the computer’s rst move. Label the cells as 1, 2, 3 across the top row; 4, 5, 6 across the middle...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT