So I am completely new to posting on this website, however I have been using it to find answers to my programming questions checking out already existing questions that had been answered.
Now, I am with a problem neither me nor my significant other are able to solve, and Google didn't succeed in giving me a valueable answer.
Is anyone of you able to find out why it loops around 1 extra time even though the while condition isn't true anymore?
NOTE: No clue if this is the right way of doing stuff on here, but here's the code. My code also isn't the most optimized, as I was planning on doing that after I got stuff working, but, well... That isn't the case right now...
import nl.saxion.app.SaxionApp;
import java.util.ArrayList;
public class Application implements Runnable {
public static void main(String[] args) {
SaxionApp.start(new Application(), 650, 500);
}
public void run() {
// Game Size
int sizeInput = boardSize(); // Call method "boardSize()"
if (sizeInput < 50) {
sizeInput = 50;
SaxionApp.printLine("Input too low, defaulted to min(50)");
} else if (sizeInput >90) {
sizeInput = 90;
SaxionApp.printLine("Input too high, defaulted to max(90)");
}
int absoluteWidth = sizeInput*7;
int absoluteHeight = sizeInput*7;
SaxionApp.resize(absoluteWidth, absoluteHeight);
int celWidth = absoluteWidth/5;
int celHeight = absoluteHeight/5;
int fontSize = celHeight / 3;
ArrayList<String> cells = new ArrayList<>();
setFields(cells);
String cell1 = cells.get(0);
String cell2 = cells.get(1);
String cell3 = cells.get(2);
String cell4 = cells.get(3);
String cell5 = cells.get(4);
String cell6 = cells.get(5);
String cell7 = cells.get(6);
String cell8 = cells.get(7);
String cell9 = cells.get(8);
// Game over code
boolean gameOver = false;
// Winconditions
String playerSymbol = "";
int activePlayer = 1;
int playerMove = 0;
while (!gameOver){
drawBoard(celWidth, celHeight, fontSize, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9);
SaxionApp.print("Player " + playerSymbol + ", make your move(1-9)?");
playerMove = SaxionApp.readInt();
if (activePlayer == 1 && !gameOver) {
// Player 1 turn
// SaxionApp.print("Player X, make your move(1-9)?");
// playerMove = SaxionApp.readInt();
activePlayer = 2;
if (playerMove == 0) {
SaxionApp.clear();
SaxionApp.printLine("Thank You For User Our App! :D");
} else if (cells.get(playerMove-1).equals("O") || cells.get(playerMove-1).equals("X")) {
SaxionApp.clear();
SaxionApp.printLine("This cell has already been chosen, pick another!");
activePlayer = 1;
} else if (playerMove == 1) {
cell1 = "X";
cells.set(0, "X");
SaxionApp.clear();
}
else if (playerMove == 2) {
cell2 = "X";
cells.set(1, "X");
SaxionApp.clear();
}
else if (playerMove == 3) {
cell3 = "X";
cells.set(2, "X");
SaxionApp.clear();
}
else if (playerMove == 4) {
cell4 = "X";
cells.set(3, "X");
SaxionApp.clear();
}
else if (playerMove == 5) {
cell5 = "X";
cells.set(4, "X");
SaxionApp.clear();
}
else if (playerMove == 6) {
cell6 = "X";
cells.set(5, "X");
SaxionApp.clear();
}
else if (playerMove == 7) {
cell7 = "X";
cells.set(6, "X");
SaxionApp.clear();
}
else if (playerMove == 8) {
cell8 = "X";
cells.set(7, "X");
SaxionApp.clear();
}
else if (playerMove == 9) {
cell9 = "X";
cells.set(8, "X");
SaxionApp.clear();
}
playerSymbol = "O";
/*else if (playerMove == 0) {
i = 9;
SaxionApp.clear();
SaxionApp.printLine("Thank You For User Our App! :D");
}*/
} else if (activePlayer == 2 && !gameOver){
// Player 2 turn
// SaxionApp.print("Player O, make your move(1-9)?");
// playerMove = SaxionApp.readInt();
activePlayer = 1;
if (playerMove == 0) {
SaxionApp.clear();
SaxionApp.printLine("Thank You For User Our App! :D");
} else if (cells.get(playerMove-1).equals("X")) {
SaxionApp.clear();
SaxionApp.printLine("Player X has already chosen this, pick another!");
activePlayer = 2;
} else if (playerMove == 1) {
cell1 = "O";
cells.set(0, "O");
SaxionApp.clear();
}
else if (playerMove == 2) {
cell2 = "O";
cells.set(1, "O");
SaxionApp.clear();
}
else if (playerMove == 3) {
cell3 = "O";
cells.set(2, "O");
SaxionApp.clear();
}
else if (playerMove == 4) {
cell4 = "O";
cells.set(3, "O");
SaxionApp.clear();
}
else if (playerMove == 5) {
cell5 = "O";
cells.set(4, "O");
SaxionApp.clear();
}
else if (playerMove == 6) {
cell6 = "O";
cells.set(5, "O");
SaxionApp.clear();
}
else if (playerMove == 7) {
cell7 = "O";
cells.set(6, "O");
SaxionApp.clear();
}
else if (playerMove == 8) {
cell8 = "O";
cells.set(7, "O");
SaxionApp.clear();
}
else if (playerMove == 9) {
cell9 = "O";
cells.set(8, "O");
SaxionApp.clear();
}
playerSymbol = "X";
}
ArrayList<Boolean> differentWins = winConditions(cells, playerSymbol);
int counter = 0;
while (counter < differentWins.size()) {
System.out.println("gameOver while works");
if (differentWins.get(counter)) {
System.out.println("gameOver if works");
gameOver = true;
SaxionApp.clear();
SaxionApp.printLine("Player " + playerSymbol + " has won! Congratulations!");
break;
} else {
System.out.println("gameOver if DOES NOT WORK");
counter++;
}
}
// gameOver = true;
}
SaxionApp.printLine("Thank you for playing our game! :D");
// Saving Image for easy preview
SaxionApp.saveImage("Exercise4/end_test_output.png");
}
public int boardSize() {
SaxionApp.print("What size do you wish the game window to be (min.50 and max.90)? ");
int input = SaxionApp.readInt();
SaxionApp.clear();
return input;
}
private void drawBoard(int celWidth, int celHeight, int fontSize, String cell1, String cell2, String cell3, String cell4, String cell5, String cell6, String cell7, String cell8, String cell9) {
SaxionApp.turnBorderOn();
// Draw Board
SaxionApp.drawLine(celWidth*2, celHeight, celWidth*2, celHeight*4);
SaxionApp.drawLine(celWidth*3, celHeight, celWidth*3, celHeight*4);
SaxionApp.drawLine(celWidth*1, celHeight*2, celWidth*4, celHeight*2);
SaxionApp.drawLine(celWidth*1, celHeight*3, celWidth*4, celHeight*3);
SaxionApp.turnBorderOff();
// Draw Cell Values
SaxionApp.drawBorderedText(" "+cell1,celWidth*1+fontSize, celHeight+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell2,celWidth*2+fontSize, celHeight+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell3,celWidth*3+fontSize, celHeight+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell4,celWidth*1+fontSize, celHeight*2+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell5,celWidth*2+fontSize, celHeight*2+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell6,celWidth*3+fontSize, celHeight*2+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell7,celWidth*1+fontSize, celHeight*3+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell8,celWidth*2+fontSize, celHeight*3+fontSize, fontSize);
SaxionApp.drawBorderedText(" "+cell9,celWidth*3+fontSize, celHeight*3+fontSize, fontSize);
}
public void setFields(ArrayList<String> cells) {
for (int i = 1; i < 10; i++) {
String cellNumber = String.valueOf(i);
cells.add(cellNumber);
}
System.out.println(cells);
}
public ArrayList<Boolean> winConditions(ArrayList<String> cells, String playerSymbol) {
boolean winConditionWidth1 = cells.get(0).equals(playerSymbol) && cells.get(1).equals(playerSymbol) && cells.get(2).equals(playerSymbol);
boolean winConditionWidth2 = cells.get(3).equals(playerSymbol) && cells.get(4).equals(playerSymbol) && cells.get(5).equals(playerSymbol);
boolean winConditionWidth3 = cells.get(6).equals(playerSymbol) && cells.get(7).equals(playerSymbol) && cells.get(8).equals(playerSymbol);
boolean winConditionHeight1 = cells.get(0).equals(playerSymbol) && cells.get(3).equals(playerSymbol) && cells.get(6).equals(playerSymbol);
boolean winConditionHeight2 = cells.get(1).equals(playerSymbol) && cells.get(4).equals(playerSymbol) && cells.get(7).equals(playerSymbol);
boolean winConditionHeight3 = cells.get(2).equals(playerSymbol) && cells.get(5).equals(playerSymbol) && cells.get(8).equals(playerSymbol);
boolean winConditionDiagonal1 = cells.get(0).equals(playerSymbol) && cells.get(4).equals(playerSymbol) && cells.get(8).equals(playerSymbol);
boolean winConditionDiagonal2 = cells.get(2).equals(playerSymbol) && cells.get(4).equals(playerSymbol) && cells.get(6).equals(playerSymbol);
ArrayList<Boolean>differentWins = new ArrayList<>();
differentWins.add(winConditionWidth1);
differentWins.add(winConditionWidth2);
differentWins.add(winConditionWidth3);
differentWins.add(winConditionHeight1);
differentWins.add(winConditionHeight2);
differentWins.add(winConditionHeight3);
differentWins.add(winConditionDiagonal1);
differentWins.add(winConditionDiagonal2);
return differentWins;
}
}