Check if a char is in a 2D array in java - java

I have been working on my homework of creating a simple chess program.
The chessboard is a 2D array, named board. The pieces are all single chars like R,Q,K, and empty space is filled with a ' ' char, so a space.
public static char[][] board;
The method get should return the char or whatever is there on the coordinate (x,y).
public char get(int x, int y) {
if(x < board[0].length && x >= 0 && y < board.length && y >= 0){
return board[x][y];
}
else
return '\0';
}
And the problem is that my program of evaluating who is the winner is not working as expected.
This program checks whether the king of each team is still in the chessboard, so the board array.
It should return 0 if both are still alive, 1, if only the white king is alive, and 2 if only the black king is alive.
I have made a program, or at least tried to make one, that goes through each coordinate and checks if there is a character 'K', which represents the white king, and 'S', the black king.
I have set the boolean kingIsAlive to false, so that if there is no K or S found in the array, it remains false.
Though, my code at the bottom, with the if and else that returns 0,1 or2, has the error, that kingWhiteIsAlive is always false and kingBlackIsAlive is always false.
So, I think my program of turning the kingIsAlive boolean to true is not working at all....
The errors I got is:
White should have won => expected: <1> but was: <-1>
No one should have won => expected: <0> but was: <1>
And after a couple of hours trying, I gave up and decided to ask here.
public int decideWinner(){
boolean kingWhiteIsAlive = false;
boolean kingBlackIsAlive = false;
for(int y = 0; y < board.length;y++){
for(int x = 0;x < board[0].length;x++){
if(get(x,y) == 'K'){
kingWhiteIsAlive = true;
}
}
}
for(int j = 0; j < board.length;j++){
for(int i = 0;i < board[0].length;i++){
if(get(i,j) == 'S'){
kingBlackIsAlive = true;
}
}
}
if(kingWhiteIsAlive && kingBlackIsAlive){
return 0;
}
else if(kingWhiteIsAlive && !kingBlackIsAlive){
return 1;
}
else if(!kingWhiteIsAlive && kingBlackIsAlive){
return 2;
}
else
return -1;
}
return -1 is for a test case that there are no kings from both teams.

I've tested your code & it's running perfectly, perhaps the problem is in your board initialization?
Try to debug your code by showing the actual content of the array at the beginning of decideWinner function.
I've used this initialization for the board, if it might help.
public void initBoard() {
board = new char[8][8];
// Init pawns
for (int j = 0; j < board.length; j++) {
board[j][1] = 'P';
board[j][6] = 'P';
}
// Rooks
board[0][0] = 'R';
board[7][0] = 'R';
board[0][7] = 'R';
board[7][7] = 'R';
// Knights
board[1][0] = 'N';
board[6][0] = 'N';
board[1][7] = 'N';
board[6][7] = 'N';
// Bishops
board[2][0] = 'B';
board[5][0] = 'B';
board[2][7] = 'B';
board[5][7] = 'B';
// Queens
board[3][0] = 'Q';
board[4][7] = 'Q';
// Kings
board[4][0] = 'K'; // White
board[3][7] = 'S'; // Black
// Empty space
for (int y = 0; y < board.length; y++) {
for (int x = 2; x < 6; x++) {
board[y][x] = ' ';
}
}
}

If you find a king, you need to interrupt your search. You can do it by a break statement.
for(int y = 0; y < board.length;y++){
for(int x = 0;x < board[0].length;x++){
if(get(x,y) == 'K'){
kingWhiteIsAlive = true;
break;
}
}
}
for(int j = 0; j < board.length;j++){
for(int i = 0;i < board[0].length;i++){
if(get(i,j) == 'S'){
kingBlackIsAlive = true;
break;
}
}
}

Related

Programming a battleships ship placing algorithm(java)

