patternpythonMinor
Python Checkers
Viewed 0 times
pythoncheckersstackoverflow
Problem
This is a program I made to practice using turtle graphics. I am looking for pointers specifically on how I handle the game board. Currently it holds all of the grid objects in a nested list. Sorry if the documentation in the program is unconventional. This is one of my first projects and I'm excited to see what I can do to improve.
```
#import needed functions
import turtle
from datetime import datetime
from random import randint
gameTitle = "pyCheckers v4.17"
#setup output window
wn = turtle.Screen()
wn.tracer(0,0)
wn.title(gameTitle)
#checks to see if coordinates (x, y) are on the board
def onGrid(x,y):
if((x in range(8)) and (y in range(8))):
return True
else:
return False
#prints a log string to the console
def logToConsole(*strings):
dt = datetime.today()
print("" % (dt.hour,dt.minute,dt.second),end=" ")
for string in strings:
print(string,end="")
print()
#main game class
class checkers:
#create game instance
def __init__(self,screen):
self.screen = screen
self.resetGame()
self.createTitles()
#resets the matrix that stores game data
def resetGame(self):
self.turn = randint(1,2)
logToConsole("\tStarting Player: %s" % (self.turn))
self.createGrid()
#create the matrix that stores the game data
def createGrid(self):
#create empty matrix
self.matrix = [[None]8,[None]8,[None]8,[None]8,[None]8,[None]8,[None]8,[None]8]
#populate matrix with grid objects and assign them attributes
for x in range(8):
for y in range(8):
self.matrix[x][y] = grid(self.screen)
self.matrix[x][y].moveGrid(x,y)
if(((x + y) % 2) == 1):
self.matrix[x][y].colored = True
if(y in [0,1,2]):
self.matrix[x][y].pawn = True
self.matrix[x][y].player = 1
if(y in [5,6,7]):
```
#import needed functions
import turtle
from datetime import datetime
from random import randint
gameTitle = "pyCheckers v4.17"
#setup output window
wn = turtle.Screen()
wn.tracer(0,0)
wn.title(gameTitle)
#checks to see if coordinates (x, y) are on the board
def onGrid(x,y):
if((x in range(8)) and (y in range(8))):
return True
else:
return False
#prints a log string to the console
def logToConsole(*strings):
dt = datetime.today()
print("" % (dt.hour,dt.minute,dt.second),end=" ")
for string in strings:
print(string,end="")
print()
#main game class
class checkers:
#create game instance
def __init__(self,screen):
self.screen = screen
self.resetGame()
self.createTitles()
#resets the matrix that stores game data
def resetGame(self):
self.turn = randint(1,2)
logToConsole("\tStarting Player: %s" % (self.turn))
self.createGrid()
#create the matrix that stores the game data
def createGrid(self):
#create empty matrix
self.matrix = [[None]8,[None]8,[None]8,[None]8,[None]8,[None]8,[None]8,[None]8]
#populate matrix with grid objects and assign them attributes
for x in range(8):
for y in range(8):
self.matrix[x][y] = grid(self.screen)
self.matrix[x][y].moveGrid(x,y)
if(((x + y) % 2) == 1):
self.matrix[x][y].colored = True
if(y in [0,1,2]):
self.matrix[x][y].pawn = True
self.matrix[x][y].player = 1
if(y in [5,6,7]):
Solution
Performance
This is very inefficient:
The
The fix is easy, with a simple range condition:
Notice that I removed the
Simplify
This, on the other hand, is a good opportunity to improve using
Like this:
Instead of
Instead of
Coding style
Python has a coding style guide called PEP8.
I suggest to read it carefully.
This is very inefficient:
def onGrid(x,y):
if((x in range(8)) and (y in range(8))):
return True
else:
return FalseThe
range(8) creates a generator of values 0..7, which will be iterated until a match is found. For example, whenever this function gets called with an invalid x value, x in range(8) will iterate over 0..7 before the condition is evaluated false.The fix is easy, with a simple range condition:
def onGrid(x,y):
return 0 <= x < 8 and 0 <= y < 8Notice that I removed the
if-else, boolean expressions can be returned directly like this, and this compact writing style is recommended too.Simplify
This, on the other hand, is a good opportunity to improve using
range:self.matrix = [[None]*8,[None]*8,[None]*8,[None]*8,[None]*8,[None]*8,[None]*8,[None]*8]Like this:
self.matrix = [[None]*8 for _ in range(8)]Instead of
if something == True, write simply if something.Instead of
if somelist != [], write simply if somelist.Coding style
Python has a coding style guide called PEP8.
I suggest to read it carefully.
Code Snippets
def onGrid(x,y):
if((x in range(8)) and (y in range(8))):
return True
else:
return Falsedef onGrid(x,y):
return 0 <= x < 8 and 0 <= y < 8self.matrix = [[None]*8,[None]*8,[None]*8,[None]*8,[None]*8,[None]*8,[None]*8,[None]*8]self.matrix = [[None]*8 for _ in range(8)]Context
StackExchange Code Review Q#144606, answer score: 4
Revisions (0)
No revisions yet.