Below is my code that detects collision between two objects, setting a boolean to true every time my player collides with the wall:
private boolean collide(){
for(int i = 0; i < handler.object.size(); i++){
if(this.getBounds().intersects(handler.object.get(i).getBounds()) && handler.object.get(i).getId() != ID.Player && handler.object.get(i).getId().equals(ID.Wall)){
System.out.println("COLLIDEEEEE");
return true;
}
}
return false;
}
My intent is to have the player stop moving upon collision.
Right now my Code for the player Movement looks like this:
public void tick() {
if (Var.A == true && Var.ableToMove == true) {
Var.OffsetX += Var.speed;
}
if (Var.D == true && Var.ableToMove == true) {
Var.OffsetX -= Var.speed;
}
What i now want to do is, I want the player stop upon colliding the wall.
I would really appreciate if anyone of you could help me.
if(this.getBounds().intersects(handler.object.get(i).getBounds())
seems fine to detect collision between this (the player) and other elements.
but this doesn't seem fine :
handler.object.get(i).getId() != ID.Player && handler.object.get(i).getId().equals(ID.Wall)){
1) It is not consistent.
The first comparison compares the reference of the id objects :
handler.object.get(i).getId() != ID.Player
The second one compares the id objects according to the equals() method :
handler.object.get(i).getId().equals(ID.Wall))
You should use the same way in both cases.
You don't specify if you use String for id but Strings should be compared with equals() as a general way.
2)You should not need to make two comparisons.
Assuming that ID.Player and ID.Wall are two distinct values, if handler.object.get(i).getId().equals(ID.Player) is true, it means that
handler.object.get(i).getId().equals(ID.Wall) is false.
handler.object.get(i).getId().equals(ID.Wall) should be enough.
Related
So this program determines the symmetry of a btree.
What is confusing me is that checkSymmetric is called twice in the same line. So wouldnt that mean we have to add two new stack frames to our callstack for each call to checkSymmetric? If thats the case shouldnt we have O(2^h) space complexity?
public static boolean isSymmetric(BinaryTreeNode<Integer> tree) {
return tree == null || checkSymmetric(tree.left, tree.right);
}
private static boolean checkSymmetric(BinaryTreeNode<Integer> subtree0,
BinaryTreeNode<Integer> subtree1) {
if (subtree0 == null && subtree1 == null) {
return true;
} else if (subtree0 != null && subtree1 != null) {
return subtree0.data == subtree1.data
&& checkSymmetric(subtree0.left, subtree1.right)
&& checkSymmetric(subtree0.right, subtree1.left);
}
// One subtree is empty, and the other is not.
return false;
}
Note that we'll help, but we won't actually do your homework for you.
What is confusing me is that checkSymmetric is called twice in the same line. So wouldnt that mean we have to add two new stack frames to our callstack for each call to checkSymmetric?
No, because the two calls are sequential, not parallel. All resources scoped to the execution of one call are, by definition, released before the second call -- they are not all held at the same time. That certainly includes all the stack frames involved.
If thats the case shouldnt we have O(2^h) space complexity?
It is not the case, so what does that tell you about the space complexity?
I have some code that I would like to make more efficient by recursion. Trouble is I don't know where to start. The code compares two arraylists a and b to see if they are equal. Assume the sizes of both arrays are equal.
The code is
public boolean isEqual(A B) {
boolean answer = false;
if (lessThanOrEqualTo(B) == true);
for (int i = 0; i < DList.size(); i++) {
if (DList.get(i) == B.DList.get(i)) answer = true;
else answer = false;
}
return answer;
}
I have currently written
public boolean isEqualRecursion(A B) {
if DList.size() == 0;
return false();
} else {
}
I know the stopping case is 0 as when size is 0 nothing happens. I have no idea what to write next
Any help will be appreciated
Thanks
I have some code that I would like to make more efficient by recursion.
It is unlikely that you can make it more efficient by recursion. The chances are that it will be less efficient, and also fragile. This is because standard Java compilers don't implement tail-call optimization. The fragility occurs because a recursive comparison algorithm is liable to trigger a stack overflow if the input arrays are large enough.
However, if you want to continue with this as "an exercise", then my HINT is to add an index argument to the isEqualRecursion signature ...
I think that this is a pretty good start for you. This looks through all your elements, assuming they are an array, and then checks if they are equal in size.
public boolean isEqual(ArrayList<?> a, ArrayList<?> b) {
if (a.size() != b.size())
return false;
for (int i = 0; i < a.size(); i++) {
if (!isEqual((ArrayList<?>)a.get(i), (ArrayList<?>)b.get(i))) {
return false;
}
}
return true;
}
Now a couple of things to consider:
This assumes that the content of a(and b) must be an ArrayList at line (ArrayList<?>)a.get(i) what if our ArrayList actually contains something else, like an Integer?
What if our array lists contain null as an item?
What if we pass in two null ArrayLists? (or even just one?)
I'm not sure the point of your function lessThanOrEqualTo(B) is this part of the question or did you write this down wrong?
Also what is a DList?
This is a typical recursion question. You might want to try something like this:
int x = 0;
if(Dlist.get(x) != B.Dlist.get(x)) {
return false;
} else {
x+1;
}
if( x!= dList.size()) {
recursion;
}
return true;
So I am making a game where there are waves of enemies. The Wave class contains an update method that updates all the enemies in an arraylist of enemies contained in the Wave class. The Wave class also has a boolean called beat that decides whether or not the player has beaten the current wave. I am now have been trying however to start the next wave after the player beats the first. All waves in the arraylist start out with their beat variable as true except for the first. There are currently only two waves. I do not know why this is not working. Thank You for any help.
for(int i = 0; i < 1;i++)
{
if(!w.get(i).beat)
w.get(i).update(g2d);
else if(w.get(i).beat)
{
if(i-1 != -1)
{
if(w.get(i-1).beat && w.get(i).beat)
{
w.get(i).beat = false;
}
}
}
}
Your loop will increment i to the next wave after setting the current wave's beat setting to false, and miss calling the update method for that case. It looks like you should either call its update method immediately after setting beat = false, or perform the if test in the opposite order like this:
for(int i = 0; i < numWaves;i++) // upper range should be the number of waves
{
if(w.get(i).beat)
{
if(i>0) // this can be simplified to "if (i>0)"
{
if(w.get(i-1).beat) // no need to check w.get(i).beat here
{
w.get(i).beat = false;
}
}
}
else
w.get(i).update(g2d);
}
I don't know why you'd initialize a wave's beat state to true then set it to false when its turn comes. Why not just initialize all to false since they really haven't been beat yet?
I'm not sure that I understand your code but I can tell you 2 things. First of all, your loop never loops because as soon as the index is 1, it ends without executing the code a second time. Secondly
if(i-1 != -1)
{
if(w.get(i-1).beat && w.get(i).beat)
{
w.get(i).beat = false;
}
}
is always false due to what I said.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I made a timer which checks if my twitch.tv stream is offline or not. the method get executed all 30 seconds.
If my stream is online the bot will automatically connect to my Stream.
If my Stream if offline there's an INT which will go +1 all 30 seconds and if it reaches 10 which are 5 minutes the bot should just part the IRC channel.
but somehow, the bot continues on counting. Did i made any faults with my if/else statements?
As soon my Stream is offline, bot starts counting all 30 seconds +1 as it should. but when it reaches 10 it just goes higher... its now at 30 already for example but i'm not sure why it doesn't part the channel.
Here is my function;
public void livemodetimer() {
if(livemode == "off" && TwitchStatus.isstreamlive){
livemode = "on";
}else
if(livemode == "on" && TwitchStatus.isstreamlive == false){
zaehler = zaehler+1;
System.out.println("Stream Offline Counter. Disconnect bei (10). Aktuell:"+zaehler);
}else
if(livemode == "on" && TwitchStatus.isstreamlive == true){
zaehler = 0;
if(zaehler >= 10){
livemode = "off";
zaehler = 0;
}
if (TwitchStatus.isstreamlive && livemode == "on" && multistartprepare == false){
joinChannel("#"+YBot.MyBot.ownerchannel+"");
multistartprepare = true;
startup();
}
if(TwitchStatus.isstreamlive == false && livemode == "off" && multistartprepare == true){
sendMessage("#"+YBot.MyBot.ownerchannel+"","Da der Stream Offline ist verzieh ich mich mal =) Bye!!");
partChannel("#"+YBot.MyBot.ownerchannel+"");
multistartprepare = false;
zaehler = 0;
TTmsg.cancel();
TTmsg.purge();
}
}
Does anyone have an idea why it doesn't call the partchannel stuff when it reaches 10.
Edited Version Below:
public void livemodetimer() {
if(livemode == false && TwitchStatus.isstreamlive){
livemode = true;
}else
if(livemode && TwitchStatus.isstreamlive == false){
zaehler = zaehler+1;
System.out.println("Stream Offline Counter. Disconnect bei (10). Aktuell:"+zaehler);
}else
if(livemode && TwitchStatus.isstreamlive == true){
zaehler = 0;
}
if(zaehler >= 10){
livemode = false;
zaehler = 0;
}
if (TwitchStatus.isstreamlive && livemode && multistartprepare == false){
joinChannel("#"+YBot.MyBot.ownerchannel+"");
multistartprepare = true;
startup();
}
if(TwitchStatus.isstreamlive == false && livemode == false && multistartprepare){
sendMessage("#"+YBot.MyBot.ownerchannel+"","Da der Stream Offline ist verzieh ich mich mal =) Bye!!");
partChannel("#"+YBot.MyBot.ownerchannel+"");
multistartprepare = false;
zaehler = 0;
TTmsg.cancel();
TTmsg.purge();
}
}
You have several problems.
You don't have an ending brace for your statement
if(livemode == "on" && TwitchStatus.isstreamlive == true){
zaehler = 0;
It's hard to tell where you want the block closed, partly because your code uses tabs to indent, which doesn't get displayed very well here (also, don't mix tabs and spaces, your code editor should have a way to convert one to the other). Using consistent indentation helps me to keep from getting confused about nesting of braces.
Because the indentation is unclear I'm not sure what you want the else clauses to do, it would be better to use braces to explicitly show the blocks that you mean to be executed. Using a consistent formatting scheme like One True Brace style would be a big help here.
Basically pick a consistent way to format your code that is very explicit and stick to it, and you will have an easier time.
Using == for string comparison is bad (because it compares references, you could have 2 separate references both holding "on", and == would return false). You should use the equals method instead because it will compare the values.
Also using == true for boolean comparisons is redundant. It doesn't hurt anything, it's just awkward-looking.
You are comparing strings with == when they need to be compared with equals
if(livemode == "on")
does not test if the value in livemode is equal to "on". Rather, it tests if these two objects are at the same physical memory location, which is most definitely what you do not want, since it is obviously possible for two strings in two different objects to contain the same value.
Change it to
if(livemode.equals("on")
Your if-statement String comparison should use .equals()
String is an Object type in Java meaning it needs to call an .equals() function in order to properly compare it to other Strings. The == operator, if I'm not mistaken (I often am), will compare the memory locations of the Object and report false when the comparison fails.
if ( livemode.equals("on") && ...
should fix your issues.
The project is to code a maze solver in Java using recursion and a tree (I'm using my own linked list, not exactly sure if it's a tree but I don't mind that).
The lecturer never explains anything, so I get all my knowledge online. I'm having trouble with my recursive method, I'm not sure what to do since I can't find an example that can relate to my project
In my linked list, I have links to the node on the right, left, bottom and top. If there is for example a wall on your right, the link would be null. I also have booleans in the linked list wallRight, wallLeft, wallBottom and wallTop to see if there is for example a wall to the right. So if there was a wall to the right, the "Right" link would be null and wallRight would be true.
I also use Labels which are images, so if I landed on a spot, an image shows. I made a method that if I'm on position 1, it makes label 1 display, so in the recursive methods I use the int pos to know which label to display.
Now comes the trouble with my recursive method. I have tried it two ways, but neither work. Here is both of them:
public boolean move(Maze rigting,int pos) // Righting = direction
{
if(rigting.goal == true)
return true; //BASE CASE - tests if it is on the goal node
else if(rigting.wallR != true) //checks if there is a wall on the right
{
pos += 1;
move(rigting.right, pos); //moves right
showLabel(pos);
return true;
}
else if(rigting.wallD != true) //checks if there is a wall below
{
pos += 10;
move(rigting.down, pos); //moves down
showLabel(pos);
return true;
}
else if(rigting.wallL != true) //checks if there is a wall on the left
{
pos -= 1;
move(rigting.left, pos); //moves left
showLabel(pos);
return true;
}
else if(rigting.wallU != true) //checks if there is a wall above
{
pos -= 10;
move(rigting.up, pos); //moves up
showLabel(pos);
return true;
}
return false; //I know this return is incorrect, but it won't run without one
}
public boolean move(Maze rigting,int pos) //Righting = direction
{
if(rigting.goal == true)
return true;
return (rigting.wallR != true) ? move(rigting.right, pos += 1) : false ||
(rigting.wallD != true) ? move(rigting.down, pos += 10) : false ||
(rigting.wallL != true) ? move(rigting.left, pos -= 1) : false ||
(rigting.wallU != true) ? move(rigting.up, pos -= 10) : false;
}
Both of these give stackoverflow exceptions...
I think my error is that, if there is a wall on the right, then the link Right is null. If I could somehow make it that none of the links are null, then I wouldn't need the booleans wallRight etc, but I have no idea how to implement that.
I would really appreciate it if you could send me in the right direction! I only need to hand in the project on 10 October, so if I'm doing it completely wrong, I don't mind starting over!
Since this is your homework, I won't give you a solution here, but some hints.
Your first solution does not consider the result of subsequent calls to move(), therefore the recursion only ends, if you reached the goal (accidentially).
In your second solution you consider the result, but are not prepared for a case, where you are moving in a loop. You need to mark the nodes you already visited and break there (with return value false).
For recursion it's best to start with the break-condition (as you did), and then implement one simple case (e.g. always go right). After managed the simple case you can add others (branches)