import java.io.*;
public class CheckersGame {
	public static void main (String []args) throws IOException {
		BufferedReader jill = new BufferedReader (new InputStreamReader (System.in));
		System.out.print("Enter name of the first to move: ");
		String name = jill.readLine();
		System.out.print("Enter name of " + name + "'s opponent: ");
		String opo = jill.readLine();
		System.out.println("You'll handle the * stones " + name+".");
		System.out.println("You'll handle the o stones " + opo);
		String[][] board = {{"  ","0 ","1 ","2 ","3 ","4 ","5 ","6 ","7 ","  "},
							{"0 ","  ","o ","  ","o ","  ","o","  "," o ","0"},
							{"1 ","o ","  ","o ","  ","o ","  ","o ","  ","1"},
							{"2 ","  ","o ","  ","o ","  ","o ","  ","o ","2"},
							{"3 ","  ","  ","  ","  ","  ","  ","  ","  ","3"},
							{"4 ","  ","  ","  ","  ","  ","  ","  ","  ","4"},
							{"5 ","* ","  ","* ","  ","* ","  ","* ","  ","5"},
							{"6 ","  ","* ","  ","* ","  ","* ","  ","* ","6"},
							{"7 ","* ","  ","* ","  ","* ","  ","* ","  ","7"},
							{"  ","0 ","1 ","2 ","3 ","4 ","5 ","6 ","7 ","  "}};
		 for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				System.out.print(board[x][y]);
			}
			System.out.println();
		}
		boolean team = true;
		System.out.println("\nInstructions: input the ordinates to move first, then, the ordinates where you will move separated with space.\n(e.g 34 45)");
		for(;;){
			if(team){
				System.out.print(name+"'s move: ");
				String move = jill.readLine();
				int a = Integer.parseInt(""+move.charAt(0))+1;
				int b = Integer.parseInt(""+move.charAt(1))+1;
				int c = Integer.parseInt(""+move.charAt(3))+1;
				int d = Integer.parseInt(""+move.charAt(4))+1;
				if(board[a][b].equals("* ")&&board[c][d].equals("  ")&&((a-c==1)||a-c==-1)&&(b-d==1||b-d==-1)){
					board[a][b] = "  ";
					board[c][d] = "* ";
				}
				else if(board[a+1][b+1].equals("o ")&&board[a][b].equals("* ")&&board[c][d].equals("  ")){
					board[a+1][b+1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "* ";
				}
				else if(board[a+1][b-1].equals("o ")&&board[a][b].equals("* ")&&board[c][d].equals("  ")){
					board[a+1][b-1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "* ";
				}
				else if(board[a-1][b+1].equals("o ")&&board[a][b].equals("* ")&&board[c][d].equals("  ")){
					board[a-1][b+1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "* ";
				}
				else if(board[a-1][b-1].equals("o ")&&board[a][b].equals("* ")&&board[c][d].equals("  ")){
					board[a-1][b-1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "* ";
				}
				else{
					System.out.println("Invalid input!");
				}
				for(int x = 0; x< 10; x++){
					for(int y = 0; y < 10; y++){
						System.out.print(board[x][y]);
					}
					System.out.println();
				}
				team = false;
				}
			else if(team == false){
				System.out.print(opo+"'s move: ");
				String move = jill.readLine();
				int a = Integer.parseInt(""+move.charAt(0))+1;
				int b = Integer.parseInt(""+move.charAt(1))+1;
				int c = Integer.parseInt(""+move.charAt(3))+1;
				int d = Integer.parseInt(""+move.charAt(4))+1;
				if(board[a][b].equals("o ")&&board[c][d].equals("  ")&&((a-c==1)||a-c==-1)&&(b-d==1||b-d==-1)){
					board[a][b] = "  ";
					board[c][d] = "o ";
				}
				else if(board[a+1][b+1].equals("* ")&&board[a][b].equals("o ")&&board[c][d].equals("  ")){
					board[a+1][b+1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "o ";
				}
				else if(board[a+1][b-1].equals("* ")&&board[a][b].equals("o ")&&board[c][d].equals("  ")){
					board[a+1][b-1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "o ";
				}
				else if(board[a-1][b+1].equals("* ")&&board[a][b].equals("o ")&&board[c][d].equals("  ")){
					board[a-1][b+1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "o ";
				}
				else if(board[a-1][b-1].equals("* ")&&board[a][b].equals("o ")&&board[c][d].equals("  ")){
					board[a-1][b-1] = "  ";
					board[a][b] = "  ";
					board[c][d] = "o ";
				}
				else{
					System.out.println("Invalid input!");
				}
				for(int x = 0; x< 10; x++){
					for(int y = 0; y < 10; y++){
						System.out.print(board[x][y]);
					}
					System.out.println();
				}
				team = true;
			}
		}
	}
}

Help me improve this code to become better one. You can email me at my email address<SNIPPED>.
This gonna be my project for my subject Basic Programming.
Thank you!

Recommended Answers

All 5 Replies

thanks! and i am sorry for posting my email. i was not aware that is not allowed.

"o "

You should use a final String for defining the contents of a square.
final String OMan = "o ";

else if(board[a+1][b+1].equals("o ")&&board[a][b].equals("* ")&&board[c][d].equals("  ")){

vs

else if(board[a+1][b+1].equals(OMan)&&board[a][b].equals(SMan)&&board[c][d].equals(MTSqr)){

Using so many hardcoded literals is a bad idea. If you mistype one the compiler will not tell you it is incorrect. If you use a variable the compiler will tell you if it doesn't know that variable.

Notice that the blocks of code at lines 30-71 and 72-113 are nearly identical. The only difference is that "*" and "o" are swapped. Create a method doMove(String title, String self, String other) , and use it as

for(;;) {
        if(team) {
            doMove(name, "* ", "o ");
            team = false;
        } else if(team == false) {
            doMove(opo, "o ", "* ");
            team = true;
        }
    }

Not only your code becomes twice as short (which means twice less maintenance), you may immediately realize that the team variable is redundant. Really, the game loop becomes

for(;;) {
        doMove(name, "* ", "o ");
        doMove(opo, "o ", "* ");
    }

BTW, you need a way to detect an end of game situation and break the loop.

Now, let's get into the doMove itself. Notice that you test board[a][b] in each clause. Again, a redundancy must be eliminated:

if(board[a][b].equals(self) {
        ... Keep processing the move ...
    } else {
        System.prinln("invalid input");

}

And last but not least, you allow horrible violations of rules.

Can i make a request? If it's okay, can you give me the code. :)
But if not okay. It's still fine. But anyway. Thanks for the help. I got a little idea on how to improve it.

And last but not least, you allow horrible violations of rules.

Thanks for reminding me. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.