current position:Home>How to set a draw when making tic tac toe chess in Python

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

2022-02-02 18:14:58 CSDN Q & A

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][0], info[i][1], info[i][2])def judge():    if info[0][0] == info[1][1] and info[1][1] == info[2][2]:        if info[0][0] != '.':            return info[0][0]    if info[0][2] == info[1][1] and info[1][1] == info[2][0]:        if info[0][2] != '.':            return info[0][2]    for i in range(3):        if info[i][0] == info[i][1] and info[i][1] == info[i][2]:            if info[i][0] != '.':                return info[i][0]    for i in range(3):        if info[0][i] == info[1][i] and info[1][i] == info[2][i]:            if info[0][i] != '.':                return info[0][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][0], info[i][1], info[i][2])def judge():    if info[0][0] == info[1][1] and info[1][1] == info[2][2]:        if info[0][0] != '.':            return info[0][0]    if info[0][2] == info[1][1] and info[1][1] == info[2][0]:        if info[0][2] != '.':            return info[0][2]    for i in range(3):        if info[i][0] == info[i][1] and info[i][1] == info[i][2]:            if info[i][0] != '.':                return info[i][0]    for i in range(3):        if info[0][i] == info[1][i] and info[1][i] == info[2][i]:            if info[0][i] != '.':                return info[0][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:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021814558306.html

Random recommended