Related
I have a method which finds the index of all the set bits in a binary string. It works fine except when the bits are set near the end. Here is the code.
public static void main(String[] args) throws InterruptedException {
indexSetBits(1101111100000000000000000000000000000000000000000000000000000000L);
}
public static ArrayList<Integer> indexSetBits(long bitboard) {
ArrayList<Integer> indices = new ArrayList<>();
int count = 0;
while (bitboard > 0L) {
if ((bitboard & 1L) == 1L) {
indices.add(count);
}
bitboard >>= 1;
count++;
}
return indices;
}
For some reason, when I pass in that 64 bit number as seen in my code, it gives the error: " Long number too large." Though i am sure what i am passing in is a 64 bit number. Is there any way to solve this?
Edit: It was pointed out that I was forgetting the "0b". Whoops!
Now that I remembered that, how would i make sure a method that returns a long, returns it in binary format, in other words how would i pass in a long without explicitly saying "0b".
For example I have this method which returns all the attack bits of a king as a long, and the logic with the bit mainuplation is fine within the actual method.
public long[] calculateKingMoves(int square, long ownSideBitboard,long enemySide, long allPieces){
/*
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 2, 3, 0, 0]
[0, 0, 0, 8, x, 4, 0, 0]
[0, 0, 0, 7, 6, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
*/
//8 possible moves for a king from a given position, unless on a border, under check, or attempting to move into a piece that is
// under the watch of another piece.
long kingBitBoard =1L<< square;
long kingAFile = kingBitBoard & Lookups.fileTables[0];
long kingHFile = kingBitBoard & Lookups.fileTables[Lookups.fileTables.length-2];
long move1 = kingAFile <<7;
long move2 = kingBitBoard <<8;
long move3 = kingHFile <<9;
long move4 = kingHFile <<1;
long move5 = kingHFile >> 7;
long move6 = kingBitBoard >>8;
long move7 = kingAFile >>9;
long move8 = kingAFile >>1;
long kingPsuedos = move1 | move2| move3| move4| move5| move6| move7| move8;
long kingMoves = kingPsuedos & ~allPieces;
long kingAttacks = kingPsuedos & enemySide;
return new long[]{kingMoves,kingAttacks};
}
How would i make sure the two longs returned were in binary format? Instead of just normal longs?
I am making a tile based game in Android Studio. I want the map to output circles (islands) if it loops past a 1, and the circle should be in the correct grid coordinates however when I run it nothing happens at all.
int gameBoard[][] = {{1, 0, 1, 0, 0}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
public void onDraw(Canvas canvas) {
for (int i = 0; i < 4; i++) {
for (int R = 0; R < 4; R++) {
if (gameBoard[i][R] == 1) {
Paint Blue = new Paint();
Blue.setColor(Color.BLUE);
canvas.drawCircle(i, R, 10, Blue);
}
}
}
}
Along with color, you also need to set a few other things:
paint.setStyle(Paint.Style.STROKE);
or
paint.setStyle(Paint.Style.FILL);
It's possible that your onDraw method isn't being called. If adding the paint style doesn't fix your problem entirely, you should probably verify that onDraw is being called with a log at the beginning of the method.
I am making a tile based game in Android Studio. I want the map to output circles (islands) if it loops past a 1, and the circle should be in the correct grid coordinates however when I run it nothing happens at all.
int gameBoard[][] = {{1, 0, 1, 0, 0}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
public void onDraw(Canvas canvas) {
for (int i = 0; i < 4; i++) {
for (int R = 0; R < 4; R++) {
if (gameBoard[i][R] == 1) {
Paint Blue = new Paint();
Blue.setColor(Color.BLUE);
canvas.drawCircle(i, R, 10, Blue);
}
}
}
}
Along with color, you also need to set a few other things:
paint.setStyle(Paint.Style.STROKE);
or
paint.setStyle(Paint.Style.FILL);
It's possible that your onDraw method isn't being called. If adding the paint style doesn't fix your problem entirely, you should probably verify that onDraw is being called with a log at the beginning of the method.
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making. In the orginal pacman when a Ghost is eaten when Pacman has picked up the Power the Ghosts eyes would navigate back to the home area and then spawn the ghost back in. I would like to do this to. It would also help me with implementing a Ghost AI to make them move smarter then just random.
So basically those eyes would have to navigate through this:
And the board is being drawn from the below 2D array:
//0's = Walls or location not allowed to go
//1's = Dot Spot
//2's = Clear Path nothing on it but safe to move
//3's = Power Dot
//-1's = only ghosts can go through
//5= Top entry spot
//6= bottom entry point
//7 = Cherry
//(Spots = row - 1 same with columns = - 1. First # is row. Second is col
public int board[][] =
{{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //1
{2, 0, 3, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 3, 0, 2}, //2
{2, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 2}, //3
{2,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,2}, //4
{2,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,2}, //5
{2,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,2}, //6
{2,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,2}, //7
{2,2,2,2,0,1,0,1,1,1,1,1,1,1,0,1,0,2,2,2,2}, //8
{0,0,0,0,0,1,0,1,0,0,-1,0,0,1,0,1,0,0,0,0,0}, //9
{5,2,2,2,2,1,1,1,0,2,2,2,0, 1 ,1,1,2,2,2,2,6}, //10 - cherry
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0}, //11
{2,2,2,2,0,1,0,1,1,1,2,1,1,1,0,1,0,2,2,2,2}, //12
{2,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,2}, //13
{2,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,2}, //14 - pacman on this row
{2,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,2}, //15
{2,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,2}, //16
{2,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,2}, //17
{2,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,2}, //18
{2,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,2}, //19
{2,0,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,2}, //20
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}}; //21
So my question is how should I go about making the eyes go to the centre home spot in the fastest way without going through walls?
Just use a precomputed inkblot. The "home" squares are labelled zero. Then, all unassigned neighbour squares of a square labelled n are assigned label n +1. Now all your "dead" ghosts have to do is move to a neighbouring square with a lower label. Eventually they will get home having taken the shortest path. Easy!
You can look into Lee's algorithm for shortest path inside a matrix with obstacles.
I'm using a 2D Array in Java and it's the first time doing so. I'm wondering what is wrong with the array as I'm getting many errors...? (probably a really stupid error)
Code
int board[21][21] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,1},
{1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1},
{1,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
Error List
Here is a link instead of posting it here since it's a lot of errors
You can't specify the size of arrays in the declaration. If you remove your size integers, it will compile. However, you should always place your brackets on your variable's type, not on the variable itself when you declare it as such:
int[][] board;
rather than:
int board[][];
If you want to create a blank array with a specific size, then you would do so with the new operator:
int[][] board = new int[21][21];
However, you never specify a size if you define your array though array initialization i.e.
int[][] board = {{...}, ...};
Remove 21. You should say
int board[][] = ....
You have to define dimensions of array when creating them. For example you can say new int[5] and create 5 elements long int array. But when you assign this array to variable you say: int[] arr = new int[5]. Here int[] is a type (int array). It is similar to int * in C. You do not have to say how big will be the array when you are defining its type. You just have to say what is its element type and how many dimensions it will have.
Firstly, you get a compile error for telling the size of your 2D array in the declaration
int board[21][21]
usually you want to do it this way:
int board[][] = {{1,2}{1,3}}
This is a 2D array with 2 columns and 2 rows
1 2
1 3
So if you want to a 2D array with 21 rows and 21 columns you should do this:
int board[][] = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{],{},{},{},{},{}}
and fill whatever you need in between the inner brackets.
Change first line to board[][] ={{ all your numbers}} and all will be ok.
Your declaration is wrong.
int board[][] = new int[][] {{...
remove dimensions from declaration:
int board[][]