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