Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I keep getting the following error.
The user can only enter a number from 1 to 9. And this is what my error shows:
hint: does getemptyspot return valid entered spot even with initial zero entry:
hint: does getemptyspot return valid entered spot even with initial 10 entry.
Also, if the board is full, my code should return -1. And if the spot is already taken, my code should return "That number is not available. Choose another from the numbered spots"
My code is as follows:
public int getEmptySpot()
{
System.out.print("Choose a number where you want your marker to go");
int spot = in.nextInt();
if(b.isAvailable(spot))
{
return spot;
}
if(spot == 0 || spot > 10)
{
System.out.println("That number is not available. Choose another from the numbered spots");
}
return -1;
}
may you post the detail of isAvailable()?
it looks like the code cannot reach the 2nd if() sub.
why use the different styles of the if()?
try like this:
if(spot >0 && spot <10){
return spot;
}else{
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
public static int flood(int x, int y) {
if(x<0||y<0||x>101||y>101||went[x][y]) return 0;
System.out.println(x + " " + y);
went[x][y] = true;
if(grid[x][y] == 1) return 1;
int result = 0;
result += flood(x+1,y);
result += flood(x,y+1);
result += flood(x-1,y);
result += flood(x,y-1);
return result;
}
The code never came back to the same coordinate, but it is still somehow crashing.
P.S. went is a 2d boolean array.
What you wrote is a recursive function, it calls itself.
At first sight, it looks ok, but it "expands very fast".
In the first call to flood(0,0), it will "stack" flood(1,0) which will then "stack" flood(2,0) ... which will then stack flood(100,100) and only at this point will the method return for the first time!
That means that the stack size is roughly 10000 before the stack (of methods to continue processing once the current one is done) starts to "shrink back" after that point.
The basics of your method is correct, and I suppose it will work for a small array size, it's just a too big stack for a default JVM.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
static int score = 0;
public static void score(){
score +=1;
OK this is the code that runs every time you score a point, my question is is there a way to record the highest score, so say you get a score of 10 i want to display that score in the top right with some text that says high score: . Also got one more question, just tried out the code and it works but is there a way to keep the numbers for the next time the game runs, whenever i close the application and then re-run it it resets the high score to 0, is there a way to keep it or not?
You just maintain another variable for max score and use that for display
static int score = 0;
static int maxScore=0;
public static void score(){
score +=1;
if(score > maxScore){
maxScore = score;
}
---
This maxScore variable gets update each time you change the score, based on the condition written.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a variable and I need to find the highest value that was there. That value constantly changes. I need to put that highest value in a textfield.
//This is full program. Posting the code won't help.
I will try to explain the program:
It's Heads or Tails. You press two buttons, heads and tails. And there is a combo JTextField, where it writes how many times in a row you get it right. And the highest value that was in Combo JTextField I need to put in the Highscore JTextField.
Just add another variable for the maximum:
SomeType currentValue, maximumValue;
//Encapsulate currentValue and maximumValue
void setValue(SomeType value){
currentValue = value;
//update the maximum
if(value.compareTo(maximumValue) > 0)
maximumValue = value;
}
SomeType getMaximum(){
return maximumValue;
}
SomeType getCurrent(){
return currentValue;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have been trying to fix my snakes and ladders code. For example if Player1's position on the gameboard is 98 and he rolls a 5 Player1's position should be 97. So,(100-98=2,(5-2)=3,100-3=97). I ahve been trying to implement this into my code in the possiblity that either player1 or player2 roll higher than 100.
MY CODE:
System.out.println (player1+" Rolled a " + P1Roll );
System.out.println (player2+" Rolled a " + P2Roll);
//If player1 position is greater than 100
if(P1Position+P1Roll>100){
P1=100-P1Position;
difference=P1Position-P1Roll;
P1Position=100-difference;
}
//If palyer2 position is greater than 100
else if(P2Position+P2Roll>100){
P2 = 100-P2Position;
difference=P2Position-P2Roll;
P2Position=100-difference;
}
System.out.println ("------------------------------------------------------------------------");
//calculate player positions
P1Position = P1Position + P1Roll;
P2Position = P2Position + P2Roll;
//call position methods
P1Position = Player1(P1Position, P1Roll, snakes, ladder, arrow);
P2Position = Player2(P2Position, P2Roll, snakes, ladder, arrow);
//Print out players current positions
System.out.println("==========================================================================");
System.out.println (player1+" is currently on square " + P1Position);
System.out.println (player2+" is currently on square " + P2Position);
System.out.println("==========================================================================");
According to your calculation it should be
difference=P1Roll-P1;
And for player 2:
difference=P2Roll-P2;
And try this more simple way for all cases:
P1Position = 100 - Math.abs(P1Position + P1Roll - 100);
Try removing the else after player 1, unless the methods are called after 1 player rolls.
(Another way of saying it: do both players roll, then you move them? If yes, remove the else. If player 1 rolls, and then you update, then keep the else. I don't see anything else wrong otherwise).
The else statement is treating the conditionals as such: "Is player 1's position plus roll greater than 100? If so, do this block, and that's it. If player 1's position plus roll is NOT greater than 100, try the next block". So, if player 1 does have to be "manipulated" using the if block, player two may never get the move to happen.
I hope my answer is not too confusing.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to remove a link in the location on the linked list that is passed to this method, however it is not working. I think my code is really bad, if the index is 0 then I think it will give me an error:
public void remove(int index)
{
DLink iterator=_firstLink;
int count=0;
while(iterator!=_lastLink)
{
iterator=iterator._next;
count++;
if(count>=index)break;
}
if(index==count)
{
iterator._previous = iterator._next;
_size--;
}
else
{
try
{
throw new Exception("IndexOutOfBoundException");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
The bug is that you count++; and then check if it is the greater or equal than the index after you have already moved the iterator.
The if(index==count) is not satisfied and you will always hit the else with the throw exception
Do something like:
while(iterator!=_lastLink && index != count){
count++;
iterator=iterator._next;
}
instead. This way for index equal 0 you don't go in the while loop and you can go in if(index==count)
You're getting the exception when index is 0 because of this code:
while(iterator!=_lastLink)
{
iterator=iterator._next;
count++;
if(count>=index)break;
}
Step through line by line with index = 0. You immediately increment the count to 1, then break. So when you hit the comparison index==count, it evaluates to 0==1 which is false. So, it goes to the else and throws the exception.
Try putting the if(count>=index)break; line first.
Also, you'll need to update your first / head references for 0, which is a special case.