Nov-01-2021, 03:22 PM
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
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()