2014-01-15 23 views
0

我有編程任務,需要製作2D棋盤遊戲。我試圖製作的遊戲是連接4遊戲。我的問題是,我似乎無法獲得勝利條件。有沒有人有任何建議。我在編程方面還是比較新的,所以我很抱歉,如果它是一個簡單的修復。這是我的代碼:連接4 Java Win條件檢查

import java.io.*; 
import java.net.*; 

class C4GameSession implements C4Constants { 

private Socket player1; 
private Socket player2; 


// Create and initialize cells 
private char[][] cell = new char[6][7]; 

private DataInputStream fromPlayer1; 
private DataOutputStream toPlayer1; 
private DataInputStream fromPlayer2; 
private DataOutputStream toPlayer2; 

// Continue to play 
private boolean continueToPlay = true; 

/** Construct a thread */ 

public C4GameSession(Socket player1, Socket player2) { 

this.player1 = player1; 
this.player2 = player2; 

// Initialize cells with a blank character 

for (int i = 0; i < 42; i++) 
    for (int j = 0; j < 42; j++) 
    cell[i][j] = ' '; 
} 

public void runGame() { 

try { 

    // Create data input and output streams 

    DataInputStream fromPlayer1 = new DataInputStream(player1.getInputStream()); 
    DataOutputStream toPlayer1 = new DataOutputStream(player1.getOutputStream()); 
    DataInputStream fromPlayer2 = new DataInputStream(player2.getInputStream()); 
    DataOutputStream toPlayer2 = new DataOutputStream(player2.getOutputStream()); 

    // Write anything to notify player 1 to start 
    // This is just to let player 1 know to start 

    // in other words, don't let the client start until the server is ready 

    toPlayer1.writeInt(CONTINUE); 

    // Continuously serve the players and determine and report 
    // the game status to the players 

    while (true) { 

    // Receive a move from player 1 

    int row = fromPlayer1.readInt(); 
    int column = fromPlayer1.readInt(); 

    cell[row][column] = 'X'; 

    // Check if Player 1 wins 

    if (isWon('X')) { 
     toPlayer1.writeInt(PLAYER1_WON); 
     toPlayer2.writeInt(PLAYER1_WON); 
     sendMove(toPlayer2, row, column); 
     break; // Break the loop 
    } 
    else if (isFull()) { // Check if all cells are filled 
     toPlayer1.writeInt(DRAW); 
     toPlayer2.writeInt(DRAW); 
     sendMove(toPlayer2, row, column); 
     break; 
    } 
    else { 

     // Notify player 2 to take the turn - as this message is not '1' then 
     // this will swicth to the relevant player at the client side 

     toPlayer2.writeInt(CONTINUE); 

     // Send player 1's selected row and column to player 2 
     sendMove(toPlayer2, row, column); 
    } 

    // Receive a move from Player 2 
    row = fromPlayer2.readInt(); 
    column = fromPlayer2.readInt(); 

    cell[row][column] = 'O'; 

    // Check if Player 2 wins 
    if (isWon('O')) { 
     toPlayer1.writeInt(PLAYER2_WON); 
     toPlayer2.writeInt(PLAYER2_WON); 
     sendMove(toPlayer1, row, column); 
     break; 
    } 
    else { 
     // Notify player 1 to take the turn 
     toPlayer1.writeInt(CONTINUE); 

     // Send player 2's selected row and column to player 1 
     sendMove(toPlayer1, row, column); 
    } 
    } 
    } 
    catch(IOException ex) { 
    System.err.println(ex); 
    } 
} 

/** Send the move to other player */ 
private void sendMove(DataOutputStream out, int row, int column) throws IOException { 

out.writeInt(row); // Send row index 
out.writeInt(column); // Send column index 
} 

/** Determine if the cells are all occupied */ 

private boolean isFull() { 

for (int i = 0; i < 43; i++) 
    for (int j = 0; j < 43; j++) 
    if (cell[i][j] == ' ') 
     return false; // At least one cell is not filled 

// All cells are filled 
return true; 
} 

/** Determine if the player with the specified token wins */ 

private boolean isWon(char token) { 

/* 
int count = 0; 
for (int i = 0; i < 6; ++i) 
for (int j = 0; j < 7; ++j) 
    if (cell[i][j] == token) 
    ++count; 
    if (count == 4) 
     return true; // found 
    /* else 
    count = 0; // reset and count again if not consecutive 
    */ 

int count_piece = 0; 

    //Checking Horizontal Win 
    for (int i = 0; i < 6; i++) { 
     count_piece = 0; 
     for (int j = 0; j < 7; j++) { 

      if (cell[i][j] == 'X') { 
       count_piece++; 
       if (count_piece == 4) { 
        System.out.println("you win"); 
        return true; 
       } 

      } else { 
       count_piece = 0; 
      } 
      } 
     } 

    return false; // no 4-in-a-line found 

    } 
} 
+1

