Calculate dots on cuboid's grid - java

I have an issue with my math skills here. So I need to calculate the positions (x, y, z) of a specific amount of dots per aixs. For example take this image
final double gapX = lengthX / dotsPerXAxis;
final double gapY = lengthY / dotsPerYAxis;
final double gapZ = lengthZ / dotsPerZAxis;
for (BlockFace blockFace : BlockHandler.DIRECT_RELATIVES) {
final DecimalVector3D minCorner = new DecimalVector3D(
blockFace.getModX(),
blockFace.getModY(),
blockFace.getModZ()
);
for (int x = 0; x < dotsPerXAxis || x == 0; x++) {
for (int y = 0; y < dotsPerYAxis; y++) {
for (int z = 0; z < dotsPerZAxis; z++) {
}
}
}
My question now is: how can I iterate over all the dots except those that are inside the cuboid and calculate their position and put them in an ImmutableList?

You need to treat point if at least one coordinate of it is zero or dotsPerZAxis.
So set flags - if X-coordinate lies on face, if Y-coordinate lies on face. If both flags are not set - get only the first and the last Z-coordinates, otherwise walk through all Z-coordinates.
Unchecked Java:
for (int x = 0; x < dotsPerXAxis; x++) {
bool atX = (x == 0) || (x == dotsPerXAxis - 1);
for (int y = 0; y < dotsPerYAxis; y++) {
bool atY = (y == 0) || (y == dotsPerYAxis - 1);
int zstep = (atX || atY)? 1: dotsPerZAxis - 1;
for (int z = 0; z < dotsPerZAxis; z+=zstep) {
treat(x,y,z)
}
}
}
Ideone Python working code as proof-of-concept gives n^3 - (n-2)^3 points (26 surface points for n=3, 56 for n=4, 98 for n=5)

Based on the help of MBo I figured this out
for (BlockFace blockFace : BlockHandler.DIRECT_RELATIVES) {
for (int x = 0; x < dotsPerXAxis || x == 0 || x == dotsPerXAxis - 1; x++) {
for (int y = 0; y < dotsPerXAxis || y == 0 || y == dotsPerYAxis - 1; y++) {
for (int z = 0; z < dotsPerXAxis || z == 0;
z += (y == 0 || y == dotsPerYAxis - 1) || (x == 0 || x == dotsPerXAxis - 1) ? 1 :
dotsPerZAxis - 1) {
results.add(new DecimalVector3D(
x,
y,
z
));
}
}
}
}

Related

Adding multiple terms to the For Statement

My question is why output is 0 - 4? But not 0 - 9?
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
also next code doesn't compile?
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
Could you explain why?
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
The above prints 0 to 4 since x < 5 && y < 10 can only be true when both inequalities are true. So as soon as x == 5, the loop exits.
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
The above doesn't compile because you have already defined x as an int. The first part of the loop tries to redefine it as a long
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
The condition is not true when x == 5, x <5 && y <10, your condition is that x is less than 5, and y is less than 10, so the condition is not true when x == 5, if you change the condition to || will output 0-9
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
This does not compile because the x value has been defined, regardless of the type.The reason for this kind of error is because Java itself is not allowed, "Java designers think that doing so will cause program confusion"

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;
}
}

Is it possible in an "if-statement" to execute the actual statement if the condition is false?

I encountered the code example below in the JavaNotes book. The answer was that after execution the x is equal to 2.
My question is how exactly does this work?
I see it is not an if-else flow, but in the second "if" the boolean expression is false, so X shall not obtain value 2.
How is this happening?
int x;
x = -1;
if (x < 0)
x = 1;
if (x >= 0)
x = 2;
Try rubber duck debugging! Read the comment in the code so you can understand how your code work :
int x;
x = -1;
if (x < 0) { //-1 < 0 = true
x = 1; //enter here -> change x = 1
}//end of the first if
if (x >= 0) {//1 >= 0 = true
x = 2; //enter here -> change x = 2
}//end of the second if
System.out.println(x);//result is 2
If you expect x = 1 then your code should look like this :
if (x < 0) { //-1 < 0
x = 1; //enter here -> change x = 1
} else if (x >= 0) {//1 >= 0
//^^^^------------------------------------------note the else
x = 2; //enter here -> change x = 2
}
x = -1;
first if: x < 0 is true,
so x gets new value 1
second if: x > 0 is true
so x gets new value 2
x = 2;

Android Minesweeper algorithm

