国际象棋游戏中的典当运动

编程入门 行业动态 更新时间:2024-10-27 04:27:03
国际象棋游戏中的典当运动 - Java(Pawn Movement in Chess Game - Java)

我正在创建的国际象棋应用程序中的运动存在问题。 以下是检查移动是否有效的方法:

public boolean isMove(int row, int col, Pawn[][] board){ Pawn p = board[row][col]; int direction = 1; if (this.color=='w') { direction = -1; } if (p == null && this.col == col && ((this.row + direction) == row) || (this.row + 2 * direction) == row && ! this.hasMoved) { //can move return true; } else if (p != null && p.color != this.color && row == (this.row + direction) && (col == (this.col - 1) || col == (this.col + 1))) { // can capture return true; } return false; }

以下是我得到的一些输出:

此举不应该有效,但它允许移动到那个方块。 我想我上面发布的方法存在问题。

There is an issue with the movement in the chess app I am creating. Here is the method that checks if a move is valid:

public boolean isMove(int row, int col, Pawn[][] board){ Pawn p = board[row][col]; int direction = 1; if (this.color=='w') { direction = -1; } if (p == null && this.col == col && ((this.row + direction) == row) || (this.row + 2 * direction) == row && ! this.hasMoved) { //can move return true; } else if (p != null && p.color != this.color && row == (this.row + direction) && (col == (this.col - 1) || col == (this.col + 1))) { // can capture return true; } return false; }

Here are some outputs that I am getting:

This move should not be valid, but yet it allows to move to that square. I am thinking there is an issue with the method I posted above.

最满意答案

我想你的&&和|| 在优先级/顺序方面存在冲突。

也许:

if (p == null && this.col == col && (this.row + direction) == row || this.row + 2 * direction == row && ! this.hasMoved)

应该:

if (p == null && this.col == col && ((this.row + direction) == row || this.row + 2 * direction == row) && ! this.hasMoved)

我没有游戏所以我不能尝试但是......

I think your && and || are conflicting in priorities/order.

Maybe:

if (p == null && this.col == col && (this.row + direction) == row || this.row + 2 * direction == row && ! this.hasMoved)

Should be:

if (p == null && this.col == col && ((this.row + direction) == row || this.row + 2 * direction == row) && ! this.hasMoved)

I don't have a game so I cannot try it but...

更多推荐

本文发布于:2023-08-04 00:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:国际象棋   典当   游戏中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!