简体   繁体   中英

How to use boolean in another method?

I'm programming a Tic Tac Toe game and it seems to be working pretty good. What I want to implement now is an option to restart the game after the game has been won, lost or resulted in a tie. To get the wanted results I embedded the whole main method into a while loop("imGange"), when the game is over the loop should be set to false. To achieve this I thought I'd set the booleans "imGange" and "cool" to false inside of all the if statements in the "checkWinner()" - method. As soon as I'm typing "cool = false" the light bulb with the error: "Unreachable code" pops up in line 136, even if the booleans are declared for the whole TicTacToe class.

What can I do about that? Thanks in advance!

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class TicTacToe {
    
    static ArrayList<Integer> playerPositions = new ArrayList<Integer>();//List<Bitch> = List of Bitch, or List of Integers in diesem BSP
    static ArrayList<Integer> cpuPositions = new ArrayList<Integer>();
    
    public static void main (String[] args) {
        
        //boolean imGange = true;
        
        while(imGange) {
            char[][] gameBoard = {{' ','|',' ', '|',' '}, {'-','+','-', '+','-'}, {' ','|',' ', '|',' '}, {'-', '+','-', '+','-'}, {' ','|',' ', '|',' '}};//2dimensionales Array mit jeder {} für eine "row" und die Symbole zwischendrin
            
            printGameBoard(gameBoard);//damit keine rote Linie unter row : gameBoard
            
            //boolean cool = true;
            
            while(cool) {
                Scanner scan = new Scanner(System.in);
                System.out.println("Enter your placement (1-9):");
                int pos = scan.nextInt();
                while(playerPositions.contains(pos) || cpuPositions.contains(playerPositions)) {
                    System.out.println("Position taken! Enter another Position!");
                    pos = scan.nextInt();
                }
                
                //System.out.println(pos);
                
                placePiece(gameBoard, pos, "player");
                
                Random rand = new Random();
                int cpuPos = rand.nextInt(9) +1;// 1-9
                while(playerPositions.contains(cpuPos) || cpuPositions.contains(cpuPos)) {
                    cpuPos = rand.nextInt(9) +1;// 1-9
                }
                placePiece(gameBoard, cpuPos, "CPU");
                
                printGameBoard(gameBoard);
                
                String result = checkWinner();
                System.out.println(result);
                
            }
        }
        System.out.println("Wollen Sie eine weitere Runde spielen?");
        Scanner s = new Scanner(System.in);
        int eingabe = s.nextInt();
        if(eingabe == 1) {
            imGange = true;
        }else if(eingabe == 0) {
            System.exit(0);
        }
    }
    public static void printGameBoard(char[][] gameBoard) {
        for(char[] row : gameBoard) {
            for(char c : row) {
                System.out.print(c);
            }
            System.out.println();
        }
    }
    public static void placePiece(char[][] gameBoard, int pos, String user) {//needs to know about gameBoard, which it is
        
        char symbol = ' ';//default
        
        if(user.equals("player")){//.equals used with Strings!
            symbol = 'X';
            playerPositions.add(pos);
        }else if(user.equals("CPU")){
            symbol = 'O';
            cpuPositions.add(pos);
        }
        
        switch(pos) {
            case 1:
                gameBoard[0][0] = symbol;
                break;
            case 2:
                gameBoard[0][2] = symbol;
                break;
            case 3:
                gameBoard[0][4] = symbol;
                break;
            case 4:
                gameBoard[2][0] = symbol;
                break;
            case 5:
                gameBoard[2][2] = symbol;
                break;
            case 6:
                gameBoard[2][4] = symbol;
                break;
            case 7:
                gameBoard[4][0] = symbol;
                break;
            case 8:
                gameBoard[4][2] = symbol;
                break;
            case 9:
                gameBoard[4][4] = symbol;
                break;
            default:
                break;
        }
    }
    
    public static String checkWinner() {
        
        List topRow = Arrays.asList(1, 2, 3);
        List midRow = Arrays.asList(4, 5, 6);
        List botRow = Arrays.asList(7, 8, 9);
        List leftCol = Arrays.asList(1, 4, 7);
        List midCol = Arrays.asList(2, 5, 8);
        List rightCol = Arrays.asList(3, 6, 9);
        List cross1 = Arrays.asList(1, 5, 9);
        List cross2 = Arrays.asList(7, 5, 3);
        
        List<List> winning = new ArrayList<List>();
        winning.add(topRow);
        winning.add(midRow);
        winning.add(botRow);
        winning.add(leftCol);
        winning.add(midCol);
        winning.add(rightCol);
        winning.add(cross1);
        winning.add(cross2);
        
        for(List l : winning){//for each List inside of winning
            if(playerPositions.containsAll(l)) {
                return"Congratulations you won!";
                cool = false;
            }else if (cpuPositions.containsAll(l)) {
                return"CPU wins!";
            }else if(playerPositions.size() + cpuPositions.size() == 9) {
                return"Tie!";
            }
        }
        
        
        return "";
    }
    static boolean cool;
    static boolean imGange;
}

Rather than returning from a method or using more static variables, just ask to continue after printing the result.

while (true) {
   ...
   
    String result = checkWinner();
    System.out.println(result);

    System.out.println("Play again? [y/N]");
    char playAgain = scan.next();
    if (playAgain != 'y' || playAgain != 'Y') break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM