Tuesday, February 7, 2017

Code to make a 3D Tic-Tac-Toe Boad


/**
*[TTTBoard3D]

*NAME:       [Gunarath A. M. Raveen]
*Assignment: []
*QUESTION:   []
*
*PURPOSE:    [To store the states of the 3D tic-tac-toe including the player turn, message to be displayed,
              reaction to the mouse clicks and determine the winner of the game]
*/

import java.awt.Color;
public class TTTBoard3D {

  private int size;//Size of the board
  private int[][][] board; //Game board
  private int winRow =150;//This will store the line that contain the winning posibility. Default is 150 because it is 
                          //larger than the max posibilities that can get from size =6.
  private static final int EMPTY = 0;
  private String message;//Message to be display under the board
  private int playerNumber = 1;//Player number
  private final Color[] boxColor = {Color.WHITE,Color.RED,Color.BLUE,Color.GREEN};//Colors of the board

//Constructor that initialize the initial message, board size and the board 
  public TTTBoard3D(int boardSize){
    message = "Player 1 Turn";
    size = boardSize;
    board = new int[boardSize][boardSize][boardSize];
  }
//

//To return the color of the box for the given coordinate and if a player wins the 
//game methord should make the winning line a different color.
  public Color getSquareColor(Coordinate c){
    Coordinate[ ][ ] waysToWin = WinLines.generateWinningLines(size);
    if (winCheck()!=EMPTY){
      for(int i=0; i<size; i++){
        Coordinate x = waysToWin[winRow][i];
        board[x.level][x.row][x.column] = 3;
      }
    }
    return boxColor[board[c.level][c.row][c.column]];
  }

  
  //Return the message to be display under the game board.
  public String getMessage( ){
    return message;
  }

  //Do nothing if the game is over, change the message if click on a used square, change the message to indicate 
  //player turn and update the board.If a player wins the game detect the winner and change the message accordingly.  
  public void handleClick(Coordinate c){
    if (gameOver() || winCheck() !=EMPTY || winRow !=150 ){
    }
    else{
      if (board[c.level][c.row][c.column] !=EMPTY){
         message = "Invalid Move";
      }
      else{
        if (playerNumber == 1){
          board[c.level][c.row][c.column] = 1;
          playerNumber ++;
          message = "Player 2 Turn";
        }
        else{
           board[c.level][c.row][c.column] = 2;
           playerNumber --;
           message = "Player 1 Turn";
        }
      }
      if (winCheck() == 1){
        message = "Player 1 Wins";
      }
      else if (winCheck() == 2){
        message = "Player 2 Wins";
      }
      else if (gameOver())
        message = "Game Over";
    }
  }

//Get the all the possible coordinates from the generateWinningLines method. For each possibility store the 
//board numbers in a array and check the winner and return the winner number. If no winner return zero.
  private int winCheck(){
   Coordinate[ ][ ] waysToWin = WinLines.generateWinningLines(size);
   int[] moves =new int[size];
   for(int r=0; r<waysToWin.length; r++){
     for(int i=0; i<size; i++){
       Coordinate z = waysToWin[r][i];
       moves[i] = board[z.level][z.row][z.column];
     }
     int winner = arrayCheck(moves);
       if (winner ==1){
         winRow = r;
         return 1;
     }
       else if (winner==2){
         winRow = r;
         return 2;
       }
     }
     return EMPTY;
  }


//Compare items in the array with first element and it they are not equal return zero or if they are equal return the 
//first element.
  private int arrayCheck(int[] val){
    int first = val[EMPTY];
    for (int i=1; i<size; i++){
       if (val[i] != first)
         return EMPTY;
    }
    return first;
  }

//Check whether the game is over because the board is full.
  private boolean gameOver(){
    for (int i=0; i<size; i++){
      for (int j=0; i<size; i++){
        for (int k=0; i<size; i++){
          if (board[i][j][k] == EMPTY){
            return false;
          }
        }
      }
    }
    return true;
  }

}

No comments:

Post a Comment