This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 4 years ago.
I have a 2D array which acts as a game board and the user can make moves into certain cells etc and each user has a specific id e.g. player 1 replaces the cells in the array with their playerId = 1.
I am trying to create a method to find out when the user's playerId gets stuck. They get stuck when they are surrounded by another a value which is not 0.
For example, if player 1 is in the cell 2,5 then P(which represents a 0) is the possible moves they can make. However, if the player was surrounded by other values then they should not be able to make a move (return false).
xxxPPPxxxx
xxxP1Pxxxx
xxxPPPxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
Method:
public boolean checkBlocked(int[][] array,List<Coordinates> track,int playerId)
{
boolean blocked = false;
int trueCount = 0;
for (Coordinates cells: track)
{
int x = cells.getX();
int y = cells.getY();
if(array[x-1][y-1] == 0 ) trueCount++; //topleft
if(array[x-1][y] == 0) trueCount++; //top
if(array[x-1][y+1] == 0) trueCount++;//topright
if(array[x][y+1] == 0) trueCount++;//right
if(array[x][y-1] == 0) trueCount++;//left
if(array[x+1][y-1] == 0) trueCount++;//bottomleft
if(array[x+1][y] == 0) trueCount++;//bottom
if(array[x+1][y+1] == 0) trueCount++; //bottomright
}
if (trueCount == 0)
{
blocked = true;
}
return blocked;
}
I tried to do this method, but then this doesn't work because the 2D array is 6x10 and then if the cell is 1,1 or 6,1 then it gives an ArrayIndexOutOfBoundsException.
Is there an easier way that I can do this check or a way around the exception?
Three solutions
Make the array larger in all directions (and make walls that are invisible/impenetrable) -- This has the highest performance and is my favorite trick for image processing.
Make a method like getBoardSquare that does the bounds checking for you and returns a special value (or 'wall' value) for out-of-bound coordinates -- this has the best 'code readability'
Do manual extensive bounds checking wherever you access the array -- not recommended
You have to check that surround cells are in the board. To do so you could add additional checks to all of the if. E.g.:
if(x > 0 && y > 0 && array[x-1][y-1] == 0) trueCount++;
if(x > 0 && array[x-1][y] == 0) trueCount++;
if(x > 0 && y < array[x-1].length - 1 && array[x-1][y+1] == 0) trueCount++;
I think, it is better to create separate method with internal check:
private static int get(int[][] array, int row, int col) {
return row < 0 || row >= arr.length || col < 0 || col >= array[row].length ? 0 : array[row][col];
}
and us it in your if:
if(get(array, x-1, y-1) == 0) trueCount++;
if(get(array, x-1, y) == 0) trueCount++;
if(get(array, x-1, y+1) == 0) trueCount++;
Finally, I think it's better to encapsulate Board logic into separate class:
public class Board {
private final int[][] board;
public Board(int row, int col) {
board = new int[row][col];
}
public boolean isBlocked(int row, int col) {
if (cell(row - 1, col - 1) == 0 || cell(row - 1, col) == 0 || cell(row - 1, col + 1) == 0)
return false;
if (cell(row, col + 1) == 0 || cell(row, col - 1) == 0)
return false;
if (cell(row + 1, col - 1) == 0 || cell(row + 1, col) == 0 || cell(row + 1, col + 1) == 0)
return false;
return true;
}
private int cell(int row, int col) {
return row < 0 || row >= board.length || col < 0 || col >= board[row].length ? 0 : board[row][col];
}
}
P.S.
Check your x and y. x - column and y - row, so it means
arr[y][x] but not arr[x][y]!
Related
I’m a senior in high school taking a computer science class. For homework, we have to create solutions to certain CodingBat (practice coding website) problems. I am experiencing problems with this question, some of which include OutOfBounds for the array. Based on my code, I can’t quite figure out why this is happening. The following attached code (below) is what I have created as a solution to the CodingBat problem for unlucky1 in Array-1 (java), which describes the challenge as: “We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky a in the first 2 or last 2 positions in the array.
public boolean unlucky1(int[] nums) {
int i = 0;
for(i = 0; i < nums.length; i++)
if(nums[i-1] == 1 && nums[i] == 3)
{
return true;
}
return false;
}
The problem statement is "Return true if the given array contains an unlucky a in the first 2 or last 2 positions in the array.", so you don't even need a loop - you just need to examine the first two and last two elements of the array:
public boolean unlucky1(int[] nums) {
return nums != null &&
nums.length >= 2 &&
(nums[0] == 1 && nums[1] == 3 ||
nums[nums.length - 2] == 1 && nums[nums.length -1] == 3);
}
The following code is the correct method.
0.public static boolean unlucky1(int[] nums){ //Firstly,declare the method
"static"
1. int length = nums.length;//Get the array length.
2.if((nums[0] == 1 && nums[1] == 3) && ( nums[length - 2] == 1 &&
nums[length] -1 == 3)){
3. return true;
}
4. return false;
}
In row 2,your code was: "if(nums[i-1] == 1 && nums[i] == 3)";
It showed arrayoutofbound because the starting array index is 0 and you decalred in the if statement
" if(nums[0-1]...)which says if nums[-1] which is out of bounds."
Also to check the last 2 numbers of the array you do the following:
( nums[length - 1] == 1 && nums[length] == 3)) where :
" nums[length - 2] == 1"
checks 1 value before the last array value
and
" nums[length] - 1 == 3 "
checks the last array value
I have to implement Conway's Game of Life. Everything works as it should and given tests are passing. My only problem is that this method gives complexity error while running PMD rules on my file. I understand that so many if sentences are the cause of that, but while trying to compact them into smaller groups I accidentally broke my code.
Here's what it says:
The method 'getNeighbourCount(int, int)' has a cyclomatic complexity of 21.
The method 'getNeighbourCount(int, int)' has an NPath complexity of 20736, current threshold is 200
What would best the best options for optimizing this method?
public Integer getNeighbourCount(int x, int y) {
// x = column (20), y = row (15)
int countNeigbours = 0;
if (x != 0 && y != 0 && isAlive(x - 1,y - 1)) {
countNeigbours++;
}
if (x != 0 && isAlive(x - 1, y)) {
countNeigbours++;
}
if (x != 0 && y != rows - 1 && isAlive(x - 1,y + 1)) {
countNeigbours++;
}
if (y != 0 && isAlive(x,y - 1)) {
countNeigbours++;
}
// check self
if (y != rows - 1 && isAlive(x,y + 1)) {
countNeigbours++;
}
if (x != columns - 1 && y != 0 && isAlive(x + 1,y - 1)) {
countNeigbours++;
}
if (x != columns - 1 && isAlive(x + 1, y)) {
countNeigbours++;
}
if (x != columns - 1 && y != rows - 1 && isAlive(x + 1,y + 1)) {
countNeigbours++;
}
return countNeigbours;
}
isAlive returns the boolean if the cell is taken (true) or not (false).
Loop over the "delta x" and "delta y" from your current position:
for (int dx : new int[]{-1, 0, 1}) {
if (x + dx < 0 || x + dx >= columns) continue;
for (int dy : new int[]{-1, 0, 1}) {
if (y + dy < 0 || y + dy >= rows) continue;
if (dx == 0 && dy == 0) continue;
if (isAlive(x + dx, y + dy)) countNeighbours++;
}
}
(Of course, you don't have to use arrays and enhanced for loops: you can just do for (int dx = -1; dx <= 1; ++dx), or however else you like)
I don't know if this would provide a speed-up, but you could try having a second array which held the sums, and increased or decreased these values when setting or clearing individual cells. That replaces the many 'isAlive' checks with a check to tell if a cell should be toggled on or off, and reduces the cell adjacency computations to just those cells which were toggled, which should be many fewer than repeating the computation for the entire array. That is, for a mostly empty grid, only a small subset of cells should require recomputation, many fewer than the entire grid.
Also, you could try having whole row isActive, minActive, and maxActive values. That would further reduce the amount of looping, but would increase the complexity and cost of each iteration. The sparseness of active cells would determine whether the extra cost was balanced by the reduction in the number of iterations.
Hey guys I started programming a year ago and recently discovered streams. So I decided to complete my old tasks using streams whenever I could just to get used to them. I know it might not be smart to force using them but it's just practice.
One of my old tasks was to program Minesweeper and right now I try to find a better solution for counting adjacent mines whenever I click a field.
Some details:
I saved a bunch of mines in a Mine[] (arsenal.getArsenal()) and each of the mines has an x and y value. Whenever I click on a field I need to count all mines around the clicked field (from x-1,y-1 till x+1,y+1).
My current solutions are:
private int calculateNearby(int x, int y) {
return (int) Arrays.stream(arsenal.getArsenal())
.filter(mine -> mine.getX() == x + 1 && mine.getY() == y
|| mine.getX() == x && mine.getY() == y + 1
|| mine.getX() == x - 1 && mine.getY() == y
|| mine.getX() == x && mine.getY() == y - 1
|| mine.getX() == x - 1 && mine.getY() == y - 1
|| mine.getX() == x - 1 && mine.getY() == y + 1
|| mine.getX() == x + 1 && mine.getY() == y - 1
|| mine.getX() == x + 1 && mine.getY() == y + 1)
.count();
}
private int calculateNearby(int x, int y) {
return (int) Arrays.stream(arsenal.getArsenal())
.filter(mine ->
{
boolean b = false;
for (int i = -1; i < 2; ++i) {
for (int j = -1; j < 2; ++j) {
if ((x != 0 || y != 0) && mine.getX() == x + i && mine.getY() == y + j) {
b = true;
}
}
}
return b;
})
.count();
}
Both solutions work fine but the first looks "wrong" because of all the cases and the seconds uses for-loops which I basically tried to avoid using.
It would be nice if you could show me a better (ideally shorter) solution using streams. :D
I'm sorry if there's already a thread about this. I really tried to find anything related but there either isn't anything or I searched for the wrong keywords.
You can simplify your stream condition. Instead of checking each case if getX() equals x, x-1 or x+1 you can just check if getX() is greater or equals than x-1 and smaller or equals x+1. The same for getY().
return (int) Arrays.stream(arsenal.getArsenal())
.filter(mine -> mine.getX() >= x - 1 && mine.getX() <= x + 1
&& mine.getY() >= y - 1 && mine.getY() <= y + 1)
.count();
You could also create a method for the check to make the code more readable.
private int calculateNearby(int x, int y) {
return (int) Arrays.stream(arsenal.getArsenal())
.filter(mine -> inRange(mine.getX(), x)
&& inRange(mine.getY(), y))
.count();
}
private boolean inRange(int actual, int target) {
return actual >= target - 1 && actual <= target + 1;
}
Another way is to check if the absolute distance in each direction is less than or equal to 1:
private int calculateNearby(int x, int y) {
return (int) Arrays.stream(arsenal.getArsenal())
.filter(mine -> Math.abs(mine.getX() - x) <= 1 && Math.abs(mine.getY() - y) <= 1)
.count();
}
Note that this also counts a mine which is at the point (x, y) which is not the case with the code in the question.
When is a mine adjacent? When its only one field horizontally or vertically away.
Horizontal distance is Math.abs(mine.getX() - x), vertical distance is Math.abs(mine.getY() - y). It doesn't matter if its -1 or 1, just that it is one field away.
But it shouldn't be more than one field, either vertical or horizontal be away, so max(dx, dy) == 1.
Predicate<Mine> isAdjacent = mine ->
Math.max(
Math.abs(mine.getX() - x)
Math.abs(mine.getY() - y)
) == 1;
return (int) Arrays.stream(arsenal.getArsenal())
.filter(isAdjacent)
.count();
i am trying to detect a jokerStraightFlush when analysing a poker hand.
I need to add a feature where this hand works 8S JK 6S JK 4S. The JK are jokers. I am using the exact same code logic as https://www.codeproject.com/Articles/38821/Make-a-poker-hand-evalutator-in-Java.
cardsTable represents the distribution of the Card ranks present in the hand. Each element of this array represents the amount of card of that rank(from 1 to 13, 1 being ace) present in the hand. So for 8S JK 6S JK 4S, the distribution would be
0 0 0 0 1 0 1 0 1 0 0 0 0 0
note that the position 1 is for ace (because it's simpler)
I need to find a way to detect if cardsTable[i] == 1 failed and decrement the amount of jokers used (numberOfJokers) to detect a jokerStraightFlush because in this incomplete piece of code, numberOfJokers dont decrement and i dont know how to write it in a nice way. What i do here is i check if the card at this rank exists cardsTable[i] == 1 or if its a joker... but i dont know how to check for a joker in the others consecutive rankings
I dont know if i'm clear, it's a twisted situation.. if i'm not let me know.
straight = false; // assume no straight
int numberOfJokers = 2; //(the maximum number of jokers in a hand is 2)
for (int i = 1; i <= 9; i++) { // can't have straight with lowest value of more than 10
numberOfJokers = 2 //reset the number of jokers available after each loop
if ((cardsTable[i] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 1] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 2] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 3] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 4] == 1 || numberOfJokers > 0)) {
straight = true;
break;
}
}
I also have this code but i don't know how to detect if the first condition failed so i can decrement the number of jokers remaining.
for (int i = 1; i <= 9; i++) {
numberOfJokers = 2
if (cardsTable[i] == 1 || numberOfJokers>0) {
if (cardsTable[i + 1] == 1 || numberOfJokers>0) {
if (cardsTable[i + 2] == 1 || numberOfJokers > 0) {
if (cardsTable[i + 3] == 1 || numberOfJokers > 0) {
if (cardsTable[i + 4] == 1 || numberOfJokers > 0) {
straight = true;
break;
}
}
}
}
}
}
Due to "short-circuiting" behaviour, you don't need to detect the left operand of a || resulted in true: the right operand gets evaluated if the left one was false, only.
(cardsTable[i + c] == 1 || numberOfJokers-- > 0) would work; note that
(numberOfJokers-- > 0 || cardsTable[i + c] == 1) would not.
Whether or not such code is maintainable or readable is an independent consideration, as is the quality of the overall approach to problem and solution.
Conceptually, you just need 3 of the 5 slots to be equal to 1, and the other 2 equal to 0. You could do something like this:
for (int i = 1; i <= 9; i++) {
boolean straight = true;
int numberOfJokers = 2;
for (int j = i; j <= i + 5; j++) { // I used a for loop instead of writing the statement 5 times
if (cardsTable[j] > 1) { // Can't have straight if more than one of a kind
straight = false;
}
if (cardsTable[j] == 0) {
numberOfJokers--;
}
}
if (numberOfJokers >= 0 && straight == true) {
break;
}
}
Alternatively, maybe a simpler way would be to add a position in the cardsTable array indicating the number of jokers.
nvm i used a function inside my conditions.. i don't know if it's the right way but it's logic to me x)
straight = false; // assume no straight
for (int i = 1; i <= 9; i++) // can't have straight with lowest value of more than 10
{
remainingJokers=jokers;
System.out.println("resetjokers : "+jokers);
if (cardsTable[i] == 1 || useJoker()) {
if (cardsTable[i + 1] == 1 || useJoker()) {
if (cardsTable[i + 2] == 1 || useJoker()) {
if (cardsTable[i + 3] == 1 || useJoker()) {
if (cardsTable[i + 4] == 1 || useJoker()) {
System.out.println("straight");
straight = true;
topStraightValue = i + 4; // 4 above bottom value
break;
}
}
}
}
}
}
}
private boolean useJoker() {
int remaining = remainingJokers;//remainingJokers is a global variable
remainingJokers--;
return remaining>0;
}
I am having trouble finishing the straight method for a poker hand. I don't understand why my code doesn't work.
public static boolean containsStraight(int [] hand)
{
boolean straight = false;
for(int i = 0; i < 5; i++)
{
if (hand[i] == 2 && hand[i] == 3 && hand[i] == 4 && hand[i] == 5 && hand[i] == 6)
{
straight = true;
}
if (hand[i] == 3 && hand[i] == 4 && hand[i] == 5 && hand[i] == 6 && hand[i] == 7)
{
straight = true;
}
if (hand[i] == 4 && hand[i] == 5 && hand[i] == 6 && hand[i] == 7 && hand[i] == 8)
{
straight = true;
}
if (hand[i] == 5 && hand[i] == 6 && hand[i] == 7 && hand[i] == 8 && hand[i] == 9)
{
straight = true;
}
}
return straight;
}
As pL4Gu33 has already stated in his answer, your comparison is faulty. Essentially, each step through the for-loop leaves hand[i] at a constant value (say, 4). That means that your if-statements are checking:
if(4 == 4 && 4 == 5 && 4 == 6 && 4 == 7 && 4 == 8) {
...
}
This will never evaluate to true. If you knew for certain that you had five elements in the hand and that the hand was already sorted, you could do
if (hand[0] == 2 && hand[1] == 3 && hand[2] == 4 && hand[3] == 5 && hand[4] == 6) {
...
}
However, I'm going to show you a better answer.
The first thing you should do is to sort your hand. Once you do that, it's easy to step through the hand and check to see if the next card in the hand is exactly one greater than the previous card. If you get to the end and this holds true, then it's a straight.
/*
* You will need to import java.util.Arrays
*/
public boolean isStraight(int[] hand) {
if(hand == null || hand.length != 5) {
return false;
}
else {
// Automatically sort the hand
Arrays.sort(hand);
// Set the "previous" variable to a theoretically impossible value
int prev = -1;
// Iterate through the hand and see if the next card is exactly one more than
// the previous one.
for(int i = 0; i < hand.length; i++) {
// If prev is -1, then this is the first time through the for-loop
// If the card that we're on has a value of the previous card + 1,
// we still have the possibility of a straight.
if(prev == -1 || (prev + 1) == hand[i]) {
prev = hand[i];
}
else {
return false;
}
}
return true;
}
}
You say in every iteration hand[i] must be 2 AND 3 AND 4 AND 5. That is impossible. There is only one number in hand[i].
The problem is, that you are using the cycle incorrectly, because you are always checking the value of the same card in hand[i]. My suggestion would be either to do a sort first, or if you want to be more efficient, you can use a second field of booleans, that would indicate, whether a card of given value is present in your hand. This way you can easily check, if you have any number of cards in succesion.
public static boolean containsStraight(int[] cards) {
int count = 0;
boolean[] valueInHand = new boolean[10];
for (int card : cards) {
valueInHand[card] = true;
}
for (boolean value : valueInHand) {
if (value == true) {
count++;
} else {
count = 0;
}
// works for any number of cards
if (count == cards.length) {
return true;
}
}
return false;
}