So im programming a battleships game, and im trying to place ships on a board. I can get it to place the ship going left and going down from the starting point but cant get it going down or left. I understand my method overall is in-effeicient.
public static int[][] SetUserBoard(int[][] board) {
int x;
int y;
y = 0;
for (int z = 0; z < 10; z++) {
String gridv;
gridv = "";
int direction;
System.out.println("Set the Position for Ship no. " + (z+1));
Scanner s1 = new Scanner(System.in);
System.out.println("Enter the grid column(A-H):");
gridv = s1.nextLine();
if (gridv.equals("A")) {
y = 0;
}
if (gridv.equals("B")) {
y = 1;
}
if (gridv.equals("C")) {
y = 2;
}
if (gridv.equals("D")) {
y = 3;
}
if (gridv.equals("E")) {
y = 4;
}
if (gridv.equals("F")) {
y = 5;
}
if (gridv.equals("G")) {
y = 6;
}
if (gridv.equals("H")) {
y = 7;
}
System.out.println("Enter the y co=ordinate: ");
x = s1.nextInt();
x -= 1;
System.out.println("Enter the direction of the ship 0-3(0=down,1=right,2=up,3=left): ");
direction = s1.nextInt();
if(z == 0) { //placing 4 unit ship in first increment
if(direction == 0 && x < 5){ //vertical placement - down
for(int i=0; i < 4; i++) {
board[x][y] = 0;
x += 1;
}
}
if(direction == 1 && y < 5) { //horizontal placement - right
for(int i=0; i < 4; i++) {
board[x][y] = 0;
y += 1;
}
}
if(direction == 2 && x > 3 ) { //vertical placement - up
for(int i=0; i < 4; i++) {
board[x][y] = 0;
x -= 1;
}
}
if(direction == 3 && y > 3) { //horizontal placement - left
for(int i=0; i < 4; i++) {
board[x][y] = 0;
y -= 1;
}
}
}
...if(z > 0 && z < 3) { //placing 3 unit ships in 2nd and 3rd increment....
return board;
}
if you ignore the bottom part, and focus on the if z=0 part as that's which part i'm using to test this with my most updated try. Originally I had an indexing error which I managed to solve but now all the program does is run as usual except when moving onto the next one the board isn't updated and is still empty. So im stumped as to what logic to use for up/left.
For diagonal changes, you need to alter both i and j as you traverse the ship. For instance:
UP_LEFT = 4
...
ship_len = 4
...
if(direction == UP_LEFT &&
y > ship_len-1 &&
x > ship_len-1) { // SE-NW placement
for(int i=0; i < ship_len; i++) {
board[x][y] = 0;
y -= 1;
x -= 1;
}
}

Moving player on Array

I have a map class, which is an Array of 100 Strings
public class map{
[...]
public void movePlayer(String entrada){
if (entrada.equalsIgnoreCase("w")){
move = -10;
}
else if (entrada.equalsIgnoreCase("s")){
move = 10;
}
else if (entrada.equalsIgnoreCase("d")){
move = 1;
}
else if (entrada.equalsIgnoreCase("a")){
move = -1;
}
for(int i = 0; i < mapa.length; i++){
if (mapa[i].equals("P")){
int moved = i+move;
mapa[moved] = "P";
}
}
}
The main looks a little like this
String[] mapa = map.getMap();
mapa[0] = "T";
mapa[99] = "P";
for (int j = 0; j<10; j++){
for(int i = 0; i < map.length; i++){
if (i == 9 || i == 19 || i == 29 || i == 39 || i == 49 || i == 59 || i == 69 || i == 79 || i == 89 || i == 99){
System.out.println(mapa[i]);
}
else{
System.out.print(mapa[i]);
}
}
System.out.print("\n");
System.out.print("Entre o movimento:");
map.movePlayer(read.nextLine());
mapa = map.getMap();
}
It runs like this: A map with random chars are printed, some are normal floors some are traps
EXAMPLE
T##_______
^__#__*^#_
##^^#_#___
_*^^^#^^#^
^^_#_^____
^#_^#___##
*#^_^_____
^###_^__^#
#_#^##^#**
##^^_##_#P
Enter movement:w
T##_______
^__#__*^#_
##^^#_#___
_*^^^#^^#^
^^_#_^____
^#_^#___##
*#^_^_____
^###_^__^#
#_#^##^#*P
##^^_##_#P
Enter movement: w
T##_______
^__#__*^#_
##^^#_#___
_*^^^#^^#^
^^_#_^____
^#_^#___##
*#^_^_____
^###_^__^P
#_#^##^#*P
##^^_##_#P
How can I make the program to print the place where the Player originally was with the previous char it had, in this case a blank space in position [99] and a "*" in position [89]? Thanks for the patience!
Instead of tracking the player inside the map, you can leave the map unchanged and just track the player's position.
int playerpos = 99;
for (int j = 0; j<10; j++){
for(int i = 0; i < map.length; i++){
if (playerpos == i)
System.out.print("P");
else
System.out.print(mapa[i]);
if (i %10 == 9){
System.out.println();
}
}
Then your movePlayer method just changes the playerpos variable.
before player's movement you need to store data from the target cell, like this
int moved = i+move;
previousChar = mapa[moved]; // store replaced symbol
mapa[moved] = "P";
so, during next move you need to restore original char in old position, it could look like this:
int moved = i+move;
mapa[i] = previousChar; // restore original symbol at current player's location
previousChar = mapa[moved]; // preserve symbol at new player's location
mapa[moved] = "P"; // move player to new location
don't forget to init previousChar before very first move
As Lashane is saying, use a variable to store the overriden char for later assignment:
public class map{
[...]
this.init(function() {
last_squarechar = INIT_SQUARECHAR
[...]
});
public void movePlayer(String entrada) {
if (entrada.equalsIgnoreCase("w")){
move = -10;
}
else if (entrada.equalsIgnoreCase("s")){
move = 10;
}
else if (entrada.equalsIgnoreCase("d")){
move = 1;
}
else if (entrada.equalsIgnoreCase("a")){
move = -1;
}
}
for(int i = 0; i < mapa.length; i++){
if (mapa[i].equals("P")){
int moved = i+move;
mapa[i] = last_squarechar
last_squarechar = mapa[moved]
mapa[moved] = "P";
}
}
}

(ADDED) Noughts and Crosses game. While loop

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
visBoard[i][j] = "[ ]";
board[i][j] = 0;
check[i][j] = false;
}
}for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
System.out.print(visBoard[i][j]);
}System.out.print("\n");
}
//Getting Names
System.out.println("Player 1 - Enter your name");
play1 = sc.nextLine();
System.out.println("Player 2 - Enter your name");
play2 = sc.nextLine();
//
moves = 0;
symbol = " X ";
do{
do{
//Get Coords
System.out.println("X Coordinate");
xcoord = sc.nextInt() -1;
System.out.println("Y Coordinate");
ycoord = sc.nextInt() -1;
if(check[xcoord][ycoord] == true){
System.out.println("Not a valid move!");
}
}while(check[xcoord][ycoord] == true);
//Making move
check[xcoord][ycoord] = true;
visBoard[xcoord][ycoord] = symbol;
if(symbol.equals(" X ")){
board[xcoord][ycoord] = 1;
}else if(symbol.equals(" O ")){
board[xcoord][ycoord] = 5;
}else{
System.out.println("You've messed up James");
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
System.out.print(visBoard[i][j]);
}System.out.print("\n");
}
//Check if game has won
//columns
total = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
total = total + board[j][i];
}if(total == 15 || total == 3){
gamewon = true;
}
}total = 0;
//rows
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
total = total + board[i][j];
}if(total == 15 || total == 3){
gamewon = true;
}
}total = 0;
//diagonals
for(int i = 0; i < 3; i++){
total = total + board[i][i];
}if(total == 15 || total == 3){
gamewon = true;
}total = 0;
diag = 2;
for(int i = 0; i < 3; i++){
total = total + board[i][diag];
diag--;
}if(total == 15 || total == 3){
gamewon = true;
}
moves++;
if(gamewon == false){
if(moves == 9){
System.out.println("Game has been drawn! No one wins!");
}else{
mod = moves % 2;
if(mod == 0){
symbol = " X ";
}else{
symbol = " O ";
}
}
}
}while(gamewon == false || moves != 9);
if(gamewon == true){
if(symbol.equals(" X ")){
System.out.println("Winner is "+play1);
}else{
System.out.println("Winner is "+play2);
}
}else{
System.out.println("Game is drawn");
}
}
}
This is a further question from a previous question I had. This game won't end until moves reaches 9 even though the while loop should stop once someone has won. The boolean will turn true, but it will continue to loop.
How do I fix this issue with keeping the while condition, and possibly without using breaks?
You need an and not an or
while(gamewon == false && moves != 9);
Reading that to yourself it says while there is no winner and we are not at move 9. However it's usually better form to code your loops to check that you haven't exceeded a bound rather than you have hit the bound exactly, and it is also nicer to simply test the boolean directly so the following is more stylish:
while(!gamewon && moves < 9);
while(gamewon == false || moves != 9)....
This tells the loop to execute while game isnt won, or moves are not 9. For it to end, BOTH conditions need to change, the game needs to be ended AND moves needs to be 9.
Change your || operator to &&. This way the game will keep going while the game is not won AND the moves is not 9. It seems a bit strange but if you can follow the logic, you'll see that you need the AND operator.
Therefore, you're looking for:
while(gamewon == false && moves != 9)

