gpt4 book ai didi

java - 将一种方法拆分为多种方法 - Java Chess

转载 作者:行者123 更新时间:2023-12-01 13:50:40 25 4
gpt4 key购买 nike

我目前正在尝试构建一个简单的棋子移动程序。我从文本文件中读取了国际象棋移动符号。国际象棋符号是这样的:

plb1 c3 = (p)pawn(l)white 从位置 b1(b1) 移动到位置 c3(c3)。然后我的正则表达式解析移动。

在代码中,我匹配不同的棋子并打印出它们的不同 Action 。我想要做的是将我的 moveBetter() 方法分解为多个较小的方法,例如 findPiece() 或 findColor() 方法。

问题是,每次我尝试分解这些方法时,它们都不起作用,或者我被告知必须使该方法成为静态的,这会进一步破坏它。 如何将 moveBetter() 拆分为多个方法?

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SuperiorFileIO {

public static void main(String[] args) throws IOException {
moveBetter();
}

public static void moveBetter() throws IOException
{
String piece = " "; //group1
String color = " "; //group2
String position = " "; //group3
String position2 = " "; //group4
String capture = " "; //group5
//or
String kingPos1 = "";//6
String kingPos2 = "";//7
String rookPos1 = "";//8
String rookPos2 = "";//9

ReadFile readFile = new ReadFile();
String input = readFile.readTextFile("TestMoves");


Pattern matchingMoves = Pattern.compile("(q|k|b|p|n|r+)(l|d)(\\w\\d) ?(\\w\\d)?([*])?|(\\w\\d) (\\w\\d) (\\w\\d) (\\w\\d)");
Matcher m1 = matchingMoves.matcher(input);

while (m1.find())
{
//find piece
if(m1.group(6) != null && m1.group(6).matches("(\\w\\d)")) //castling part 1
kingPos1 = m1.group(6);
else if (m1.group(1).equals("q"))
piece = "Queen";
else if (m1.group(1).equals("k"))
piece = "King";
else if (m1.group(1).equals("b"))
piece = "Bishop";
else if (m1.group(1).equals("p"))
piece = "Pawn";
else if (m1.group(1).equals("n"))
piece = "Knight";
else if (m1.group(1).matches("(q|k|b|p|n|r+)") && m1.group(1).equals("r"))
piece = "Rook";
else {
System.out.println("PIECE FAILED");
piece = "piece Failed";
}
//find color
if(m1.group(7) != null && m1.group(7).matches("(\\w\\d)")) //castling part 2
kingPos2 = m1.group(7);
else if(m1.group(2).equals("d"))
color = "Black";
else if(m1.group(2).matches("(l|d)") && m1.group(2).equals("l"))
color = "White";
else {
System.out.println("COLOR FAILED");
piece = "color Failed";
}
//set starting position
if (m1.group(7) != null && m1.group(7).matches("(\\w\\d)") )
rookPos1 = m1.group(7); //castling part 3
else
position = m1.group(3);
//Castling
if (m1.group(8) != null && m1.group(9) != null && m1.group(8).matches("(\\w\\d)") && m1.group(9).matches("(\\w\\d)"))
{
rookPos1 = m1.group(8);
rookPos2 = m1.group(9);
System.out.println("The king moves from " + kingPos1 + " to " + kingPos2 + " while the rook moves from " + rookPos1 + " to " + rookPos2);
}
//if the player does not move // finds a piece
else if (m1.group(4) == null)
System.out.println("The " + color + " " + piece + " resides at " + position);

//set ending position
else if (m1.group(4) != null && m1.group(5) == null)
{
position2 = m1.group(4);
System.out.println("The " + color + " " + piece + " moves from " + position + " to " + position2);
}
//set ending position and capture
else if (m1.group(4) != null && m1.group(5) != null)
{
//System.out.println("capture occured");
position2 = m1.group(4);
capture = m1.group(5); //unused
System.out.println("The " + color + " " + piece + " moves from " + position + " to " + position2 + " and captures the piece on " + position2);
}

else {
System.out.println("PARSE ERROR");
}
}


}

}

最佳答案

不要从静态 main 方法中调用 movePiece。定义一个 ChessEngine 类或其他东西,在 main 方法中实例化一个,然后让该 ChessEngine 对象处理所有逻辑。然后您可以按照其他答案/评论中的建议拆分您的方法。

关于java - 将一种方法拆分为多种方法 - Java Chess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19986785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com