java - Java:根据棋子位置实现胜利条件?
问题描述
我是Java的初学者。我有一个班级项目,但我无法通过这部分。该游戏基本上是一个稍微修改过的跳棋游戏,我是按照教程做的,并按照我需要的方式进行了修改。它看起来像这样:修改过的跳棋
所有棋子都可以向左或向右移动一个棋子,但也允许白色棋子向后移动。如果白棋越过黑棋,白棋获胜,如果黑棋将白棋逼到不能移动的角,则黑棋获胜。
这是整个班级:
package com.gameproject.Model;
import com.gameproject.Controller.MoveResult;
import com.gameproject.Controller.Moves;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.util.Arrays;
import static com.gameproject.Model.PieceType.*;
public class Board {
public static final int WIDTH = 8;
public static final int HEIGHT = 8;
public static final int TILE_SIZE = 50;
private Tile[][] board = new Tile[WIDTH][HEIGHT];
private Group tileGroup = new Group();
private Group pieceGroup = new Group();
//Create the game board and the pieces
private Parent createContent(){
Pane root = new Pane();
root.setPrefSize(WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE);
root.getChildren().addAll(tileGroup, pieceGroup);
for (int y = 0 ; y < HEIGHT ; y++){
for (int x = 0; x < WIDTH ; x++){
Tile tile = new Tile((x + y) % 2 == 0, x, y);
board[x][y] = tile;
tileGroup.getChildren().add(tile);
Piece piece = null;
if (y == 0 && x == 2){
piece = makePiece(PieceType.WHITE, x, y);
}
if (y == 7 && x % 2 == 1){
piece = makePiece(PieceType.BLACK, x, y);
}
if (piece != null) {
tile.setPiece(piece);
pieceGroup.getChildren().add(piece);
}
}
}
return root;
}
//Normal move types, that every piece can do
private MoveResult tryMove(Piece piece, int newX, int newY) {
if (board[newX][newY].hasPiece() || (newX + newY) % 2 != 0) {
return new MoveResult(Moves.NONE);
}
int x0 = toBoard(piece.getOldmouseX());
int y0 = toBoard(piece.getOldmouseY());
if (Math.abs(newX - x0) == 1 && newY - y0 == piece.getType().moveDirection) {
return new MoveResult(Moves.NORMAL);
}
//Backward moves, only for the white piece
if (piece.getType() == WHITE) {
if ((newX - x0) == 1 && (newY - y0) == -1 || (newX - x0) == -1 && (newY - y0) == -1 ) {
return new MoveResult(Moves.BACKWARDS);
}
}
return new MoveResult(Moves.NONE);
}
private int toBoard(double pixel) {
return(int)(pixel + TILE_SIZE /2) / TILE_SIZE;
}
public void start(Stage mainStage) {
Scene scene = new Scene(createContent());
mainStage.setTitle("Fox Catcher");
mainStage.setScene(scene);
mainStage.show();
}
//Class which answers for the visual representations of the moves
private Piece makePiece(PieceType type, int x, int y){
Piece piece = new Piece(type, x, y);
//Move when mouse released
piece.setOnMouseReleased(e -> {
int newX = toBoard(piece.getLayoutX());
int newY = toBoard(piece.getLayoutY());
MoveResult result = tryMove(piece, newX, newY);
//Getting the piece place before movement happens
int x0 = toBoard(piece.getOldmouseX());
int y0 = toBoard(piece.getOldmouseY());
switch(result.getType()) {
//If no movement happened, abort the step
case NONE:
piece.abortMove();
break;
//If movement happened, clear the old tile(x0,y0) and set the moved piece to the new tile
case NORMAL:
case BACKWARDS:
piece.move(newX, newY);
board[x0][y0].setPiece(null);
board[newX][newY].setPiece(piece);
break;
}
});
return piece;
}
}
我试着做这样的白赢案例:
Tile[][] whitemovepos = new Tile[newX][newY];
Tile[][] blackmovepos = new Tile[newX][newY];
if (piece.getType() == WHITE) {
whitemovepos = new Tile[x][y];
}
if (piece.getType() == BLACK) {
blackmovepos = new Tile[newX][newY];
}
if (blackmovepos.length < whitemovepos.length){
System.out.println("The fox has win");
}
但它没有奏效(我在 if 语句中尝试了 x、newX、y、newY 和 Tile[][] 的所有组合)。它运行了,但打印的行似乎是随机的。一旦它在第一步打印,一旦它在第五步打印等等。我试图把它放在线下
MoveResult result = tryMove(piece, newX, newY);
以及在最后一种情况下,就在返回片之前;
你有什么建议,我该如何实现这两个胜利条件?提前致谢!我完全有可能从错误的方向接近它,而且根本没有意义,对此我深表歉意。
解决方案
推荐阅读
- terraform - Terrafrom 是否允许将新的 EC2 部署到已建立的云基础架构中
- jquery - jQuery Datatable 搜索在使用自动完成字段填充数据和依赖数据时不起作用
- r - R 中的 Cox:我如何解释我收到的奇怪的输出数据?
- git - 如何解释 Tortoise Git Revision Graph?
- c++ - 我是 C++ 的初学者,我想知道如何获得 2D 向量中的重复值
- google-cloud-functions - Google Cloud DataFlow:ModuleNotFoundError:没有名为“main”的模块
- javascript - 日期格式不正确
- class - 如何在飞镖中将类实例的创建限制为一个?
- javascript - 如何显示 React Ace Editor onClick 的 value 属性?
- c - 为什么hello world只打印两次,为什么a的最大值是2