如何运行井字游戏。里面有一些错误

问题描述 投票:-4回答:1

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

我只是想知道如何运行此代码...

这是一个数字的井字游戏

非常感谢!

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#----------------------------------------------------
# Lab 3: Numerical Tic Tac Toe class
# 
# Author: 
# Collaborators:
# References:
#----------------------------------------------------

class NumTicTacToe:
    def __init__(self):
        '''
        Initializes an empty Numerical Tic Tac Toe board.
        Inputs: none
        Returns: None
        '''       
        self.board = [] # list of lists, where each internal list represents a row
        self.size = 3   # number of columns and rows of board

        # populate the empty squares in board with 0
        for i in range(self.size):
            row = []
            for j in range(self.size):
                row.append(0)
            self.board.append(row)


    def drawBoard(self):
        '''
        Displays the current state of the board, formatted with column and row 
        indicies shown.
        Inputs: none
        Returns: None
        '''        

        grid_row = 0
        grid = [['0  ', ' ', ' | ', ' ', ' | ' , ' ' , ' '], ['1  ', ' ', ' | ', ' ', ' | ' , ' ' , ' '], ['2  ', ' ', ' | ', ' ', ' | ' , ' ' , ' ']]
        print('   0   1   2 ')
        for row in self.board:
            grid_col = 1
            for col in row:
                if col != 0:
                    grid[grid_row][grid_col] = str(col)
                grid_col += 2
            print(''.join(grid[grid_row]))
            grid_row += 1
            if grid_row < 3:
                print('  -----------')

    def squareIsEmpty(self, row, col):
        '''
        Checks if a given square is empty, or if it already contains a number 
        greater than 0.
        Inputs:
           row (int) - row index of square to check
           col (int) - column index of square to check
        Returns: True if square is empty; False otherwise
        '''

        if self.board[row][col] == 0:
            return True
        else:
            return False

    def update(self, row, col, num):
        '''
        Assigns the integer, num, to the board at the provided row and column, 
        but only if that square is empty.
        Inputs:
           row (int) - row index of square to update
           col (int) - column index of square to update
           num (int) - entry to place in square
        Returns: True if attempted update was successful; False otherwise
        '''

        if self.squareIsEmpty(row,col):
            self.board[row][col] = num
            return True
        else:
            return False

    def boardFull(self):
        '''
        Checks if the board has any remaining empty squares.
        Inputs: none
        Returns: True if the board has no empty squares (full); False otherwise
        '''

        flag = True
        for row in self.board:
            for col in row:
                if col == 0:
                    flag = False
        return flag

    def isWinner(self):
        '''
        Checks whether the current player has just made a winning move.  In order
        to win, the player must have just completed a line (of 3 squares) that 
        adds up to 15. That line can be horizontal, vertical, or diagonal.
        Inputs: none
        Returns: True if current player has won with their most recent move; 
                 False otherwise
        '''

        total = 0
        total2 = 0

        for index in range(0, self.size):
            total = total + self.board[index][index]
            total2 = total2 + self.board[index][self.size - 1 - index]
        if total == 15 or total2 == 15:
            return True
        else:
            total = 0
            total2 = 0

        for col_index in range(0,self.size):
            for row_index in range(0,self.size):
                total = total + self.board[row_index][col_index]
            if total == 15:
                return True
            else:
                total = 0

        for row_index in range(0, self.size):
            for col_index in range (0, self.size):
                total = total + self.board[row_index][col_index]
            if total == 15:
                return True
            else:
                total = 0

        return False   


def getEntry(player, entries)


def getCoord(player, dimension)

if __name__ == "__main__":
    # TEST EACH METHOD THOROUGHLY HERE
    # suggested tests are provided as comments, but more tests may be required

    # start by creating empty board and checking the contents of the board attribute
    myBoard = NumTicTacToe()
    print('Contents of board attribute when object first created:')
    print(myBoard.board)

    # does the empty board display properly?
    myBoard.drawBoard()

    # assign a number to an empty square and display

    # try to assign a number to a non-empty square. What happens?

    # check if the board has a winner. Should there be a winner after only 1 entry?

    # check if the board is full. Should it be full after only 1 entry?

    # add values to the board so that any line adds up to 15. Display

    # check if the board has a winner

    # check if the board is full

    # write additional tests, as needed
python-3.x tic-tac-toe
1个回答
-1
投票

您之所以被否决,是因为这是入门级的东西,您显然甚至没有尝试自己弄清楚。因此,SO希望仅在您至少进行了一些尝试之后才会提出问题。您还需要说明您尝试过的内容以及遇到的任何错误。 https://realpython.com/run-python-scripts/应该说明您需要什么才能运行此脚本。

© www.soinside.com 2019 - 2024. All rights reserved.