Hello, i have my naughts and crosses code as pasted below. Can someone help me with code to recognise if the game has come to a draw and recognising any invalid moves.

Row1 = [" "," "," "]
Row2 = [" "," "," "]
Row3 = [" "," "," "]
Grid = [Row1,Row2,Row3]


Won = False


def check():
    if Grid[0][0] == 'X' and  Grid[0][1] == 'X' and Grid[0][2] == 'X':
        print("player 1 wins!")
        return True
    elif Grid[1][0] == 'X' and  Grid[1][1] == 'X' and Grid[1][2] == 'X':
        print("player 1 wins!")
        return True
    elif Grid[2][0] == 'X' and  Grid[2][1] == 'X' and Grid[2][2] == 'X':
        print("player 1 wins!")
        return True
    elif Grid[0][0] == 'X' and  Grid[1][0] == 'X' and Grid[2][0] == 'X':
        print("player 1 wins!")
        return True
    elif Grid[0][1] == 'X' and  Grid[1][1] == 'X' and Grid[2][1] == 'X':
        print("player 1 wins!")
        return True
    elif Grid[0][2] == 'X' and  Grid[1][2] == 'X' and Grid[2][2] == 'X':
        print("player 1 wins!")
        return True
    elif Grid[0][0] == 'X' and  Grid[1][1] == 'X' and Grid[2][2] == 'X':
        print("player 1 wins!")
        return True
    elif Grid[2][0] == 'X' and  Grid[1][1] == 'X' and Grid[0][2] == 'X':
        print("player 1 wins!")
        return True

    if Grid[0][0] == 'O' and  Grid[0][1] == 'O' and Grid[0][2] == 'O':
        print("player 2 wins!")
        return True
    elif Grid[1][0] == 'O' and  Grid[1][1] == 'O' and Grid[1][2] == 'O':
        print("player 2 wins!")
        return True
    elif Grid[2][0] == 'O' and  Grid[2][1] == 'O' and Grid[2][2] == 'O':
        print("player 2 wins!")
        return True
    elif Grid[0][0] == 'O' and  Grid[1][0] == 'O' and Grid[2][0] == 'O':
        print("player 2 wins!")
        return True
    elif Grid[0][1] == 'O' and  Grid[1][1] == 'O' and Grid[2][1] == 'O':
        print("player 2 wins!")
        return True
    elif Grid[0][2] == 'O' and  Grid[1][2] == 'O' and Grid[2][2] == 'O':
        print("player 2 wins!")
        return True
    elif Grid[0][0] == 'O' and  Grid[1][1] == 'O' and Grid[2][2] == 'O':
        print("player 2 wins!")
        return True
    elif Grid[2][0] == 'O' and  Grid[1][1] == 'O' and Grid[0][2] == 'O':
        print("player 2 wins!")
        return True



def NAC():


    #Pasting in the grid
    DisplayGrid()
    while Won == False:

        YourGo = input ("Your go\nEnter Number -  ")

        this_row = ['7','8','9']            # Possible inputs from this row
        if YourGo in this_row:
            row = 0                         # Tells python which row it is in
            col = this_row.index(YourGo)    # This_row.index selects your choice from this_row
            Grid[row][col] = 'X'            # Places X in the Grid

        this_row = ['4','5','6']
        if YourGo in this_row:
            row = 1
            col = this_row.index(YourGo)
            Grid[row][col] = 'X'

        this_row = ['1','2','3']
        if YourGo in this_row:
            row = 2
            col = this_row.index(YourGo)
            Grid[row][col] = 'X'

        DisplayGrid()
        #checking if won
        if check() == True:
            break

        YourGo2 = input("Play 2's turn\nEnter number - ")

        this_row = ['7', '8', '9']          # Possible inputs from this row
        if YourGo2 in this_row:
            row = 0                         # Tells python which row it is in
            col = this_row.index(YourGo2)   # This_row.index selects your choice from this_row
            Grid[row][col] = 'O'            # Places X in the Grid

        this_row = ['4','5','6']
        if YourGo2 in this_row:
            row = 1
            col = this_row.index(YourGo2)
            Grid[row][col] = 'O'



        this_row = ['1','2','3']
        if YourGo2 in this_row:
            row = 2
            col = this_row.index(YourGo2)
            Grid[row][col] = 'O'

        DisplayGrid()

        if check() == True:
            break

        check()











def DisplayGrid():
    print("  -----------")
    DisplayRow(Grid[0][0],Grid[0][1],Grid[0][2])
    print("  -----------")
    DisplayRow(Grid[1][0],Grid[1][1],Grid[1][2])
    print("  -----------")
    DisplayRow(Grid[2][0],Grid[2][1],Grid[2][2])
    print("  -----------")

def DisplayRow (a,b,c):
    print(" | ",end="")
    print(a,end="")
    print(" | ",end="")
    print(b,end="")
    print(" | ",end="")
    print(c,end="")
    print(" |")


NAC()

Too much code.
How about that?

def check():
    rows=[[(i,j) for i in range(3)] for j in range(3)]
    cols=[[(j,i) for i in range(3)] for j in range(3)]
    diags=[[(i,i) for i in range(3)],[(i,2-i) for i in range(3)]]
    ss=0
    for r in rows+cols+diags:
        sx=0
        so=0
        for c in r:
            v=Grid[c[0]][c[1]]
            if v=='X': sx+=1
            elif v=='O':so+=1
            elif v==' ':ss+=1
        if sx==3:
            print("player 1 wins!")
            return True
        if so==3:
            print("player 2 wins!")
            return True
    if ss==0:
        print("Draw!")
        return True

def NAC():

    turn= True
    #Pasting in the grid
    while True:
        DisplayGrid()
        if turn:
            msg="Your go\nEnter Number -  "
        else:
            msg="Play 2's turn\nEnter number - "
        YourGo = input(msg)
        row=(int(YourGo)-1)%3
        col=(int(YourGo)-1)//3
        value='X' if turn else 'O'
        if Grid[row][col]!=' ':
            print("Invalid move!")
            continue
        Grid[row][col]=value
        if check():
            DisplayGrid()
            break
        turn=not turn
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.