Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package delli001.proj4;
- public class MatchThree {
- static int height = 8, width = 8;
- Piece[][] pieces;
- int score = 0;
- int moves = 0;
- MatchThree() {
- // 2 d arrays are addressed by row and then column , row = y, columns = x
- pieces = new Piece[height][width];
- for (int y=0; y < height; y++){
- for(int x = 0; x < width; x++){
- pieces[y][x] = new Piece (x, y);
- }
- }
- } // constructor
- // only swap two pieces if it is valid
- // adjacent, horizontally or vertically
- boolean swapPieces(int x1, int y1, int x2, int y2) { // Acts as a guard to the exchangePieces function
- if (validLoc(x1,y1)
- && validLoc(x2,y2) &&
- ((Math.abs(x1 - x2) == 1) && (y1 == y2)) ||
- ((Math.abs(y1 - y2) == 1) && (x1 == x2))){
- exchangePieces(x1, y1, x2, y2);
- moves++;
- return true;
- }
- return false;
- } // swap
- // swap 2 pieces internally regardless of game rules
- private void exchangePieces(int x1, int y1, int x2, int y2) {
- // move in 2d
- Piece p = pieces[y1][x1];
- pieces[y1][x1] = pieces[y2][x2];
- pieces[y2][x2] = p;
- // update internal
- pieces[y1][x1].setLoc(x1, y1);
- pieces[y2][x2].setLoc(x2, y2);
- } // exchangePieces
- // rotate pieces horizontally
- void horizontalRotate(int y, int offset) {
- if ((offset < 1) || (offset >= width)){
- return; //is treated as "invalid"
- }
- // shift right offset times
- for(int i = 0; i < offset; i++){
- //shift all pieces right one
- for (int j = 0; j < width -1; j++){
- exchangePieces(j,y,j+1,y);
- }
- }
- moves++;
- } // horizontal Rotate
- public void verticalRotate(int x, int offset) {
- if ((offset < 1) || (offset >= height)){
- return;
- }
- for(int i = 0; i < offset; i++){
- for (int j = 0; j < height - 1; j++){
- exchangePieces(x,j,x,j + 1); // This could be where I need to look
- }
- }
- moves++;
- }
- boolean validLoc(int x, int y) {
- if ((x >= 0) && (x < width) && (y >= 0) && (y < height) ){
- return true;
- }
- return false;
- } // validLoc
- // mark the pieces that need to be removed
- void markHorizontalMatches() {
- moves++;
- for (int y = 0; y < pieces.length; y++){
- for(int x = 0; x < pieces[y].length; x++){
- // check horizontal match by looking right
- if (x < width - 2){
- int matches = 0;
- int mtype = pieces[y][x].type;
- for (int i = x + 1; i < width; i++){
- if (pieces[y][i].type == mtype){
- matches++;
- }else{
- // get out of the match loop
- break;
- } //if match
- } // match loop
- if (matches > 1){
- for (int i = x; i < x + matches + 1; i++){
- pieces[y][i].marked = true;
- } // mark matching pieces
- score += matches * matches;
- }
- // if there were sufficient matches
- } // far enough from the edge for a horizontal match
- }
- }
- }
- void markVerticalMatches() {
- moves++;
- for (int x = 0; x < pieces.length; x++){
- for(int y = 0; y < pieces[x].length; y++){
- // check horizontal match by looking right
- if (y < height - 2){
- int matches = 0;
- int mtype = pieces[y][x].type;
- for (int i = y + 1; i < height; i++){
- if (pieces[i][x].type == mtype){
- matches++;
- }else{
- // get out of the match loop
- break;
- } //if match
- } // match loop
- if (matches > 1){
- for (int i = y; i < y + matches + 1; i++){
- pieces[i][x].marked = true;
- } // mark matching pieces
- score += matches * matches;
- }
- // if there were sufficient matches
- } // far enough from the edge for a horizontal match
- }
- }
- }
- // mark Matches
- // go through the board and "drop" elements
- void updateBoard() {
- // iterate through the board by column
- for (int x = 0; x < width; x++){
- for(int y = height - 1; y >= 0; y--){ //This is where the off by one error was, changed y>0 to y>=0;
- if (pieces[y][x].marked){
- dropPiece(x,y);
- }
- }
- }
- } // updateBoard
- // find the correct piece for the location x, y
- void dropPiece(int x, int y) {
- for (int i = y - 1; i >= 0; i--){
- if(pieces[i][x].marked == false){
- exchangePieces(x,y,x,i);
- return;
- }
- }
- pieces[y][x] = new Piece(x, y);
- } // dropPiece
- } // MatchThree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement