If you want to have a variable with a limited number of different values, you might want to use something better than string (you could use or emulate enums use or emulate enums).
If you want to have a variable with a limited number of different values, you might want to use something better than string (you could use or emulate enums).
If you want to have a variable with a limited number of different values, you might want to use something better than string (you could use or emulate enums).
Get rid of magic numbers and magic strings (see also second point).
Use relevant types (and not just strings).
If you want to have a variable with two different status (yes/no, true/false, active/sunk, etc), it would probably make sense to use booleans.
If you want to have a variable with a limited number of different values, you might want to use something better than string (you could use or emulate enums).
- Organise your code in a better way.
You've started to create classes which is probably a good idea. However, I am not quite sure that it makes sense to make things that complicated. Also, the dependency between the different parts of the code suggest that the definition is not as good as it could be.
I might be missing something but things could be pretty simple :
A game is a list of players and the record of the player who is supposed to play.
A player is just a name, a type of user (bot or player) and a board
A board is just a table of cells.
Each cell contains two pieces of information : what it contains (airCraftCarrier, battleship, submarine, cruiser, destroyer or water) and whether is has been bombed.
As most of these concepts are basically just container with no or little logic, it might or might not make sense to use classes for them. This is up to you.
- Don't repeat yourself.
Whenever you are writing the same piece of code twice, you are punishing your future self. If one of them needs to be updated, the other will need to be too which is going to be boring in the best case, forgotten and lead to weird bugs in the worst case.
- Let's do things in a pythonic way
You've written :
i=0;turnList=[]
maxTurns=(xGrid*yGrid)*2
while i < maxTurns:
if i%2==0:
#turnList.append('player')
turnList.append(player)
else:
turnList.append('Joshua')
i+=1
return turnList
which becomes, after using range
(or xrange
), removing useless parenthesis useless comments and useless variables and using the ternary operator (which is a good way to remove duplicated code) :
turnList=[]
for i in range(xGrid*yGrid*2):
turnList.append('Joshua' if i%2 else player)
return turnList
which now looks like a good candidate for list comprehension :
return [('Joshua' if i%2 else player) for i in range(xGrid*yGrid*2)]
The same kind of idea applies to :
validPoints=[]
x=0
while x < self.xGridSize:
y=0
while y < self.yGridSize:
validPoints.append("%s,%s"%(x,y))
y+=1
x+=1
return validPoints
becomes :
validPoints=[]
for x in range(self.xGridSize):
for y in range(self.yGridSize):
validPoints.append("%s,%s"%(x,y))
return validPoints
and could then be transformed with list comprehension. However, I'd like to point out that this method : 1) probably shouldn't be doing the string conversion itself 2) doesn't seem that useful.
I don't have more time to continue but I guess that's already a good amount of comments to start with.