Essentially, I am trying to access the value of one function and I’m trying to use it within another function.
This is the current function I have:
def getMove(self):
print("Coordinates must range from 0 to 2, 0 corresponding to the first row/column, onwards")
xcoor = int(input("What is your x coordinate?"))
ycoor = int(input("What is your y coordinate?"))
if xcoor and ycoor not in range(0,3):
print("Coordinates must be between 0 and 2")
return f"({xcoor},{ycoor})"
and I am trying to use the ‘xcoor’ and y’coor’ variables here to set the move with the marker wherever the user specified. I thought that since you can access a 2d array’s individual element and it assign it a value, you could do the same thing when using variables, but this does not appear to be the case. Maybe I don’t understand how classes work when it comes to access or its syntactical issue, either way, some guidance and/or further reading would be appreciated.
def makeMove(board,marker):
#todo: code to alternate between players
marker = "X"
#makesmove
board[xcoor][ycoor] == marker
Full code if needed:
class Tictactoe:
def __init__(self,playerone,playertwo):
self.playerone = playerone
self.playertwo = playertwo
def newBoard(self):
# creates new board
return [[None, None, None],
[None, None, None],
[None, None, None]
]
def renderBoard(self):
board = self.newBoard()
# Prints current state of the board
for row in board:
print("|", end="")
for cell in row:
if cell is None:
print(" |", end="")
else:
print(f" {cell} |", end="")
print("n" + "-" * 13)
def getMove(self):
print("Coordinates must range from 0 to 2, 0 corresponding to the first row/column, onwards")
xcoor = int(input("What is your x coordinate?"))
ycoor = int(input("What is your y coordinate?"))
if xcoor and ycoor not in range(0,3):
print("Coordinates must be between 0 and 2")
return f"({xcoor},{ycoor})"
def makeMove(board,marker):
#todo: code to alternate between players
marker = "X"
#makesmove
board[xcoor][ycoor] == marker
def getWinner(self):
#todo write win conditions
'''if player one marks 3 in a row, player one wins, end game
if player two marks 3 in a row, player 2 wins, end game
'''
pass
def resetButton(self):
#todo: create a reset button that clears the board.
pass
def runGame(self):
#todo: write code that starts the game.
pass
def isBoardFull(self):
#todo: write code that checks if board is full
pass
def main():
test = Tictactoe("playerone", "playertwo")
print(test.makeMove())
if __name__ == '__main__':
main()
4