# How to set a draw when making tic tac toe chess in Python

2022-02-02 18:14:58

Would like to ask this code how to set a draw

``info = [['.', '.', '.'],        ['.', '.', '.'],        ['.', '.', '.']]def show():    print(' 0 1 2')    for i in range(3):        print(i, info[i], info[i], info[i])def judge():    if info == info and info == info:        if info != '.':            return info    if info == info and info == info:        if info != '.':            return info    for i in range(3):        if info[i] == info[i] and info[i] == info[i]:            if info[i] != '.':                return info[i]    for i in range(3):        if info[i] == info[i] and info[i] == info[i]:            if info[i] != '.':                return info[i]    return 'y'show()while True:     a = input(' The player x, Which line do you want to put the pieces on :')    b = input(' The player x, Which row do you want to put the pieces in :')    a = int(a)    b = int(b)    info[a][b] = 'x'    show()    flag = judge()    if flag == 'x':        print(' The player x victory ')        break;    a = input(' The player o, Which line do you want to put the pieces on :')    b = input(' The player o, Which row do you want to put the pieces in :')    a = int(a)    b = int(b)    info[a][b] = 'o'    show()    flag = judge()    if flag == 'o':        print(' The player o victory ')        break;    ``

Refer to the answer 1：

Your code doesn't take into account ：1、 User input error ;2、 There are already pieces in the chess position . I added it all , You can have a look , Leave a message if you have any questions

``info = [['.', '.', '.'],        ['.', '.', '.'],        ['.', '.', '.']]def show():    print(' 0 1 2')    for i in range(3):        print(i, info[i], info[i], info[i])def judge():    if info == info and info == info:        if info != '.':            return info    if info == info and info == info:        if info != '.':            return info    for i in range(3):        if info[i] == info[i] and info[i] == info[i]:            if info[i] != '.':                return info[i]    for i in range(3):        if info[i] == info[i] and info[i] == info[i]:            if info[i] != '.':                return info[i]    l = []    for i in [0, 1, 2]:        for j in [0, 1, 2]:            l.append(info[i][j])    if '.' not in l:        return 'z'show()while True:    while True:        while True:            a = int(input(' The player x, Which line do you want to put the pieces on :'))            if a not in [0, 1, 2]:                print(' Incorrect input , Please re-enter ')            else:                break        while True:            b = int(input(' The player x, Which row do you want to put the pieces in :'))            if a not in [0, 1, 2]:                print(' Incorrect input , Please re-enter ')            else:                break        if info[a][b] == '.':            info[a][b] = 'x'            break        else:            print(' There are pieces in this position ')    show()    flag = judge()    if flag == 'x':        print(' The player x victory ')        break    if flag == 'z':        print(' It ends in a draw ')        break    while True:        while True:            a = int(input(' The player o, Which line do you want to put the pieces on :'))            if a not in [0, 1, 2]:                print(' Incorrect input , Please re-enter ')            else:                break        while True:            b = int(input(' The player o, Which row do you want to put the pieces in :'))            if a not in [0, 1, 2]:                print(' Incorrect input , Please re-enter ')            else:                break        if info[a][b] == '.':            info[a][b] = 'o'            break        else:            print(' There are pieces in this position ')    show()    flag = judge()    if flag == 'o':        print(' The player o victory ')        break    if flag == 'z':        print(' It ends in a draw ')        break``

Refer to the answer 2：