2014-12-05 98 views
3

我有贏取代碼的問題。我一直在嘗試很長時間,我不知道問題是什麼。香港專業教育學院試圖調試,但我得到了,沒有什麼。(對不起,瑞典評論)Tic Tac Toe檢查贏家

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

你能描述你認爲算法的哪一部分工作不充分嗎?這將有助於我們縮小我們應該檢查的範圍。 – 2014-12-05 13:39:06

+0

「勝利代碼」是指檢查是否有人獲勝? – rzysia 2014-12-05 13:41:19

+0

我相信它是水平或垂直或兩者兼而有之。 – Marc 2014-12-05 13:41:25

回答

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

在你的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; 
} 

的方式,循環會在第一次迭代與真正的退出。

+0

並且可以在'resolved = false'之後添加'break';'。另外寫'if(resolved){'是更好的風格 – 2014-12-05 13:57:32