2013-12-14 54 views
-2

我正在做連接4程序,我遇到了檢查獲勝者的問題。我知道我需要做什麼。尋找垂直贏家的'for'循環,水平贏家的循環以及對角贏家的循環。我想不出如何讓它尋找一個勝利者。如何檢查連接4與循環中的贏家

這是我迄今爲止

import javax.swing.JOptionPane; 
public class connect4 
{ 
    public static void main(String [] args)//[row][column] 
    { 
     String grid[][] = new String[7][8]; 
     for(int x = 1; x <= 6; x++) 
     { 
      for(int y = 1; y <= 7; y++) 
      { 
       grid[x][y] = " "; 
      } 
     } 
     String p1 = JOptionPane.showInputDialog("Player 1 pick a single letter for your piece."); 
     String p2 = JOptionPane.showInputDialog("Player 2 pick a single letter for your piece."); 
     board(grid); 
     playerinterface(grid, p1, p2); 
    } 

    public static void board(String grid[][]) 
    { 
     String board = 
      (" 1 2 3 4 5 6 7" + "\n" + 
       "| "+grid[1][1]+" | "+grid[1][2]+" | "+grid[1][3]+" | "+grid[1][4]+" | "+grid[1][5]+" | "+grid[1][6]+" | "+grid[1][7]+" | " +"\n" + 
       "| "+grid[2][1]+" | "+grid[2][2]+" | "+grid[2][3]+" | "+grid[2][4]+" | "+grid[2][5]+" | "+grid[2][6]+" | "+grid[2][7]+" | " +"\n" + 
       "| "+grid[3][1]+" | "+grid[3][2]+" | "+grid[3][3]+" | "+grid[3][4]+" | "+grid[3][5]+" | "+grid[3][6]+" | "+grid[3][7]+" | " +"\n" + 
       "| "+grid[4][1]+" | "+grid[4][2]+" | "+grid[4][3]+" | "+grid[4][4]+" | "+grid[4][5]+" | "+grid[4][6]+" | "+grid[4][7]+" | " +"\n" + 
       "| "+grid[5][1]+" | "+grid[5][2]+" | "+grid[5][3]+" | "+grid[5][4]+" | "+grid[5][5]+" | "+grid[5][6]+" | "+grid[5][7]+" | " +"\n" + 
       "| "+grid[6][1]+" | "+grid[6][2]+" | "+grid[6][3]+" | "+grid[6][4]+" | "+grid[6][5]+" | "+grid[6][6]+" | "+grid[6][7]+" | " 
      ); 
     JOptionPane.showMessageDialog(null, board); 
    } 

    public static void playerinterface(String grid[][], String p1, String p2) 
    { 
     boolean done = false; 
     while(!done) 
     { 
      int player = 1;   
      if(player%2 != 0)//player1 
      { 
       player++; 
       String q = JOptionPane.showInputDialog("    PLAYER 1"+"\n"+"Put in the number of the column that"+"\n"+"you woulf like to drop your piece in."); 
       int choice1 = Integer.parseInt(q); 

      } 
      else//player2 
      { 
       player++; 
       String q2 = JOptionPane.showInputDialog("    PLAYER 2"+"\n"+"Put in the number of the column that"+"\n"+"you woulf like to drop your piece in."); 
       int choice2 = Integer.parseInt(q2); 
      } 
     } 
    } 

    public static void drop(int choice1, int choice2, int player) 
    { 
     while() 
     { 
     } 
    } 

    public static void check() 
    { 
     for(int x = 1; x <= 6; x++)//rows 
     { 
     } 
    } 
} 
+0

你是不是指'心理'? –

+0

哈哈大聲笑雅我剛剛取得了我正在聽的歌曲的名稱,我想我拼錯了它。哎呀哈哈 –

回答

1

你能分享你已經有任何的代碼?我只是從我的頭頂ra but作響,但我如何能夠做到這一點..

有一種有限的方式可以贏得連接4(取決於你的棋盤有多大),一種方法會有一種檢查所有垂直行並查找「勝利」的方法,一種方法查看水平行,另一種查看對角線「勝利」(兩種方式)。然後在我的代碼我可以撥打

checkVertial() 
checkHorizontal() 
checkDiagonalRight() 
checkDiagonalLeft() 

並檢查是否一個返回true。在這些方法中,只需循環遍歷x和y行並查看是否存在(布爾值,如果標記放置在該點中)?

如果紙板大小大於標記的數量,這可能會變得棘手你需要一個勝利的一排。你將不得不檢查那些緊挨着對方的那個。

只是一個想法,不知道最好的解決方案是什麼,而不看你的代碼。

編輯: 我有另一個想法 2)每當玩家在棋盤上添加一個標記片段,將1添加到您的跟蹤值。因此,例如在遊戲中的第一個舉動我留下一個標記下來PLAYER1一些價值得到值1。現在,如果我把一個新的標記一塊了我的下一回合我可以:

1. check if its next to a red piece 
2. if it is add 1 to that value. 
3. if it isn't next to a red piece create a new value 

所以最終例如,如果該值達到5,那麼我們就贏了。這裏的訣竅是,如果我在棋盤的兩端加兩塊棋子,我需要做兩個變量來跟蹤。在我看來,這是棘手的,但不需要循環檢查每一次。我想象如果你這樣做了,你有一堆創建的變量,但你只關心那個達到最終目標的人。

+0

雅我在我的tic-tac-toe程序中做到了這一點,但我覺得它太低效了。它會是多麼低效? –

+0

您的董事會規模有多大,贏得多少連贏? – user3037172

+0

6行7列,並在任何方向連續需要4個 –