簡體   English   中英

Tic Tac Toe檢查獲勝者

[英]Tic Tac Toe Checking winner

我有勝利代碼的問題。 我已經嘗試了很長時間,我不知道問題是什么。 我試過調試,但我什么都沒有。(對不起瑞典的評論)

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

要做的兩件事(一般來說):

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]),因為除了字段你在檢查之外,只有兩個其他字段;)所以在水平和垂直檢查一個就足夠了(而且,你可以檢查這兩種可能性都在一起)。 而對於檢查對角線,不需要 - 只有兩種可能性以這種方式贏得比賽。

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

在你的isHorizo​​ntalSolved中你有一個錯誤。 如果出現以下情況,for循環結束括號應放在以下之前:

//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退出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM