Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. package delli001.proj4;
  2.  
  3. public class MatchThree {
  4.  
  5.     static int height = 8, width = 8;
  6.     Piece[][] pieces;
  7.     int score = 0;
  8.     int moves = 0;
  9.  
  10.     MatchThree() {
  11.         // 2 d arrays are addressed by row and then column , row = y, columns = x
  12.         pieces = new Piece[height][width];
  13.         for (int y=0; y < height; y++){
  14.             for(int x = 0; x < width; x++){
  15.                 pieces[y][x] = new Piece (x, y);
  16.             }
  17.         }
  18.        
  19.     } // constructor
  20.  
  21.     // only swap two pieces if it is valid
  22.     // adjacent, horizontally or vertically
  23.    
  24.     boolean swapPieces(int x1, int y1, int x2, int y2) {  // Acts as a guard to the exchangePieces function
  25.         if (validLoc(x1,y1)
  26.                 && validLoc(x2,y2) &&
  27.                 ((Math.abs(x1 - x2) == 1) && (y1 == y2)) ||
  28.                 ((Math.abs(y1 - y2) == 1) && (x1 == x2))){
  29.            
  30.             exchangePieces(x1, y1, x2, y2);
  31.             moves++;
  32.             return true;
  33.            
  34.         }
  35.        
  36.         return false;
  37.     } // swap
  38.  
  39.     // swap 2 pieces internally regardless of game rules
  40.     private void exchangePieces(int x1, int y1, int x2, int y2) {
  41.         // move in 2d
  42.         Piece p = pieces[y1][x1];
  43.        
  44.         pieces[y1][x1] = pieces[y2][x2];
  45.         pieces[y2][x2] = p;
  46.        
  47.         // update internal
  48.         pieces[y1][x1].setLoc(x1, y1);
  49.         pieces[y2][x2].setLoc(x2, y2);
  50.        
  51.        
  52.        
  53.     } // exchangePieces
  54.  
  55.     // rotate pieces horizontally
  56.     void horizontalRotate(int y, int offset) {
  57.         if ((offset < 1) || (offset >= width)){
  58.             return;  //is treated as "invalid"
  59.         }
  60.        
  61.         // shift right offset times
  62.         for(int i = 0; i < offset; i++){
  63.             //shift all pieces right one
  64.             for (int j = 0; j < width -1; j++){
  65.                 exchangePieces(j,y,j+1,y);
  66.             }
  67.         }
  68.         moves++;
  69.     } // horizontal Rotate
  70.    
  71.     public void verticalRotate(int x, int offset) {
  72.         if ((offset < 1) || (offset >= height)){
  73.             return;
  74.         }
  75.        
  76.         for(int i = 0; i < offset; i++){
  77.             for (int j = 0; j < height - 1; j++){
  78.                 exchangePieces(x,j,x,j + 1);       // This could be where I need to look
  79.             }
  80.         }
  81.         moves++;
  82.     }
  83.  
  84.     boolean validLoc(int x, int y) {
  85.         if ((x >= 0) && (x < width) && (y >= 0) && (y < height) ){
  86.             return true;
  87.         }
  88.         return false;
  89.     } // validLoc
  90.  
  91.     // mark the pieces that need to be removed
  92.     void markHorizontalMatches() {
  93.         moves++;
  94.         for (int y = 0; y < pieces.length; y++){
  95.             for(int x = 0; x < pieces[y].length; x++){
  96.                
  97.                 // check horizontal match by looking right
  98.                 if (x < width - 2){
  99.                     int matches = 0;
  100.                     int mtype = pieces[y][x].type;
  101.                     for (int i = x + 1; i < width; i++){
  102.                         if (pieces[y][i].type == mtype){
  103.                             matches++;
  104.                         }else{
  105.                             // get out of the match loop
  106.                             break;
  107.                         } //if match
  108.                     } // match loop
  109.                     if (matches > 1){
  110.                         for (int i = x; i < x + matches + 1; i++){
  111.                             pieces[y][i].marked = true;
  112.                         } // mark matching pieces
  113.                         score += matches * matches;
  114.                     }
  115.                     // if there were sufficient matches
  116.                 } // far enough from the edge for a horizontal match
  117.             }
  118.         }
  119.     }
  120.    
  121.     void markVerticalMatches() {
  122.         moves++;
  123.         for (int x = 0; x < pieces.length; x++){
  124.             for(int y = 0; y < pieces[x].length; y++){
  125.                
  126.                 // check horizontal match by looking right
  127.                 if (y < height - 2){
  128.                     int matches = 0;
  129.                     int mtype = pieces[y][x].type;
  130.                     for (int i = y + 1; i < height; i++){
  131.                         if (pieces[i][x].type == mtype){
  132.                             matches++;
  133.                         }else{
  134.                             // get out of the match loop
  135.                             break;
  136.                         } //if match
  137.                     } // match loop
  138.                     if (matches > 1){
  139.                         for (int i = y; i < y + matches + 1; i++){
  140.                             pieces[i][x].marked = true;
  141.                         } // mark matching pieces
  142.                         score += matches * matches;
  143.                     }
  144.                     // if there were sufficient matches
  145.                 } // far enough from the edge for a horizontal match
  146.             }      
  147.         }          
  148.     }
  149.            
  150.        
  151.      // mark Matches
  152.  
  153.     // go through the board and "drop" elements
  154.     void updateBoard() {
  155.        
  156.         // iterate through the board by column
  157.         for (int x = 0; x < width; x++){
  158.             for(int y = height - 1; y >= 0; y--){  //This is where the off by one error was, changed y>0 to y>=0;
  159.                 if (pieces[y][x].marked){
  160.                     dropPiece(x,y);
  161.                 }
  162.             }
  163.         }
  164.        
  165.     } // updateBoard
  166.  
  167.     // find the correct piece for the location x, y
  168.     void dropPiece(int x, int y) {
  169.         for (int i = y - 1; i >= 0; i--){
  170.             if(pieces[i][x].marked == false){
  171.                 exchangePieces(x,y,x,i);
  172.                 return;
  173.             }
  174.         }
  175.         pieces[y][x] = new Piece(x, y);
  176.     } // dropPiece
  177.  
  178.    
  179.  
  180. } // MatchThree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement