Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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 tic tac toe Jcreator卡滞_Java_Jcreator - Fatal编程技术网

Java tic tac toe Jcreator卡滞

Java tic tac toe Jcreator卡滞,java,jcreator,Java,Jcreator,下面是整个事情现在的样子 然后是你不得不添加的细节,越来越多的细节,越来越多的细节,越来越多的细节,越来越多越来越多 import java.util.Scanner; public class Tictactoe { static char[][] MakeMove(char[][] spelplan, char spelare, int rad, int kolumn) { spelplan[rad][kolumn] = spelare; char[

下面是整个事情现在的样子

然后是你不得不添加的细节,越来越多的细节,越来越多的细节,越来越多的细节,越来越多越来越多

import java.util.Scanner;

public class Tictactoe {
    static char[][] MakeMove(char[][] spelplan, char spelare, int rad, int kolumn) {
        spelplan[rad][kolumn] = spelare;
        char[][] board = new char[4][4];
        System.out.println(spelplan[rad][kolumn]);
        return spelplan;
    }

    static boolean CheckMove(char[][] spelplan, int x, int y) {
        if (spelplan[x][y] != ' ') {
            return false;
        } else {
            return true;
        }
    }

    static void SkrivUtSpelplan(char[][] spelplan) {
        System.out.println("-------");
        System.out.println("|" + spelplan[1][1] + "|" + spelplan[1][2] + "|" + spelplan[1][3] + "|");
        System.out.println("|-+-+-|");
        System.out.println("|" + spelplan[2][1] + "|" + spelplan[2][2] + "|" + spelplan[2][3] + "|");
        System.out.println("|-+-+-|");
        System.out.println("|" + spelplan[3][1] + "|" + spelplan[3][2] + "|" + spelplan[3][3] + "|");
        System.out.println("-------");
    }

    static boolean KollaVinst(char[][] spelplan) {
        return false;
    }

    public static void main(String[] args) {
        char spelplan[][] = new char[4][4];
        char spelare;
        int rad = 3, kolumn = 3, i = 0;
        for (int x = 1; x < 4; x++) {
            for (int y = 1; y < 4; y++) {
                spelplan[x][y] = ' ';
            }
        }

        System.out.println("-------");
        System.out.println("| | | |");
        System.out.println("|-+-+-|");
        System.out.println("| | | |");
        System.out.println("|-+-+-|");
        System.out.println("| | | |");
        System.out.println("-------");

        while (KollaVinst(spelplan) == false) {

            CheckMove(spelplan, rad, kolumn);

            for (i = 0; i < 9; i++) {
                if (i % 2 == 0) {
                    spelare = 'X';
                } else {
                    spelare = 'O';
                }

                System.out.println("Spelare 1 skriv vilken rad: 1-3");
                int x = new Scanner(System.in).nextInt();

                System.out.println("Spelare 1 skriv vilken kolumn: 1-3");
                int y = new Scanner(System.in).nextInt();

                if (CheckMove(spelplan, x, y) == true) {
                    MakeMove(spelplan, spelare, x, y);
                }
                System.out.println(" ");
                SkrivUtSpelplan(spelplan);
            }
        }
    }
}
import java.util.Scanner;
公共类Tictatcoe{
静态字符[][]MakeMove(字符[][]spelplan、字符spelare、int rad、int kolumn){
spelplan[rad][kolumn]=spelare;
字符[][]板=新字符[4][4];
System.out.println(spelplan[rad][kolumn]);
返回spelplan;
}
静态布尔CheckMove(char[][]spelplan,int x,int y){
如果(spelplan[x][y]!=''){
返回false;
}否则{
返回true;
}
}
静态void SkrivUtSpelplan(char[][]spelplan){
System.out.println(“----”);
System.out.println(“|”+spelplan[1][1]+“|”+spelplan[1][2]+“|”+spelplan[1][3]+“|”);
System.out.println(“|-+-+-|”);
System.out.println(“|”+spelplan[2][1]+“|”+spelplan[2][2]+“|”+spelplan[2][3]+“|”);
System.out.println(“|-+-+-|”);
System.out.println(“|”+spelplan[3][1]+“|”+spelplan[3][2]+“|”+spelplan[3][3]+“|”);
System.out.println(“----”);
}
静态布尔KollaVinst(char[][]spelplan){
返回false;
}
公共静态void main(字符串[]args){
char spelplan[][]=新字符[4][4];
煤焦;
int rad=3,kolumn=3,i=0;
对于(int x=1;x<4;x++){
对于(int y=1;y<4;y++){
spelplan[x][y]='';
}
}
System.out.println(“----”);
System.out.println(“| | | | |”);
System.out.println(“|-+-+-|”);
System.out.println(“| | | | |”);
System.out.println(“|-+-+-|”);
System.out.println(“| | | | |”);
System.out.println(“----”);
while(KollaVinst(spelplan)=false){
检查移动(spelplan、rad、kolumn);
对于(i=0;i<9;i++){
如果(i%2==0){
spelare='X';
}否则{
spelare='O';
}
系统输出打印LN(“Spelare 1 skriv vilken rad:1-3”);
int x=新扫描仪(System.in).nextInt();
系统输出打印(“Spelare 1 skriv vilken kolumn:1-3”);
int y=新扫描仪(System.in).nextInt();
if(CheckMove(spelplan,x,y)=true){
MakeMove(spelplan、spelare、x、y);
}
System.out.println(“”);
SkrivUtSpelplan(spelplan);
}
}
}
}

基于您目前发布的内容(我真的希望编辑能够澄清您正在尝试的内容)

我假设您正在尝试构建一个直到电路板完成才结束的循环。 在完成之前,玩家需要移动。这一举措已经实施(如果合法的话)。然后印制电路板

您的代码存在许多问题:

  • CheckMove(spelplan、rad、kolumn)仅在rad=3、kolumn=3时执行。我希望这是使用用户输入运行的
  • 您可以重用循环变量。(int i=新扫描仪)和更高版本(对于i=i)。永远不要那样做。不管怎样,i=i是没有意义的,你最终做的是i++,这意味着你失去了用户输入
  • 在while循环中,你永远不会做makeMove(spelplan,i,j)。我希望在打印出电路板之前会发生这种情况
  • for循环(i和j)似乎没有什么用处。您的方法skrivutSpelPlan已经打印了电路板,只需调用一次。不使用循环变量i或j
  • 我正在与写建议/代码的冲动作斗争,因为你表示不想要它。 在伪代码中,您希望循环的结构如下:

    while (!isBoardSolved) {
        (x,y) = getUserInput(); // this is not java :D don't use it
        if (isLegalMove(x,y)) {
             makeMove(x,y);
        } else {
             reportError();
        }
        printBoard();
    }
    
    在printBoard方法中,应该循环。您当前已硬编码[0][1]等。行使用forloop,列使用forloop

    你的支票移动不会检查棋盘。它检查电路板[x][y]!=董事会[x][y],这从来都不是真的。(从技术上讲,在多线程环境中是可以的,如果条件发生并且可以访问,则可能是真的) 你必须想想checkMove做了什么

    MakeMove是可以的,除了你不需要返回新的比赛场地。 如果你这样做

    char[][] board = new char[4][4];
    MakeMove(board, 'a', x, y);
    System.out.println(board[x][y]); // should print 'a' unless x or y were out of bounds.
    
    然后更新电路板并打印a

    好吧,我不想破坏你的乐趣,只是给点建议。 请用问题的详细信息更新问题。你被困在哪里,你需要什么

    当你有这个工作,我建议张贴到,并获得一些良好的编码风格和实践更多的反馈

    要测试对角线,可以使用:

    // y = row number, so y = y + 1 means the row below y
    // x = column number. so x = x + 1 means the column to the right of x
    public boolean isSolved(char[][] board) {
        // check horizontal rows
        return isHorizontalSolved(board) || isVerticalSolved(board) || isDiagonalSolved(board);
    }
    
    public boolean isHorizontalSolved(board) {
        for (int y = 0; y < board.length; ++y) { 
            // for each row, test if all columns are the same
            boolean solved = true;
            char first = board[y][0];
            for (int x = 0; x < boards[y].length; ++x) {
                if (boards[y][x] == ' ' || first != boards[y][x]) { 
                    // if an element is not filled in, this row is not solved
                    // if an element in this row is different than any other element, this row is not solved
                    solved = false;          
                }
            }
            if (solved == true) {
                return true;
            }
        }
        return false;
    }
    
    // check vertical rows
    // leaving this for your own imagination
    
    // check diagonals
    public boolean isDiagonalSolved(char[][] board) {
        // check topLeft to bottomRight:
        char first = board[0][0];
        boolean solved = true;
        for (int y = 0, x = 0; y < board.length && x < board[y].length; ++y, ++x) {
            if (board[y][x] == ' ' || first != board[y][x]) {
                // if field is empty or the fields are not all equal to one another
                solved = false;
            }
        }
        if (solved) {
            return true;
        }
    
        int topRightX = board[0].length - 1;
        solved = true;
        first = board[0][topRightX];
        for (int y = 0, x = topRightX; y < board.length && x >= 0; ++y, --x) {
            if (board[y][x] == ' ' || first != board[y][x]) {
                // if field is empty or the fields are not all equal to one another
                solved = false;
            }
        }
        if (solved) {
            return true;
        }
    
    }
    
    //y=行号,因此y=y+1表示y下面的行
    //x=列编号。所以x=x+1表示x右边的列
    公共布尔值已解析(字符[][]板){
    //检查水平行
    返回isHorizontalSolved(board)| | isVerticalSolved(board)| | isDiagonalSolved(board);
    }
    公共布尔isHorizontalSolved(板){
    对于(int y=0;y