patternpythonMinor
Tic Tac Toe in Python, converted to use classes
Viewed 0 times
toetictacpythonclassesuseconverted
Problem
I am new to using Classes and Objects, and as such thought i would edit a previous Tic Tac Toe game i created, this time using Classes. I found it easier using entirely public (Class?) variables (ie, ClassName.Variable). This way any variable can be accessed from anywhere, making utilising said variables much easier across the code. Lists are then used to store things like the board, the game types, and in other cases besides this one, whose go it would be next (ie, when you introduce three players) etc. Lists are beautiful things.
I am aware i have put self in some initialisers, but not actually used it. i am also aware some Public variables are named Class1.Variable when they are only ever used in Class2, as i set them to whichever class the variable would belong to.
Oh, and i always use Capitals for Class/Function/Variable names. i know its frowned upon but i personally find it much more organised and neat :)
I would like to ask :
How can i use self and protected/private variables to best suit the game of Tic Tac Toe and avoid all-Public variables?
Is it a Crime to use entirely Public variables?
Are there any other issues or problems with my coding style, and how can i correct that?
Original Code
```
from random import *
from time import *
#Noughts and Crosses
#Only works in Python 3
def PrintBoard(Places): #Prints the Board, mirrors a laptop's keypad
print('',Places[6],'|',Places[7],'|',Places[8],'')
print('---|---|---')
print('',Places[3],'|',Places[4],'|',Places[5],'')
print('---|---|---')
print('',Places[0],'|',Places[1],'|',Places[2],'')
print()#empty line at the bottom to make room for further prompts
def VerifyPosition(Places, Type):#Verifys and asks for your selection
Valid = False
print(Type+"'s Go...")#O's Go... / X's Go...
while not Valid:
Position = input("Please type where you wish to place: ")
while Position.isdigit() == False or int(Position) > 9 or int(Position) <= 0:#Ensures the play
I am aware i have put self in some initialisers, but not actually used it. i am also aware some Public variables are named Class1.Variable when they are only ever used in Class2, as i set them to whichever class the variable would belong to.
Oh, and i always use Capitals for Class/Function/Variable names. i know its frowned upon but i personally find it much more organised and neat :)
I would like to ask :
How can i use self and protected/private variables to best suit the game of Tic Tac Toe and avoid all-Public variables?
Is it a Crime to use entirely Public variables?
Are there any other issues or problems with my coding style, and how can i correct that?
Original Code
```
from random import *
from time import *
#Noughts and Crosses
#Only works in Python 3
def PrintBoard(Places): #Prints the Board, mirrors a laptop's keypad
print('',Places[6],'|',Places[7],'|',Places[8],'')
print('---|---|---')
print('',Places[3],'|',Places[4],'|',Places[5],'')
print('---|---|---')
print('',Places[0],'|',Places[1],'|',Places[2],'')
print()#empty line at the bottom to make room for further prompts
def VerifyPosition(Places, Type):#Verifys and asks for your selection
Valid = False
print(Type+"'s Go...")#O's Go... / X's Go...
while not Valid:
Position = input("Please type where you wish to place: ")
while Position.isdigit() == False or int(Position) > 9 or int(Position) <= 0:#Ensures the play
Solution
I'm not going to go into line by line detail, because there's little there I would salvage and I don't think just writing you an actual OOP version counts as code review. Instead, I want to talk about your approach.
I am new to using Classes and Objects, and as such thought i would
edit a previous Tic Tac Toe game i created, this time using Classes.
This is a good idea. Rewriting existing code in new paradigms can give you a feel for their strengths and weaknesses. Sometimes you'll feel a tension where a particular paradigm doesn't fit a particular task, and that provides useful information.
I found it easier using entirely public (Class?) variables (ie,
ClassName.Variable). This way any variable can be accessed from
anywhere, making utilising said variables much easier across the code.
That is a terrible reason to do something. The use of global state in general is strongly discouraged because it makes your programs harder to understand and test. In particular it seems ludicrous to try an "OOP" approach that only uses class objects, never instances. You should be able to instantiate and test a
Lists are then used to store things like the board, the game types,
and in other cases besides this one, whose go it would be next (ie,
when you introduce three players) etc. Lists are beautiful things.
I would probably use nested lists for the board. A flat list is fine, but
However there is a clear problem in the way you reverse the board for printing; if you used the order it was displayed in to start with, the code would be simpler and you wouldn't have to keep transforming it in your head (another tension you should have listened to, see below).
Also be aware of the other data structures Python offers; the dictionary in particular is everywhere.
I am aware i have put self in some initialisers, but not actually used
it.
Why? I mentioned feeling tension above. You clearly felt it here but apparently decided that adding comments that this was probably a bad idea was the right thing to do. Why didn't you instead try to find a good idea? The tension was telling you something; you aren't actually writing classes here. This isn't OOP.
i am also aware some Public variables are named Class1.Variable when
they are only ever used in Class2, as i set them to whichever class
the variable would belong to.
I'm not sure quite what you mean here, but I suspect you need to read up on "encapsulation" and "separation of concerns".
Oh, and i always use Capitals for Class/Function/Variable names. i
know its frowned upon but i personally find it much more organised and
neat :)
That's fine if nobody else ever reads your code. But that's rarely the case, and specifically not here where you're asking other people to review it. As a more direct benefit, following the conventions in your own code makes it easier for you to read and understand third party code that also follows them.
How can i use self and protected/private variables to best suit the
game of Tic Tac Toe and avoid all-Public variables?
This is more a rewrite than a code review, I've provided some starting points below. But there are loads of resources on OOP in general and in Python specifically out there, do some research and use them.
Is it a Crime to use entirely Public variables?
Literally, no. But also Python is more permissive here than other OOP languages; "we're all consenting adults". Your problem here isn't that they're public, it's that they're all class attributes. You use classes as singleton instances.
Are there any other issues or problems with my coding style, and how
can i correct that?.
Read more of other people's code; see the standard library, look at similar stuff on e.g. GitHub. Read other code reviews here, so you can avoid the mistakes others have already had corrected. There are quite a few purely on tic tac toe in Python: https://codereview.stackexchange.com/search?q=%5Bpython%5D+tic+tac+toe+is%3Aq
I am new to using Classes and Objects, and as such thought i would
edit a previous Tic Tac Toe game i created, this time using Classes.
This is a good idea. Rewriting existing code in new paradigms can give you a feel for their strengths and weaknesses. Sometimes you'll feel a tension where a particular paradigm doesn't fit a particular task, and that provides useful information.
I found it easier using entirely public (Class?) variables (ie,
ClassName.Variable). This way any variable can be accessed from
anywhere, making utilising said variables much easier across the code.
That is a terrible reason to do something. The use of global state in general is strongly discouraged because it makes your programs harder to understand and test. In particular it seems ludicrous to try an "OOP" approach that only uses class objects, never instances. You should be able to instantiate and test a
Board completely independent of the game and the players, for example; if it doesn't have to know or care about those things and can focus purely on spaces with or without values in it becomes much simpler. Lists are then used to store things like the board, the game types,
and in other cases besides this one, whose go it would be next (ie,
when you introduce three players) etc. Lists are beautiful things.
I would probably use nested lists for the board. A flat list is fine, but
(row, col) indexing makes the tasks involved in tic tac toe a bit more straightforward. However there is a clear problem in the way you reverse the board for printing; if you used the order it was displayed in to start with, the code would be simpler and you wouldn't have to keep transforming it in your head (another tension you should have listened to, see below).
Also be aware of the other data structures Python offers; the dictionary in particular is everywhere.
I am aware i have put self in some initialisers, but not actually used
it.
Why? I mentioned feeling tension above. You clearly felt it here but apparently decided that adding comments that this was probably a bad idea was the right thing to do. Why didn't you instead try to find a good idea? The tension was telling you something; you aren't actually writing classes here. This isn't OOP.
i am also aware some Public variables are named Class1.Variable when
they are only ever used in Class2, as i set them to whichever class
the variable would belong to.
I'm not sure quite what you mean here, but I suspect you need to read up on "encapsulation" and "separation of concerns".
Oh, and i always use Capitals for Class/Function/Variable names. i
know its frowned upon but i personally find it much more organised and
neat :)
That's fine if nobody else ever reads your code. But that's rarely the case, and specifically not here where you're asking other people to review it. As a more direct benefit, following the conventions in your own code makes it easier for you to read and understand third party code that also follows them.
How can i use self and protected/private variables to best suit the
game of Tic Tac Toe and avoid all-Public variables?
This is more a rewrite than a code review, I've provided some starting points below. But there are loads of resources on OOP in general and in Python specifically out there, do some research and use them.
Is it a Crime to use entirely Public variables?
Literally, no. But also Python is more permissive here than other OOP languages; "we're all consenting adults". Your problem here isn't that they're public, it's that they're all class attributes. You use classes as singleton instances.
Are there any other issues or problems with my coding style, and how
can i correct that?.
Read more of other people's code; see the standard library, look at similar stuff on e.g. GitHub. Read other code reviews here, so you can avoid the mistakes others have already had corrected. There are quite a few purely on tic tac toe in Python: https://codereview.stackexchange.com/search?q=%5Bpython%5D+tic+tac+toe+is%3Aq
Context
StackExchange Code Review Q#158494, answer score: 5
Revisions (0)
No revisions yet.