获取字符,我的国际象棋游戏不断出现无效的移动

我真的很抱歉让我烦恼,但我对此感到有些恐慌和压力。请不要仇恨,因为这篇文章可以被视为“请帮助调试”的帖子,但我很绝望。我不明白我做错了什么,我花了很长时间试图弄清楚。它运行但只是不断给出非法移动消息。我真的很感激对此的任何见解。


游戏...


public class Game {



    private static String WHITEPLAYS_MSG = "White plays. Enter move:";

    private static String BLACKPLAYS_MSG = "Black plays. Enter move:";

    private static String ILLEGALMOVE_MSG = "Illegal move!";

    private static String WHITEWINS_MSG = "White wins!";

    private static String BLACKWINS_MSG = "Black wins!";


    private Board gameBoard;



    // Minimal constructor. Expand as needed (kt54)

    public Game() {

        gameBoard = new Board();

    }


    // Build on this method to implement game logic.

    public void play() {


        Player player1 = new Player("white");           //player one plays white

        Player player2 = new Player("black");           //player two plays black

        Piece piece1 = new Piece();

        Piece piece2 = new Piece();


        EasyIn2 reader = new EasyIn2();


        gameBoard = new Board();     //initializes the board so dont need to do so in main


        boolean done = false;      //2 while loops within large while loop?


        while (!done) {                     //keeps looping when no one has won yet

            gameBoard.printBoard();


            System.out.println(WHITEPLAYS_MSG);



            String Player1Pos1 = reader.getString();         //gets user input ... move from... to....   temporary variables

            int x1From = Player1Pos1.charAt(0) - 'a';                           //to transform the letter      ASCII values

            int y1From = 8 - (Player1Pos1.charAt(1) - '1') - 1;                           // to transform the number


            String Player1Pos2 = reader.getString();

            int x1To = Player1Pos2.charAt(0) - 'a';                           //to transform the letter

            int y1To = 8 - (Player1Pos2.charAt(1) - '1') - 1;                           // to transform the number



吃鸡游戏
浏览 130回答 2
2回答

胡子哥哥

我在代码中看到了一些问题,但这将有助于解决您的错误。您在帖子中询问了如何调试,我将尝试向您展示如何重新排列代码:这个剪断是完全一样的:&nbsp;String Player1Pos1 = reader.getString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//gets user input ... move from... to....&nbsp; &nbsp;temporary variables&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x1From = Player1Pos1.charAt(0) - 'a';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//to transform the letter&nbsp; &nbsp; &nbsp; ASCII values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int y1From = 8 - (Player1Pos1.charAt(1) - '1') - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// to transform the number为什么不使用单独的函数:Pair<Integer, Integer> getPlayerPosition(String x, String y)在函数 legalPieceMove(...) 你有很多重复的代码。我认为您不需要白色和黑色之间的区别。那么这个功能真的很短。使用 switch 语句或“if”“else if”“else”语句,代码更具可读性。在 Board 类中,几乎每个函数都需要 board 变量。为什么不使用实例变量。然后你的函数参数列表会短一点。我无法修改您的代码,因为我没有完整的代码。

小唯快跑啊

我找到了这个:for (...) {&nbsp; &nbsp; if (getPieceColour(gameBoard, xFrom - i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; }}如果 getPieceColour(...) == getPieceColour(...),则必须离开 for 循环:for (...) {&nbsp; &nbsp; if (getPieceColour(gameBoard, xFrom - i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java