Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 井字游戏。环路困难和扫描仪问题_Java_Arrays_Methods_Global - Fatal编程技术网

Java 井字游戏。环路困难和扫描仪问题

Java 井字游戏。环路困难和扫描仪问题,java,arrays,methods,global,Java,Arrays,Methods,Global,我正在做一个课堂练习。我已经构建了表格和程序中的所有内容,但是我仍然有一些问题。这是我的全部代码 import java.util.Scanner; public class TicTacToe { //These two variables are for placing the X's and O's in the TicTacToe table. static int row; static int col; //This array is for the TicTacToe tab

我正在做一个课堂练习。我已经构建了表格和程序中的所有内容,但是我仍然有一些问题。这是我的全部代码

import java.util.Scanner;

public class TicTacToe {

//These two variables are for placing the X's and O's in the TicTacToe table.
static int row;
static int col;

//This array is for the TicTacToe table.
static char[][] table = new char[3][3];

//This is for ending the while statement that controls the game
static boolean continuePlaying = true;
static boolean exitProgram = false;

//This is for returning true when the user inputs a valid combination of numbers
static boolean validMove = true;

//This is for returning false when the user inputs an invalid combination of numbers
static boolean invalidMove = false;

//These will store the names for player 1 and 2
static String p1;
static String p2;

//This variable will change every turn to either p1 or p2 depending on who's turn it is. (This is a bad idea at the moment)
static String activePlayer;

//This variable is to count the number of games played
static int gamesCounter = 0;

//These two variables are to keep track of the wins each player has
static int p1Wins = 0;
static int p2Wins = 0;

static void clearTable(){
    for(int i = 0; i < table.length; i++){
        table[i][i] = ' ';
    }
}

//This is my displayTable() method. This will show the table and the X's or O's each player has played
static void displayTable(){
    System.out.println("  0  1  2");
    System.out.println("0[" + table[0][0] +"][" + table[0][1] +"][" + table[0][2] +"]");
    System.out.println("1[" + table[1][0] +"][" + table[1][1] +"][" + table[1][2] +"]");
    System.out.println("2[" + table[2][0] +"][" + table[2][1] +"][" + table[2][2] +"]");
}

//This is my move(int row,int col) method. This will record the moves each player takes and insert X's or O's depending on which player
static boolean move(int row,int col) {
    //This if statement will return false if the user enters in coordinates outside the 3x3 zone.
    if (row > 2 || row < 0) {
        System.out.println("Invalid move."); return false; 
    }
    //This if statement checks if the array already has an X or O in the chosen space. If the array is already filled it will return false.
    else if (table[row][col] == 'X' || table[row][col] == 'O') {
        System.out.println("Not available."); return false;
    }
    else return true;
}

//This is my checkRow method. It checks for 3 X's or O's in a row. If there are 3 in a row it will return true, if not, it returns false.
static boolean checkRow(int row){
    if((table[row][0] & table[row][1] & table[row][2]) == 'X') return true;     
    if((table[row][0] & table[row][1] & table[row][2]) == 'O') return true;
    else return false;      
}

//This is my checkCol method. It checks for 3 X's or O's in a row. If there are 3 in a row it will return true, if not, it returns false.
static boolean checkCol(int col){
    if((table[0][col] & table[1][col] & table[2][col]) == 'X') return true;
    if((table[0][col] & table[1][col] & table[2][col]) == 'O') return true;
    else return false;
}

//This is my checkDiagonal method. It checks for 3 X's or O's in a row. If there are 3 in a row it will return true, if not, it returns false.
static boolean checkDiagonal(){
    if((table[0][0] & table[1][1] & table[2][2]) == 'X') return true;
    if((table[2][0] & table[1][1] & table[0][2]) == 'X') return true;
    if((table[0][0] & table[1][1] & table[2][2]) == 'O') return true;
    if((table[2][0] & table[1][1] & table[0][2]) == 'O') return true;
    else return false;      
}

//This is my checkWinner method. It runs all the other checks to see if anyone won.
//If there is a winner the method returns true. If there is no winner yet, the method returns false.
static boolean checkWinner(){
    if(checkRow(0) == true) return true;
    if(checkRow(1) == true) return true;
    if(checkRow(2) == true) return true;
    if(checkCol(0) == true) return true;
    if(checkCol(1) == true) return true;
    if(checkCol(2) == true) return true;
    if(checkDiagonal() == true) return true;
    else return false;
}

public static void main(String[] args) {
    //The Scanner for asking each player's names
    Scanner s = new Scanner(System.in);

    //The beginning structure of the TicTacToe program
    System.out.println("TicTextToe");
    System.out.print("Name of player 1: ");
    p1 = s.nextLine();

    //This line of code is just to check if it's taking Player 1's name correctly
    //System.out.println(p1);

    //Asks for Player 2's name
    System.out.print("Name of player 2: ");
    p2 = s.nextLine();

    //This line of code is just to check if it's taking Player 2's name correctly
    //System.out.println(p2);

    //Printing move(row, col) to see why it's printing unusually
    //System.out.print(move(row, col));


    //The TicTacToe table set up, coordinates provided around the squares
    //The displayTable() method will be used to display the table here.  There will be no X's or O's in the table this first time.
    while (continuePlaying == true) {
    displayTable();
    System.out.print("Player " + p1 + ":");
    row = s.nextInt();
    col = s.nextInt();
    move(row, col);

    //This will display the table again and ask for proper coordinates.
    while(move(row, col) == false) {
        displayTable();
        System.out.println("Player " + p1 + ":");
        row = s.nextInt();
        col = s.nextInt();
        move(row,col);
    }

    //This inputs the X into the table if move(row, col) returns true.
    if(move(row, col) == true) {
        table[row][col] = 'X';
    }   
    //This will check if p1 just won the game or if the game needs to continue
    checkRow(0);
    //System.out.println(checkRow(0)); //This prints out if row 0 is true or false
    checkRow(1);
    //System.out.println(checkRow(1)); //This prints out if row 1 is true or false
    checkRow(2);
    //System.out.println(checkRow(2)); //This prints out if row 2 is true or false
    checkCol(0);
    //System.out.println(checkCol(0)); //This prints out if column 0 is true or false
    checkCol(1);
    //System.out.println(checkCol(1)); //This prints out if column 1 is true or false
    checkCol(2);
    //System.out.println(checkCol(2)); //This prints out if column 2 is true or false
    checkDiagonal();    
    //System.out.println(checkDiagonal()); //This prints out true or false depending on the diagonals
    checkWinner();  
    //System.out.println(checkWinner()); //This prints out if checkWinner is true or false. If it's true the while loop should end

    if(checkWinner() == true){
        displayTable();
        System.out.println("Player " + p1 + " wins!");
        gamesCounter++;
        p1Wins++;
        clearTable();
        System.out.println(p1 + " " + p1Wins + "/" + gamesCounter);
        System.out.println(p2 + " " + p2Wins + "/" + gamesCounter);
        System.out.println("Another game? Yes/No :");
        if(s.next() == "Yes") {
            break;
        }
        if(s.next() == "No") {
            continuePlaying = false;
            System.out.println("Statistitcs:");
            System.out.println(p1 + p1Wins + "/" + gamesCounter);
            System.out.println(p2 + p2Wins + "/" + gamesCounter);
            break;
        }
    }

    displayTable();
    System.out.print("Player " + p2 + ":");
    row = s.nextInt();
    col = s.nextInt();
    move(row, col);

    //This will display the table again and ask for proper coordinates.
            while(move(row, col) == false) {
                displayTable();
                System.out.println("Player " + p2 + ":");
                row = s.nextInt();
                col = s.nextInt();
                move(row,col);
            }

    //This inputs the O into the table if move(row, col) returns true.
    if(move(row, col) == true) {
        table[row][col] = 'O';
    }
    //This will check if p2 just won the game or if the game needs to continue
    checkRow(0);
    //System.out.println(checkRow(0)); //This prints out if row 0 is true or false
    checkRow(1);
    //System.out.println(checkRow(1)); //This prints out if row 1 is true or false
    checkRow(2);
    //System.out.println(checkRow(2)); //This prints out if row 2 is true or false
    checkCol(0);
    //System.out.println(checkCol(0)); //This prints out if column 0 is true or false
    checkCol(1);
    //System.out.println(checkCol(1)); //This prints out if column 1 is true or false
    checkCol(2);
    //System.out.println(checkCol(2)); //This prints out if column 2 is true or false
    checkDiagonal();    
    //System.out.println(checkDiagonal()); //This prints out true or false depending on the diagonals
    checkWinner();  
    //System.out.println(checkWinner()); //This prints out if checkWinner is true or false. If it's true the while loop should end

    if(checkWinner() == true){
        displayTable();
        System.out.println("Player " + p2 + " wins!");
        gamesCounter++;
        p2Wins++;
        System.out.println(p1 + " " + p1Wins + "/" + gamesCounter);
        System.out.println(p2 + " " + p2Wins + "/" + gamesCounter);
        System.out.println("Another game? Yes/No :");
        if(s.next() == "Yes") {
            break;
        }
        if(s.next() == "No") {
            continuePlaying = false;
            System.out.println("Statistitcs:");
            System.out.println(p1 + p1Wins + "/" + gamesCounter);
            System.out.println(p2 + p2Wins + "/" + gamesCounter);
            break;
        }

    }

}
    {
    } while (exitProgram == false);
}
}
import java.util.Scanner;
公共类Tictatcoe{
//这两个变量用于将X和O放置在TicTacToe表格中。
静态int行;
静态整数列;
//此数组用于TicTacToe表。
静态字符[][]表=新字符[3][3];
//这用于结束控制游戏的while语句
静态布尔连续播放=真;
静态布尔exitProgram=false;
//这用于在用户输入有效的数字组合时返回true
静态布尔值validMove=true;
//这用于在用户输入无效的数字组合时返回false
静态布尔值invalidMove=false;
//这些将存储玩家1和玩家2的名字
静态字符串p1;
静态字符串p2;
//根据轮到谁,该变量将在每次轮到p1或p2时改变。(目前这是个坏主意)
静态字符串播放器;
//此变量用于计算所玩游戏的数量
静态整数游戏中心=0;
//这两个变量用于跟踪每个玩家的胜利
静态int p1Wins=0;
静态int=0;
静态void clearTable(){
对于(int i=0;i2 | |行<0){
System.out.println(“无效移动”);返回false;
}
//此if语句检查数组在所选空间中是否已包含X或O。如果数组已填充,则返回false。
else if(表[行][col]='X'| |表[行][col]='O'){
System.out.println(“不可用”);返回false;
}
否则返回true;
}
//这是我的checkRow方法。它检查一行中是否有3个X或O。如果一行中有3个,它将返回true,如果没有,它将返回false。
静态布尔校验行(int行){
if((表[row][0]&表[row][1]&表[row][2])=='X')返回true;
if((表[row][0]&表[row][1]&表[row][2])==“O”)返回true;
否则返回false;
}
//这是我的checkCol方法。它检查一行中是否有3个X或O。如果一行中有3个,它将返回true,如果没有,它将返回false。
静态布尔校验列(整数列){
if((表[0][col]&表[1][col]&表[2][col])=='X')返回true;
if((表[0][col]&表[1][col]&表[2][col])==“O”)返回true;
否则返回false;
}
//这是我的checkDiagonal方法。它检查一行中的3个X或O。如果一行中有3个,它将返回true,如果没有,它将返回false。
静态布尔检查对角(){
if((表[0][0]&表[1][1]&表[2][2])=='X')返回true;
if((表[2][0]&表[1][1]&表[0][2])=='X')返回true;
if((表[0][0]&表[1][1]&表[2][2])==“O”)返回true;
if((表[2][0]&表[1][1]&表[0][2])==“O”)返回true;
否则返回false;
}
//这是我的checkWinner方法。它运行所有其他检查以查看是否有人赢了。
//如果有赢家,则该方法返回true。如果还没有赢家,则该方法返回false。
静态布尔checkWinner(){
if(checkRow(0)=true)返回true;
if(checkRow(1)=true)返回true;
if(checkRow(2)=true)返回true;
if(checkCol(0)=true)返回true;
if(checkCol(1)=true)返回true;
if(checkCol(2)=true)返回true;
if(checkDiagonal()==true)返回true;
否则返回false;
}
公共静态void main(字符串[]args){
//询问每个玩家姓名的扫描仪
扫描仪s=新的扫描仪(System.in);
//TicTacToe计划的开始结构
System.out.println(“TicTextToe”);
系统输出打印(“玩家1的名字:”);
p1=s.nextLine();
//这行代码只是为了检查它是否正确地使用了玩家1的名字
//系统输出打印LN(p1);
//询问玩家2的名字
系统输出打印(“玩家2的名字:”);
p2=s.nextLine();
//这行代码只是为了检查它是否正确地使用了玩家2的名字
//系统输出打印LN(p2);
//打印移动(行、列)以查看其打印异常的原因
//系统输出打印(移动(行、列));
//设置Tictatcoe表格,在广场周围提供坐标
//displayTable()方法将用于在此处显示该表。第一次该表中不会出现X或O。
while(continuePlaying==true){
displayTable();
系统输出打印(“播放器”+p1+“:”);
行=s.nextInt();
col=s.nextInt();
移动(行、列);
//这将再次显示表格并要求提供正确的坐标。
while(移动(行、列)=false){
displayTable();
System.out.println(“播放器”+p1+“:”);
行=s.nextInt();
col=s.nextInt();
移动(行、列);
}
//如果move(row,col)返回true,则将X输入表中。
如果(移动(行,列)=真){
表[行][col]=“X”;
}   
//这将检查p1是否刚刚赢得比赛,或者是否需要继续比赛
检查行(0);
//System.out.println(checkRow(0));//如果第0行为true或false,则打印输出
支票行(1);
//System.out.println(checkRow(1));//如果第1行为true或false,则打印输出
static void clearTable(){
    for(int i = 0; i < table.length; i++){
        table[i][i] = ' ';
    }
}
static void clearTable(){
    for(int i = 0; i < table.length; i++)
        for(int j=0; j< table[0].length; j++)
            table[i][j] = ' ';
}
string ans = s.next();
if(ans.equals("Yes"))
    break;
else if(ans.equals("No")) {
    continuePlaying = false;
    System.out.println("Statistitcs:");
    System.out.println(p1 + p1Wins + "/" + gamesCounter);
    System.out.println(p2 + p2Wins + "/" + gamesCounter);
    break;
}