Python Forum
TicTacToe Game Add Exception Handling and Warning Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TicTacToe Game Add Exception Handling and Warning Function
#1
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()
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.
Reply


Messages In This Thread
TicTacToe Game Add Exception Handling and Warning Function - by ShaikhShaikh - Nov-01-2021, 03:22 PM
Invalid Column Name - by ShaikhShaikh - Nov-02-2021, 11:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 3,382 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  create my exception to my function korenron 2 1,456 Nov-09-2022, 01:50 PM
Last Post: korenron
  Array in tictactoe machine not working? Illumimint 4 2,403 Jun-02-2022, 02:24 AM
Last Post: Illumimint
  Exception handling in regex using python ShruthiLS 1 2,887 May-04-2020, 08:12 AM
Last Post: anbu23
  Exception handling Calli 2 3,202 Apr-20-2020, 06:13 PM
Last Post: Calli
  TicTacToe PC vs PC harig3 3 3,041 Mar-28-2020, 01:13 PM
Last Post: harig3
  Handling exception from a module dchi2 11 7,521 Nov-25-2019, 08:47 AM
Last Post: dchi2
  problem using custom exception handling in python srm 3 3,777 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  an easy way to disable exception handling Skaperen 6 7,811 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception handling KyawMyo 3 3,584 May-07-2019, 07:53 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020