In: Computer Science
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.
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.