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?
Related
I'm coding Conway's Game of Life on Processing 3 and I want to store x and y so that the squares don't change right away, but I don't know how to store it to use later. Any help is appreciated!
void keyPressed() {
for (int x = 0; x < 30; x++) {
for (int y = 0; y < 30; y++) {
int numNeighbours = numNeighbours(x,y);
if (cells[x][y] == true) {
if (numNeighbours > 3 || numNeighbours <= 1) { //underpopulation or overpopulation
}
}
else if (cells[x][y] == false) {
if (numNeighbours == 3) {
}
}
}
}
}
Based on my understanding of your code (and the Game of Life), you don't need to store x and y. What you actually need to do is to store the (x, y) pairs where the cell changes state.
You could do this by creating pairs and adding them to lists.
But a another idea is to use a second array representing the next generation of the game and put all of the new values there; e.g.
for (int x = 0; x < 30; x++) {
for (int y = 0; y < 30; y++) {
int numNeighbours = numNeighbours(x,y);
if (cells[x][y] == true) {
if (numNeighbours > 3 || numNeighbours <= 1) {
cellsNext[x][y] = false;
} else {
cellsNext[x][y] = true;
}
}
else if (cells[x][y] == false) {
if (numNeighbours == 3) {
cellsNext[x][y] = true;
} else {
cellsNext[x][y] = false;
}
}
}
}
Note: that could be simplified / written better, but I have written it as above so that you can see clearly what I have done.
I have made a sudoku game where the computer plays against the user(human) and need help implementing one part of it. I have tried everything!!
So I've done Mode 1 where the user selects moves for both the human and the computer using StdIn, but now I have to do mode 0 where the computer does a random move.
Here is my code so far:
public class Twoduko {
static int Board[][];
public static void main(String[] args) {
StdOut.print("Enter the height of the grid:");
int H = StdIn.readInt();
StdOut.print("Enter the width of the grid:");
int W = StdIn.readInt();
StdOut.print("Enter the mode:");
int M = StdIn.readInt();
generateBoard(H, W);
drawBoard(H, W);
playGameModeZero(H, W, M);
playGameModeOne(H, W, M);
playGameModeTwo(H, W, M);
}
public static void generateBoard(int H, int W) {
int tempBoard[][] = new int[H * H][W * W];
for (int i = 0; i < H * H; i++) {
for (int j = 0; j < W * W; j++) {
tempBoard[i][j] = 0;
}
}
Board = tempBoard;
}
public static void drawBoard(int H, int W) {
if (W == 2) {
System.out.println("+---+---++---+---+");
} else if (W == 3) {
System.out.println("+---+---+---++---+---+---++---+---+---+");
} else if (W == 4) {
System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+");
} else if (W == 5) {
System.out.println(
"+---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---+");
}
for (int i = 0; i < H * H; i++) {
for (int j = 0; j < W * W; j++) {
if (W == 2 && j == 2) {
System.out.print("||");
} else if (W == 3 && j == 3) {
System.out.print("||");
} else if (W == 3 && j == 6) {
System.out.print("||");
} else if (W == 4 && j == 4) {
System.out.print("||");
} else if (W == 4 && j == 8) {
System.out.print("||");
} else if (W == 4 && j == 12) {
System.out.print("||");
} else if (W == 5 && j == 5) {
System.out.print("||");
} else if (W == 5 && j == 10) {
System.out.print("||");
} else if (W == 5 && j == 15) {
System.out.print("||");
} else if (W == 5 && j == 20) {
System.out.print("||");
} else {
System.out.print("|");
}
System.out.print(Board[i][j] + " ");
}
System.out.print("" + "|");
System.out.println("");
if (W == 2 && H == 2) {
if (i == 1) {
System.out.println("+===+===++===+===+");
} else {
System.out.println("+---+---++---+---+");
}
}
if (W == 3 && H == 3) {
if (i == 2) {
System.out.println("+===+===+===++===+===+===++=== +===+===+");
} else if (i == 5) {
System.out.println("+===+===+===++===+===+===++=== +===+===+");
} else {
System.out.println("+---+---+---++---+---+---++---+---+---+");
}
}
if (W == 3 && H == 2) {
if (i == 1) {
System.out.println("+===+===+===++===+===+===++=== +===+===+");
} else {
System.out.println("+---+---+---++---+---+---++---+---+---+");
}
}
if (W == 4 && H == 4) {
if (i == 3) {
System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+");
} else if (i == 7) {
System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+");
} else if (i == 11) {
System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+");
} else {
System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+");
}
}
if (W == 4 && H == 2) {
if (i == 1) {
System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+");
} else {
System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+");
}
}
if (W == 4 && H == 3) {
if (i == 2) {
System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+");
} else if (i == 5) {
System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+");
} else {
System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+");
}
}
if (W == 5 && H == 2) {
if (i == 1) {
System.out.println(
"+===+===+===+===+===++===+===+===+===+===++===+== =+===+===+===++===+===+===+===+===++===+===+===+== =+===+");
} else {
System.out.println(
"+---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---+");
}
}
}
}
public static void playGameModeZero(int H, int W, int M) {
if (M == 0) {
boolean play = true;
boolean humanTurn = false;
boolean computerTurn = false;
int turn = 0;
if (turn % 2 == 0) {
System.out.println("Computer's turn");
}
if (turn % 2 == 0) {
computerTurn = true && humanTurn == false;
}
if (turn % 2 != 0) {
System.out.println("Human's turn");
}
if (turn % 2 != 0) {
humanTurn = true && computerTurn == false;
}
turn++;
while (play) {
if (humanTurn) {
int row = 0;
int column = 0;
int entry = 0;
StdOut.print("Enter row:");
row = StdIn.readInt();
StdOut.print("Enter column:");
column = StdIn.readInt();
StdOut.print("Enter your entry value:");
entry = StdIn.readInt();
if (entry <= 0 || entry >= (W * W + 1)) {
play = false;
System.out.println("Illegal move.");
}
if (row > W * W || column > H * H) {
play = false;
System.out.println("Illegal move. ");
}
for (int i = 0; i < H * H - 1; i++) {
if (Board[i][column] == entry) { // check row
play = false;
System.out.println("Illegal move.");
}
}
for (int j = 0; j < W * W; j++) { // check column
if (Board[row][j] == entry) {
play = false;
System.out.println("Illegal move.");
}
}
int subRow = 0;
int subCol = 0;
subRow = row / H;
subCol = column / W;
for (int i = 0; i < H; i++) { // check subgrid
for (int j = 0; j < W; j++) {
if (Board[i + H * subRow][j + W * subCol] == entry) {
play = false;
System.out.println("Illegal move.");
}
}
}
}
if (computerTurn) {
Board[(int) Math.random() * (W * W)][(int) Math.random() * (H * H)] = (int) (Math.random() * H * W)
+ 1;
}
}
}
}
public static void playGameModeOne(int W, int H, int M) {
if (M == 1) {
boolean play = true;
boolean humanTurn = false;
boolean computerTurn = false;
int turn = 0;
if (turn % 2 == 0) {
System.out.println("Computer's turn");
}
if (turn % 2 == 0) {
computerTurn = true && humanTurn == false;
}
if (turn % 2 != 0) {
System.out.println("Human's turn");
}
if (turn % 2 != 0) {
humanTurn = true && computerTurn == false;
}
turn++;
while (play) {
int row = 0;
int column = 0;
int entry = 0;
StdOut.print("Enter row:");
row = StdIn.readInt();
StdOut.print("Enter column:");
column = StdIn.readInt();
StdOut.print("Enter your entry value:");
entry = StdIn.readInt();
if (entry <= 0 || entry >= (W * W + 1)) {
play = false;
System.out.println("Illegal move.");
}
if (row > W * W || column > H * H) {
play = false;
System.out.println("Illegal move. ");
}
for (int i = 0; i < H * H - 1; i++) {
if (Board[i][column] == entry) { // check row
play = false;
System.out.println("Illegal move.");
}
}
for (int j = 0; j < W * W; j++) { // check column
if (Board[row][j] == entry) {
play = false;
System.out.println("Illegal move.");
}
}
int subRow = 0;
int subCol = 0;
subRow = row / H;
subCol = column / W;
for (int i = 0; i < H; i++) { // check subgrid
for (int j = 0; j < W; j++) {
if (Board[i + H * subRow][j + W * subCol] == entry) {
play = false;
System.out.println("Illegal move.");
}
}
}
if (play == true && turn % 2 == 0) {
System.out.println("Computer's turn");
}
if (play == true && turn % 2 != 0) {
System.out.println("Human's turn");
}
turn++;
Board[row][column] = entry;
if (play) {
drawBoard(W, H);
}
}
if (computerTurn && play == false) {
{
System.out.println("Human wins!");
}
if (humanTurn && play == false) {
System.out.println("Computer wins!");
}
}
}
}
So as you can see for Mode 0, I started by making an integer to alternate between the human and computers turns and if its the computer's turn the boolean computerTurn is true and vice versa. Except whatever I've tried, a random integer won't input in the grid I've drawn.
if (computerTurn) {
Board[(int) Math.random() * (W * W)][(int) Math.random() * (H * H)] = (int) (Math.random() * H * W)
+ 1;
}
So that is the part of the code which "should" input a random number (ie. if the grid is 2x2, then it'll be a random number from 1-4 and if its 3x3, it'll be a random number from 1-9. But whenever I run the code, it doesn't place a random number in my grid.
In the code of computerTurn,
(int) Math.random() * (W * W)
This line will always give result as 0.
Math.random() returns a double which varies from 0.0 <= Math.random() < 1.0 and when you do typecasting in int by from double by (int) Math.random() , It becomes 0.
Then whatever you do with it, It remains still 0.
So If you want to generate a random from 0 to W*W there, replace it with,
(int) (Math.random() * W * W)
These lines are wrong:
if (turn % 2 == 0) {
computerTurn = true && humanTurn == false;
}
if (turn % 2 != 0) {
humanTurn = true && computerTurn == false;
}
Here's what you meant to write:
if (turn % 2 == 0) {
computerTurn = true;
humanTurn = false;
}
if (turn %2 != 0) {
computerTurn = false;
humanTurn = true;
}
I am pretty new to java, I am working on implementing a pacman game, I initialized a ghost my using one image, and currently using one object in my game, but I want to have multiple images so I can have different colored ghosts. But when I initialized multiple images the just overlaps on another. Here's how I intialized the Ghost
private Image RedGhost;
private Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down;
here the code that I use to moveGhosts
private void moveGhosts(Graphics2D g2d) {
short i;
int pos;
int count;
for (i = 0; i < nrofghosts; i++) {
if (ghostx[i] % blocksize == 0 && ghosty[i] % blocksize == 0) {
pos = ghostx[i] / blocksize + nrofblocks * (int) (ghosty[i] / blocksize);
count = 0;
if ((screendata[pos] & 1) == 0 && ghostdx[i] != 1) {
dx[count] = -1;
dy[count] = 0;
count++;
}
if ((screendata[pos] & 2) == 0 && ghostdy[i] != 1) {
dx[count] = 0;
dy[count] = -1;
count++;
}
if ((screendata[pos] & 4) == 0 && ghostdx[i] != -1) {
dx[count] = 1;
dy[count] = 0;
count++;
}
if ((screendata[pos] & 8) == 0 && ghostdy[i] != -1) {
dx[count] = 0;
dy[count] = 1;
count++;
}
if (count == 0) {
if ((screendata[pos] & 15) == 15) {
ghostdx[i] = 0;
ghostdy[i] = 0;
} else {
ghostdx[i] = -ghostdx[i];
ghostdy[i] = -ghostdy[i];
}
} else {
count = (int) (Math.random() * count);
if (count > 3) {
count = 3;
}
ghostdx[i] = dx[count];
ghostdy[i] = dy[count];
}
}
ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
drawGhost(g2d, ghostx[i] + 1, ghosty[i] + 1);
//drawblueGhost(g2d, ghostx[i] + 1, ghosty[i] + 1);
if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12)
&& pacmany > (ghosty[i] - 12) && pacmany < (ghosty[i] + 12)
&& ingame) {
dying = true;
}
}
}
In summery all the Ghosts are currently Red, I would like it to be different colors
With your current code, you could modify the drawGhost method to take an identifier for the color of the ghost being drawn. You could then have another array that holds the identifier for each ghost's color.
However, since you are using Java I would recommend taking advantage of OOP and create a Ghost class that holds all of a ghost's properties (x position, y position, color, etc). You can then have a collection of Ghost objects that represent each individual ghost.
Here is an example:
Make a Ghost class that holds all the properties a ghost can have:
public class Ghost {
private String color;
private int x;
private int y;
private boolean alive;
// getters and setters. You could even move the logic for moving a ghost to this class
...
}
Then you would initialize the ghosts like this:
Ghost redGhost = new Ghost("Red");
Ghost greenGhost = new Ghost("Green");
Ghost pinkGhost = new Ghost("Pink");
You could store them in a set:
Set<Ghost> ghostSet = new HashSet<Ghost>();
ghostSet.add(redGhost);
...
Then your method can change to something like this:
private void moveGhosts(Graphics2D g2d) {
// Utilize an enhanced for loop
for(Ghost ghost : ghosts){
...
...
ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
drawGhost(g2d, ghost);
if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12)
...
...
}
}
Now the drawGhost method would look at the fields within the ghost class (by calling getter and setter methods) to determine the x and y to start drawing. The color field is used to determine which image to draw.
I have been working on a versions of Conway's Game of Life for school and I have run into a problem: cells are becoming dead or alive in the wrong places.
How can I fix that?
if (alive == 3 && aryBOARD[x][y] == 0) { //rule 4
aryCHANGE[x][y] = 1;
}
if (alive > 3 && aryBOARD[x][y] == 1) { //rule 3
aryCHANGE[x][y] = 0;
}
if (alive >= 2 && alive <= 3 && aryBOARD[x][y] == 1) { //rule 2
aryCHANGE[x][y] = 1;
}
if (alive < 2 && aryBOARD[x][y] == 1) { //rule 1
aryCHANGE[x][y] = 0;
}
if (dead > 5) { //rule check
aryCHANGE[x][y] = 0;
}
That is where I assume the problem is but if having the whole code would help:
package gameoflife2;
public class Game {
public static void main(String[] args) {
int[][] aryBOARD = new int[5][5];
int x = 0;
int y = 0;
int dead = 0;
int alive = 0;
int i, j;
// Board numbers
// 00011
// 00001
// 01000
// 01100
// 00000
aryBOARD[0][0] = 0;
aryBOARD[0][1] = 0;
aryBOARD[0][2] = 0;
aryBOARD[0][3] = 1;
aryBOARD[0][4] = 1;
aryBOARD[1][0] = 0;
aryBOARD[1][1] = 0;
aryBOARD[1][2] = 0;
aryBOARD[1][3] = 0;
aryBOARD[1][4] = 1;
aryBOARD[2][0] = 0;
aryBOARD[2][1] = 1;
aryBOARD[2][2] = 0;
aryBOARD[2][3] = 0;
aryBOARD[2][4] = 0;
aryBOARD[3][0] = 0;
aryBOARD[3][1] = 1;
aryBOARD[3][2] = 1;
aryBOARD[3][3] = 0;
aryBOARD[3][4] = 0;
aryBOARD[4][0] = 0;
aryBOARD[4][1] = 0;
aryBOARD[4][2] = 0;
aryBOARD[4][3] = 0;
aryBOARD[4][4] = 0;
// end of array
int[][] aryCHANGE = aryBOARD.clone(); // array change is equal to array
// board
// printing array
int rows = 5;
int colums = 5;
for (i = 0; i < rows; i++) {
for (j = 0; j < colums; j++) {
System.out.print(aryBOARD[i][j] + " ");
}
System.out.println("");
}
System.out.println("---------------------------");
// done printing array
// check for dead or alive cells
for (x = 0; x <= 4; x++) {
for (y = 0; y <= 4; y++) {
alive = 0;
dead = 0;
if ((x + 1 < 4) && (x + 1 > 0)) { // right
if (aryBOARD[x + 1][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y - 1 < 4) && (y - 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // bottom
// right
// corner
if (aryBOARD[x + 1][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
if (((y + 1 < 4) && (y + 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // top
// right
// corner
if (aryBOARD[x + 1][y + 1] == 0) {
dead++;
} else {
alive++;
}
}
if ((y + 1 < 4) && (y + 1 > 0)) {// top middle
if (aryBOARD[x][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y + 1 < 4) && (y + 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// top
// left
// corner
if (aryBOARD[x - 1][y + 1] == 0) {
dead++;
} else {
alive++;
}
}
if ((x - 1 < 4) && (x - 1 > 0)) {// left
if (aryBOARD[x - 1][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y - 1 < 4) && (y - 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// bottom
// left
// corner
if (aryBOARD[x - 1][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
// x++
if ((y - 1 < 4) && (y - 1 > 0)) {// bottom middle
if (aryBOARD[x][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
// RULES
// 1 Any live cell with fewer than two live neighbors dies, as if caused
// by under-population.
// 2 Any live cell with two or three live neighbors lives on to the next
// generation.
// 3 Any live cell with more than three live neighbors dies, as if by
// overcrowding.
// 4 Any dead cell with exactly three live neighbors becomes a live
// cell, as if by reproduction.
// test alive and dead
if (alive == 3 && aryBOARD[x][y] == 0) {// rule 4
aryCHANGE[x][y] = 1;
}
if (alive > 3 && aryBOARD[x][y] == 1) {// rule 3
aryCHANGE[x][y] = 0;
}
if (alive >= 2 && alive <= 3 && aryBOARD[x][y] == 1) {// rule 2
aryCHANGE[x][y] = 1;
}
if (alive < 2 && aryBOARD[x][y] == 1) {// rule 1
aryCHANGE[x][y] = 0;
}
if (dead > 5) {// rule check
aryCHANGE[x][y] = 0;
}
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < colums; j++) {
System.out.print(aryCHANGE[i][j] + " ");
}
System.out.println("");
}
System.out.println("---------------------------");
} // end main
} // end class
You need to think a lot more about structuring your code and breaking things up into smaller chunks. That will help you a lot, especially when you move onto larger projects in the future.
For example write a simple method to count the number of living cells around a given cell.
Now your main loop through just becomes:
for (int x=0;x<width;x++) {
for (int y=0;y<height;y++) {
switch(countLivingAround(x,y)) {
case 0: // Less than 2 always dies
case 1:
grid(x,y) = 0;
break;
case 2: // Do nothing, keep current state
break;
case 3: // Breed
grid(x,y) = 1;
break;
case 4: // Dies from overcrowding
grid(x,y) = 0;
break;
}
}
}
Your count function can be simple, it just adds up the values at [x-1,y],[x,y+1], etc, remember to check for the edges of the board and handle that case correctly though.
In addition to breaking the logic up into different methods you need to either use a debugger to step thru the code or use printlns to display the x and y values your evaluating (See the sample below) You'll see that some of your calculations on the adjoining cells x and y coordinates need work.
for (x = 0; x <= 4; x++) {
for (y = 0; y <= 4; y++) {
alive = 0;
dead = 0;
System.out.println("evaluating cell x=" + x + ", y = " + y);
if ((x + 1 < 4) && (x + 1 > 0)) {// right
if (aryBOARD[x + 1][y] == 0) {
dead++;
}
else {
alive++;
}
System.out.println(" check 1 x=" + (x + 1) + ", y = " + y);
}
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;
}