1

課題で三目並べゲームを書いています。オブジェクト指向プログラミングを使用する必要があり、比較的スマートである必要があります。プレーヤーの成功をブロックする必要があります。私はこれに多くの問題を抱えています。

私の問題は、私のrowabouttowin方法に起因しています。非常に複雑なリスト内包表記を行ったのですが、それが正しく行われたとは思えません。

私が欲しいのは、プレーヤーがゲームに水平に勝とうとしているかどうか (O _ O、OO _、またはマーク = O の場合は _ OO) をチェックし、プレーヤーが勝っている場合は、コンピューターがプレイする位置を返すメソッドです。

これにどのようにアプローチするのが最善かについての助けやアドバイスはありますか?

from random import randint

class TTT:
    board = [[' ' for row in range(3)] for col in range(3)]
    currentgame = []

    def print(self):
    """Displays the current board."""
        print("\n-----\n".join("|".join(row) for row in self.board))

    def mark(self,pos,mark):
    """Method that places designated mark at designated position on the board."""
        x,y = pos
        self.board[x][y] = mark

    def win(self,mark):
    """Method that checks if someone has won the game."""
        if mark == self.board[0][0] == self.board[1][1] == self.board[2][2]:
            return True
        if mark == self.board[2][0] == self.board[1][1] == self.board[0][2]:
            return True
        elif mark == self.board[0][0] == self.board[1][0] == self.board[2][0]:
            return True
        elif mark == self.board[1][0] == self.board[1][1] == self.board[1][2]:
            return True
        elif mark == self.board[0][1] == self.board[1][1] == self.board[2][1]:
            return True
        elif mark == self.board[0][2] == self.board[1][2] == self.board[2][2]:
            return True
        elif mark == self.board[0][0] == self.board[0][1] == self.board[0][2]:
            return True
        elif mark == self.board[2][0] == self.board[2][1] == self.board[2][2]:
            return True
        else:
            return False



    def choose(self,mark):
    """The computer chooses a place to play. If the player is not about to win,
    plays randomly. Otherwise, does a series of checks to see if the player is about
    to win horizontally, vertically, or diagonally. I only have horizontal done."""
        spotx = randint(0,2)
        spoty = randint(0,2)
        if self.rowabouttowin(mark):
            self.mark((self.rowabouttowin(mark)),mark)
        elif self.legalspace(spotx,spoty):
            self.mark((spotx,spoty),mark)
        else:
            self.choose(mark)


    def legalspace(self,spotx,spoty):
    """Returns True if the provided spot is empty."""
        if self.board[spotx][spoty] == ' ':
            return True
        else:
            return False


    def rowabouttowin(self,mark):
    """If the player is about to win via a horizontal 3-in-a-row,
    returns location where the computer should play"""
        for row in range(3):
            if any(' ' == self.board[row][1] for i in range(3)) and any(self.board[row][i] == self.board[row][j] for i in range(3) for j in range(3)):
                    if self.board[row][i] == ' ' : yield(self.board[row][i % 3], self.board[row][i])

これにより、現在、次のエラー メッセージが表示されます。

Traceback (most recent call last):
  File "<pyshell#49>", line 1, in <module>
    x.choose('x')
  File "/Users/richiehoffman/Documents/Python Programs/Tic Tac Toe.py", line 40, in choose
    self.mark((self.rowabouttowin(mark)),mark)
  File "/Users/richiehoffman/Documents/Python Programs/Tic Tac Toe.py", line 11, in mark
    x,y = pos
  File "/Users/richiehoffman/Documents/Python Programs/Tic Tac Toe.py", line 61, in rowabouttowin
    if self.board[row][i] == ' ' : yield(self.board[row][i % 3], self.board[row][i])
NameError: global name 'i' is not defined
4

1 に答える 1

2

いくつかのヒント:

  • インスタンス変数ではなくクラス変数を使用しているため、違いを調べてください。設定する変数はインスタンスに属する必要があるため、インスタンス変数を使用するようにクラスを変更しました。

  • 物事を読みやすくすることを検討してください。

  • __str__クラスの印刷可能なバージョンを作成するために使用します。そうすれば、print(class_instance)すべてうまくいきます。

ここに私が変更したものがあります:

from random import randint

class TTT(object):
    def __init__(self):
        self.board = [[' ' for row in range(3)] for col in range(3)]
        self.currentgame = []

    def __str__(self):
        """Displays the current board."""

        return "\n-----\n".join("|".join(row) for row in self.board)

    def mark(self, pos, mark):
        """Method that places designated mark at designated position on the board."""

        x, y = pos
        self.board[x][y] = mark

    def win(self, mark):
        """Method that checks if someone has won the game."""

        for row in self.board:
            if row[0] == row[1] == row[2]:
                return True

        for i in range(3):
            if self.board[0][i] == self.board[1][i] == self.board[2][i]:
                return True

        if board[0][0] == board[1][1] == board[2][2]:
            return True
        elif board[0][2] == board[1][1] == board[2][0]:
            return True
        else:
            return False



    def choose(self, mark):
        """The computer chooses a place to play. If the player is not about to win, 
        plays randomly. Otherwise, does a series of checks to see if the player is about
        to win horizontally, vertically, or diagonally. I only have horizontal done."""

        spotx = randint(0, 2)
        spoty = randint(0, 2)

        if self.rowabouttowin(mark):
            self.mark((self.rowabouttowin(mark)), mark)
        elif self.legalspace(spotx, spoty):
            self.mark((spotx, spoty), mark)
        else:
            self.choose(mark)


    def legalspace(self, spotx, spoty):
        """Returns True if the provided spot is empty."""

        return self.board[spotx][spoty] == ' '


    def rowabouttowin(self, mark):
        """If the player is about to win via a horizontal 3-in-a-row, 
        returns location where the computer should play"""

        for row in range(3):
            check_one = any(' ' == self.board[row][1] for i in range(3))
            check_two = any(self.board[row][i] == self.board[row][j] for i in range(3) for j in range(3))

            # I have no idea what this code does

            if self.board[row][i] == ' ' :
                yield self.board[row][i % 3], self.board[row][i]
于 2012-12-05T23:16:05.003 に答える