Whenever my game hits a score of 3, it crashes. After much reviewing, I noticed that it has to do with this code because I would remove it and then it wouldn't crash anymore. The weirdest part about it, is that it would only crash after playing for a while. I can play a new game, it would be more than 3 and not crash. I don't understand why it only happens once in a while The error code I then get is:
Assertion failed: (m_nodes[proxyId].IsLeaf()), function DestroyProxy, file /Users/badlogic/jenkins/workspace/libgdx-mac/gdx/jni/Box2D/Collision/b2DynamicTree.cpp, line 127.
Code:
int j = Asteroids.bodies.size;
if(j == 9 && p == 0) {
score ++;
p ++;
store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
}
if(j == 10 && p == 1) {
score ++;
p ++;
store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
}
if(j == 11 && p == 2) {
score ++;
p ++;
store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
}
try {
if(j > 11) {
for(int i = 0; i < Asteroids.bodies.size - 11; score ++) {
world.destroyBody(Asteroids.bodies.get(i));
Asteroids.bodies.removeIndex(i);
store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
}
}
}
catch(NullPointerException e) {
System.out.println("oops");
}
}
Why is it related to the score: because you only start running the code when asteroids.bodies.size>11 - this presumably only happens after you've shot something up.
In general it is not safe to remove items from a collection that are currently iterating over - without the declaration of asteroids.bodies it is difficult to see whether this is causing your problem. But something along the lines of:
while (there are more bodies than I want)
remove the first body in the list
is generally used for this situation.
Related
I'm a total begginer in java and I'm having some trouble at understanding how things work... could someone explain to me why the computer understands "i" as horizontal row and "j" as vertical row since both "for" loops are the same, just with different variables?
public class DiagonalStar {
public static void printSquareStar(int number) {
if (number < 5) {
System.out.println("Invalid Value");
} else {
for (int i = 1; i <= number; i++) {
for (int j = 1; j <= number; j++) {
if ((i == 1) || (j == 1) || (i == number) || (j == number) || (i == j) || (j == number - i + 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
It's actually not a matter of vertical or horizontal, is based on the order the lines of code are executed.
For example:
for(int n=0;n<10;n++)
{
System.out.println(n);
}
Would print
0
1
2
3
4
5
6
7
8
9
But if you put another loop inside it, it will execute it before passing to the next loop of n.
for(int n=0;n<10;n++)
{
for(int m=10;m<15;m++)
{
System.out.println(n + "." + m);
}
}
That would print
0.10
0.11
0.12
0.13
0.14
0.15
All that before getting to 1.10, 1.11, etc...
So when you're printing the "*" you're just looping in that logic, and whenever you complete the inner for you use println (that prints the next line)
I would suggest messing with the variables, see what the program outputs when you switch i with j or when you change the conditions.
Good luck!
You have nested one for loop into another. This means that for each value of i you are going thru all values of j.
After inner loop you have System.out.println() and this moves you to another row.
println() - prints text with a newline
print() - just prints text
System.out.print prints in a row, while System.out.println prints in a column
I tried searching stackoverflow for answer but i could not find it. i figured out when i remove part of the program that divides numbers:
if(znak == 0) {
Resenje = x + y;
}else if(znak == 1) {
Resenje = x - y;
}else if(znak == 2) {
Resenje = x/y;
} else if(znak == 3) {
Resenje = x*y;
}else {
System.out.println("Greska u programu");
}
that error does not appear. i think the problem maybe if number is float but it is stored in int... Thank you, if you need any additional information im here to provide it.. :)
PS code is messy because i made it long time ago.. sorry, i cant figure out how to properly format it, i posted it on pastebin, i hope you dont mind it.. :)
https://pastebin.com/sfG9JEbR link for code
while(vece = true) {
// System.out.println(Odabir1);
x = random.nextInt(Odabir1);
// System.out.println(x);
y = random.nextInt(Odabir1);
// System.out.println(y);
//if(x == (int)x) {
if(x-y >= 0 && x+y <= Odabir1 && x+y!=0 && x-y!=0 && x/y >= 0 && x/y == (int)x && x*y >= 0 && x*y <= Odabir1) {
System.out.println(x + " " + y);
break;
}
}
when i try adding && y == 0; i get same error in every possibility
I am making a program that checks to see if an elements positive and negative charges are able to combine to make 0. A thing i want to do is output the reasons why the two elements are not able to combine. But it is more difficult than i expected. for example if sodium were trying to combine with copernicium, it would output this:
Sodium doesn't combine with Copernicium:
Both valence charges have same polarity.
One or more elements is man-made.
but i can not think of a way to implement this into my code.
here is my code:
public void combine(Element element){
if ((element.getValence() > 0 && valence < 0) || (element.getValence() < 0 && valence > 0)) { //one element needs a positive valence, and one needs a negative valence
if (valence != 0 && element.getValence() != 0) { //checks to see if valence is not equal to 0
if (natural == true && element.isNatural() == true) { //checks to see if both elements are natural
for (int x = 1; x <= 4; x++) {//bruteforce the atoms to see if they both add up to 0.
for (int y = 1; y <= 4; y++) {
if ((valence * x) + (element.getValence() * y) == 0) {
System.out.println(name + " combines with " + element.getName() + " to form " + symbol + "" + x + "" + element.getSymbol() + "" + y);
}
}
}
}
}
}
}
Thanks for any help!
The way to do this is to add else clauses for each if that return an appropriate message.
if ((element.getValence() > 0 && valence < 0) || (element.getValence() < 0 && valence > 0)) { //one element needs a positive valence, and one needs a negative valence
{
// the inner tests
}
else
{
System.out.println("The elements are both positive or both negative");
}
}
This should get you started in the right direction.
public Action getMove(CritterInfo info) {
count++;
Direction d = info.getDirection();
if (count < 100) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else {
return Action.RIGHT;
}
}
if (count >= 100) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else if (count / 100.0 < 2.0 && count / 100.0 >= 1.0 && !(d == Direction.EAST)) {
return Action.LEFT;
} else if (count / 100.0 < 3.0 && count / 100.0 >= 2.0 && !(d == Direction.WEST)) {
return Action.RIGHT;
} else {
return Action.HOP;
}
}
return Action.INFECT;
}
Right now I have this code that is part of my critter and i'm having problems are the if (count >= 100) part of the code. I can't get my go east and go west code to repeat itself because when I divide count by 100.0, it only works up until 299 then it just stays going west running into the wall. I've tried to set an else if statement after my go west code stating
} else if (count == 299) {
count = 0;
}
but this didn't solve my problem either. Any ideas? I just want my critter to sweep east and west over and over again.
Just use variables instead of your numbers, and change them each time their numbers are reached.Then you can create a method that changes the direction each time the number reaches 100+(100*n).
So, if you reach 200, it will check that the condition I set above is true and will therefore change directions for the next 100 numbers.
Was this what you were looking for , or did I misunderstand what you wanted?
You can use some kind of "cyclic" function like modulo (%) instead of the absolute value of count. E.g.
public Action getMove(CritterInfo info) {
count++;
Direction d = info.getDirection();
if (count < 100) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else {
return Action.RIGHT;
}
}
else {
int choiceOfAction = (count - 100)%200;
if (0 <= choiceOfDir && choiceOfDir < 100 && !(d == Direction.EAST)) {
return Action.LEFT;
} else if (100 <= choiceOfDir && choiceOfDir < 200 && !(d == Direction.WEST)) {
return Action.RIGHT;
} else {
return Action.HOP;
}
}
}
The line int choiceOfAction = (count - 100)%200; will yield a value of choiceOfAction with:
if count is in [100, 200[ : choiceOfAction is in [0, 100[
if count is in [200, 300[ : choiceOfAction is in [100, 200[
if count is in [300, 400[ : choiceOfAction is in [0, 100[
etc.
Note that I removed the last return in your method that was never reached, and also, that in the case above you will HOP only when you reach the limit and change direction.
So I got stuck with a whole lot of this:
package test;
import javax.swing.*;
import java.util.Random;
import java.util.Scanner;
public class GuessGame {
public static void main(String[] args)
{
Scanner keys = new Scanner(System.in);
System.out.println("Hello! Would you like to play?");
String choice = keys.next();
for (choice.equals("y"); choice.equals("yes");)
{
System.out.println("Awesome!");
choice = "";
Random bill = new Random();
int j;
j = bill.nextInt(50);
System.out.println("Guess what the number I'm thinking is ");
int number;
number = keys.nextInt();
for (number <= (j + 10); number >= (j - 10);)
{
System.out.println("Warm!");
number = 0;
number = keys.nextInt();
}
for (number = (j + 5); number == (j - 10);)
{
System.out.println("Hot!!!");
number = 0;
number = keys.nextInt();
}
}
for (choice.equals("n"); choice.equals("no");)
{
System.out.println("okay");
keys.close();
System.exit(0);
}
}
}
On the line with " for (number <= (j + 10); number >= (j - 10);)", I'm getting an error on the "<=", and I've got no idea how to make amends on it. As well, I'm not sure if I should be using the for statement for this. Please help me understand my mistake, and if there is a better alternate than for.
Thank you!
That is because the first parameter of the for statement is used for initialization of the variable, thus giving you an error.
documentation:
for (initialization; termination;increment) {
statement(s)
}
problem:
for (number <= (j + 10); number >= (j - 10);)
solution:
use an if statement if you are going to check both variable
if(number <= (j + 10) && number >= (j - 10))
Try replacing each of your for loops with if statements:
if (choice.equals("y") || choice.equals("yes") {
...
if (number <= (j + 5) && number >= (j - 5)) { ... } // I assume this is what you meant here
else if (number <= (j + 10) && number >= (j - 10)) { ... }
...
}
else {
System.out.println("okay");
keys.close();
System.exit(0);
}
Note that you want your most restrictive if statement first, so I changed the order for the part where you are checking if it is within certain ranges. This way it checks whether you are "hot" before checking whether you are "warm" (since hot is contained in warm).
Also note that the else if statement for checking "warm" means that if you are hot, it will not bother checking if you are warm and updating your output twice.
I also replaced the "no" case with just an else statement, which will catch any answer that is not "y" or "yes".