简体   繁体   中英

Java Array Game - Game Logic Concept

I want to create a game that starts with 3 green dots on the left and 3 red dots on the right with a space between them. The endgame should have the colors switched sides.

The rules of the game are that, if there is a space next to a dot it should be able to move there, also if an opposite color is next to it - it should be able to jump over iff there is a space behind the opposite color.

My question is located below the code.

Here is the code:

//import mind.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.awt.*;

public class Drive {
String[] middle = new String[7];

public Drive() {
    int player1, player2;
    boolean gameEnd, checkempty, checkempty2, enemy, enemy2;
    // Gamepieces
    String gr,rd,tom;
    gr="G"; rd="R"; tom=" ";
    // beginning of the game, endgame and the updating array
    String[] begin = {gr,gr,gr,tom,rd,rd,rd};
    String[] mellan = new String[7];
    String[] end = {rd,rd,rd,tom,gr,gr,gr};
    Scanner in = new Scanner(System.in);
    while (mellan!=end) {
        mellan=begin;
        for(int i=0; i<mellan.length; i++) {
            System.out.print(mellan[i]);
        }
        System.out.print("        Choose 0-6: ");
        int digits = in.nextInt();

        //BOOLEAN for game rules!!!
        checkempty = mellan[digits+1]==tom;
        checkempty2 = mellan[digits-1]==tom;
        enemy = (mellan[digits]==gr && mellan[digits+1]==rd && mellan[digits+2]==tom);
        enemy2 = (mellan[digits]==rd && mellan[digits-1]==gr && mellan[digits-2]==tom);

        if(checkempty) {
            mellan[digits+1]=mellan[digits];
            mellan[digits]=tom;
        } else if (checkempty2) {
            mellan[digits-1]=mellan[digits];
            mellan[digits]=tom;
        } else if (enemy) {
            mellan[digits+2]=mellan[digits];
            mellan[digits]=tom;
        } else if (enemy2) {
            mellan[digits-2]=mellan[digits];
            mellan[digits]=tom;
        }
    }
    System.out.print("Nicely Done");
}

// Test Drive!
public static void main(String args[]) {
    new Drive();
}
}

Problem :
Right now, it's making up the game logic. If the dots weren't able to move backwards I would have done the task. But since they are able to move backwards, it gives me the error when the code checks outside the array (understandable though).

The solution on top of my head is to make the array longer with insignificant signs as to not get the error. But I'm asking if there is another way? Because, the way it is now, I CAN'T MOVE my FIRST and LAST dots the middle numbers work as should be though!

The issue occurs when digits is either 0 or the maximum index for the array. You can't check mellan[digits-1] when digits is 0, and you can't check mellan[digits+1] when digits is the maximum index for the array.

So, you need to check for these situations before you try to access the array. You could try something like this:

checkempty2 = (digits > 0) && (mellan[digits-1] == tom);

Because Java uses short-circuit evaluation for boolean operations like this, the second part of this expression will only be evaluated if the first part evaluates to true . Therefore, mellan[digits-1] will only be accessed if digits is greater than 0.

Obviously, you also need to deal with the position 2 spaces to the left or right, but the same principle could apply there too.

You may consider one of the following:

1) Pad the two sides with 2 green (or red) dots. They will work as barriers.

2) Add conditions whenever you check the next/previous position of the array.

I would choose the second approach...

I also suggest refactoring the code a little. You can extract the code that validates a jump (either right or left) into a separate method, that receives a direction(+1/-1, better as an enum) as parameter.

Simply check if digits + n is greater than or equal to 0 (and smaller than mellan.length ). eg:

if(digits-2 >= 0)
{
    //put core here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM