patternpythonMinor
Chess game in Python - follow-up
Viewed 0 times
chessgamefollowpython
Problem
Three weeks ago I wrote the first version of my chess game in Python and shared on Code Review. Thanks to your suggestions, I improved my code. I would like to know if I am going in good direction with all this stuff.
```
__author__ = 'Goldsmitd'
class Condition:
def range(x,y):
return x > 8 and y > 8
def if_figure(board,x,y):
return board[x][y].sl == '.'
def same_team(x1,y1,x2,y2,board):
if board[x1][y1].team == board[x2][y2].team:
return True
else:
return False
def s_choice(board):
while True:
try:
print('give x and y')
x = int(input())
y = int(input())
except:
print('Coordinates can only be integers')
if Condition.range(x,y):
print('Coordinates are out of range')
elif Condition.if_figure(board,x,y):
print('Square chosen by you is empty')
else:
return x,y
break
def d_choice(board):
while True:
try:
print('give x and y')
x=int(input())
y=int(input())
except:
print('Coordinates can only be integers')
if Condition.range(x,y):
print('Coordinates are out of range')
else:
return x,y
break
def kill(x1,y1,x2,y2,board):
if board[x1][y1].team == 'white' and board[x2][y2].team == 'black':
return True
elif board[x1][y1].team == 'black' and board[x2][y2].team == 'white':
return True
else:
return False
def Pawnkill(x1,y1,x2,y2,board):
if board[x1][y1].team == 'white' and board[x2][y2].team == 'black' and board[x1][y1].name == 'Pawn':
return True
elif board[x1][y1].team == 'black' and board[x2][y2].team == 'white'and board[x1][y1].name == 'Pawn'
```
__author__ = 'Goldsmitd'
class Condition:
def range(x,y):
return x > 8 and y > 8
def if_figure(board,x,y):
return board[x][y].sl == '.'
def same_team(x1,y1,x2,y2,board):
if board[x1][y1].team == board[x2][y2].team:
return True
else:
return False
def s_choice(board):
while True:
try:
print('give x and y')
x = int(input())
y = int(input())
except:
print('Coordinates can only be integers')
if Condition.range(x,y):
print('Coordinates are out of range')
elif Condition.if_figure(board,x,y):
print('Square chosen by you is empty')
else:
return x,y
break
def d_choice(board):
while True:
try:
print('give x and y')
x=int(input())
y=int(input())
except:
print('Coordinates can only be integers')
if Condition.range(x,y):
print('Coordinates are out of range')
else:
return x,y
break
def kill(x1,y1,x2,y2,board):
if board[x1][y1].team == 'white' and board[x2][y2].team == 'black':
return True
elif board[x1][y1].team == 'black' and board[x2][y2].team == 'white':
return True
else:
return False
def Pawnkill(x1,y1,x2,y2,board):
if board[x1][y1].team == 'white' and board[x2][y2].team == 'black' and board[x1][y1].name == 'Pawn':
return True
elif board[x1][y1].team == 'black' and board[x2][y2].team == 'white'and board[x1][y1].name == 'Pawn'
Solution
Very nice game. Good effort!
A few suggestions to make it better.
The instruction to give
i tried entering
that I needed to enter one and then the other. making this small change could make it easier:
Not really a "code problem" per se, but rather a chess problem. The numbering on the board should start from the bottom left corner and move out.
And it should count from 1, not from 0.
So that the white Rook on The Queen side is at coordinates
The system accepts negative values for
all of these generate an error (since the grid currently starts counting at
At the start of the game, the left-most white pawn is sitting all the way across the board
You could use unicode chess pieces, this would allow you to free the alphabet and use the traditional Letter-Number chess coordinates.
This is the Wikipedia link. You can get them into python using chr()
output:
A few suggestions to make it better.
The instruction to give
x and y is a little confusing i tried entering
3, 6 then 3 and 6 ... it took me a while to figure out that I needed to enter one and then the other. making this small change could make it easier:
try:
print('give x and y')
x = int(input("x:"))
y = int(input("y:"))Not really a "code problem" per se, but rather a chess problem. The numbering on the board should start from the bottom left corner and move out.
And it should count from 1, not from 0.
r n b q k b n r 0
p p p p p p p p 1
P . . . . . . . 2
. . . . . . . . 3
. . . . . . . . 4
. . . . . . . . 5
. P P P P P P P 6
R N B Q K B N R 7
0 1 2 3 4 5 6 7 8So that the white Rook on The Queen side is at coordinates
(1,1) and the other white rook at coordinates (1,8)The system accepts negative values for
x and y, as well as the number 8... all of these generate an error (since the grid currently starts counting at
0) At the start of the game, the left-most white pawn is sitting all the way across the board
r n b q k b n r 0
p p p p p p p p 1
>P . . . . . . . 2
. . . . . . . . 3
. . . . . . . . 4
. . . . . . . . 5
. P P P P P P P 6
R N B Q K B N R 7
0 1 2 3 4 5 6 7 8You could use unicode chess pieces, this would allow you to free the alphabet and use the traditional Letter-Number chess coordinates.
This is the Wikipedia link. You can get them into python using chr()
for i in range(12):
chr(9812+i)output:
'♔'
'♕'
'♖'
'♗'
'♘'
'♙'
'♚'
'♛'
'♜'
'♝'
'♞'
'♟'Code Snippets
try:
print('give x and y')
x = int(input("x:"))
y = int(input("y:"))r n b q k b n r 0
p p p p p p p p 1
P . . . . . . . 2
. . . . . . . . 3
. . . . . . . . 4
. . . . . . . . 5
. P P P P P P P 6
R N B Q K B N R 7
0 1 2 3 4 5 6 7 8r n b q k b n r 0
p p p p p p p p 1
>P . . . . . . . 2
. . . . . . . . 3
. . . . . . . . 4
. . . . . . . . 5
. P P P P P P P 6
R N B Q K B N R 7
0 1 2 3 4 5 6 7 8for i in range(12):
chr(9812+i)'♔'
'♕'
'♖'
'♗'
'♘'
'♙'
'♚'
'♛'
'♜'
'♝'
'♞'
'♟'Context
StackExchange Code Review Q#104467, answer score: 6
Revisions (0)
No revisions yet.