-2
class game:
   gamestillgoing=True
   if gamestillgoing:
     def __init__(self, board, turn):
        self.board = ["#", "-", "-", "-",
                      "-", "-", "-",
                      "-", "-", "-"]
        self.turn = input("Choose your turn X or O?\n")
        print("Player {} goes first!".format(turn))

     def displayboard(self):
        print(self.board[1] + "|" + self.board[2] + "|" + self.board[3])
        print(self.board[4] + "|" + self.board[5] + "|" + self.board[6])
        print(self.board[7] + "|" + self.board[8] + "|" + self.board[9])
        game.displayboard(self)

     def choosepositionX(self):

        position = int(input("Choose the position from 1-9\n"))
        position = int(position)
        if self.turn == "X":
            self.board[position] = "X"
            game.displayboard(self)
        else:
            while not self.turn == "X":
                print("Choose the valid input.")

     def choosepositonO(self):
        position = int(input("Choose the position from 1-9\n"))
        position = int(position)
        if self.turn == "O":
            self.board[position] = "O"
            game.displayboard(self)
        else:
            while not self.turn == "O":
                print("Choose the valid input.")

     def checkwinrow(self):
        row1 = self.board[1] == self.board[2] == self.board[3]
        row2 = self.board[4] == self.board[5] == self.board[6]
        row3 = self.board[7] == self.board[8] == self.board[9]
        if row1 or row2 or row3 == "X":
            print("Player X has won!\n ")
            gamestillgoing = False
        elif row1 or row2 or row3 == "O":
            print("Player O has won!\n")
            gamestillgoing = False
        else:
            gamestillgoing = True

     def checkwincolumn(self):
        column1 = self.board[1] == self.board[4] == self.board[7]
        column2 = self.board[2] == self.board[5] == self.board[8]
        column3 = self.board[3] == self.board[6] == self.board[9]
        if column1 or column2 or column3 == "X":
            print("Player X has won!\n")
            gamestillgoing = False
        elif column1 or column2 or column3 == "O":
            print("Player O has won!\n")
            gamestillgoing = False
        else:
            gamestillgoing = True

     def checkwindiagonal(self):
        diagonal1 = self.board[1] == self.board[5] == self.board[9]
        diagonal2 = self.board[3] == self.board[5] == self.board[7]
        if diagonal1 or diagonal2 == "X":
            print("Player X has won!\n")
            gamestillgoing = False
        elif diagonal1 or diagonal2 == "O":
            print("Player O has win!\n")
            gamestillgoing = False
        else:
            gamestillgoing = True

     def checktie(self):
        row1 = self.board[1] == self.board[2] == self.board[3]
        row2 = self.board[4] == self.board[5] == self.board[6]
        row3 = self.board[7] == self.board[8] == self.board[9]
        column1 = self.board[1] == self.board[4] == self.board[7]
        column2 = self.board[2] == self.board[5] == self.board[8]
        column3 = self.board[3] == self.board[6] == self.board[9]
        diagonal1 = self.board[1] == self.board[5] == self.board[9]
        diagonal2 = self.board[3] == self.board[5] == self.board[7]
        if row1 == row2 == row3 or column1 == column2 == column3 or diagonal1 == diagonal2 != "X" or "O":
            print("The game is Tie!")
            gamestillgoing = False
        else:
            gamestillgoing = True

     def playerturn(self):
        if self.turn == "X":
            game.choosepositionX(self)
        elif self.turn == "O":
            game.choosepositonO(self)
        else:
            "Choose the valid Input!\n"
        game.playerturn(self)
   else:
        gamestillgoing=False
 Start=game()
 Start()

When I try to run this game its saying that there is an error in the last two lines. Can anyone help me to solve this problem? I just need the solution for the errors in the last two lines (i.e Start=game() Start()) The main problem is in calling the class.

Samwise
  • 68,105
  • 3
  • 30
  • 44
Logesh R.
  • 9
  • 3
  • 1
    Just remove the `Start()` line. The line `Start=game()` already calls your `__init__` function. Your `game` instance isn't itself callable as a function after you've initialized it. – Samwise May 16 '20 at 16:09
  • 1
    That's a lot of code! Its best to post a small example showing the problem. You say there is an error - don't make us guess, post the full traceback python gives you. I notice you include `self` in method calls, e.g., `game.playerturn(self)`. Python fills in the `self` on method calls so take that out. And thinking about it, `self` would have to be a variable in the scope of the caller to even work.... – tdelaney May 16 '20 at 16:10

1 Answers1

