0

Here is how the entire thing looks like now.

And then there is the details that you're forced to add and more details and more details and more details and more and more and more and more and more and more and more and more and more and more and more

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);
            }
        }
    }
}
4

1 回答 1

0

根据您到目前为止发布的内容(我真的希望进行编辑以澄清您正在尝试的内容)

我假设您正在尝试构建一个在电路板完成之前不会结束的循环。在完成之前,会要求玩家移动。采取行动(如果合法)。然后印制电路板。

您的代码有许多问题:

  1. CheckMove(spelplan, rad, kolumn) 只在 rad=3,kolumn=3 时执行。我希望这将使用用户输入运行。
  2. 您重用循环变量。(int i = 新扫描仪)和更高版本(对于 i=i)。永远不要那样做。i=i 无论如何都是毫无意义的,你最终会做 i++ 这意味着你失去了用户输入。
  3. 在 while 循环中,您永远不会执行 makeMove(spelplan, i, j)。我希望这会在打印出电路板之前发生。
  4. 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,对 col 使用 forloop。

你的 checkmove 不检查董事会。它检查 board[x][y] != board[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。

好的,我尽量不破坏您的乐趣,只是提供指点。请使用您的问题的详细信息更新问题。你在哪里卡住了,你需要什么。

当您完成这项工作时,我建议您将其发布到https://codereview.stackexchange.com/并获得有关良好编码风格和实践的更多反馈。

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

// 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;
    }

}

不能保证它的错误更少,但这大致就是我会做的。对于对角线,遍历左上角到右下角 (0,0) (1,1) (2,2) 和右上角到左下角 (0, 2), (1,1) (2,0)

使用 3*3 的游戏板,您可以对其进行硬编码,但我习惯于使用循环。

于 2014-11-12T13:45:49.170 回答