2013-12-09 105 views
0

我有一個井字遊戲的代碼,我需要一些幫助,因爲我不知道該怎麼做。 (與處理2.0)井字遊戲的勝利條件

1)我需要寫一個勝利的條件測試。給玩家1和玩家填寫完整的一行2.

它應該是這樣的

INT rowFilledByPlayer(INT行){//代碼

}

2)然後我需要在1測試類似列和對角線的功能)

功能,然後我要所有的功能

我該怎麼辦結合起來那?我希望有一個人可以幫助我。

在此先感謝。

這裏是我的代碼

// Global variables 

int board [][] = new int [3][3]; // Game board 3x3 array 
int cellWidth, cellHeight;  // Cell size computed from window size 
int player = 1; 
int rowFilledByPlayer; 

void setup() { 
    size (400, 400); 
    rectMode(CORNER); 
    ellipseMode(CORNER); 
    // Divide window in 3 by 3 cells 
    cellWidth = width/3; 
    cellHeight = height/3; 

    clearBoard(); 
} 

void draw() { 
    background(255); 
    drawBoard(); 
    if (boardFilled()) 
    drawGameOver(); 
} 

void drawGameOver() { 
    fill(0, 102, 153); 
    textSize(width/7); 
    textAlign(CENTER, CENTER); 
    text("Game Over!", width/2, height/2); 
} 

void mouseClicked() { 
    if (boardFilled()) 
    clearBoard(); 
    else 
    playerMove(); 
} 

void clearBoard() { 
    for (int row=0; row<3; row++) 
    for (int col=0; col<3; col++) 
     board[row][col] = 0; 
} 

void drawBoard() { 
    for (int row=0; row<3; row++) 
    for (int col=0; col<3; col++) 
     drawCell(row, col); 
} 

void drawCell(int row, int col) { 

    noFill(); 
    stroke(0); 
    rect (col * cellWidth, row * cellHeight, cellWidth, cellHeight); 

    switch (board[row][col]) { 
    case 1: 
     ellipse(col * cellWidth, row * cellHeight, cellWidth, cellHeight); 
    break; 
    case 2: 
     line(col * cellWidth, row * cellHeight, (col+1) * cellWidth, (row+1) * cellHeight); 
     line((col+1) * cellWidth, row * cellHeight, col * cellWidth, (row+1) * cellHeight); 
    break; 

    } 
} 


void playerMove() { 
    int row = mouseY/cellHeight; 
    int col = mouseX/cellWidth; 

    if (board[row][col] == 0) { 
    board[row][col] = player; 
    player = oppositePlayer(); 
    } 
} 

int oppositePlayer() { 
    return player = 3 - player; 
} 

boolean boardFilled() { 
    for (int row = 0; row < 3; row++) { 
    if (!rowFilled(row)) 
     return false; 
    } 
    return true; 
} 

boolean rowFilled(int row) { 
    for (int col = 0; col < 3; col++) { 
    if (board[row][col] == 0) return false; 
    } 
    return true; 
} 

void CheckRows(); 
+2

什麼是你努力這麼遠?你看過'rowfilled'方法嗎?這似乎是相當不錯的候選人複製到'rowFilledByPlayer'然後修改。向我們展示您的個人努力! –

回答

0

首先,你必須尋找三排和三列,如果它們被填充。 之後,你必須檢查兩個對角線。 此外,我建議通過boardFilled傳遞播放器變量以獲得更乾淨的代碼!

此方法將取代你的方法boardFilledrowFilledCheckRows

boolean boardFilled(int player) { 
    for (int i = 0; i < 3; i++) { 
    if (board[i][0] == player && board[i][1] == player && board[i][2] == player) { 
     return true; 
    } 

    if (board[0][i] == player && board[1][i] == player && board[2][i] == player) { 
     return true; 
    } 
    } 

    if (board[0][0] == player && board[1][1] == player && board[2][2] == player) { 
     return true; 
    } 

    if (board[0][2] == player && board[1][1] == player && board[2][0] == player) { 
     return true; 
    } 

    return false; 
}