我花了很长时间试图找出这个问题,我需要一双新鲜的眼睛和一个对python有所了解的人。
我开始实施一个国际象棋游戏,以帮助我学习python,我基本上只实现移动的棋子和显示板。这是问题所在:
- 当我将任何顶级球员的棋子向前移动1或2个空格时,一切都很好。
- 当我将任何一名底层球员的棋子向前移动时,一切都很好。
- 当我将任何一名底层球员的棋子向前移动时,一切都会出错。在显示器上,新位置的整行变成了棋子,但是在打印结果之后,所有空位现在都“属于”底部玩家(他们应该属于没有人,0)。
我尝试使用相同的结果测试不同的列。
以下是该问题的说明:
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
行
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
??不行吗? - (移动后,所有'空白'空格属于底部玩家,所有第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
使它基本上什么都不做,所以我想你可以说这就是问题所在,但它适用于相反的玩家,而且它是相同的代码,所以这就是为什么我不确定问题是否真的存在在这些方面。
非常感谢任何帮助。
答案 0 :(得分:0)
我采取完全不同的方法,而不是使用板来存储位置和空白件,我会将位置信息存储在32 {{1}在安装时生成并仅使用Piece
类来存储片段列表并执行Board
e.g。一些事情:
Board.draw()