Nov-01-2021, 03:22 PM
(This post was last modified: Nov-01-2021, 04:03 PM by Gribouillis.)
I built a TicTacToe game in Python, but I want to add exception handling in it And i also want to create a warning function which shows the position is filled try another position and it also shows if the player1 insert the position 1 and 9 then it'll show alert msg to player2 that if you'll not insert on 5 position then the player1 will won
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
from IPython.display import clear_output def tictactoe(): print ( "Game is Starting" ) play_again = True #While loop for play again while play_again: clear_output() #Function for clear output board = [ 'p' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] # make board and storing all the positions in a list player = 1 print ( "player" ,player, "Select your marker" ) #Asking player to select marker marker = input () #set player marker's input in a variable 'marker' player_1, player_2 = set_marker(marker) #set marker while True : #while loop for taking a inputs from user if player > 2 : player = 1 print ( "Player" ,player, ": Enter your position" ) #Asking to player enter the position pos = int ( input ()) #typecasting if check_pos(pos, board): #check position has been filled or not if this position has been filled then continue continue if player = = 1 : set_board(board,player_1,pos) #setting player1's marker else : set_board(board,player_2,pos) #setting player 2's marker display(board) #display board if player = = 1 : if check_win(board, player_1): #will return boolean value print ( "Player" ,player, "has won" ) break else : if check_win(board, player_2): print ( "Player" ,player, "has won" ) break if check_space(board): # check space on board if there is no space then game will draw print ( "Draw !!!" ) break player + = 1 if replay(): # Play again play_again = True In [ 19 ]: def set_marker(marker): if marker = = "X" : return ( "X" , "O" ) else : return ( "O" , "X" ) def check_pos(pos, board): #check position if board[pos] = = " " : # board is a list so check the position is empty or not if it is empty then return false return False else : return True def set_board(board,marker,pos): #here marker is a player board[pos] = marker def display(board): #display board clear_output() print ( f '{board[7]} | {board[8]} | {board[9]}' ) print ( "----------------------------" ) print ( f '{board[4]} | {board[5]} | {board[6]}' ) print ( "----------------------------" ) print ( f '{board[1]} | {board[2]} | {board[3]}' ) print ( "----------------------------" ) def check_win(board, marker): return ((board[ 1 ] = = marker and board[ 2 ] = = marker and board[ 3 ] = = marker) or (board[ 4 ] = = marker and board[ 5 ] = = marker and board[ 6 ] = = marker) or (board[ 7 ] = = marker and board[ 8 ] = = marker and board[ 9 ] = = marker) or (board[ 9 ] = = marker and board[ 5 ] = = marker and board[ 1 ] = = marker) or (board[ 7 ] = = marker and board[ 5 ] = = marker and board[ 3 ] = = marker) or (board[ 7 ] = = marker and board[ 4 ] = = marker and board[ 1 ] = = marker) or (board[ 8 ] = = marker and board[ 5 ] = = marker and board[ 2 ] = = marker) or (board[ 9 ] = = marker and board[ 6 ] = = marker and board[ 3 ] = = marker)) def check_space(board): if " " in board: return False else : return True def replay(): print ( "Do you want to play again Yes/No" ) ans = input () if ans = = "Yes" or ans = = "yes" : return True else : return False In [ 15 ]: tictactoe() |
Gribouillis write Nov-01-2021, 04:03 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.