How to output game properly [closed] - java

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.

Related

What is the incrementation process in recursion? [closed]

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 years ago.
Improve this question
As I was working through practice problems I came across this one where I am not sure how I got the wrong answer. I tried increasing the value of "i" by 1 and printing that value. This should print the output "5 6 7" but for some reason does not. I tried to google my answer but can't phrase the question correctly or get good results.
//assuming that the value of 'i' is 4
public static void test(int i) {
if (i < 8) {
test(i + 1);
System.out.print (i + “ “);
}
}
You call test before you print the value. If you originally invoke test with 5, the machine does this in pseudocode:
test(5)
test(5 + 1)
test(5 + 1 + 1)
test(5 + 1 + 1 + 1)
print 5 + 1 + 1 // 7
print 5 + 1 // 6
print 5
test is called; but before printing anything, it calls itself again with i + 1. This repeats until i is 8, and then it starts printing in the "most inside" function.
You should print first, and then call test again.
Since you are printing the value of i after the recursive call, the code will wait until the recursive function returns before printing the value. Which cause the number to be printed backward.
To prevent this, you might want to call the print statement before going into the recursive call.
public static void test(int i) {
if (i < 8) {
System.out.print (i + "");
test(i + 1);
}
}
You can test it with this anyfiddle

How do you add the value of one integer to another? [closed]

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 5 years ago.
Improve this question
I took it upon myself to make an RPG game, and already it's starting to confuse me a little.
In the fighting part of the game, I use (what is probably) a pretty bad system to register how much damage the enemy takes when you attack it.
int healthStatPlayer = 1;
Double damageTaken = 0.0;
Double maxHealth = (healthStatPlayer * 100.0);
Double playerHealth = (maxHealth - damageTaken);
...
Double attackDamageEnemy (attackStatEnemy * damageMultiplierEnemy);
...
{
if (actionCommand.equals("Attack"))
{
System.out.println("You dealt " + attackDamage + " damage to the enemy.");
damageTakenEnemy = (damageTakenEnemy + attackDamage);
//The above line is where the problem is, it's wrong - both with regards to code and to mathematics - because I don't know how to make it right
System.out.println("The enemy has " + enemyHealth + " health
left.");
}
}
I had it set up so that the current health of the enemy is its max health minus any damage taken. This meant that, to be able to alter the current health, I would have to add the value of the player's attack damage to the enemy's "damage taken."
I just want to be able to add the number value of attackDamage (in this case, 22.5) to the value of damageTakenEnemy (initially 0, since all enemies start with max health).
Your attackDamageEnemy variable is missing an = and you need to make sure to set the enemyHealth to enemyHealth - damageTakenEnemy otherwise the variable will never actually be affected by your damage values.
Just use the plus operator.
So it would be myVariable + myOtherVariable, you can also store this in a variable like this myStoreVar = myVar + myOtherVar.
If you want to increment a variable with something you can do myStoreVar += myVar, which will just plus myVar to myStoreVar.
In your case the answer would be damageTakenEnemy += attackDamage which is the same as damageTakenEnemy = damageTakenEnemy + attackDamage. You can clearly see here which to use, the shorter one.
You also have operators like -, *, / and %.

Java- Fantasy Sports Draft [closed]

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 5 years ago.
Improve this question
Having a bit of issue here:
Question(input): complete the function draftPick(). function takes two arguments: an array of integers teams representing the order of teams and how many spots they have to fill, and an integer i that determines which team in the order (zero-indexed) we want to know the last pick for.
Output: function should return an integer representing which player the ith team in the draft order (zero-indexed) will select with their last pick
Expected runtime: O(n)
sample input: lineArray = [5, 1, 2]
position = 2
sample output: 5
Explanation: call the tree teams A, B, C. The draft has team A picking first and trying to fill 5 roster spots, team B picking second and filling 1 roster spot, and team C picking last and filling 2 roster spots:
turn 1: A picks 1st best player
turn 2: B picks 2nd best player
turn 3: team C picks 3rd best player
turn 4: team A picks again and get 4th best player
turn 5: since team b has already filled all their roster spots, team C picks 5th best player (THUS THE ANSWER 5)
Attempt: trying to develop an algorithm - i see that the ith team for example in [5,1,2] this team has 2 spots to fill. we know that the order of play he picks depends on the number of players in the other elements. i tried traversing the array subtracting (-1) from each element from i=0 to i
Here's an algorithm that I made that suffices according to my testing:
int draftPick(int[] teams, int target) {
int targetTeam = teams[target];
int finalPick = 0;
for(int i = 0; i <= target; i++) {
int team = teams[i];
if(team > targetTeam) team = targetTeam;
finalPick += team;
}
for(int i = (target + 1); i < teams.length; i++) {
int team = teams[i];
if(team > targetTeam - 1) team = targetTeam - 1;
finalPick += team;
}
return finalPick;
}

generate random objects to get spawning for java [closed]

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 6 years ago.
Improve this question
I'm trying to generate a random double for this section of my assignment.
The question is
"Each week, each female Guppy who is 10 weeks old or older has a 25 percent chance of spawning".
I would like to generate a random double to determine if each female Guppy should spawn or not.
My code so far:
Random r = new Random();
if (isFemale == true && getAgeInWeeks() >= 10) {
//?
}
I don't see any reason to generate a random double according to your question. What you need is an integer ranging from 0 to 3 inclusive where each number account for 25% for the spawning.
Random r = new Random();
if (isFemale == true && getAgeInWeeks() >= 10) {
// Generate a random integer in [0,3]
// Since there is a 25% or 1/4 chance
if(Math.abs(r.nextInt()) % 4 == 1){
//Note that the 1 in the condition can be replaced by any
// integer in [0,3]
//Put spawning code here
}
}
Check out this link for more information on random:
Random (Java Platform SE 7
To generate a random double, you can look at this question, however, this problem can more easily be solved by generating random ints.
In your situation, generating an int between 0 to 3 is what you want because checking if it is 0 will be true 25% of the time (1 Value / 4 Possible values = 25%).
EDIT: If you would like to also generate a random number to see how many spawn a Guppy will have use threadLocalRandomInstance.nextInt(int bound); like before.
These constraints can be translated to code like this:
import java.util.concurrent.ThreadLocalRandom;
public class Test {
public static void main(String[] args) {
ThreadLocalRandom tlr = ThreadLocalRandom.current();
int num = tlr.nextInt(3 + 1); //Bound is exclusive so add 1.
int spawn;
if(num == 0) {
spawn = tlr.nextInt(100 + 1); //Again, bound is exclusive so add 1.
} else spawn = 0;
System.out.println("This guppy had " + spawn + " spawn.");
}
}
I use ThreadLocalRandom as it is more straightforward as supported by this answer.
If you are not using Java 1.7+, use Random#nextInt(int) instead as also shown by that answer.

error on one of my methods of my tictactoe game [closed]

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

Categories