如何让我的代码更好 python (tic tac toe)

问题描述

我尝试用 Python 制作井字游戏。从我做的事情来看,我觉得我的代码正在运行,但它真的很糟糕。无论如何要改进此代码

class Board():
    def __init__(self): 
        self.board = ["[]","[]","[]"]
    def createboard(self):
        board = ["[]","[]"]
        print(self.board[0],self.board[1],self.board[2])
        print(self.board[3],self.board[4],self.board[5])
        print(self.board[6],self.board[7],self.board[8])
    def checkwin(self):
        if self.board[0]=="[O]" and self.board[1]=="[O]" and self.board[2]=="[O]":
            print("O Wins!")
            #break
        elif self.board[3]=="[O]"and self.board[4]=="[O]"and self.board[5]=="[O]":
            print("O Wins!")
           # break
        elif self.board[6]=="[O]"and self.board[7]=="[O]"and self.board[8]=="[O]":
            print("O Wins!")
           # break
        elif self.board[0]=="[O]"and self.board[3]=="[O]"and self.board[6]=="[O]":
            print("O Wins!")
          #  break
        elif self.board[1]=="[O]"and self.board[4]=="[O]"and self.board[7]=="[O]":
            print("O Wins!")
          #  break
        elif self.board[2]=="[O]"and self.board[5]=="[O]"and self.board[8]=="[O]":
            print("O Wins!")
         #   break
        elif self.board[0]=="[O]"and self.board[4]=="[O]"and self.board[8]=="[O]":
            print("O Wins!")
           # break
        elif self.board[2]=="[O]"and self.board[4]=="[O]"and self.board[6]=="[O]":
            print("O Wins!")
          #  break
        elif self.board[0]=="[X]"and self.board[1]=="[X]"and self.board[2]=="[X]":
            print("X Wins!")
           # break
        elif self.board[3]=="[X]"and self.board[4]=="[X]"and self.board[5]=="[X]":
            print("X Wins!")
          #  break
        elif self.board[6]=="[X]"and self.board[7]=="[X]"and self.board[8]=="[X]":
            print("X Wins!")
          #  break
        elif self.board[0]=="[X]"and self.board[3]=="[X]"and self.board[6]=="[X]":
            print("X Wins!")
         #   break
        elif self.board[1]=="[X]"and self.board[4]=="[X]"and self.board[7]=="[X]":
            print("X Wins!")
        #    break
        elif self.board[2]=="[X]"and self.board[5]=="[X]"and self.board[8]=="[X]":
            print("X Wins!")
         #   break
        elif self.board[0]=="[X]"and self.board[4]=="[X]"and self.board[8]=="[X]":
            print("X Wins!")
         #   break
        elif self.board[2]=="[X]"and self.board[4]=="[X]"and self.board[6]=="[X]":
            print("O Wins!")
         #   break
        
    def omove(self):
        o = int(input("Its 'O's'move please insert 1-9"))
        if o == 1:
            self.board[0] = "[O]"
            self.createboard()
        elif o == 2:
            self.board[1] = "[O]"
            self.createboard()
        elif o == 3:
            self.board[2] = "[O]"
            self.createboard()
        elif o == 4:
            self.board[3] = "[O]"
            self.createboard()
        elif o == 5:
            self.board[4] = "[O]"
            self.createboard()
        elif o == 6:
            self.board[5] = "[O]"
            self.createboard()
        elif o == 7:
            self.board[6] = "[O]"
            self.createboard()
        elif o == 8:
            self.board[7] = "[O]"
            self.createboard()
        elif o == 9:
            self.board[8] = "[O]"
            self.createboard()
        else:
            print('that column is out of range')
            o = int(input("please insert 1-9"))
    def xmove(self):
        x = int(input("Its 'x's'move please insert 1-9"))
        if x == 1:
            self.board[0] = "[X]"
            self.createboard()
        elif x == 2:
            self.board[1] = "[X]"
            self.createboard()
        elif x == 3:
            self.board[2] = "[X]"
            self.createboard()
        elif x == 4:
            self.board[3] = "[X]"
            self.createboard()
        elif x == 5:
            self.board[4] = "[X]"
            self.createboard()
        elif x == 6:
            self.board[5] = "[X]"
            self.createboard()
        elif x == 7:
            self.board[6] = "[X]"
            self.createboard()
        elif x == 8:
            self.board[7] = "[X]"
            self.createboard()
        elif x == 9:
            self.board[8] = "[X]"
            self.createboard()
        else:
            print('that column is out of range')
            o = int(input("please insert 1-9"))
    
a = Board()
a.createboard()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()

我希望得到任何反馈,以提高我的编程技能。我的想法是我觉得我用的太多了,如果条件也许可以通过另一种简单的方法来交换,我怎么能在游戏结束时中断例如:如果 X 赢得了游戏,那么游戏就关闭

解决方法

一个重大变化是重构移动函数以传递您需要的令牌并避免使用数学的 elifs。

def move(self,token):
    o = int(input(f"Its '{token}'s'move please insert 1-9"))
    
    while not (0 < o <= 9):
        print('that column is out of range')
        o = int(input("please insert 1-9"))
        
    self.board[o-1] = f"[{token}]"
    self.createboard()

通过...使用

move("O")
move("X")
,
self.board = 9*['[]']

def createboard(self):
    for i,j in enumerate(board,1):
        print(j)
        if i%3 == 0: print('\n')

看看python教程:https://docs.python.org/3/tutorial/