How can I shorten many subtractions of the exact same nature in a while loop in Java? I feel like it's very redundant and there can definitely be a shorter way.
while (x != 0) {
if (x - 100 >= 0) {
x -= 100;
}
if (x - 50 >= 0) {
x -= 50;
}
if (x - 25 >= 0) {
x -= 25;
}
...
First of all, you don't need to subtract and compare to zero, instead just compare to the number you are subtracting.
while (x != 0) {
if (x >= 100) {
x -= 100;
}
if (x >= 50) {
x -= 50;
}
if (x >= 25) {
x -= 25;
}
...
Secondly, what you're asking is a case by case problem. You could shorten the code above like this:
int[] nums = {100, 50, 25};
while (x != 0) {
for (int num : nums) {
if (x >= num) {
x -= num;
}
}
}
Related
Currently I'm trying to implement heuristics for a 3D tic-tac-toe but it seems like my counter is way of it,f but I'm unsure where I've done wrong, I'm not gonna post all of the code since it's a lot, but here is a part:
public void countDiagonal(GameState gameState) {
/*
* yz-plane (negativ)
*/
int z;
for (int x = 0; x < GameState.BOARD_SIZE; x++) {
for (int y = 0; y < GameState.BOARD_SIZE; y++) {
z = y;
if (gameState.at(x, y, z) == myPlayer) {
myCounter++;
opponentCounter = 0;
}
if (gameState.at(x, y, z) == opponentPlayer) {
opponentCounter++;
myCounter = 0;
}
if (gameState.at(x, y, z) == Constants.CELL_EMPTY) {
emptyCells++;
}
}
evaluateBoard();
myCounter = 0;
opponentCounter = 0;
emptyCells = 0;
}
The evaluation is done here:
public void evaluateBoard() {
if (myCounter == 1 && emptyCells == 3) {
myScore = myScore + 5;
}
if (myCounter == 2 && emptyCells == 2) {
myScore = myScore + 10;
}
if (myCounter == 3 && emptyCells == 1) {
myScore = myScore + 20;
}
if (myCounter == 4) {
myScore = myScore + 1000;
}
if (opponentCounter == 1 && emptyCells == 3) {
opponentScore = opponentScore + 5;
}
if (opponentCounter == 2 && emptyCells == 2) {
opponentScore = opponentScore + 10;
}
if (opponentCounter == 3 && emptyCells == 1) {
opponentScore = opponentScore + 20;
}
if (opponentCounter == 4) {
opponentScore = opponentScore + 1000;
}
}
When I try to run it, I use alpha-beta prune, but it seems like the calculation are done horrbly wrong, when I use the value, I take myScore - opponentScore and I use an alpha-beta tree with depth 1, but even after only playing one move, I'm down -15 in points, as I'm a noob in java, im therefore asking for help, is there an obvious mistake in my way of trying to evaluate the board?
So I'm making a program that takes in a 2D array of 5x5, and lists all the characters around any given index of the array. For example, if I input list[1][1], it will give the indexes: [0][0], [0][1], [0][2], [1][0], [1][2], [2][0], [2][1] ,[2][2].
I can print out all the letters around the indexes except for the ones on the edges such as index [0][0]. I can't seem to figure out how to get past that.
private static void checkSurrounding(char[][] list, int x, int y) {
for(int dx = -1; dx <= 1; dx++) {
for(int dy = -1; dy <= 1; dy++) {
if(!(dx == 0 && dy == 0)) {
System.out.print(list[x + dx][y + dy]);
}
}
}
}
Your code is almost correct! You exclude the middle point here:
private static void checkSurrounding(char[][] list, int x, int y) {
for(int dx = -1; dx <= 1; dx++) {
for(int dy = -1; dy <= 1; dy++) {
if(!(dx == 0 && dy == 0)) {
System.out.print(list[x + dx][y + dy]);
}
}
}
}
The only thing you miss is to avoid getting out of bounds. Just make sure that you do not get out of bounds and it should work impeccably:
private static void checkSurrounding(char[][] list, int x, int y) {
for(int dx = -1; dx <= 1; dx++) {
if ((x + dx >= 0) && (x + dx < list.length)) {
for(int dy = -1; dy <= 1; dy++) {
if ((y + dy >= 0) && (y + yd < list[x + dx].length) && (!(dx == 0 && dy == 0))) {
System.out.print(list[x + dx][y + dy]);
}
}
}
}
}
How does a statement like this execute?
int x = 2, y = 3;
if (x != 0) if (x < 10) if (y < 10) {
x++;
y++;
System.out.printf("X and Y are: %d, and %d", x, y);
}
If it could be compiled, it would get executed exactly like this:
if (x != 0)
if (x < 10)
if (y < 1o) {
x++;
y++;
System.out.println(x, y);
}
However, it's not very readable. You could improve its readability by using logical operators or proper line breaks and indentation.
Like this:
int x = 2, y = 3;
if ((x != 0) && (x < 10) && (y < 10))
{
x++;
y++;
System.out.println(x, y);
}
It operates like a Nested if. Why dont you check all condtion in the first if using a conditional AND. This way you would not need others.
However its a good practice to have braces for each if. This way its more readable and less error prone when the code undergoes changes in the future
if () if() if() is just short hand
what it is really doing is
if(x != 0){
if(x < 10){
if(y < 1o){
x++;
y++;
System.out.println(x, y);
}else{}
}else{}
}else{}
this is similar to
if (x != 0) {
if (x < 10) {
if (y < 10) {
x++;
y++;
System.out.println(x, y);
}
}
}
in an alternate way you can write
if((x != 0) && (x < 10) && (y < 10)) {
x++;
y++;
System.out.println(x, y);
}
Just use the && operator
int x = 2, y = 3;
if (x != 0 && x < 10 && y < 10) {
x++;
y++;
System.out.println(x, y);
}
It operates like a nested if as said by Andy. Instead of using this write all the conditions in one if statement and use && operator.
They're nested. Basically, the curly brace is optional for one line statement blocks. I assume y < 1o should be y < 10, also your println looks suspicious.
if (x != 0) {
if (x < 10) {
if (y < 10) {
x++;
y++;
// System.out.println(x, y);
System.out.println(Integer.toString(x) + " " + y);
}
}
}
You could certainly combine those into a single if (and even one line) with an and like
if (x != 0 && x < 10 && y < 10) {
System.out.printf("%d %d%n", x++, y++);
}
Using Java Logical Operators will resolve your problem.
Click this link to learn more
http://www.cafeaulait.org/course/week2/45.html
So I have this piece of code
if (x >= gameView.getWidth()) { //if gone of the right side of screen
x = x - gameView.getWidth(); //Reset x
y = random.nextInt(gameView.getHeight());
xSpeed =
ySpeed =
}
but I need to get both xSpeed and ySpeed to choose between two values, either '10' or '-10' and only those two numbers, nothing in between.
Everywhere I've looked says use the random.nextInt but that also chooses from the numbers in between -10 and 10...
You may try using
xSpeed = (random.nextInt() % 2 == 0) ? 10 : -10;
ySpeed = (random.nextInt() % 2 == 0) ? 10 : -10;
Good luck
How about this:
if(random.nextBoolean()){
xSpeed = 10;
}
else xSpeed = -10;
What about this? Math.random() returns values between 0.0 (include) and 1.0 (exclude).
public class RandomTest {
public static void main(String[] args) {
int xSpeed = 0;
int ySpeed = 0;
if (Math.random() >= 0.5) {
xSpeed = -10;
} else {
xSpeed = 10;
}
if (Math.random() >= 0.5) {
ySpeed = -10;
} else {
ySpeed = 10;
}
}
}
Lets suppose your random.nextInt(gameView.getHeight()); distributes evenly between even and odd numbers, then you could write something like this:
y = random.nextInt(gameView.getHeight()) % 2 == 0 ? 10 : -10;
I wonder if it is possible to have minimal code for this:
for (int x = 1; x < 10; x++){
/*I want to replace this condition with (x%number == 0)
instead of writing out condition for every number, but
it did not work with for (int number = 1; number <= 3; number++)
in (x%number == 0), as it prints out every x and number
*/
if ((x%1) == 0 && (x%2) == 0 & (x%3) == 0){
System.out.println(success!);
}
}
I think
x % a == 0 && x % b == 0 && x % c == 0
is equalent to
x % (a * b * c) == 0
UPDATE
Multiplication is incorrect, you need to use LCM: x % lcm(a, b, c)
Have a look :
for (int x = 1; x < 10; x++){
boolean flag = false;
for(int num = 1; num <= 3; num++){
if ((x%num) == 0 ){
flag = true;
}else{
flag = false;
break;
}
}
if(flag){
System.out.println(x + " success!");
}
}
OUTPUT :
6 success!
I know the code is looking a little horrified but will work for any value of x and num
This is what you'd need to make a comp sci professor happy:
for (int x = 1; x < 10; x++){
boolean success = true;
for (int number = 1; number <= 3; number++) {
if ((x % number) != 0) {
success = false;
}
}
if (success) {
System.out.println("success!");
}
}
although note: (x % 1) is always 0.
This is what you'd need to make me happy, according to my rule of "avoid nested loops":
for (int x = 1; x < 10; x++) {
if (testNumber(x))
System.out.println(x + " success!");
}
}
private static boolean testNumber(int x) {
for (int number = 1; number <= 3; number++) {
if ((x % number) != 0) {
return false;
}
}
return true;
}