【问题标题】:Splitting one method into multiple methods - Java Chess将一种方法拆分为多种方法 - Java Chess
【发布时间】:2013-11-14 19:39:59
【问题描述】:

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

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

在代码中,我匹配不同的片段并打印出它们的不同动作。我想要做的是将我的 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");
            }
    }


}

}

【问题讨论】:

  • 将其他方法设为静态有什么问题?
  • 不工作怎么办?此外,如果您想从静态方法调用它们,这些方法必须是静态的。
  • 当我将 moveBetter() 拆分为不同的静态方法时,它会迫使我将所有其他变量设为静态,这会破坏代码。但如果一切都在 moveBetter() 下,一切正常。
  • 你必须将共享变量移到 moveBetter 方法之外,否则你不能将它们设为静态
  • 我愿意,但是当我使用 findPiece() 之类的方法使用这些共享变量时,我在 findPiece() 方法上遇到错误,该方法表示将该方法转换为静态方法。一旦我这样做了,这个方法中的所有变量突然要求我将它们的所有修饰符更改为静态。

标签: java methods chess


【解决方案1】:

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

【讨论】:

  • 他/她似乎是初学者,我怀疑他/她已经准备好理解你所说的任何内容
  • 知道了。谢谢,这是我完全错过的简单修复。是时候重新研究我忘记的所有 java了。
【解决方案2】:

你可以这样做:

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

private static void findPiece()
{
    //Code
}

private static void findColor()
{
    //Code
}

方法可见性可以是私有的或公开的,具体取决于您希望如何使用它,我希望这些方法是私有的。

findPiece():

while(m1.find())
{
    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";
    }
}

其他方法几乎相同,循环和复制粘贴您要执行的部分。 虽然如果每个方法中的代码在另一个方法中都需要,那么拆分它就不会简单

【讨论】:

  • 是的,我试过了,还是不行。我不明白我做错了什么,代码要我为所有内容添加静态修饰符。我觉得这是一个非常简单的修复,但我错过了一些东西
  • 好吧,你不是。您不能在静态方法中引用非静态方法或变量。在同一个类中创建但在主方法之外的静态主方法中使用的每个方法都必须是静态的。如果不想使用static,则需要创建一个包含以上所有方法的不同类,并在您的main方法中创建该类的对象并正常使用这些方法
【解决方案3】:

考虑这个基本示例:

public static void moveBetter() throws IOException
{
    String piece = "pluto";
    piece = "goofy";
}

为了将其拆分为两种方法,您必须:

1.提取变量并将其放在moveBetter之外

string piece;

public static void moveBetter() throws IOException
{
    piece = "pluto";
    piece = "goofy";
}

2. 将变量定义为静态,因为静态方法(如 moveBetter)无法访问非静态变量或非静态方法

static string piece;

3.创建一个新方法并移动其中的代码。记住新方法也必须是静态的才能被 moveBetter 访问

string static piece;

public static void moveBetter() throws IOException
{
    piece = "pluto";
    assignGoofy();
}

public static void assignGoofy()
{
    piece = "goofy";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2011-12-10
    • 2013-02-10
    相关资源
    最近更新 更多