patternpythonMinor
Checkers in Python 3
Viewed 0 times
pythoncheckersstackoverflow
Problem
I'm writing a small checkers game in python (international rules) and I need some pointers here and there. For the moment, I am only drawing the board and checking the syntax of moves with a regex.
For review:
Here is how looks:
```
#CONSTANTS SECTION
CLEAR = lambda: os.system('cls' if os.name == 'nt' else 'clear')
GRID_HEIGHT = 10
GRID_WIDTH = 10
#White pawn
WP = "▓"
#White queen
WQ = '£'
#Empty cell
EC = ' '
#Black pawn
BP = '░'
#Black Queen
BQ = '$'
#players
PLAYERS = Enum("Players", "White Black")
#END OF CONSTANTS SECTION
def init_grid():
"""Initialize the new game grid"""
grid = [[EC, BP, EC, BP, EC, BP, EC, BP, EC, BP],
[BP, EC, BP, EC, BP, EC, BP, EC, BP, EC],
[EC, BP, EC, BP, EC, BP, EC, BP, EC, BP],
[BP, EC, BP, EC, BP, EC, BP, EC, BP, EC],
[EC, EC, EC, EC, EC, EC, EC, EC, EC, EC],
[EC, EC, EC, EC, EC, EC, EC, EC, EC, EC],
[EC, WP, EC, WP, EC, WP, EC, WP, EC, WP],
[WP, EC, WP, EC, WP, EC, WP, EC, WP, EC],
[EC, WP, EC, WP, EC, WP, EC, WP, EC, WP],
[WP, EC, WP, EC, WP, EC, WP, EC, WP, EC]]
return grid
def move(value_package)
For review:
- Usage of a value package for passing around game state (grid, turn_count, and current player), is there a better way, or is this acceptable?
- I have some issue with
GRID_HEIGHTandGRID_WIDTH. As they are constants and supposed to be reflecting the size of my grid, I know it is bad to let them hardcoded to 10 that way. However, as far as I can tell, I can't set their values after generating the grid. I guess I should compute them and put them in the value package.
- As for
transform_response_into_tuples(response), I would like to find a neater way to make the tuples (I should say the pairs). I mean it's working but I think it is ugly.
- Also, the printing is quite rigid, note the use of empty print statement in order to line break.
- Finally, is there some statement that could be made more "pythonic"?
Here is how looks:
```
#CONSTANTS SECTION
CLEAR = lambda: os.system('cls' if os.name == 'nt' else 'clear')
GRID_HEIGHT = 10
GRID_WIDTH = 10
#White pawn
WP = "▓"
#White queen
WQ = '£'
#Empty cell
EC = ' '
#Black pawn
BP = '░'
#Black Queen
BQ = '$'
#players
PLAYERS = Enum("Players", "White Black")
#END OF CONSTANTS SECTION
def init_grid():
"""Initialize the new game grid"""
grid = [[EC, BP, EC, BP, EC, BP, EC, BP, EC, BP],
[BP, EC, BP, EC, BP, EC, BP, EC, BP, EC],
[EC, BP, EC, BP, EC, BP, EC, BP, EC, BP],
[BP, EC, BP, EC, BP, EC, BP, EC, BP, EC],
[EC, EC, EC, EC, EC, EC, EC, EC, EC, EC],
[EC, EC, EC, EC, EC, EC, EC, EC, EC, EC],
[EC, WP, EC, WP, EC, WP, EC, WP, EC, WP],
[WP, EC, WP, EC, WP, EC, WP, EC, WP, EC],
[EC, WP, EC, WP, EC, WP, EC, WP, EC, WP],
[WP, EC, WP, EC, WP, EC, WP, EC, WP, EC]]
return grid
def move(value_package)
Solution
-
The
puts the code on the verge of being off-topic.
-
The
-
In logical context
directly. In fact,
Also I recommend to parse the response directly: syntax validation is a side effect of successful parsing.
-
Docstrings are not very informative.
It doesn't tell me much that
The
def check_move_legality(board, tuples):
"""This function checks if the move is legal"""
return Trueputs the code on the verge of being off-topic.
-
The
from and to fields is not enough to uniquely identify the move: there could be two multiple captures following different paths.-
In logical context
None evaluates to False. You mayreturn re.match("^([0-9][A-J]){2}$", response)directly. In fact,
return False if condition else True is considered an anti-pattern.Also I recommend to parse the response directly: syntax validation is a side effect of successful parsing.
-
Docstrings are not very informative.
It doesn't tell me much that
This function decompose the response into tuples. Consider something along the lines of Parse response into start and end fields. If the function has a side effect (such as ultimate change of a board state) it must be in docstring as well.Code Snippets
def check_move_legality(board, tuples):
"""This function checks if the move is legal"""
return Truereturn re.match("^([0-9][A-J]){2}$", response)Context
StackExchange Code Review Q#119336, answer score: 6
Revisions (0)
No revisions yet.