我正在创建一个国际象棋游戏,我刚刚完成了基本的Ai,但每当一个棋子被拿走时它会消失并在几个回合之后回来。这件作品甚至不应该在呈现作品的列表中。 这是渲染/添加/设置新片段的类
setPieceLoc()
将旧片段设置在新位置,并从所有列表中删除original
和newLoc
。
有谁知道为什么会这样?没有其他类可以向列表添加新信息,所以据我所知,这里必定是错误。
package me.xthegamerplayz.FirstGame.board;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import me.xthegamerplayz.FirstGame.board.tiles.ChessImages;
import me.xthegamerplayz.FirstGame.rules.LegalMoves;
public class Board {
public static void addPiece(int x, int y, int piece) {
Point point = new Point();
point.setLocation(x, y);
if(piece > 16)
ChessBoard.black.put(point, piece);
else
ChessBoard.white.put(point, piece);
}
public static void setPieceLoc(Point original, Point newLoc) {
int piece = 8;
if(ChessBoard.white.containsKey(original))
piece = ChessBoard.white.get(original);
else if(ChessBoard.black.containsKey(original))
piece = ChessBoard.black.get(original);
//Remove piece at newLoc
if(ChessBoard.white.containsKey(newLoc))
ChessBoard.white.remove(newLoc);
else if(ChessBoard.black.containsKey(newLoc))
ChessBoard.black.remove(newLoc);
//Remove piece from previous location
if(ChessBoard.white.containsKey(original))
ChessBoard.white.remove(original);
else if(ChessBoard.black.containsKey(original))
ChessBoard.black.remove(original);
if(piece > 16)
ChessBoard.black.put(newLoc, piece);
else
ChessBoard.white.put(newLoc, piece);
}
public void registerPieces() {
//Pawns
for(int x = 0; x < 8; x++) {
addPiece(x, 1, 8);
addPiece(x, 6, 24);
}
//White
addPiece(0, 0, 9);
addPiece(1, 0, 10);
addPiece(2, 0, 11);
addPiece(5, 0, 11);
addPiece(6, 0, 10);
addPiece(7, 0, 9);
addPiece(4, 0, 12);
addPiece(3, 0, 13);
//Black
addPiece(0, 7, 25);
addPiece(1, 7, 26);
addPiece(2, 7, 27);
addPiece(5, 7, 27);
addPiece(6, 7, 26);
addPiece(7, 7, 25);
addPiece(3, 7, 28);
addPiece(4, 7, 29);
}
public void render(Graphics g) {
for(Point point : ChessBoard.white.keySet()) {
int piece = ChessBoard.white.get(new Point(point));
int x = (int) (point.getX()*64) + 20;
int y = (int) (point.getY()*64) + 20;
boolean showTiles = false;
if(showTiles) {
if(piece == 24) {
LegalMoves lm = new LegalMoves(point);
for(Point p : lm.moves()) {
int bx = (int) (p.getX()*64) + 20;
int by = (int) (p.getY()*64) + 20;
g.setColor(Color.green);
g.fillRect(bx, by, 64, 64);
}
}
}
g.drawImage(ChessImages.getPieceImage(piece), x, y, null);
}
for(Point point : ChessBoard.black.keySet()) {
int piece = ChessBoard.black.get(new Point(point));
int x = (int) (point.getX()*64) + 20;
int y = (int) (point.getY()*64) + 20;
boolean showTiles = false;
if(showTiles) {
if(piece == 24) {
LegalMoves lm = new LegalMoves(point);
for(Point p : lm.moves()) {
int bx = (int) (p.getX()*64) + 20;
int by = (int) (p.getY()*64) + 20;
g.setColor(Color.green);
g.fillRect(bx, by, 64, 64);
}
}
}
g.drawImage(ChessImages.getPieceImage(piece), x, y, null);
}
}
}
答案 0 :(得分:0)
看起来你正试图从一个空的地方移动一块。
请注意,当您的原件既不是黑色也不是白色地图时,它将使用第8块,它看起来是白色的棋子。这恰好是您视频中重新出现的内容。
当发生这种情况时你真的应该抛出异常,因为从空方块移动一块是没有意义的。这样做可以帮助您调试发生这种情况的情况:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
<div ng-controller="DefaultController as ctrl">
{{ctrl.row.categoryId}}
<select class="select"
convert-to-number
ng-options="option.id as option.name for option in ctrl.abc"
ng-model="ctrl.row.categoryId">
</select>
</div>
</div>