Java: Programming a simple maze game

I'm coding a simple maze game in java. The program reads in a text "map" from an input file for the layout of the maze. The rules are simple: navigate the maze (represented by a 2D array) through user input and avoid the cave-ins (represented by Xs), and get to the 'P' (player) the the spot marked 'T'. Right now, I've got most of the code written, it's just a matter of getting it to work properly. I've set up most of the game to run with a while loop, with the boolean "got treasure" set to false. Once this rings true, it should end the game.
However, I haven't coded the circumstance in which the player actually gets the treasure though, so I'm wondering why my code simply spits out "Congratulations! You've found the treasure!" and nothing else. If anyone could shed some light on this, I'd be very grateful. My code is somewhat of a mess of loops, as our teacher has just gotten to methods, constructors, and creating our own classes. Here is the code I have so far:
import java.util.*;
import java.io.File;
public class MazeGame {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new File("maze.txt"));
Scanner user = new Scanner(System.in);
int rows = scan.nextInt();
int columns = scan.nextInt();
int px = 0;
int py = 0;
String [][] maze = new String[rows][columns];
String junk = scan.nextLine();
for (int i = 0; i < rows; i++){
String temp = scan.nextLine();
String[] arrayPasser = temp.split("");
for (int j = 0; j < columns; j++){
maze[i][j] = arrayPasser[i];
}
}
boolean gotTreasure = false;
while (gotTreasure = false){
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
System.out.print(maze[i][j]);
System.out.print(" ");
}
System.out.print("\n");
}
System.out.printf("\n");
System.out.println("You may:");
System.out.println("1) Move up");
System.out.println("2) Move down");
System.out.println("3) Move left");
System.out.println("4) Move right");
System.out.println("0) Quit");
int choice = user.nextInt();
int i = 0;
if (choice == 1 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("X") == false){
maze[px][py] = ".";
maze[k][l-1] = "P";
maze[px][py] = maze[k][l-1];
}else if (maze[px][py-1] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 2 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("X") == false){
maze[px][py] = ".";
maze[k][l+1] = "P";
maze[px][py] = maze[k][l+1];
}else if (maze[px][py+1] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 3 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("X") == false){
maze[px][py] = ".";
maze[k-1][l] = "P";
maze[px][py] = maze[k-1][l];
}else if (maze[px-1][py] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 4 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("X") == false){
maze[px][py] = ".";
maze[k+1][l] = "P";
maze[px][py] = maze[k+1][l];
}else if (maze[px+1][py] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 0){
System.exit(0);
}
}
System.out.println("Congratulations, you found the treasure!");
scan.close();
user.close();
}
}
And here is the sample input file:
5 5
P.XX.
.X...
...X.
XXT..
..X..
(sigh) one equals sign instead of two. You have "while (gotTreasure = false)", which assigns the value false to gotTreasure and does not enter the loop. Change it to "while (gotTreasure == false) and it enters the loop.
For future questions: please attempt to figure out on your own what is happening, and let others know what you have tried and what specific questions you have about it. It is arguable I should just have let this go, since it is essentially a request to debug your code for you. Learn to debug yourself. If trace statements aren't getting executed, it's most likely the code at that point isn't getting executed. If a loop isn't getting entered, it is almost certainly because the conditions for entering the loop don't exist.
Learn to use a debugger - eclipse (and, I am sure, lots of other development tools) has an excellent one. Find out what a breakpoint is, how to set it and examine variables when it is hit, and figure out from there what has gone wrong.
If this is a typo ignore this, if it isnt
while (gotTreasure = false) is wrong.
you are not checking if gotTreasure is false, you are assigning it false.
to check if gotTreasure is false use == operator
while(gotTreasure==false)
lemme know if this is a type, i ll delete the answer. :)
You have a simple mistake in your while loop condition,
Instead of,
while (gotTreasure = false)
You should use,
while (gotTreasure == false)
In the first case, you are assigning false to gotTreasure and in the second you are evaluating if gotTreasure is false.
I refeactored your code, because there are a lot of bad programming-styles. Now the game should run as intended.
I used a Construktor and a lot of methods, to divide your big method in small parts. -> easier to understand.
I declared attributes (known in the whole class), so that the different methods can use this variables.
You often checked for a condition like if(variable == false). Try to use if(!variable), the exclamation mark negates the value of the variable.
Your update-Methode had a lot of redundandancies.
By adding the following switch-case-Part, I could seperate the different directions:
General code for setting directions by a userinput:
switch (choice){
case 0: System.exit(0);
case 1: xdir = 0; ydir = -1; break;
case 2: xdir = 0; ydir =1; break;
case 3: xdir = -1; ydir = 0; break;
case 4: xdir = 1; ydir = 0; break;
}
Afterwards I could calculate the new position by adding xdir to x and ydir to y. This comes handy, if you try to check if the new position is in the bounds of the array.
//1. Check if the new position is in the array.
if (x+xdir >= 0 && x+xdir <columns && y+ydir >=0 && y+ydir < rows){
Here follows the whole class:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class MazeGame2 {
Scanner scan;
Scanner user;
int rows;
int columns;
String [][] maze;
int x; //Player x-Position
int y; //Player y-Position
boolean gotTreasure;
/**
* Konstruktor for the class.
*/
public MazeGame2(){
init();
game();
scan.close();
user.close();
}
/**
* Initialisation of the maze and all attributes.
*/
public void init(){
user = new Scanner(System.in); //Scanner for Userinput
/********************************
* Scanning the maze from a file.
*/
//1. Open the file. Has to be in a try-catch-Bracket, because the file might not be there.
try{
scan = new Scanner(new File("maze.txt"));
}catch(FileNotFoundException e){
e.printStackTrace();
}
//2. Scan the dimensions of the maze.
rows = scan.nextInt();
columns = scan.nextInt();
scan.nextLine(); // So that the next Line can be scanned.
maze = new String[rows][columns];//Create the maze-Array with the right dimensions.
for (int i = 0; i < rows; i++){
String temp = scan.nextLine(); //Scan one line.
for (int j = 0; j < columns; j++){
maze[i][j] = temp.substring(j, j+1);//Put every character in the maze
if (maze[i][j].equals("P")){ //Look out for the Player-Position
x = j;
y = i;
}
}
}
gotTreasure = false;
}
/**
* Prints the Input of the maze-Array. But only if the spots are visible by the player.
*/
public void printMaze(){
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
System.out.print(maze[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
/**
* Prints the possebilities to move by the player.
*/
public void printUserPossebilities(){
System.out.println();
System.out.println("You may:");
System.out.println("1) Move up");
System.out.println("2) Move down");
System.out.println("3) Move left");
System.out.println("4) Move right");
System.out.println("0) Quit");
}
/**
*
*/
public void update(int choice){
int xdir=0;
int ydir=0;
// Update the direction based on the userChoice
switch (choice){
case 0: System.exit(0);
case 1: xdir = 0; ydir = -1; break;
case 2: xdir = 0; ydir =1; break;
case 3: xdir = -1; ydir = 0; break;
case 4: xdir = 1; ydir = 0; break;
}
/**
* Update the situation based on the current direction and step.
*/
//1. Check if the new position is in the array.
if (x+xdir >= 0 && x+xdir <columns && y+ydir >=0 && y+ydir < rows){
//2. Check if a step is possible
if (maze[y+ydir][x+xdir].equals("X")){
System.out.println("Cannot move into a cave-in! Try something else.");
}else{
//3. clear the P from the old Position
maze[y][x] =".";
//4. Check if the Player is over the treasure
if (maze[y+ydir][x+xdir].equals("T")){
gotTreasure = true;
}
x = x+xdir;
y = y + ydir;
maze[y][x] = "P"; //Show the new position of the player.
}
}else{
System.out.println("That's not a possible Move.");
}
}
/**
* The game-Methode that includes the game-loop and
*/
public void game(){
while (!gotTreasure){
//System.out.print('\u000C');
printMaze();
printUserPossebilities();
int userInput = user.nextInt(); //Wait for userinput
update(userInput);
}
//System.out.print('\u000C');
printMaze();
System.out.println("Congratulations, you found the treasure!");
}
public static void main(String[] args){
MazeGame2 m = new MazeGame2();
}
}

Stuck on this bit of code

What I am doing is creating a command line "game" where there is a 3x3 grid (Array) where you can move a "1" through it by typing the direction (up, down, left, right).
For example:
0 0 0
0 1 0
0 0 0
I've made it so if the 1 is on the edge of the array it is not allowed to move out of the boundaries (read: resulting in an out of index error).
I'm completely lost as whenever I try to move right, I receiving the following:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Logic.setMove(Logic.java:87)
at Logic.getMove(Logic.java:10)
at GridGameMain.main(GridGameMain.java:13)
Here's my code:
public class GridGameMain {
static int[][] board = new int[3][3];
public static void main(String[] args){
board[(int) (Math.random() * 2.5)][(int) (Math.random() * 2.5)] = 1;
for (int i =0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(" " + board[j][i]);
}
System.out.println("");
}
Logic l = new Logic();
l.getMove();
}
}
import java.util.Scanner;
public class Logic extends GridGameMain{
void getMove(){ //takes user input then calls setMove
String direction; //string to hold the direction
Scanner user_input = new Scanner(System.in);
direction = user_input.next();
Logic l = new Logic();
l.setMove(direction);
}
void setMove(String direction){ //called when user presses enter upon typing a move
Logic l = new Logic();
if(direction.equals("up")){
if(board[0][0] == 1 || board[1][0] == 1 || board[2][0] == 1 ){
System.out.println("Invalid move!");
l.getMove();
}else{
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
if(board[a][b] == 1){
board[a][b-1] = 1;
board[a][b] = 0;
break;
}
}
}
l.printBoard();
System.out.println("you moved up");
l.getMove();
}
}
if(direction.equals("down")){
if(board[0][2] == 1 || board[1][2] == 1 || board[2][2] == 1 ){
System.out.println("Invalid move!");
l.getMove();
}else{
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
if(board[a][b] == 1){
board[a][b+1] = 1;
board[a][b] = 0;
break;
}
}
}
l.printBoard();
System.out.println("you moved down");
l.getMove();
}
}
if(direction.equals("left")){
if(board[0][0] == 1 || board[0][1] == 1 || board[0][2] == 1 ){
System.out.println("Invalid move!");
l.getMove();
}else{
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
if(board[a][b] == 1){
board[a-1][b] = 1;
board[a][b] = 0;
break;
}
}
}
l.printBoard();
System.out.println("you moved left");
l.getMove();
}
}
if(direction.equals("right")){
if(board[2][0] == 1 || board[2][1] == 1 || board[2][2] == 1 ){
System.out.println("Invalid move!");
l.getMove();
}else{
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
if(board[a][b] == 1){
board[a+1][b] = 1;
board[a][b] = 0;
break;
}
}
}
l.printBoard();
System.out.println("you moved right");
l.getMove();
}
}
}
void printBoard(){
for (int i =0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(" " + board[j][i]);
}
System.out.println("");
}
}
}
I'm just not sure why I can't move right when I can move up, down, and left just fine. Please tell me I'm not crazy!
I think the trouble is, you check to make sure the 1 is not on the far right, and then you start shifting things right. That means that, if your 1 was on the column 0, it's moved to column 2, then at the next and last iteration, it's moved to column 3.
Also, are you sure this doesn't happen when you go down?
This doesn't happen going down because, as #Keppil says, you break out of the "rows" (relevant) loop, while going right, you break out of the columns one, which is not what you wanted.
Also, you can use tags to break out of whatever loop you want. Here's how.
The problem is that you are moving the "1" from left to right, so since you break the first for, you don't break the second one, so it keep moving the "1", until it is put outside the array (ArrayIndexOutOfBoundsException).
An example to solve it:
boolean found = false;
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
if(board[a][b] == 1){
board[a+1][b] = 1;
board[a][b] = 0;
found = true;
break;
}
if(found){
break;
}
}
}
On your move right:
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
if(board[a][b] == 1){
board[a+1][b] = 1; // what if a is 2??
board[a][b] = 0;
break;
}
}
When a is 2, accessing board[a+1][b] is accessing board[3][b]. On a 3*3 board, that's beyond bounds.
Edit: the problem is due to the fact that after you move the 1, you continue the outer loop - but without the initial bounds check. So after the 1 is moved from the central column to the right one, you try to move it again. This only happens on a right move due to the why you loop.
There are two easy solutions:
Use some flag to make sure you break out of the outer loop as well, or better yet -
Change the bounds of the fors according to what you're actually going to check. If you're not going to move a 1 that is on column #2, you can have a loop between 0 and 1. No reason to go up to 2, if you're never going to make it move from that point. Same goes to all other moves - change the appropriate bounds from < 3 to < 2.
The break; inside your for-loops seems to interrupt only the inner for-loop. after breaking the for(int b = 0; b < 3; b++){ .. }, there is no reason for the outer loop to stop.
Your loop finds the 1 at [1][1] and shifts it to the right. then it breaks the inner loop. Nevertheless it calls the inner loop for a=2, where your 1 is now. He tries to shift again.. and fails with exception.
I believe you should be getting an error when you go to the right. The reason is because when you shift one to the right, you break and then you increment a, but you do not check that it went out of bounds again. Once the 'b' loop finishes and then increments 'a' then it will register it as having a value of 1 and move it once more to the right. This keeps going until it is out of bounds. An easy way to correct your code is to just reverse your loops so the outer loop is incrementing 'b' and the inner loop is incrementing 'a'.

Categories