我开始制作一个跳棋游戏,我已经获得了所有的图形和绘制的棋盘.在我开始创作作品之前,我想知道什么是一种简单的方法来解决碎片运动的逻辑方面.我应该制作一个每个正方形的表格,检测它是否有一块,如果有的话,是什么颜色的?(即0 =空,1 =红色,2 =黑色)或者你们对这个问题有更好的想法吗?
通过使用OOP原则,我会使用以下内容:
enum Side {
BLACK,
RED;
}
class Position {
int x, int y;
}
class Piece
{
Position position; // position inside the board
Side side; // which side the piece is
}
class Board
{
Piece[][] board = new Piece[8][8];
boolean isMoveLegal(Piece p, Position newPosition) {
...
}
void doMove(Piece p, Position newPosition) {
if (isMoveLegal(p, newPosition) {
// game logic of movement and eating other pieces if needed
}
}
}
Run Code Online (Sandbox Code Playgroud)
更天真的方法可以使用简单的地图:
class Position {
int x, int y;
}
class Piece
{
Side side; // which side the piece is
}
class Board
{
HashMap<Piece, Position> board;
boolean isMoveLegal(Piece p, Position newPosition) {
...
}
void doMove(Piece p, Position newPosition) {
if (isMoveLegal(p, newPosition) {
// game logic of movement and eating other pieces if needed
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可以用于避免将片的当前位置存储在其自身内.