我花了很长时间试图弄清楚这个问题,我需要一双新的眼睛和一个更了解python的人。
我开始实现一个国际象棋游戏来帮助我学习python,而我基本上只实现了移动棋子和显示棋盘。这是问题:
- 当我将任何顶级玩家的棋子向前移动 1 或 2 个空格时,一切都很好。
- 当我将任何底部玩家的棋子向前移动 2 个空格时,一切都很好。
- 当我将任何底部玩家的棋子向前移动 1 个空间时,一切都会出错。在显示屏上,新位置的整行变成了棋子,但在打印结果后,所有空位现在都“属于”底部玩家(它们应该不属于任何人,0)。
我试过用相同的结果测试不同的列。
这是该问题的说明:
设置
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
好的
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
好的
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
好的
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
??不好??- (移动后,所有'blank'空格都属于底部玩家,并且第6行(如下图)的所有类型变量都设置为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
和代码:
#!/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()
注释掉这些行:
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
让它基本上什么都不做,所以我想你可以说这是问题所在,但它适用于相反的玩家,而且它是相同的代码,所以我不确定问题是否真的在这些行中.
任何帮助深表感谢。