What I want to do: 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 sid*e. The rules of the game* is 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 unsignificant 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!
Best Regards, Zopyrus