Winning Conditions for Tic Tac Toe Game

Hi guys.

I have the code of a tic tac toe game and I need some help because I dont know how to do it. ( with Processing 2.0)

1) I need to write a winning condition test. for a complete row filled by Player 1 and for player 2.

it should look like this

int rowFilledByPlayer(int row){ //code

}

2) then I need functions for testing columns and diagonals similar to the function in 1)

and then I have to combine all the functions

How can I do that ? I hope someone can help me.

Thanks in advance.

Here is my Code

// Global variables

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

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

Answers

  • I tried to start like this :

    return 1 means that all 3 colums are filled by player 1 for player 2 it should return 2

        int rowFilledByPlayer(int row) {
            boolean CheckAllOne = true;
            for (int col = 0; col < 3; col++)
            if (
            {
    
            }
    
          return 1;
        }
    
  • well, board[row][0] is the value of the the first column, board[row][1] is the second, board[row][2] is the third...

    you just need to check that all 3 values are the same (and not zero) and then return that value.

    you don't really need the for loop (imo).

  • edited December 2013

    If you store +1 for one player, -1 for the other in a 2d array, the logic is simple:

    If absolute value of any row, column or diagonal sums to 3, a player has won.

          int diag1 = 0, diag2 = 0;
          for (int i=0; i<3; i++) {
            int rowSum = 0;
            int colSum = 0;
              diag1 += board[i][i];
              diag2 += board[i][2-i];
            for (int j=0; j<3; j++) {
              rowSum += board[i][j];
              colSum += board[j][i];
    
            }
            if (abs(rowSum) == 3 || abs(colSum) == 3 || abs(diag1) == 3 || abs(diag2) == 3) {
               // declare winner
            }
          }
    
  • edited December 2013

    since you represent your board as a grid you can easily make a list of the rows, cols and diags:

    you need to convert the values into x,y of the grid

    int[][] possibleWins = {
      {    
        0, 1, 2
      }  
      , 
      {    
        3, 4, 5
      }
      , 
      {    
        6, 7, 8
      }
      , 
      {    
        0, 3, 6
      }
      , 
      {    
        1, 4, 7
      }
      , 
      {    
        2, 5, 8
      }
      , 
      {    
        0, 4, 8
      }
      , 
      {    
        2, 4, 6
      }
    };
    
    int rowSum = 0; 
    
    // i represents the row to test, j the cell within this row 
    
    for (int i=0; i < possibleWins.length; i++) {
      rowSum = 0; 
      for (int j=0; j < possibleWins[1].length; j++) {
        // 
        print (possibleWins[ i ][ j ] ) ;
        int x=possibleWins[ i ][ j ] % 3;
        int y=possibleWins[ i ][ j ] / 3;
        print (" is " + x+"," +y + " " + "--- ");
        // test now x,y 
        rowSum += board[x][y];
      }
      if  ( abs(rowSum) == 3 ) {
        // game over = true
      }
      println(  );
    }
    //
    
  • this is all very well but if you read the original post you'll see that it's an assignment and the method signatures are explicitly defined. your answers are all good but no actual use 8)

  • also with assignments it's better to supply hints rather than complete solutions otherwise they'll never learn. I know it's tempting if something is quite poorly written to just redo it but...

  • ok, I see

    Thanks, koogs...

Sign In or Register to comment.