I'm developing a game "Minesweeper" for Andoid on Java and I have a problem when opening the cells. How to make sure that I click on the cell opened adjacent empty cells? (How it is done in Miner for Windows).
Introduction: I have an array which i receive from bluetooth socket stream. Array like this :
1 9 1 0
1 1 1 0
0 0 0 0
0 0 0 0
9-is a mine
0-is blank cell
1-nearest mines count
After that i calculate game field
array = Model.getGameField();
int size = array.length;
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
{
((TableRow) table.getChildAt(i)).getChildAt(j).setTag(array[i][j] + "");
}
OnClick function :
if (iWantToSetFlag == 0)
{
tmpBtn = ((Button) v);
if (!(tmpBtn.getTag().equals("9")))
{
OpenButtons(tmpBtn.getId() / 10, tmpBtn.getId() % 10);
recreateTable();
}
else
startLose();
}
else
{
if (((Button) v).getText().equals("M"))
((Button) v).setText("");
else
((Button) v).setText("M");
}
I have a function
private void OpenButtons(int x, int y)
{
array[x][y] = -1;
for (int k = -1; k < 2; k++)
{
for (int k1 = 1; k1 >= -1; k1--)
{
if (x + k >= 0 && x + k < array.length && y - k1 >= 0 && y - k1 < array[x + k].length)
if (array[x + k][y - k1] == 0)
OpenButtons(x + k, y - k1);
}
}
}
which recursively open cells but i have a StackOverFlow error. Help please.
You should be calling your recursion with changed parameters:
if (array[x + k][y - k1] == 0)
OpenButtons(x + k, y - k1);
And of course, as has been mentioned in the comments of the question, you should check for the array bounds yourself instead of just ignoring the exceptions:
if (x + k >= 0 && x + k < array.length &&
y - k1 >= 0 && y - k1 < array[x + k].length) { ...
put before your other if-clause will only check fields which actually exist. Ridding you of your malicious empty try-catch.
Since the recursive algorithm will still cause a StackOverflowException for large fields, an iterative algorithm might be better suited here.
private void OpenButtons(int x, int y) {
Queue<Point> toOpen = new LinkedBlockingQueue<>();
toOpen.add(new Point(x, y));
array[x][y] = -1;
while (!toOpen.isEmpty()) {
Point p = toOpen.poll();
x = p.x;
y = p.y;
for (int k = -1; k < 2; k++) {
for (int k1 = 1; k1 >= -1; k1--) {
if (x + k >= 0 && x + k < array.length && y - k1 >= 0
&& y - k1 < array[x + k].length)
if (array[x + k][y - k1] == 0) {
array[x + k][y - k1] = -1;
toOpen.add(new Point(x + k, y - k1));
}
}
}
}
}

Android collision detection implementation

I am making a space shooting game for Android and I am experiencing some problems with the collision detection between enemy and player ship(// Detect enemy collision with player). I 'ld appreciate if anyone can help me with the best way to approach this. check out my detectCollisions() below:
**private void detectCollisions(){
// Detect enemy collision with player; to be revised
for (int z = 0; z < SSEngine.TOTAL_INTERCEPTORS + SSEngine.TOTAL_SCOUTS + SSEngine.TOTAL_WARSHIPS - 1; z++ ){
if(enemies[z].posX <= SSEngine.playerBankPosX
&& enemies[z].posY <= SSEngine.playerBankPosX )
{
player1.applyDamage();
}
}
//Detect Player's fire
for (int y = 0; y < 3; y ++)
if (playerFire[y].shotFired){
for (int x = 0; x < SSEngine.TOTAL_INTERCEPTORS + SSEngine.TOTAL_SCOUTS + SSEngine.TOTAL_WARSHIPS - 1; x++ ){
if(!enemies[x].isDestroyed && enemies[x].posY < 4.25 ){
if ((playerFire[y].posY >= enemies[x].posY - 1
&& playerFire[y].posY <= enemies[x].posY )
&& (playerFire[y].posX <= enemies[x].posX + 1
&& playerFire[y].posX >= enemies[x].posX - 1)){
int nextShot = 0;
enemies[x].applyDamage();
playerFire[y].shotFired = false;
if (y == 3){
nextShot = 0;
}else{
nextShot = y + 1;
}
if (playerFire[nextShot].shotFired == false){
playerFire[nextShot].shotFired = true;
playerFire[nextShot].posX = SSEngine.playerBankPosX;
playerFire[nextShot].posY = 1.25f;
}
}
}
}
}
}**
Your check in if is wrong-
if,
playerFire[y].posY >= enemies[x].posY - 1 is true
how come
playerFire[y].posY <= enemies[x].posYwill hold true?
And you are ANDING true && false, so it will always be false.

Categories