【问题标题】:Tic Tac Toe Checking winner井字游戏获胜者
【发布时间】:2014-12-05 13:33:20
【问题描述】:

我的 win 代码有问题。我已经尝试了很长时间,我不知道问题是什么。我试过调试,但我没有从中得到任何东西。(对不起瑞典 cmets)

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

【问题讨论】:

  • 你能描述一下你认为算法的哪一部分工作不充分吗?这将帮助我们缩小我们应该检查的范围。
  • “赢码”是指检查是否有人赢了?
  • 我相信它是水平的或垂直的或两者兼而有之。
  • 老实说,它们都不起作用:P
  • 真的吗? xD哈哈。我的课现在要结束了,所以我要等到 2-3 小时后回家。 (让每个人都知道)

标签: java tic-tac-toe jcreator


【解决方案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 就足够了(此外,您可以在一个 for 中检查这两种可能性)。对于检查对角线,不需要 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;
    }

【讨论】:

    【解决方案2】:

    在你的 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 退出。

    【讨论】:

    • 并且可以在solved = false; 之后添加break;。写if (solved) { 也是更好的风格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多