Reputation: 21
I'm writing a program for a Python class. It must use OOP to create a Tic Tac Toe game. There are a few requirements that I have completed. The final requirement is that the computer player must be 'smart' - doing things like blocking, playing consecutive spots when it can, and so forth. My professor didn't really give us any instruction on how to do this, so I'm a bit baffled.
Where I'm at right now in the program is attempting to provide a check for whether or not the player is one move away from winning via horizontal row. Here's my code (it's extremely messy and inefficient, I know).
from random import randint
class TTT:
board = [[' ' for row in range(3)] for col in range(3)]
currentgame = []
def print(self):
print("\n-----\n".join(["|".join(row) for row in self.board]))
def mark(self,pos,mark):
x,y = pos
self.board[x][y] = mark
def win(self,mark):
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):
if self.board == [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]:
self.mark((randint(0,2),randint(0,2)),mark)
def choose(self,mark):
spotx = randint(0,2)
spoty = randint(0,2)
if self.board[spotx][spoty] == ' ':
self.mark((spotx,spoty),mark)
else:
self.choose(mark)
def choose(self,mark):
spotx = randint(0,2)
spoty = randint(0,2)
if self.rowabouttowin(mark) == (0,0) or (0,1) or (0,2) or (1,0) or (1,1) or (1,2) or (2,0) or (2,1) or (2,2):
self.mark((self.rowabouttowin(mark)),mark)
if self.legalspace(spotx,spoty):
self.mark((spotx,spoty),mark)
else:
self.choose(mark)
def legalspace(self,spotx,spoty):
if self.board[spotx][spoty] == ' ':
return True
else:
return False
def rowabouttowin(self,mark):
for row in range(3):
if self.board[row] == [mark,mark,'']:
return (row,2)
if self.board[row] == [mark,'',mark]:
return (row,1)
if self.board[row] == ['',mark,mark]:
return (row,0)
else:
return 4
My problem is that I get an error message:
Traceback (most recent call last):
File "<pyshell#194>", line 1, in <module>
x.choose('x')
File "/Users/richiehoffman/Documents/Python Programs/Tic Tac Toe.py", line 64, in choose
self.mark((self.rowabouttowin(mark)),mark)
File "/Users/richiehoffman/Documents/Python Programs/Tic Tac Toe.py", line 23, in mark
x,y = pos
TypeError: 'int' object is not iterable
I'm frankly out of my zone of knowledge here, as the assignment is significantly beyond what the couple of days in class we spent on object oriented Python. I'm sure that I made an error at some point in the choose or the rowabouttowin methods that is outputting an integer instead of some iterable data.
Any help?
Upvotes: 1
Views: 1567
Reputation: 2250
def rowabouttowin(self,mark):
[...]
return 4
This returns an int
instead of (int,int)
as expected in self.mark()
By the way, I believe your code would be far more readable if you did not use the same names (such as mark
) for both variables, methods, and parameters.
Upvotes: 2