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 in between. The endgame must be cross-colored.
The rules of the game are that if there is a place near the point, it should be able to move there, also if there is an opposite color next to it, it should be able to jump over if there is space behind the opposite color.
My question is under 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 :
Now he is making up the logic of the game. If the points could not move backwards, I would have completed this task. But since they can move backwards, it gives me an error when the code checks outside the array (understandable though).
The solution on my head is to make the array longer with negligible signs to avoid getting an error. But I ask, is there any other way? Because like now, I CANNOT MOVE my FIRST and LAST points , the averages are working as they should!
a source to share
The problem occurs when digits
is either 0 or the maximum index for the array. You cannot check mellan[digits-1]
when digits
is 0, and you cannot check mellan[digits+1]
when digits
is the maximum index for an array.
So, you need to check these situations before trying to access the array. You can try something like this:
checkempty2 = (digits > 0) && (mellan[digits-1] == tom);
Since Java uses short-circuit evaluation for boolean operations such as this, the second part of this expression will only evaluate if the first part evaluates to true
. Therefore, it mellan[digits-1]
will be available only if it is digits
greater than 0.
Obviously you also need to deal with position 2 space on the left or right, but the same principle can apply there too.
a source to share
You might consider one of the following:
1) Place the two sides with two green (or red) dots. They will work as barriers.
2) Add conditions when you check next / previous position of array.
I would take the second approach ...
I also suggest refactoring the code a bit. You can extract the code that checks the jump (right or left) into a separate method that takes the direction (+ 1 / -1, better as an enumeration) as a parameter.
a source to share