3

我的win代码有问题。我已经尝试了很长时间,我不知道问题是什么。我试过调试,但我什么也没得到。(对不起瑞典评论)

import java.util.Scanner;
public class Tictactoe {

static char[][] MakeMove (char[][] spelplan, char spelare, int rad, int kolumn){
spelplan[rad][kolumn]=spelare;
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("-------");

}

这是检查获胜者的代码部分

public static boolean KollaVinst(char[][] spelplan) {

return isHorizontalSolved(spelplan) || isVerticalSolved(spelplan) || isDiagonalSolved(spelplan);
}

水平的

 //Kollar om horisontella är löst
 public static boolean isHorizontalSolved(char[][] spelplan) {
 for (int y = 0; y < spelplan.length; ++y) {
    //För varje rad kolla om varje kolumn är fylld
    boolean solved = true;
    char first = spelplan[0][y];
    for (int x = 0; x < spelplan[y].length; ++x) {
        if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
            // Om en kolumn inte är fylld så är raden inte klar
            // Om en kolumn i raden är fylld med olika tecken så är den inte klar
            solved = false;
        }

    if (solved == true) {
        return true;
    }
}
}
return false;
}

水平端

垂直的

//Kollar om vertikala är löst
public static boolean isVerticalSolved(char[][] spelplan) {
for (int x = 0; x < spelplan.length; ++x) {

    boolean solved = true;
char first = spelplan[x][0];
for (int y = 0; y < spelplan[x].length; ++y){
    if (spelplan[x][y] == ' ' || first != spelplan[x][y]){
        solved = false;
    }
}
if (solved == true){
    return true;
}
}
return false;
}

垂直端。

对角线从左到右

// Kollar om digonalen är löst
public static boolean isDiagonalSolved(char[][] spelplan) {
// Kollar vänster till höger
char first = spelplan[0][0];
boolean solved = true;
for (int y = 0, x = 0; y < spelplan.length && x < spelplan[y].length; ++y, ++x) {
    if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
        //Om en plats är tom eller om det är olika tecken så är den inte klar
        solved = false;
    }
}
if (solved) {
    return true;
}

对角线从左到右两端

从右到左对角线

//Kollar höger till vänster
int topRightX = spelplan[0].length - 1;
solved = true;
first = spelplan[0][topRightX];
for (int y = 0, x = topRightX; y < spelplan.length && x >= 0; ++y, --x) {
    if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
        //Om en plats är tom eller om det är olika tecken så är den inte klar
        solved = false;
    }
}
return solved;
}

支票赢家代码到此结束。

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("-------");

    for (i=0; i<=9; i++){
    if (KollaVinst(spelplan) == false){
    break;
}
    else

    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

2 回答 2

1

要做两件事(一般来说):

1)改变你的主要课程 - 你在第一步之前和最后一步之后检查获胜者......所以游戏应该看起来像这样:

    for (i = 0; i < 9; i++) {
        if (KollaVinst(spelplan)) {
            break;
        } else {
            CheckMove(spelplan, rad, kolumn);
        }

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

2)检查获胜者 - 我升级了你的功能(见下文)。您不需要遍历所有内容(尤其是fileds [0] [x]),因为除了您检查的字段之外,只有其他两个字段;)所以在水平和垂直检查中,一个就足够了(此外,您可以检查这两种可能性合二为一)。并且对于检查对角线, for 是不需要的——以这种方式赢得比赛只有两种可能性。

    public static boolean isHorizontalSolved(char[][] spelplan) {
        boolean solved = false;
        for (int y = 1; y < spelplan.length; y++) {
            if ((spelplan[y][1] == spelplan[y][2]) && (spelplan[y][1] == spelplan[y][3]) && (spelplan[y][1] != ' ')) {
                solved = true;
            }
        }
        return solved;
    }

    public static boolean isVerticalSolved(char[][] spelplan) {
        boolean solved = false;
        for (int y = 1; y < spelplan.length; y++) {
            if ((spelplan[1][y] == spelplan[2][y]) && (spelplan[1][y] == spelplan[3][y]) && (spelplan[1][y] != ' ')) {
                solved = true;
            }
        }
        return solved;
    }

    public static boolean isDiagonalSolved(char[][] spelplan) {
        boolean solved = false;
        if ((spelplan[1][1] == spelplan[2][2]) && (spelplan[1][1] == spelplan[3][3]) && (spelplan[1][1] != ' ')) {
                solved = true;
            }

        if ((spelplan[1][3] == spelplan[2][2]) && (spelplan[1][3] == spelplan[3][1]) && (spelplan[1][3] != ' ')) {
                solved = true;
            }

        return solved;
    }
于 2014-12-05T14:22:36.513 回答
0

在你的 isHorizo​​ntalSolved 你有一个错误。for 循环结束括号应放在以下 if 之前:

//Kollar om horisontella är löst
public static boolean isHorizontalSolved(char[][] spelplan) {
    for (int y = 0; y < spelplan.length; ++y) {
        //För varje rad kolla om varje kolumn är fylld
        boolean solved = true;
        char first = spelplan[0][y];
        for (int x = 0; x < spelplan[y].length; ++x) {
           if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
              // Om en kolumn inte är fylld så är raden inte klar
              // Om en kolumn i raden är fylld med olika tecken så är den inte klar
              solved = false;
           }
        }

        if (solved == true) {
           return true;
        }
    }
    return false;
}

按照您的操作方式,循环将在第一次迭代中以 true 退出。

于 2014-12-05T13:55:23.140 回答