I’ve spent a long time trying to figure out this issue, I need a fresh pair of eyes and someone who knows python a little better.
I started implementing a chess game to help me learn python, and I’ve basically only implemented moving pawns and displaying the board. Here’s the issue:
-when I move any of the top players’ pawns either 1 or 2 spaces forward, everything is fine.
-when I move any of the bottom players’ pawns 2 spaces forward, everything is fine.
-when I move any of the bottom players’ pawns 1 space forward, everything goes awry. On the display, the whole row of the new position turns into pawns, but after printing results, all of the open spots now “belong” to the bottom player (they should belong to no one, 0).
I’ve tried testing different columns with the same results.
Here’s an illustration of the issue:
SETUP
1 [R][k][B][K][Q][B][k][R]
2 [P][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [P][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
a b c d e f g h
OK
Enter a move [r0,c0,r1,s1]: [2,a,3,a]
1 [R][k][B][K][Q][B][k][R]
2 [ ][P][P][P][P][P][P][P]
3 [P][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [P][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
a b c d e f g h
OK
Enter a move [r0,c0,r1,s1]: [2,a,4,a]
1 [R][k][B][K][Q][B][k][R]
2 [ ][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [P][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [P][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
a b c d e f g h
OK
Enter a move [r0,c0,r1,s1]: [7,a,5,a]
1 [R][k][B][K][Q][B][k][R]
2 [P][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [P][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [ ][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
a b c d e f g h
??NOT OK?? – (after moving, all ‘blank’ spaces belong to bottom player, and all of row 6 (as shown below) have the type variable set to pawn)
Enter a move [r0,c0,r1,s1]: [7,a,6,a]
1 [R][k][B][K][Q][B][k][R]
2 [P][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [P][P][P][P][P][P][P][P]
7 [ ][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
a b c d e f g h
And the code:
#!/usr/bin/python
import sys
# This is easy to solve with a simple tiny wrapper:
class Callable:
def __init__(self, anycallable):
self.__call__ = anycallable
class Piece:
Arr_Slot = 0
Row = 0
Col = 0
Sym = "[ ]"
Id = ""
Player = 0
def __init__(self, row, col, player):
self.Row = row
self.Col = col
self.Player = player
self.Arr_Slot = self.Arr_Slot + 1
class Blank(Piece):
Sym = "[ ]"
Id = "Blank"
Row = 0
Col = 0
def __init__(self,row,col):
self.Row = row
self.Col = col
class King(Piece):
Sym = "[K]"
Id = "King"
class Queen(Piece):
Sym = "[Q]"
Id = "Queen"
class Rook(Piece):
Sym = "[R]"
Id = "Queen"
class Bishop(Piece):
Sym = "[B]"
Id = "Bishop"
class Knight(Piece):
Sym = "[k]"
Id = "Knight"
class Pawn(Piece):
Sym = "[P]"
Id = "Pawn"
def TryMove(board,row0,col0,row1,col1):
type = board.Board[row0][col0].Id
player = board.Board[row0][col0].Player
if player == 1:
if (row1 == (row0+1) or ((row1 == (row0+2)) and row0 == 1)) and (col0 == col1):
if board.Board[row1][col1].Player != player:
board.Board[row1][col1].Sym = board.Board[row0][col0].Sym
board.Board[row1][col1].Id = board.Board[row0][col0].Id
board.Board[row1][col1].Player = board.Board[row0][col0].Player
board.MakeBlank(row0,col0)
return 1
else:
print "Error: Spot taken by same player"
return 0
else:
print "Error: Illegal move"
return 0
elif player == 2:
if (row1 == (row0-1) or ((row1 == (row0-2)) and row0 == 6)) and (col0 == col1):
if board.Board[row1][col1].Player != player:
board.Board[row1][col1].Sym = board.Board[row0][col0].Sym
board.Board[row1][col1].Id = board.Board[row0][col0].Id
board.Board[row1][col1].Player = board.Board[row0][col0].Player
board.MakeBlank(row0,col0)
return 1
else:
print "Error: Spot taken by same player"
return 0
else:
print "Error: Illegal move"
return 0
else:
print "Error: Don't own that spot"
TryMove = Callable(TryMove)
class Board:
"""The game board"""
Board_Dims = 8
Arr_Slot = 0
Board = []
def __init__(self):
self.Board = [[Piece for i in range(self.Board_Dims)] for j in range(self.Board_Dims)]
def AddPiece(self, Piece):
self.Board[Piece.Row][Piece.Col] = Piece
self.Board[Piece.Row][Piece.Col].Arr_Slot = self.Arr_Slot
self.Arr_Slot = self.Arr_Slot + 1
def MakeBlank(self,row,col):
self.Board[row][col].Sym = "[ ]"
self.Board[row][col].Id = "Blank"
self.Board[row][col].Player = 0
def PrintBoard(self):
sys.stdout.write("\n")
for i in range(self.Board_Dims):
sys.stdout.write(str(i+1) + " ")
for j in range(self.Board_Dims):
sys.stdout.write(self.Board[i][j].Sym)
sys.stdout.write("\n")
sys.stdout.write(" a b c d e f g h\n")
def TryMove(self, row0, col0, row1, col1):
type = self.Board[row0][col0].Id
player = self.Board[row0][col0].Player
if type == "Blank":
print "Error: No piece there"
return 0
if type == "Pawn":
return Pawn.TryMove(self,row0,col0,row1,col1)
GameBoard = Board()
GameBoard.AddPiece(King(0,3,1))
GameBoard.AddPiece(Queen(0,4,1))
GameBoard.AddPiece(Rook(0,0,1))
GameBoard.AddPiece(Rook(0,7,1))
GameBoard.AddPiece(Knight(0,1,1))
GameBoard.AddPiece(Knight(0,6,1))
GameBoard.AddPiece(Bishop(0,2,1))
GameBoard.AddPiece(Bishop(0,5,1))
for i in range(GameBoard.Board_Dims):
GameBoard.AddPiece(Pawn(1,i,1))
GameBoard.AddPiece(King(7,3,2))
GameBoard.AddPiece(Queen(7,4,2))
GameBoard.AddPiece(Rook(7,0,2))
GameBoard.AddPiece(Rook(7,7,2))
GameBoard.AddPiece(Knight(7,1,2))
GameBoard.AddPiece(Knight(7,6,2))
GameBoard.AddPiece(Bishop(7,2,2))
GameBoard.AddPiece(Bishop(7,5,2))
for j in range(GameBoard.Board_Dims):
GameBoard.AddPiece(Pawn(6,j,2))
for i in range(2,5):
for j in range(GameBoard.Board_Dims):
GameBoard.AddPiece(Blank(i,j))
GameBoard.PrintBoard()
result = 1
while 1:
inp = raw_input("Enter a move [r0,c0,r1,s1]: ")
r1 = int(inp[1])-1
c1 = int(ord(inp[3]))-97
r2 = int(inp[5])-1
c2 = int(ord(inp[7]))-97
result = GameBoard.TryMove(r1,c1,r2,c2)
if result == 0:
print "ERROR: TRY AGAIN"
print GameBoard.Board[3][3].Player
print GameBoard.Board[3][3].Sym
GameBoard.PrintBoard()
Commenting these lines out:
board.Board[row1][col1].Sym = board.Board[row0][col0].Sym
board.Board[row1][col1].Id = board.Board[row0][col0].Id
board.Board[row1][col1].Player = board.Board[row0][col0].Player
Makes it basically do nothing, so I guess you could say this is where the problem is, but it works for the opposite player, and it’s the same code, so that’s why I’m uncertain as to whether the problem is actually in these lines.
Any help is much appreciated.
I would take a completely different approach, instead of using a board to store positions and blank pieces, I would store the position information in the 32
Pieces generated at setup and only useBoardclass to store the list of pieces on it and to doBoard.draw()e.g. something along the lines: