Home > Software engineering >  Is the use of while loops ok here? Or it's excesive
Is the use of while loops ok here? Or it's excesive

Time:02-09

I want to know if it is ok to use as much while loops as I want or it is just not conventional. Also searhing for another solution to check only if the user enters the two symbols of the tic tac toe (X, O), but only one user can have each of them.

In other words, when the user is prompted for choosing the symbol he wants for playing, if he introduces an invalid one, keep asking till it's one of the both above. Then, for the second player he can only choose, for example, the 'O' if the first player have chosen the 'X', and also ensuring it's only one of the two symbols and not an invalid one.

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String playerName, playerMove;

        System.out.print("Introduce el nombre del primer jugador: ");
        playerName = sc.next();
        playerName = playerName.substring(0, 1).toUpperCase()   playerName.substring(1);

        System.out.print("Introduce el símbolo que quieres jugar ('X', 'O'): ");
        playerMove = sc.next();

        while (!playerMove.equalsIgnoreCase("X") && !playerMove.equalsIgnoreCase("O")) {
            System.out.print("Introduce un símbolo válido: ");
            playerMove = sc.next();
        }

        playerMove = playerMove.toUpperCase();

        Player player1 = new Player(playerName, playerMove);
        System.out.println();
        System.out.println("¡Bienvenido "   player1.name   "!");

        System.out.println();
        System.out.print("Introduce el nombre del segundo jugador: ");
        playerName = sc.next();
        playerName = playerName.substring(0, 1).toUpperCase()   playerName.substring(1);

        System.out.print("Introduce el símbolo que quieres jugar ('X', 'O'): ");
        playerMove = sc.next();


        while (!playerMove.equalsIgnoreCase("X") && !playerMove.equalsIgnoreCase("O")) {
            System.out.print("Introduce un símbolo válido: ");
            playerMove = sc.next();
        }

        while (playerMove.equalsIgnoreCase(player1.moveType)) {
            System.out.print("Introduce un símbolo distinto al del otro jugador: ");
            playerMove = sc.next();

            while (!playerMove.equalsIgnoreCase("X") && !playerMove.equalsIgnoreCase("O")) {
                System.out.print("Introduce un símbolo válido: ");
                playerMove = sc.next();
            }
        }

        playerMove = playerMove.toUpperCase();

        Player player2 = new Player(playerName, playerMove);
        System.out.println();
        System.out.println("¡Bienvenido "   player2.name   "!");
        System.out.println();

        System.out.println("El símbolo de "   player1.name   " es "   player1.moveType   ".");
        System.out.println("El símbolo de "   player2.name   " es "   player2.moveType   ".");

        System.out.println();

        Board board = new Board();

        System.out.println("El tablero es: ");
        System.out.println();

        board.displayBoard();
        System.out.println();
    }
}

CodePudding user response:

Of course there are other efficient ways for this type of problems as your question is concern that it not conventional. most of the developers are using it but nested loops are not a good choice performance wise.

CodePudding user response:

There are absolutely alternatives / more efficient ways to solve this program but to answer your question, nested loops are not at all uncommon or unconventional in Java and this would be acceptable. The negative effect of using nested loops would be the algorithm runtime which you likely don't need to worry about for this problem!

  •  Tags:  
  • Related