Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Bejeweled.java (Skeleton)
- *
- * This class represents a Bejeweled (TM)
- * game, which allows player to make moves
- * by swapping two pieces. Chains formed after
- * valid moves disappears and the pieces on top
- * fall to fill in the gap, and new random pieces
- * fill in the empty slots. Game ends after a
- * certain number of moves or player chooses to
- * end the game.
- */
- import java.awt.Color;
- public class Bejeweled {
- /*
- * Constants
- */
- final Color COLOUR_DELETE = Color.RED;
- final Color COLOUR_SELECT = new Color(224, 227, 229);
- final int CHAIN_REQ = 3; // minimum size required to form a chain
- final int NUMMOVE = 10; // number of moves to be play in one game
- final int EMPTY = -1; // represents a slot on the game board where the piece has disappear
- final int NUMPIECESTYLE; // number of different piece style
- final int NUMROW; // number of rows in the game board
- final int NUMCOL; // number of columns in the game boar
- final int MOVES = 20; // number of moves available
- final int WHITE = 7; // number of the white piece
- /*
- * Global variables
- */
- BejeweledGUI gui; // the object referring to the GUI, use it when calling methods to update the
- // GUI
- int board[][]; // the 2D array representing the current content of the game board
- boolean highlighted[][]; // array that keep tracks of all the highlight slots
- boolean firstSelection; // indicate if the current selection is the selection of the first piece
- int slot1Row, slot1Col; // store the location of the first selection
- int score; // current score of the game
- int numMoveLeft; // number of move left for the game
- boolean horChainMade; // checking if a horizontal chain was made
- boolean verChainMade; // checking if a vertical chain was made
- int icon; // new icon that fills in the slots using gui
- int tempChainCount;
- int tempWhiteChecker;
- /**************************
- * Constructor: Bejeweled
- **************************/
- public Bejeweled(BejeweledGUI gui) {
- this.gui = gui;
- NUMPIECESTYLE = gui.NUMPIECESTYLE;
- NUMROW = gui.NUMROW;
- NUMCOL = gui.NUMCOL;
- // TO DO:
- // - creation of arrays
- // - initialization of variables
- // - initialization of game board (on 2D array and on GUI)
- // initializing and creating the starting game board
- initBoard();
- // array to keep track of which pieces are highlighted
- highlighted = new boolean[NUMROW][NUMCOL];
- // setting the score
- score = 0;
- // setting the number of moves left
- numMoveLeft = MOVES;
- firstSelection = false;
- chainCheckHorizontal();
- chainCheckVertical();
- if (horChainMade == true || verChainMade == true) {
- // checking for highlighted pieces, each highlight is one point
- for (int i = 0; i < NUMROW; i++) {
- for (int j = 0; j < NUMCOL; j++) {
- if (highlighted[i][j] == true) {
- score++;
- }
- }
- }
- // updating the score
- gui.setScore(score);
- // shifting all pieces down and replacing the empty slots with a random piece
- fallAndReplace();
- }
- }
- /*****************************************************
- * play This method is called when a piece is clicked. Parameter "row" and
- * "column" is the location of the piece that is clicked by the player
- *****************************************************/
- public void play(int row, int column) {
- // TO DO: implement the logic of the game
- // checking if the first selection has been made
- if (!firstSelection) {
- // unhighlighting everything
- for (int i = 0; i < NUMROW; i++) {
- for (int j = 0; j < NUMCOL; j++) {
- gui.unhighlightSlot(i, j);
- }
- }
- // highlighting the selected slot
- gui.highlightSlot(row, column, COLOUR_SELECT);
- // storing the selcted slot's location
- slot1Row = row;
- slot1Col = column;
- firstSelection = true;
- } else {
- // setting firstSelection back to false so the player can make next move
- firstSelection = false;
- gui.unhighlightSlot(slot1Row, slot1Col);
- // storing the location of the second selection
- int slot2Row = row;
- int slot2Col = column;
- // checking if the selections are adjacent
- if (adjacentPieces(slot1Row, slot1Col, slot2Row, slot2Col)) {
- // retrieving the icons of the selections and storing them in a variable
- int icon1 = board[slot1Row][slot1Col];
- int icon2 = board[slot2Row][slot2Col];
- // swapping the values of the selections in the actual board array
- board[slot1Row][slot1Col] = icon2;
- board[slot2Row][slot2Col] = icon1;
- // swapping the icons on the display
- gui.setPiece(slot1Row, slot1Col, icon2);
- gui.setPiece(slot2Row, slot2Col, icon1);
- // resetting all pieces in the highlight array to false
- for (int i = 0; i < NUMROW; i++) {
- for (int j = 0; j < NUMCOL; j++) {
- highlighted[i][j] = false;
- }
- }
- // resetting the variables that tell you if a chain was made to false
- horChainMade = false;
- verChainMade = false;
- // checking for chains and highlighting them if they are formed
- chainCheckHorizontal();
- chainCheckVertical();
- // checking if a chain was made horizontally or vertically
- if (horChainMade == true || verChainMade == true) {
- // checking for highlighted pieces, each highlight is one point
- for (int i = 0; i < NUMROW; i++) {
- for (int j = 0; j < NUMCOL; j++) {
- if (highlighted[i][j] == true) {
- score++;
- }
- }
- }
- // updating the score
- gui.setScore(score);
- // updating the moves left
- numMoveLeft--;
- gui.setMoveLeft(numMoveLeft);
- // shifting all pieces down and replacing the empty slots with a random piece
- fallAndReplace();
- // resetting all pieces in the highlight array to false
- for (int i = 0; i < NUMROW; i++) {
- for (int j = 0; j < NUMCOL; j++) {
- highlighted[i][j] = false;
- }
- }
- // checking how many moves are left to determine whether or not the game will
- // end
- if (numMoveLeft == 0) {
- gui.showGameOverMessage(score, (MOVES - numMoveLeft));
- gui.resetGameBoard();
- }
- // no chain was made, so the values are swapped back to original positions and
- // an error msg is outputted
- } else {
- // swapping the values of the selections back to their original positions
- board[slot1Row][slot1Col] = icon1;
- board[slot2Row][slot2Col] = icon2;
- // swapping the values of the grpahics back to their original positions
- gui.setPiece(slot1Row, slot1Col, icon1);
- gui.setPiece(slot2Row, slot2Col, icon2);
- // outputting invalid move message
- gui.showInvalidMoveMessage();
- }
- } else {
- // outputting invalid move message
- gui.showInvalidMoveMessage();
- }
- }
- }
- // initializing the board
- public void initBoard() {
- board = new int[NUMROW][NUMCOL];
- for (int i = 0; i < NUMROW; i++) {
- for (int k = 0; k < NUMCOL; k++) {
- icon = (int) (Math.random() * NUMPIECESTYLE);
- board[i][k] = icon;
- gui.setPiece(i, k, icon);
- }
- }
- }
- // method to check if two pieces selected are adjacent
- public boolean adjacentPieces(int row1, int col1, int row2, int col2) {
- boolean adjacent = false;
- if (row1 == row2) {
- if ((Math.abs(col1 - col2)) == 1) {
- adjacent = true;
- }
- } else if (col1 == col2) {
- if ((Math.abs(row1 - row2)) == 1) {
- adjacent = true;
- }
- }
- return adjacent;
- }
- // checking all horizontal chains
- public void chainCheckHorizontal() {
- int currentPiece;
- int chain;
- for (int j = 0; j < NUMROW; j++) {
- currentPiece = board[j][0];
- chain = 1;
- for (int k = 1; k < NUMCOL; k++) {
- if (board[j][k] == currentPiece) {
- chain++;
- } else {
- if (chain >= CHAIN_REQ) {
- for (int i = (k - 1); i >= (k - chain); i--) {
- gui.highlightSlot(j, i, COLOUR_SELECT);
- highlighted[j][i] = true;
- }
- horChainMade = true;
- gui.showChainSizeMessage(chain);
- }
- currentPiece = board[j][k];
- chain = 1;
- }
- if (k == 7) {
- if (chain >= CHAIN_REQ) {
- for (int i = k; i >= (k - chain + 1); i--) {
- gui.highlightSlot(j, i, COLOUR_SELECT);
- highlighted[j][i] = true;
- }
- horChainMade = true;
- gui.showChainSizeMessage(chain);
- }
- }
- }
- }
- }
- // checking all vertical chains
- public void chainCheckVertical() {
- int currentPiece;
- int chain;
- for (int j = 0; j < NUMCOL; j++) {
- currentPiece = board[0][j];
- chain = 1;
- for (int k = 1; k < NUMROW; k++) {
- if (board[k][j] == currentPiece) {
- chain++;
- } else {
- if (chain >= CHAIN_REQ) {
- ;
- for (int i = (k - 1); i >= (k - chain); i--) {
- gui.highlightSlot(i, j, COLOUR_SELECT);
- highlighted[i][j] = true;
- }
- verChainMade = true;
- gui.showChainSizeMessage(chain);
- }
- currentPiece = board[k][j];
- chain = 1;
- }
- if (k == 7) {
- if (chain >= CHAIN_REQ) {
- for (int i = k; i >= (k - chain + 1); i--) {
- gui.highlightSlot(i, j, COLOUR_SELECT);
- highlighted[i][j] = true;
- }
- verChainMade = true;
- gui.showChainSizeMessage(chain);
- }
- }
- }
- }
- }
- // moving all pieces down to fill in the chains, and filling in the empty slots
- // with random pieces
- public void fallAndReplace() {
- // setting highlighted pieces to white
- for (int i = (NUMROW - 1); i >= 0; i--) {
- for (int j = 0; j < NUMCOL; j++) {
- if (highlighted[i][j] == true) {
- board[i][j] = WHITE;
- }
- }
- }
- // shifting pieces down if there is a white piece
- for (int i = (NUMROW - 1); i >= 0; i--) {
- for (int j = 0; j < NUMCOL; j++) {
- if (board[i][j] == WHITE) {
- tempChainCount = 0;
- tempWhiteChecker = i;
- while (board[tempWhiteChecker][j] == WHITE) {
- if (board[tempWhiteChecker][j] == WHITE && tempWhiteChecker >= 0) {
- tempChainCount++;
- }
- tempWhiteChecker--;
- }
- for (int l = 0; l <= i; l++) {
- if (i - l - tempChainCount >= 0) {
- board[i - l][j] = board[i - l - tempChainCount][j];
- icon = board[i - l][j];
- gui.setPiece(i - l, j, icon);
- } else {
- board[i - l][j] = WHITE;
- icon = board[i - l][j];
- gui.setPiece(i - l, j, icon);
- }
- }
- }
- }
- }
- // setting the white pieces to a random colored piece
- for (int i = 0; i < NUMROW; i++) {
- for (int j = 0; j < NUMCOL; j++) {
- if (board[i][j] == WHITE) {
- icon = (int) (Math.random() * NUMPIECESTYLE);
- board[i][j] = icon;
- gui.setPiece(i, j, icon);
- }
- }
- }
- }
- /*
- * public int horizontalRight (int row, int col, int piece) { int samePiece = 0;
- * col++;
- *
- * while (col < NUMCOL && board [row][col] == piece) { samePiece++; col++; }
- *
- * return samePiece; }
- *
- * public int horizontalLeft (int row, int col, int piece) { int samePiece = 0;
- * col--;
- *
- * while (col >= 0 && board [row][col] == piece) { samePiece++; col--; }
- *
- * return samePiece; }
- *
- * public int verticalUp (int row, int col, int piece) { int samePiece = 0;
- * row++;
- *
- * while (row < NUMROW && board [row][col] == piece) { samePiece++; row++; }
- *
- * return samePiece; }
- *
- * public int verticalDown (int row, int col, int piece) { int samePiece = 0;
- * row--;
- *
- * while (row >= 0 && board [row][col] == piece) { samePiece++; row--; }
- *
- * return samePiece; }
- */
- /*
- * public void chain () { int currentIcon; int currentChainSize = 0; int
- * chainSize = 0; int score = 0;
- *
- *
- * for (int j = 0; j < NUMPIECESTYLE; j++) { //looping thru each type of icon
- * currentIcon = j; //storing the current icon being looped to a variable for
- * (int i = 0; i < NUMROW; i++) { //looping thru each row to see if there is a
- * chain formed with the current icon for (int k = 0; k < (NUMCOL - 2); k++) {
- * //looping thru each column of the row up until the 6th column if (board
- * [i][k] == currentIcon) { //checking if the current locaiton being checked is
- * equal to the current icon chainSize++;
- *
- * int y = k + 1; //storing the k value in a new variable so it can be used in
- * another loop
- *
- * while (board [i][y] == currentIcon && y < NUMCOL) { //looping thru the rest
- * of the row to see if a chain forms chainSize++; y++; }
- *
- * if (chainSize >= CHAIN_REQ) { //checking if the chain formed is 3 or more if
- * (chainSize > currentChainSize) { //checking if the chain formed in this
- * specific row is the largest chain there is score += chainSize; //updating the
- * score firstRow = i; firstCol = k; lastRow = i; lastCol = y; } } chainSize =
- * 0; //setting the chain size back to 0 for the next loop to be made } } } } }
- */
- /*****************************************************
- * endGame This method is called when the player clicks on the "End Game" button
- *****************************************************/
- public void endGame() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement