samedi 23 mars 2019

Error with computers choice and selecting an invalid point in tic tac toe game

I created a program for tictactoe but there are a few issues

1) The computer replaces my space on the board 2) I am able to replace the computers place (even though i created a if statement to make it print "Spot is taken" 3)If the user selects a spot that is not allowed (ex. 4), the game just stops, it wont tell the user to select another spot

My teacher has provided a board for us. If the board program is needed, please ask.

Thanks!

I've tried creating if statements but something is wrong with what I did. I looked my code over and it makes sense, but there is something wrong with the way I wrote it down and I am unable to catch my mistake.

if (players == 1) {
    while (gameContinue == true) {
        System.out.println("Enter a row: ");
        xc = reader.nextInt();
        System.out.println("Enter a column: ");
        yc = reader.nextInt();
        if (xc <0 || xc > 2 || yc < 0 || yc > 2)
            System.out.print("Invalid postition, please enter another point");
        cx = (int)(Math.random()*3);
        cy = (int)(Math.random()*3);
        int random = (int)(Math.random()*3);
        int random2 = (int)(Math.random()*3);

        if ((arr[xc][yc] != '-') || (arr[cx][cy] != '-' )) {
            System.out.println("Spot is taken. ");
            System.out.println("Enter a row: ");
            xc = reader.nextInt();
            System.out.println("Enter a column: ");
            yc = reader.nextInt();
            if (arr[xc][yc] == '-') {
                arr[xc][yc] = 'x';
                board.repaint();
            }
            if (arr[cx][cy] == '-') {
                arr[cx][cy] = 'o';
                board.repaint();
            }
        }

        arr[xc][yc] = 'x';
        if (arr[cx][cy] == arr[xc][yc]) {
            arr[random][random2] = 'o';
            board.repaint();

        } else {
            arr[cx][cy] = 'o';
            board.repaint();
        }
    }
}

I expect the output to say "Invalid position, select another postition" but it automatically just stops the game. Also if the computer selects my spot, it should go somewhere else but the computer is overlapping my spot.

Aucun commentaire:

Enregistrer un commentaire