OOP Python Tic Tac Toe更新板的类

时间:2013-11-24 22:39:41

标签: python oop

这是使用一些OOP的井字游戏的开始。我在更新类的实例变量时遇到了麻烦(如果我的术语正确的话)。当我经历while循环时,我不存储条目。

我已阅读文档并对OOP有一点经验,我尝试了其他一些方法,但我不知道我的前提是否错误,我需要朝另一个方向前进,或者我是否'离解决方案一条线。 (我希望这种格式正确!)

class Board:

    def __init__(self, square1=' ', square2=' ', square3=' ', square4=' ', square5=' ', square6=' ', square7=' ', square8=' ', square9=' '):
        self.square1 = square1
        self.square2 = square2
        self.square3 = square3
        self.square4 = square4
        self.square5 = square5
        self.square6 = square6
        self.square7 = square7
        self.square8 = square8
        self.square9 = square9
        self.grid = '\nSQUARES ARE 0-8, TOP LEFT TO BOTTOM RIGHT, TRAVEL HORIZONTALLY\n   |   |   \n '+square1+' | '+square2+' | '+square3+'  \n___|___|___\n   |   |   \n '+square4+' | '+square5+' | '+square6+'  \n___|___|___\n   |   |   \n '+square7+' | '+square8+' | '+square9+'  \n   |   |   '


game=Board()

print(game.grid)

while True:
    entry = input('Please enter a number\n')
    if entry == '0':
        update = Board(square1='X')

    elif entry == '1':
        update = Board(square2='X')

    elif entry == '2':
        update = Board(square3='X')

    elif entry == '3':
        update = Board(square4='X')

    elif entry == '4':
        update = Board(square5='X')

    elif entry == '5':
        update = Board(square6='X')

    elif entry == '6':
        update = Board(square7='X')

    elif entry == '7':
        update = Board(square8='X')

    elif entry == '8':
        update = Board(square9='X')

    print(update.grid)

5 个答案:

答案 0 :(得分:3)

OOP代表面向对象的编程。在这种情况下,您的对象是Boardclass就像您用来创建其他电路板的模板一样,使用:

new_board = Board()

从那时起,您必须使用new_board,这是Board实例。我的意思是,在while循环中,当您update = Board...而不是修改之前的版本时,您会创建新的电路板。

为此,您应该将方法添加到您的主板。这些功能将修改电路板。此外,您应该使用其他类型的数据结构(例如列表)将数据存储在类中。例如:

class Board:

    def __init__(self):
        self.squares = [0] * 9

    def check_square(self, number):
        self.squares[number] = 'X'

    def print_grid(self):
        # printing code here

game=Board()
game.print_grid()

while True:
    entry = input('Please enter a number\n')

    game.check_square(int(entry))
    game.print_grid()

我希望你能有这种感觉。

答案 1 :(得分:2)

您可以尝试这种方法:

class Board:
    def __init__(self):
        self.fields = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

    def update(self, n, seed):
        self.fields[(n / 3)][n % 3] = seed

    def printBoard(self):
        print
        for l in self.fields:
            for e in l:
                print e,
            print

b = Board()

b.printBoard()

b.update(1, 'X')
b.printBoard()

>>> 0 0 0
    0 0 0
    0 0 0

    0 X 0
    0 0 0
    0 0 0

答案 2 :(得分:1)

答案 3 :(得分:0)

如评论中所述,您应该使用列表而不是声明9个不同的变量 另外你面临的问题是你没有更新游戏板,而是你正在创建一个新的棋盘对象,初始化时将某个方块设置为“X”。

您可以在Board Class

中使用setter功能
class Board(object):
    def __init__(self):
        self.block=[]
        for i in range(0,9):
            self.block.append(" ")

    def print_Grid(self):
        #print self.block
        # print in a required manner using list self.block

    def setBlock(self,index):
        self.block[index]='X'

obj=Board()#create board

while True:
    entry = input('Index of grid to be filled\n')
    obj.setBlock(entry)
    obj.print_Grid()

这是实现目标的基本方法...... 您也可以谷歌了解settergetter函数。

答案 4 :(得分:0)

以下是我认为从当前代码到实际示例的最短路径。

这绝对不是我建议从头开始编写代码的方法 - 其他答案显示了如何使用Python列表和方法/函数来干净利落地完成。

但是看到我改变的东西并将其与原始版本进行比较可能会很有用。我认为你在如何使用类(在update = Board(square2='X'))这样的地方遇到了麻烦。

class Board:

    def __init__(self, square1=' ', square2=' ', square3=' ', square4=' ', square5=' ', square6=' ', square7=' ', square8=' ', square9=' '):
        self.square1 = square1
        self.square2 = square2
        self.square3 = square3
        self.square4 = square4
        self.square5 = square5
        self.square6 = square6
        self.square7 = square7
        self.square8 = square8
        self.square9 = square9

    def grid(self):
        message = '\nSQUARES ARE 0-8, TOP LEFT TO BOTTOM RIGHT, TRAVEL HORIZONTALLY\n   |   |   \n ' + self.square1+' | '+self.square2+' | '+self.square3+'  \n___|___|___\n   |   |   \n '+self.square4+' | '+self.square5 + ' | ' + self.square6+'  \n___|___|___\n   |   |   \n ' + self.square7 + ' | ' + self.square8+' | ' + self.square9+'  \n   |   |   '
        print(message)


game=Board()

print(game.grid)

while True:
    entry = raw_input('Please enter a number\n')
    if entry == '0':
        game.square1='X'

    elif entry == '1':
        game.square2='X'

    elif entry == '2':
        game.square3='X'

    elif entry == '3':
        game.square4='X'

    elif entry == '4':
        game.square5='X'

    elif entry == '5':
        game.square6='X'

    elif entry == '6':
        game.square7='X'

    elif entry == '7':
        game.square8='X'

    elif entry == '8':
        game.square9='X'

    print(game.grid())