澄清「不起作用」。你會得到一個編譯錯誤,運行時異常,誤報,漏報,什麼? – cHao

+0

問題是即使我連續連接四個遊戲,遊戲也無法識別它並且沒有聲明勝利者。 – user3199712

+0

快速瀏覽一下,從'isWon'方法中的邏輯看起來,如果4個棋子在同一行,從左到右,看起來唯一贏得勝利的方式。在連接4(如果有內存服務),你可以贏得對角,垂直或水平。做一個測試,看看你的遊戲是否「水平」排列起來? –

回答

1

(我將在僞寫)

開始用一個簡單的方法:您需要檢查的縱向,橫向和對角線的勝利,然後做三個獨立的校驗碼塊(你不需要一次解決所有問題)。

一個用於水平方向:

for(every row) 
    count = 0; 
    for(each column) 
     if(cell value = token) 
      then count++; 
     else // reset the counting, the eventual sequence has been interrupted 
      count = 0; 

    if(count >= 4) then win = 1; // you can break out here, when improving you can break out directly in the inner for loop if count is => 4 

如果沒有檢測到的勝利,去爲垂直方向:

// similar comments for the previous block apply here 
for(every column) 
    count = 0; 
    for(each row) 
     if(cell value = token) 
      then count++; 
     else 
      count = 0; 

    if(count >= 4) then win = 1 and break; 

如果沒有檢測到的勝利,去對角線方向:

// a bit harder, you have to move diagonally from each cell 
for(every column from the left) 
    for(each row from the top) 
     count = 0 
      for(delta starting from 0 to 5) 
       // add more checks to avoid checking outside the cell matrix bounds 
       // when improving the code, you can compute a better end for the delta 
       if(cell[row+delta][column+delta] = token) 
        then count++; 
       else 
        count = 0; 

當你編寫並測試了所有這三個部分時,如果你想要,你可以逐步改進算法m,即從底部開始而不是從頂部開始(因爲大多數上級單元在遊戲的大部分時間將是空的)。接下來,因爲必須找到具有相同元素的4個連續單元格,如果你在檢查一行時可能沒有找到足夠的連續令牌,您可能會更早停止,而不是檢查該行中的所有7個單元。

儘管我沒有給你提供工作代碼的完整解決方案,但我希望我的回答能讓你走上正軌。

+0

謝謝你的回覆。請您介紹一下delta的含義,以及您通過「cell value」所表達的含義。對於不理解的道歉,因爲我還是這個新手。 – user3199712

+0

@ user3199712希望沃特迪不介意我回答這個問題。 「單元格值」表示該行和列的值。換句話說,你可以把你的'char []'數組看作一個表。位置'row'和'column'的值是單元格值,類似於你的'cell [i] [j]',其中'i'是行,'j'是該行的列。在這種情況下,「delta」只是當前行和列索引的修飾符。要確定一個對角線的勝利,你可以從一個單元格開始,並通過在行和列上添加三角形來對角地進行遍歷。 –

+0

謝謝你的回覆。你是否認爲你可以用示例代碼向我展示你對delta的含義,因爲我仍然不確定這個問題。再次抱歉。 – user3199712