Question

In: Computer Science

Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...

Python:

Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.

Solutions

Expert Solution

import os, random

def D_Dgme_bd():
        print(" %c | %c | %c " % (gme_bd[1],gme_bd[2],gme_bd[3]))
        print("___|___|___")
        print(" %c | %c | %c " % (gme_bd[4],gme_bd[5],gme_bd[6]))
        print("___|___|___")
        print(" %c | %c | %c " % (gme_bd[7],gme_bd[8],gme_bd[9]))
        print("   |   | ")

def ch_POS(x):
        if(gme_bd[x] == ' '):
                return True
        #check if blank
        else:
        #if not return false
                return False

#for different winning options
def CheckW_N():
        global ttt_gme
        #for horizontal
        if(gme_bd[1] == gme_bd[2] and gme_bd[2] == gme_bd[3] and gme_bd[1] != ' '):
                ttt_gme = W_N
        elif(gme_bd[4] == gme_bd[5] and gme_bd[5] == gme_bd[6] and gme_bd[4] != ' '):
                ttt_gme = W_N
        elif(gme_bd[7] == gme_bd[8] and gme_bd[8] == gme_bd[9] and gme_bd[7] != ' '):
                ttt_gme = W_N
        #for vertical
        elif(gme_bd[1] == gme_bd[4] and gme_bd[4] == gme_bd[7] and gme_bd[1] != ' '):
                ttt_gme = W_N
        elif(gme_bd[2] == gme_bd[5] and gme_bd[5] == gme_bd[8] and gme_bd[2] != ' '):
                ttt_gme = W_N
        elif(gme_bd[3] == gme_bd[6] and gme_bd[6] == gme_bd[9] and gme_bd[3] != ' '):
                ttt_gme=W_N
        #for diagonal
        elif(gme_bd[1] == gme_bd[5] and gme_bd[5] == gme_bd[9] and gme_bd[5] != ' '):
                ttt_gme = W_N
        elif(gme_bd[3] == gme_bd[5] and gme_bd[5] == gme_bd[7] and gme_bd[5] != ' '):
                ttt_gme=W_N
        #for tie or draw
        elif(gme_bd[1]!=' ' and gme_bd[2]!=' ' and gme_bd[3]!=' ' and gme_bd[4]!=' ' and gme_bd[5]!=' ' and gme_bd[6]!=' ' and gme_bd[7]!=' ' and gme_bd[8]!=' ' and gme_bd[9]!=' '):
                ttt_gme=D_D
        else:
                ttt_gme=R_N

print("Welcome Tic-Tac-Toe game")
print("Player 1 -X \nPlayer 2 -O \n")

choice = 'y'

while choice == 'y':
                
        gme_bd = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']

        ply = random.randint(1, 2)
        W_N= 1
        D_D = -1
        R_N = 0
        s_p = 1
        
        ttt_gme = R_N
        sym_lm = 'X'

        while(ttt_gme == R_N):
                D_Dgme_bd()
                if(ply % 2 != 0):
                        print("its your turn - Player 1")
                        sym_lm = 'X'
                else:
                        print("its your turn - Player 2")
                        sym_lm = 'O'

                op = int(input("select between[1-9]: "))
                if(ch_POS(op)):
                        gme_bd[op] = sym_lm

                ply+=1
                CheckW_N()
        
        
        D_Dgme_bd()
        if(ttt_gme==D_D):
                print("Game over..Draw!!!!")
        elif(ttt_gme==W_N):
                ply-=1
                if(ply%2!=0):
                        print("hurray!!! player 1 has won")
                else:
                        print("hurray!!! player 2 has won")

        print('\n')
        choice = input('Do you want to play again(y/n): ')
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
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...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
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...
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm =...
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new('andy', 'mike') => #<Game:0x2e91d78 @players=[Andy, Mike]> >> gm.play_game('mike') Mike, enter your next O O-- --- --- Andy, enter your next X O-X --- --- Mike, enter your next O O-X -O- --- Andy, enter your next X move Bad move dude! You go again. O-X -O- --- Andy, enter your next X move O-X -OX --- Mike, enter your next O move O-X -OX...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assume they know how to play the game). You should code the program to do the five items below only. You do not have to write the entire program, just the five items below. • Use a 2D array(3 rows, 3 columns) of datatype char. Initialize the 2D array with...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins or if a draw...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT