views:

198

answers:

3

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

A: 

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

if(digits-2 >= 0)
{
    //put core here
}
Tedil
Thank you for your input Tedil!
Zopyrus
+3  A: 

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.

Syntactic
Thank you Syntactic! Understood your explanation very well! A side question, is this the only way to move/switch around indexes in arrays?
Zopyrus
+1  A: 

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.

Eyal Schneider
Thanks for your input Eyal! I considered 1) at first but that would have made my array longer which I didn't want to do if there was another way - thus asking here :)I will refactor the code a little with methods and develop it graphically (MVC) but I felt a need to have the game logic done first as easy as possible and from that point on modify it more and more.
Zopyrus