0

No, you can't generally "call" a class in Python. But you can call its methods (including its constructor). (If you really must call a class, or an instance thereof, you have some extra work to do.

You have many syntax, logic, and architectural errors in your code.

  1. Your use of gamestillgoing as a class attribute (instead of a member attribute) isn't technically impossible, but it is not advised. You should make gamestillgoing be a member, or better yet, a property.

  2. You have an infinite recursion in displayboard().

  3. You have a mix of indentations, 2-5 spaces. You should always indent 4 spaces.

  4. You can't just call your class once. You need to call the playerturn() method repeatedly until gamestillgoing is False.

I cleaned up those problems. This is far from a working game (eg, it doesn't alternate turns yet - you have do that after each player takes their turn), but this should be enough to get you un-stuck.

class game:
    def __init__(self):
        self._gamestillgoing = True
        self.board = ["#", "-", "-", "-",
                      "-", "-", "-",
                      "-", "-", "-"]
        self.turn = input("Choose your turn X or O?\n")
        print("Player {} goes first!".format(self.turn))

    @property
    def gamestillgoing(self):
        return self._gamestillgoing


    def displayboard(self):
        print(self.board[1] + "|" + self.board[2] + "|" + self.board[3])
        print(self.board[4] + "|" + self.board[5] + "|" + self.board[6])
        print(self.board[7] + "|" + self.board[8] + "|" + self.board[9])

    def choosepositionX(self):
        position = int(input("Choose the position from 1-9\n"))
        position = int(position)
        if self.turn == "X":
            self.board[position] = "X"
            game.displayboard(self)
        else:
            while not self.turn == "X":
                print("Choose the valid input.")

    def choosepositonO(self):
        position = int(input("Choose the position from 1-9\n"))
        position = int(position)
        if self.turn == "O":
            self.board[position] = "O"
            game.displayboard(self)
        else:
            while not self.turn == "O":
                print("Choose the valid input.")

    def checkwinrow(self):
        row1 = self.board[1] == self.board[2] == self.board[3]
        row2 = self.board[4] == self.board[5] == self.board[6]
        row3 = self.board[7] == self.board[8] == self.board[9]
        if row1 or row2 or row3 == "X":
            print("Player X has won!\n ")
            self._gamestillgoing = False
        elif row1 or row2 or row3 == "O":
            print("Player O has won!\n")
            self._gamestillgoing = False
        else:
            self._gamestillgoing = True

    def checkwincolumn(self):
        column1 = self.board[1] == self.board[4] == self.board[7]
        column2 = self.board[2] == self.board[5] == self.board[8]
        column3 = self.board[3] == self.board[6] == self.board[9]
        if column1 or column2 or column3 == "X":
            print("Player X has won!\n")
            self._gamestillgoing = False
        elif column1 or column2 or column3 == "O":
            print("Player O has won!\n")
            self._gamestillgoing = False
        else:
            self._gamestillgoing = True

    def checkwindiagonal(self):
        diagonal1 = self.board[1] == self.board[5] == self.board[9]
        diagonal2 = self.board[3] == self.board[5] == self.board[7]
        if diagonal1 or diagonal2 == "X":
            print("Player X has won!\n")
            self._gamestillgoing = False
        elif diagonal1 or diagonal2 == "O":
            print("Player O has win!\n")
            self._gamestillgoing = False
        else:
            self._gamestillgoing = True

    def checktie(self):
        row1 = self.board[1] == self.board[2] == self.board[3]
        row2 = self.board[4] == self.board[5] == self.board[6]
        row3 = self.board[7] == self.board[8] == self.board[9]
        column1 = self.board[1] == self.board[4] == self.board[7]
        column2 = self.board[2] == self.board[5] == self.board[8]
        column3 = self.board[3] == self.board[6] == self.board[9]
        diagonal1 = self.board[1] == self.board[5] == self.board[9]
        diagonal2 = self.board[3] == self.board[5] == self.board[7]
        if row1 == row2 == row3 or column1 == column2 == column3 or diagonal1 == diagonal2 != "X" or "O":
            print("The game is Tie!")
            self._gamestillgoing = False
        else:
            self._gamestillgoing = True

    def playerturn(self):
        if self.turn == "X":
            self.choosepositionX()
        elif self.turn == "O":
            self.choosepositonO()
        else:
            "Choose the valid Input!\n"
            self.playerturn(self)


g=game()
while g.gamestillgoing:
    g.playerturn()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Rusty Widebottom
  • 985
  • 2
  • 5
  • 14