Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.96 KB | None | 0 0
  1. /**
  2. * Bejeweled.java (Skeleton)
  3. *
  4. * This class represents a Bejeweled (TM)
  5. * game, which allows player to make moves
  6. * by swapping two pieces. Chains formed after
  7. * valid moves disappears and the pieces on top
  8. * fall to fill in the gap, and new random pieces
  9. * fill in the empty slots.  Game ends after a
  10. * certain number of moves or player chooses to
  11. * end the game.
  12. */
  13.  
  14. import java.awt.Color;
  15.  
  16. public class Bejeweled {
  17.  
  18.     /*
  19.      * Constants
  20.      */
  21.     final Color COLOUR_DELETE = Color.RED;
  22.     final Color COLOUR_SELECT = new Color(224, 227, 229);
  23.  
  24.     final int CHAIN_REQ = 3; // minimum size required to form a chain
  25.     final int NUMMOVE = 10; // number of moves to be play in one game
  26.     final int EMPTY = -1; // represents a slot on the game board where the piece has disappear
  27.  
  28.     final int NUMPIECESTYLE; // number of different piece style
  29.     final int NUMROW; // number of rows in the game board
  30.     final int NUMCOL; // number of columns in the game boar
  31.  
  32.     final int MOVES = 20; // number of moves available
  33.  
  34.     final int WHITE = 7; // number of the white piece
  35.  
  36.     /*
  37.      * Global variables
  38.      */
  39.     BejeweledGUI gui; // the object referring to the GUI, use it when calling methods to update the
  40.                         // GUI
  41.  
  42.     int board[][]; // the 2D array representing the current content of the game board
  43.  
  44.     boolean highlighted[][]; // array that keep tracks of all the highlight slots
  45.  
  46.     boolean firstSelection; // indicate if the current selection is the selection of the first piece
  47.     int slot1Row, slot1Col; // store the location of the first selection
  48.  
  49.     int score; // current score of the game
  50.     int numMoveLeft; // number of move left for the game
  51.  
  52.     boolean horChainMade; // checking if a horizontal chain was made
  53.     boolean verChainMade; // checking if a vertical chain was made
  54.  
  55.     int icon; // new icon that fills in the slots using gui
  56.  
  57.     int tempChainCount;
  58.     int tempWhiteChecker;
  59.  
  60.     /**************************
  61.      * Constructor: Bejeweled
  62.      **************************/
  63.     public Bejeweled(BejeweledGUI gui) {
  64.         this.gui = gui;
  65.         NUMPIECESTYLE = gui.NUMPIECESTYLE;
  66.         NUMROW = gui.NUMROW;
  67.         NUMCOL = gui.NUMCOL;
  68.  
  69.         // TO DO:
  70.         // - creation of arrays
  71.         // - initialization of variables
  72.         // - initialization of game board (on 2D array and on GUI)
  73.  
  74.         // initializing and creating the starting game board
  75.         initBoard();
  76.  
  77.         // array to keep track of which pieces are highlighted
  78.         highlighted = new boolean[NUMROW][NUMCOL];
  79.  
  80.         // setting the score
  81.         score = 0;
  82.  
  83.         // setting the number of moves left
  84.         numMoveLeft = MOVES;
  85.  
  86.         firstSelection = false;
  87.  
  88.         chainCheckHorizontal();
  89.         chainCheckVertical();
  90.         if (horChainMade == true || verChainMade == true) {
  91.             // checking for highlighted pieces, each highlight is one point
  92.             for (int i = 0; i < NUMROW; i++) {
  93.                 for (int j = 0; j < NUMCOL; j++) {
  94.                     if (highlighted[i][j] == true) {
  95.                         score++;
  96.                     }
  97.                 }
  98.             }
  99.  
  100.             // updating the score
  101.             gui.setScore(score);
  102.  
  103.             // shifting all pieces down and replacing the empty slots with a random piece
  104.             fallAndReplace();
  105.         }
  106.     }
  107.  
  108.     /*****************************************************
  109.      * play This method is called when a piece is clicked. Parameter "row" and
  110.      * "column" is the location of the piece that is clicked by the player
  111.      *****************************************************/
  112.     public void play(int row, int column) {
  113.         // TO DO: implement the logic of the game
  114.  
  115.         // checking if the first selection has been made
  116.         if (!firstSelection) {
  117.             // unhighlighting everything
  118.             for (int i = 0; i < NUMROW; i++) {
  119.                 for (int j = 0; j < NUMCOL; j++) {
  120.                     gui.unhighlightSlot(i, j);
  121.                 }
  122.             }
  123.  
  124.             // highlighting the selected slot
  125.             gui.highlightSlot(row, column, COLOUR_SELECT);
  126.  
  127.             // storing the selcted slot's location
  128.             slot1Row = row;
  129.             slot1Col = column;
  130.  
  131.             firstSelection = true;
  132.         } else {
  133.             // setting firstSelection back to false so the player can make next move
  134.             firstSelection = false;
  135.  
  136.             gui.unhighlightSlot(slot1Row, slot1Col);
  137.  
  138.             // storing the location of the second selection
  139.             int slot2Row = row;
  140.             int slot2Col = column;
  141.  
  142.             // checking if the selections are adjacent
  143.             if (adjacentPieces(slot1Row, slot1Col, slot2Row, slot2Col)) {
  144.                 // retrieving the icons of the selections and storing them in a variable
  145.                 int icon1 = board[slot1Row][slot1Col];
  146.                 int icon2 = board[slot2Row][slot2Col];
  147.  
  148.                 // swapping the values of the selections in the actual board array
  149.                 board[slot1Row][slot1Col] = icon2;
  150.                 board[slot2Row][slot2Col] = icon1;
  151.  
  152.                 // swapping the icons on the display
  153.                 gui.setPiece(slot1Row, slot1Col, icon2);
  154.                 gui.setPiece(slot2Row, slot2Col, icon1);
  155.  
  156.                 // resetting all pieces in the highlight array to false
  157.                 for (int i = 0; i < NUMROW; i++) {
  158.                     for (int j = 0; j < NUMCOL; j++) {
  159.                         highlighted[i][j] = false;
  160.                     }
  161.                 }
  162.  
  163.                 // resetting the variables that tell you if a chain was made to false
  164.                 horChainMade = false;
  165.                 verChainMade = false;
  166.  
  167.                 // checking for chains and highlighting them if they are formed
  168.                 chainCheckHorizontal();
  169.                 chainCheckVertical();
  170.  
  171.                 // checking if a chain was made horizontally or vertically
  172.                 if (horChainMade == true || verChainMade == true) {
  173.                     // checking for highlighted pieces, each highlight is one point
  174.                     for (int i = 0; i < NUMROW; i++) {
  175.                         for (int j = 0; j < NUMCOL; j++) {
  176.                             if (highlighted[i][j] == true) {
  177.                                 score++;
  178.                             }
  179.                         }
  180.                     }
  181.  
  182.                     // updating the score
  183.                     gui.setScore(score);
  184.  
  185.                     // updating the moves left
  186.                     numMoveLeft--;
  187.                     gui.setMoveLeft(numMoveLeft);
  188.  
  189.                     // shifting all pieces down and replacing the empty slots with a random piece
  190.                     fallAndReplace();
  191.  
  192.                     // resetting all pieces in the highlight array to false
  193.                     for (int i = 0; i < NUMROW; i++) {
  194.                         for (int j = 0; j < NUMCOL; j++) {
  195.                             highlighted[i][j] = false;
  196.                         }
  197.                     }
  198.  
  199.                     // checking how many moves are left to determine whether or not the game will
  200.                     // end
  201.                     if (numMoveLeft == 0) {
  202.                         gui.showGameOverMessage(score, (MOVES - numMoveLeft));
  203.                         gui.resetGameBoard();
  204.                     }
  205.                     // no chain was made, so the values are swapped back to original positions and
  206.                     // an error msg is outputted
  207.                 } else {
  208.                     // swapping the values of the selections back to their original positions
  209.                     board[slot1Row][slot1Col] = icon1;
  210.                     board[slot2Row][slot2Col] = icon2;
  211.  
  212.                     // swapping the values of the grpahics back to their original positions
  213.                     gui.setPiece(slot1Row, slot1Col, icon1);
  214.                     gui.setPiece(slot2Row, slot2Col, icon2);
  215.  
  216.                     // outputting invalid move message
  217.                     gui.showInvalidMoveMessage();
  218.                 }
  219.             } else {
  220.                 // outputting invalid move message
  221.                 gui.showInvalidMoveMessage();
  222.             }
  223.         }
  224.     }
  225.  
  226.     // initializing the board
  227.     public void initBoard() {
  228.         board = new int[NUMROW][NUMCOL];
  229.  
  230.         for (int i = 0; i < NUMROW; i++) {
  231.             for (int k = 0; k < NUMCOL; k++) {
  232.                 icon = (int) (Math.random() * NUMPIECESTYLE);
  233.                 board[i][k] = icon;
  234.                 gui.setPiece(i, k, icon);
  235.             }
  236.         }
  237.     }
  238.  
  239.     // method to check if two pieces selected are adjacent
  240.     public boolean adjacentPieces(int row1, int col1, int row2, int col2) {
  241.         boolean adjacent = false;
  242.  
  243.         if (row1 == row2) {
  244.             if ((Math.abs(col1 - col2)) == 1) {
  245.                 adjacent = true;
  246.             }
  247.         } else if (col1 == col2) {
  248.             if ((Math.abs(row1 - row2)) == 1) {
  249.                 adjacent = true;
  250.             }
  251.         }
  252.  
  253.         return adjacent;
  254.     }
  255.  
  256.     // checking all horizontal chains
  257.     public void chainCheckHorizontal() {
  258.         int currentPiece;
  259.         int chain;
  260.  
  261.         for (int j = 0; j < NUMROW; j++) {
  262.             currentPiece = board[j][0];
  263.             chain = 1;
  264.             for (int k = 1; k < NUMCOL; k++) {
  265.                 if (board[j][k] == currentPiece) {
  266.                     chain++;
  267.                 } else {
  268.                     if (chain >= CHAIN_REQ) {
  269.                         for (int i = (k - 1); i >= (k - chain); i--) {
  270.                             gui.highlightSlot(j, i, COLOUR_SELECT);
  271.  
  272.                             highlighted[j][i] = true;
  273.                         }
  274.                         horChainMade = true;
  275.                         gui.showChainSizeMessage(chain);
  276.                     }
  277.  
  278.                     currentPiece = board[j][k];
  279.                     chain = 1;
  280.                 }
  281.  
  282.                 if (k == 7) {
  283.                     if (chain >= CHAIN_REQ) {
  284.                         for (int i = k; i >= (k - chain + 1); i--) {
  285.                             gui.highlightSlot(j, i, COLOUR_SELECT);
  286.  
  287.                             highlighted[j][i] = true;
  288.                         }
  289.                         horChainMade = true;
  290.                         gui.showChainSizeMessage(chain);
  291.                     }
  292.                 }
  293.             }
  294.         }
  295.     }
  296.  
  297.     // checking all vertical chains
  298.     public void chainCheckVertical() {
  299.         int currentPiece;
  300.         int chain;
  301.  
  302.         for (int j = 0; j < NUMCOL; j++) {
  303.             currentPiece = board[0][j];
  304.             chain = 1;
  305.             for (int k = 1; k < NUMROW; k++) {
  306.                 if (board[k][j] == currentPiece) {
  307.                     chain++;
  308.                 } else {
  309.                     if (chain >= CHAIN_REQ) {
  310.                         ;
  311.                         for (int i = (k - 1); i >= (k - chain); i--) {
  312.                             gui.highlightSlot(i, j, COLOUR_SELECT);
  313.  
  314.                             highlighted[i][j] = true;
  315.                         }
  316.                         verChainMade = true;
  317.                         gui.showChainSizeMessage(chain);
  318.                     }
  319.  
  320.                     currentPiece = board[k][j];
  321.                     chain = 1;
  322.                 }
  323.  
  324.                 if (k == 7) {
  325.                     if (chain >= CHAIN_REQ) {
  326.                         for (int i = k; i >= (k - chain + 1); i--) {
  327.                             gui.highlightSlot(i, j, COLOUR_SELECT);
  328.  
  329.                             highlighted[i][j] = true;
  330.                         }
  331.                         verChainMade = true;
  332.                         gui.showChainSizeMessage(chain);
  333.                     }
  334.                 }
  335.             }
  336.         }
  337.     }
  338.  
  339.     // moving all pieces down to fill in the chains, and filling in the empty slots
  340.     // with random pieces
  341.     public void fallAndReplace() {
  342.         // setting highlighted pieces to white
  343.         for (int i = (NUMROW - 1); i >= 0; i--) {
  344.             for (int j = 0; j < NUMCOL; j++) {
  345.                 if (highlighted[i][j] == true) {
  346.                     board[i][j] = WHITE;
  347.                 }
  348.             }
  349.         }
  350.  
  351.         // shifting pieces down if there is a white piece
  352.         for (int i = (NUMROW - 1); i >= 0; i--) {
  353.             for (int j = 0; j < NUMCOL; j++) {
  354.                 if (board[i][j] == WHITE) {
  355.  
  356.                     tempChainCount = 0;
  357.                     tempWhiteChecker = i;
  358.                     while (board[tempWhiteChecker][j] == WHITE) {
  359.                         if (board[tempWhiteChecker][j] == WHITE && tempWhiteChecker >= 0) {
  360.                             tempChainCount++;
  361.                         }
  362.                         tempWhiteChecker--;
  363.                     }
  364.  
  365.                     for (int l = 0; l <= i; l++) {
  366.                         if (i - l - tempChainCount >= 0) {
  367.                             board[i - l][j] = board[i - l - tempChainCount][j];
  368.                             icon = board[i - l][j];
  369.                             gui.setPiece(i - l, j, icon);
  370.                         } else {
  371.                             board[i - l][j] = WHITE;
  372.                             icon = board[i - l][j];
  373.                             gui.setPiece(i - l, j, icon);
  374.                         }
  375.                     }
  376.                 }
  377.             }
  378.         }
  379.  
  380.         // setting the white pieces to a random colored piece
  381.         for (int i = 0; i < NUMROW; i++) {
  382.             for (int j = 0; j < NUMCOL; j++) {
  383.                 if (board[i][j] == WHITE) {
  384.                     icon = (int) (Math.random() * NUMPIECESTYLE);
  385.                     board[i][j] = icon;
  386.                     gui.setPiece(i, j, icon);
  387.                 }
  388.             }
  389.         }
  390.  
  391.     }
  392.  
  393.     /*
  394.      * public int horizontalRight (int row, int col, int piece) { int samePiece = 0;
  395.      * col++;
  396.      *
  397.      * while (col < NUMCOL && board [row][col] == piece) { samePiece++; col++; }
  398.      *
  399.      * return samePiece; }
  400.      *
  401.      * public int horizontalLeft (int row, int col, int piece) { int samePiece = 0;
  402.      * col--;
  403.      *
  404.      * while (col >= 0 && board [row][col] == piece) { samePiece++; col--; }
  405.      *
  406.      * return samePiece; }
  407.      *
  408.      * public int verticalUp (int row, int col, int piece) { int samePiece = 0;
  409.      * row++;
  410.      *
  411.      * while (row < NUMROW && board [row][col] == piece) { samePiece++; row++; }
  412.      *
  413.      * return samePiece; }
  414.      *
  415.      * public int verticalDown (int row, int col, int piece) { int samePiece = 0;
  416.      * row--;
  417.      *
  418.      * while (row >= 0 && board [row][col] == piece) { samePiece++; row--; }
  419.      *
  420.      * return samePiece; }
  421.      */
  422.  
  423.     /*
  424.      * public void chain () { int currentIcon; int currentChainSize = 0; int
  425.      * chainSize = 0; int score = 0;
  426.      *
  427.      *
  428.      * for (int j = 0; j < NUMPIECESTYLE; j++) { //looping thru each type of icon
  429.      * currentIcon = j; //storing the current icon being looped to a variable for
  430.      * (int i = 0; i < NUMROW; i++) { //looping thru each row to see if there is a
  431.      * chain formed with the current icon for (int k = 0; k < (NUMCOL - 2); k++) {
  432.      * //looping thru each column of the row up until the 6th column if (board
  433.      * [i][k] == currentIcon) { //checking if the current locaiton being checked is
  434.      * equal to the current icon chainSize++;
  435.      *
  436.      * int y = k + 1; //storing the k value in a new variable so it can be used in
  437.      * another loop
  438.      *
  439.      * while (board [i][y] == currentIcon && y < NUMCOL) { //looping thru the rest
  440.      * of the row to see if a chain forms chainSize++; y++; }
  441.      *
  442.      * if (chainSize >= CHAIN_REQ) { //checking if the chain formed is 3 or more if
  443.      * (chainSize > currentChainSize) { //checking if the chain formed in this
  444.      * specific row is the largest chain there is score += chainSize; //updating the
  445.      * score firstRow = i; firstCol = k; lastRow = i; lastCol = y; } } chainSize =
  446.      * 0; //setting the chain size back to 0 for the next loop to be made } } } } }
  447.      */
  448.  
  449.     /*****************************************************
  450.      * endGame This method is called when the player clicks on the "End Game" button
  451.      *****************************************************/
  452.     public void endGame() {
  453.  
  454.     }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement