Question

I'm currently trying to build a simple chess piece movement program. I read in the chess movement notation from a text file. The chess notation goes something like this:

plb1 c3 = (p)pawn(l)white moves from position b1(b1) to position c3(c3). my regex then parses the move.

In the code I match various pieces and print out their various movements. What I want to do is break my moveBetter() method into mutliple smaller methods such as a findPiece() or findColor() methods.

The problem is, everytime I try to break up the methods, they don't work or i'm told i have to make the method static and that breaks it further. How do I split moveBetter() into multiple methods?

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");
            }
    }


}

}

Était-ce utile?

La solution

Don't call movePiece from your static main method. define a ChessEngine class or something, instantiate one in your main method, and then have that ChessEngine object handle all of the logic. Then you can split up your method as suggested in the other answers/comments.

Autres conseils

You can do this:

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

private static void findPiece()
{
    //Code
}

private static void findColor()
{
    //Code
}

Method visibility can be private or public depending on how you want to use it, I would prefer private for these methods.

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";
    }
}

Pretty much the same for the other methods, loop and copy paste the part you want to execute. Though if the code in each method is needed in another method, then splitting it will not work simply

Consider this basic example:

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

in order to split this in two method you have to:

1.extract the variable and put it outside moveBetter

string piece;

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

2.define the variable as static since a static method (like moveBetter) can't access a non static variable or a non static method

static string piece;

3.create a the new method and move the code inside it. remember the new method must be static too in order to be accessed by moveBetter

string static piece;

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

public static void assignGoofy()
{
    piece = "goofy";
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top