I am supposed to write a basic Tic Tac Toe game with Dr Java. I came up with code, but the professor isn't telling me what the problem is...?
{
System.out.println("Invalid marker, try again");
}
}
markerOk = false;
while (!markerOk) {
System.out.print("Select any letter as " + game.getPlayer2() + "'s marker: ");
String marker = game.getPrompt();
if (marker.length() == 1 &&
Character.isLetter(marker.toCharArray()[0]))
{
markerOk = true;
game.setMarker2(marker.toCharArray()[0]);
}
else
{
System.out.println("Invalid marker, try again");
}
}
boolean continuePlaying = true;
while (continuePlaying) {
game.init();
System.out.println();
System.out.println(game.getRules());
System.out.println();
System.out.println(game.drawBoard());
System.out.println();
String player = null;
while (!game.winner() && game.getPlays() < 9) {
player = game.getCurrentPlayer() == 1 ? game.getPlayer1() : game.getPlayer2();
boolean validPick = false;
while (!validPick) {
System.out.print("It is " + player + "'s turn. Pick a square: ");
String square = game.getPrompt();
if (square.length() == 1 && Character.isDigit(square.toCharArray()[0])) {
int pick = 0;
try {
pick = Integer.parseInt(square);
} catch (NumberFormatException e) {
//Do nothing here, it'll evaluate as an invalid pick on the next row.
}
validPick = game.placeMarker(pick);
}
if (!validPick) {
System.out.println("Square can not be selected. Retry");
}
}
game.switchPlayers();
System.out.println();
System.out.println(game.drawBoard());
System.out.println();
}
if (game.winner()) {
System.out.println("Game Over - " + player + " WINS!!!");
} else {
System.out.println("Game Over - Draw");
}
System.out.println();
System.out.print("Play again? (Yes or No): ");
String choice = game.getPrompt();
if (!choice.equalsIgnoreCase("Yes")) {
continuePlaying = false;
}
}
}
public static void main(String[] args) {
Main main = new Main();
main.play();
}
}
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Verified answer
This code is incomplete. Some is missing off the top, and some has been truncated. I can't necessarily find the problem if I can't see all of the relevant code.
O that's uncomplicated look interior your for statement the position you've x+=index so on the first loop x = 0 and index = 0 2d loop x=0 index = 2 output = 2 third loop x = 2 index = 4 output = 6 do you word it yet?