如何在小型终端机游戏中清理代码?

时间:2019-04-01 12:35:31

标签: python

我不确定是否允许这样的帖子,以免造成歉意。

我是Python的新手,我刚刚完成了一次学校作业,在那里我不得不制作专注于OOP的ticticactoe游戏。 我很想获得一些有关如何清理代码(尤其是我的Tictactoe类)的指示,因为它现在很混乱。

到目前为止,我所做的就是尽可能多地注释以使其易于理解,但是我觉得我的tictactoe类很烂,我想获得一些如何优化它的指示。

下面的类负责执行规则并画图:

import os
class Board:

    board = [0,1,2,3,4,5,6,7,8,9]
    win_combinations = [
        (1,2,3),
        (4,5,6),
        (7,8,9),
        (1,5,9),
        (3,5,7),
        (1,4,7),
        (2,5,8),
        (3,6,9),
    ]
    GameOver = False

    #Draws the board
    def drawboard(self): 
        print('=========')
        print(self.board[7], '|', self.board[8], '|', self.board[9])
        print(self.board[4], '|', self.board[5], '|', self.board[6])
        print(self.board[1], '|', self.board[2], '|', self.board[3])
        print('=========')

    #Checks if the move the player just made, made him/she win the game
    def checkIfWon(self, choice):

        for a, b, c in self.win_combinations:
            if self.board[a] == self.board[b] == self.board[c]:
                print('Game over, player ' + choice + ' won the game')
                self.GameOver = True

    #Update the current board 
    def update(self, input, choice):

        self.board[input] = choice
        os.system('clear')
        self.drawboard()
        self.checkIfWon(choice)

    #Resets the board
    def resetBoard(self):
        self.board = [0,1,2,3,4,5,6,7,8,9]

    #Stops the game if tie
    def tie(self):
        list = []
        for x in self.board:
            if type(x) != int:
                list.append(x)

        if len(list) == 9:
            return True

下面的类包含启动游戏的runGame方法:

import os
from board import Board

class Tictactoe():

    b = Board()
    choicePlayer1 = ''
    choucePlayer2 = ''
    corretChoice = False
    correctPlayer1 = False
    correctPlayer2 = False


    def runGame(self):
        os.system('clear')

        #Resets the game when a new game is started
        #Is necessary if the players wish to play more than 1 game
        resetGame(self)

        #Makes sure the game only starts if player1 picks X or O
        while self.corretChoice == False:

            self.choicePlayer1 = input('Do you want to play X or O? ')
            print()
            if self.choicePlayer1 == 'X':
                self.choicePlayer2 = 'O'
                self.corretChoice = True
                print('Starting player selected X')

            elif self.choicePlayer1 == 'O':
                self.choicePlayer2 = 'X'
                self.corretChoice = True
                print('Starting player selected O')
            else:
                print('ERROR - input has to be either X or O!')
                continue

        os.system('clear')
        self.b.drawboard()

        while self.b.GameOver == False:
            self.correctPlayer1 = False
            self.correctPlayer2 = False

            #For player1
            while self.correctPlayer1 == False:
                while True:
                    try:
                        x = int(input(self.choicePlayer1 + ' Where do you want to place your piece? '))
                        break
                    except:
                        print('Input has to be a number, try again')

                if x > 0 and x < 10 and type(self.b.board[x]) != str:
                    self.b.update(x, self.choicePlayer1)
                    self.correctPlayer1 = True
                elif x == 10:
                    quit()
                else: 
                    print('Spot is taken, try again: ')

                if self.b.GameOver == True:
                    self.correctPlayer2 = True

                if self.b.tie() == True:
                    self.correctPlayer2 = True
                    self.b.GameOver = True
                    print('Game is a tie')
            #For player2
            while self.correctPlayer2 == False:
                while True:
                    try:
                        x = int(input(self.choicePlayer2 + ' Where do you want to place your piece? '))
                        break
                    except:
                        print('Input has to be a number, try again')

                if x > 0 and x < 10 and type(self.b.board[x]) != str:
                    self.b.update(x, self.choicePlayer2)
                    self.correctPlayer2 = True
                elif x == 10:
                    quit()
                else: 
                    print('Spot is taken, try again: ')

                if self.b.tie() == True:
                    self.b.gameOver = True
                    print('Game is a tie')

#Resets the game if the players wishes to play again
def resetGame(self):
    self.b = Board()
    self.choicePlayer1 = ''
    self.choucePlayer2 = ''
    self.corretChoice = False
    self.correctPlayer1 = False
    self.correctPlayer2 = False
    self.b.resetBoard()

我用来启动游戏的脚本:

from tictac import Tictactoe

run = Tictactoe()

while True:
    run.runGame()
    if input("Play again? (y/n)") == "n":
        quit()

0 个答案:

没有答案