[英]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.
为了获得想要的结果,我将整个 main 方法嵌入到一个 while 循环(“imGange”)中,当游戏结束时,循环应该设置为 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.
为了实现这一点,我想我会在“checkWinner()”方法中的所有 if 语句中将布尔值“imGange”和“cool”设置为 false。 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.
一旦我输入“cool = false”,灯泡就会出现错误:“无法访问的代码”在第 136 行弹出,即使布尔值是为整个 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.与其从方法返回或使用更多 static 变量,不如在打印结果后要求继续。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.