Reputation:
I understand this seems like a common questions I have seen it asked in regards to C++ and python but could not seem to find explanation that fixed my problem for java.
Attached is my code for my program, rather long because I am still fairly new and learning how to streamline it.
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
boolean gameActive = true;
boolean player1turn = true;
String board[][]= {{" ","|"," ","|"," "},{"-","-","-","-","-",},{" ","|"," ","|"," "},{"-","-","-","-","-",},{" ","|"," ","|"," "}};
commands();
printBoard(board);
while (gameActive == true) {
if (player1turn == true) {
System.out.println("\nPlayer 1, noughts turn. Please choose a position.");
} else {
System.out.println("\nPlayer 2, crosses turn. Please choose a position.");
}
String position = null;
while ("retry".equals(changePosition(position, player1turn, board))) {
Scanner keyboard = new Scanner (System.in);
position = keyboard.nextLine();
if ("help".equalsIgnoreCase(position)) {
commands();
}
else if ("turn".equalsIgnoreCase(position)){
turn(player1turn);
}
else if ("board".equalsIgnoreCase(position)){
printBoard(board);
}
else {
if (!"retry".equalsIgnoreCase(changePosition(position, player1turn, board))) {
System.out.println(changePosition(position, player1turn, board));
if (player1turn == true) {
player1turn = false;
}
else if (player1turn == false) {
player1turn = true;
}
}
}
}
}
}
public static void commands() {
System.out.println("\nPlayer 1 is noughts, O");
System.out.println("Player 2 is crosses, X");
System.out.println("\nThe board uses a grid format");
System.out.println("\"A1\" is the top-left position");
System.out.println("\"A2\" is the top-middle position");
System.out.println("\"A3\" is the top-right position");
System.out.println("\"B1\" is the middle-left position");
System.out.println("\"B2\" is the middle position");
System.out.println("\"B3\" is the middle-right position");
System.out.println("\"C1\" is the bottom-left position");
System.out.println("\"C2\" is the bottom-middle position");
System.out.println("\"C3\" is the bottom-right position");
System.out.println("\n\"help\" will reveal this help message");
System.out.println("\"board\" will reveal the board");
System.out.println("\"turn\" will reveal whos turn it is");
}
public static void turn(boolean player1turn) {
if (player1turn == true) {
System.out.println("\nPlayer 1, noughts turn.");
} else {
System.out.println("\nPlayer 2, crosses turn.");
}
}
public static void printBoard(String[][] board) {
System.out.print("\n");
for (int x=0;x<5;x++) {
for (int y=0;y<5;y++) {
System.out.print(board[x][y]+"");
}
System.out.println();
}
}
public static String changePosition(String position, boolean player1turn, String[][] board) {
if (("A1".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[0][0])) {
board[0][0] = "O";
return board[0][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B1".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[2][0])) {
board[2][0] = "O";
return board[2][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C1".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[4][0])) {
board[4][0] = "O";
return board[4][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A2".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[0][2])) {
board[0][2] = "O";
return board[0][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B2".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[2][2])) {
board[2][2] = "O";
return board[2][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C2".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[4][2])) {
board[4][2] = "O";
return board[4][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A3".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[0][4])) {
board[0][4] = "O";
return board[0][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B3".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[2][4])) {
board[2][4] = "O";
return board[2][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C3".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[4][4])) {
board[4][4] = "O";
return board[4][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A1".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[0][0])) {
board[0][0] = "X";
return board[0][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B1".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[2][0])) {
board[2][0] = "X";
return board[2][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C1".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[4][0])) {
board[4][0] = "X";
return board[4][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A2".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[0][2])) {
board[0][2] = "X";
return board[0][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B2".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[2][2])) {
board[2][2] = "X";
return board[2][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C2".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[4][2])) {
board[4][2] = "X";
return board[4][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A3".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[0][4])) {
board[0][4] = "X";
return board[0][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B3".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[2][5])) {
board[2][4] = "X";
return board[2][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C3".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[4][4])) {
board[4][4] = "X";
return board[4][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else {
if ((position == null) || ("help".equalsIgnoreCase(position)) || ("turn".equalsIgnoreCase(position)) || ("board".equalsIgnoreCase(position))) {
return "retry";
} else {
System.out.println("Error please enter a valid position found by entering \"help\" ");
return "retry";
}
}
}
}
From as far as I can tell the issue spans from my length changePosition() method. If I enter in the console "A1" the results yield:
Player 1, noughts turn. Please choose a position.
A1
That spot is taken please choose another place
retry
That spot is taken please choose another place
board
O| |
-----
| |
-----
| |
This confuses me as it seems the if block is actually triggered and causes board[0][0] to be changed but for some reason must move to the else block and print the message to the console and return "retry".
I have tried changing the return value within the if block to a standard string like "success" however this does not seem to resolve the problem. Additionally when I hover over the return type the IDE identifies these as return points. I have made sure (I am confident) that I am using the correct syntax.
I expect it is a problem with my lengthy code which I am sure is gore for you all.
Thank you.
Upvotes: 1
Views: 62
Reputation:
The Problem is that you call you changePosition
method multiple times in each iteration of this loop:
while ("retry".equals(changePosition(position, player1turn, board))) {
Scanner keyboard = new Scanner (System.in);
position = keyboard.nextLine();
if ("help".equalsIgnoreCase(position)) {
commands();
}
else if ("turn".equalsIgnoreCase(position)){
turn(player1turn);
}
else if ("board".equalsIgnoreCase(position)){
printBoard(board);
}
else {
if (!"retry".equalsIgnoreCase(changePosition(position, player1turn, board))) {
System.out.println(changePosition(position, player1turn, board));
if (player1turn == true) {
player1turn = false;
}
else if (player1turn == false) {
player1turn = true;
}
}
}
}
}
First it is called in the condition of the while
loop:
while ("retry".equals(changePosition(position, player1turn, board)))
But because the parameter position is still null at first it will simply return "retry"
and enter the loop when it first happens.
After you entered the position you call the method here:
if (!"retry".equalsIgnoreCase(changePosition(position, player1turn, board)))
And if the spot is free it will work and not return "retry"
and therefor enter the if
statement block. This is what happens in your case and from that moment on the place A1 was actually filled.
You then immedietly call the method again in the next line:
System.out.println(changePosition(position, player1turn, board));
But since now A1 is occupied it will this time output the error "That spot is taken please choose another place" and return "retry"
which you also print.
The method is then once again called when the loop condition is checked again in the beginning of the next iteration. And since you are now calling it a third time with the same parameters it will again output the error and return "retry"
as a return value therefor entering the loop again.
The solution is to only call the function once per iteration and save the return value and working with that. You should also use a do-while-loop because it should allways run at least once:
String changePositionResult = null;
do {
final Scanner keyboard = new Scanner(System.in);
position = keyboard.nextLine();
if ("help".equalsIgnoreCase(position)) {
commands();
} else if ("turn".equalsIgnoreCase(position)) {
turn(player1turn);
} else if ("board".equalsIgnoreCase(position)) {
printBoard(board);
} else {
changePositionResult = changePosition(position, player1turn, board);
if (!"retry".equalsIgnoreCase(changePositionResult)) {
System.out.println(changePositionResult);
if (player1turn == true) {
player1turn = false;
} else if (player1turn == false) {
player1turn = true;
}
}
}
} while ("retry".equals(changePositionResult));
Upvotes: 1