Advertisement
shywolf91

Grid code?

May 24th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1.  
  2. class Grid {
  3.   int x, y;
  4.   int myWidth, myHeight;
  5.   int rows, cols;
  6.   int[][] colors;
  7.   ArrayList<Integer> clearedRows = new ArrayList<Integer>();
  8.   int animateCount = -1;
  9.   Grid(int x, int y, int w, int h, int rows, int cols) {
  10.     this.x = x
  11.  
  12.     this.y = y;
  13.  
  14.  
  15.     myWidth = w;
  16.  
  17.  
  18.     myHeight = h;
  19.  
  20.  
  21.     this.rows = rows;
  22.  
  23.     this.cols = cols;
  24.  
  25.     colors = new int[cols][rows];
  26.  
  27.     for (int i = 0; i < cols; ++i)
  28.  
  29.       for (int j = 0; j < rows; ++j)
  30.  
  31.         colors[i][j] = 0;
  32.  
  33.  
  34.   }
  35.  
  36.   void clear() {
  37.  
  38.  
  39.  
  40.  
  41.  
  42.     for (int i = 0; i < cols; ++i)
  43.  
  44.  
  45.  
  46.  
  47.  
  48.       for (int j = 0; j < rows; ++j)
  49.  
  50.  
  51.  
  52.  
  53.  
  54.         colors[i][j] = 0;
  55.  
  56.  
  57.  
  58.  
  59.  
  60.   }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.    
  67.  
  68.  
  69.  
  70.  
  71.  
  72.   void draw() {
  73.  
  74.  
  75.  
  76.  
  77.  
  78.     stroke(255);
  79.  
  80.  
  81.  
  82.  
  83.  
  84.     strokeWeight(2);
  85.  
  86.  
  87.  
  88.  
  89.  
  90.     rect(x, y, myWidth, myHeight);
  91.  
  92.  
  93.  
  94.  
  95.  
  96.     for (int i = 0; i < cols; ++i)
  97.  
  98.  
  99.  
  100.  
  101.  
  102.       for (int j = 0; j < rows; ++j)
  103.  
  104.  
  105.  
  106.  
  107.  
  108.         fillSquare(i, j, colors[i][j]);
  109.  
  110.  
  111.  
  112.  
  113.  
  114.     // line clear animation
  115.  
  116.  
  117.  
  118.  
  119.  
  120.     if (animateCount >= 0) {
  121.  
  122.  
  123.  
  124.  
  125.  
  126.       //calculate a background that smoothly oscillates between black and white
  127.  
  128.  
  129.  
  130.  
  131.  
  132.       int c = (animateCount < 255) ? animateCount : 255 - animateCount%255;
  133.  
  134.  
  135.  
  136.  
  137.  
  138.       if (clearedRows.size() == 4)
  139.  
  140.  
  141.  
  142.  
  143.  
  144.         c = color(0, c, c); // cyan animation for a Tetris
  145.  
  146.  
  147.  
  148.  
  149.  
  150.       for (int row : clearedRows)
  151.  
  152.  
  153.  
  154.  
  155.  
  156.         for (int i = 0; i < cols; ++i)
  157.  
  158.  
  159.  
  160.  
  161.  
  162.           fillSquare(i, row, color(c, 200));
  163.  
  164.  
  165.  
  166.  
  167.  
  168.       animateCount += 10;
  169.  
  170.  
  171.  
  172.  
  173.  
  174.       if (animateCount > 2*255) {
  175.  
  176.  
  177.  
  178.  
  179.  
  180.         // stop animation, clear the lines, and load the next Tetromino
  181.  
  182.  
  183.  
  184.  
  185.  
  186.         animateCount = -1;
  187.  
  188.  
  189.  
  190.  
  191.  
  192.         eraseCleared();
  193.  
  194.  
  195.  
  196.  
  197.  
  198.         loadNext();
  199.  
  200.  
  201.  
  202.  
  203.  
  204.       }
  205.  
  206.  
  207.  
  208.  
  209.  
  210.     }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.   }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.    
  223.  
  224.  
  225.  
  226.  
  227.  
  228.   void fillSquare(int col, int row, color c) {
  229.  
  230.  
  231.  
  232.  
  233.  
  234.     if (col < 0 || col >= cols || row < 0 || row >= rows)
  235.  
  236.  
  237.  
  238.  
  239.  
  240.       return;
  241.  
  242.  
  243.  
  244.  
  245.  
  246.     noStroke();
  247.  
  248.  
  249.  
  250.  
  251.  
  252.     fill(c);
  253.  
  254.  
  255.  
  256.  
  257.  
  258.     rect(x + col*(myWidth/cols), y + row*(myHeight/rows), myWidth/cols, myHeight/rows);
  259.  
  260.  
  261.  
  262.  
  263.  
  264.   }
  265.  
  266.  
  267.  
  268.  
  269.  
  270.    
  271.  
  272.  
  273.  
  274.  
  275.  
  276.   void outlineSquare(int col, int row) {
  277.  
  278.  
  279.  
  280.  
  281.  
  282.     if (col < 0 || col >= cols || row < 0 || row >= rows)
  283.  
  284.  
  285.  
  286.  
  287.  
  288.       return;
  289.  
  290.  
  291.  
  292.  
  293.  
  294.     noFill();
  295.  
  296.  
  297.  
  298.  
  299.  
  300.     stroke(255);
  301.  
  302.  
  303.  
  304.  
  305.  
  306.     strokeWeight(2);
  307.  
  308.  
  309.  
  310.  
  311.  
  312.     rect(x + col*(myWidth/cols), y + row*(myHeight/rows), myWidth/cols, myHeight/rows);
  313.  
  314.  
  315.  
  316.  
  317.  
  318.   }
  319.  
  320.  
  321.  
  322.  
  323.  
  324.    
  325.  
  326.  
  327.  
  328.  
  329.  
  330.   void endTurn() {
  331.  
  332.  
  333.  
  334.  
  335.  
  336.     for (int i = 0; i < curr.shape.matrix.length; ++i)
  337.  
  338.  
  339.  
  340.  
  341.  
  342.       for (int j = 0; j < curr.shape.matrix.length; ++j)
  343.  
  344.  
  345.  
  346.  
  347.  
  348.         if (curr.shape.matrix[i][j] && j + curr.y >= 0)
  349.  
  350.  
  351.  
  352.  
  353.  
  354.           colors[i + curr.x][j + curr.y] = curr.getColor();
  355.  
  356.  
  357.  
  358.  
  359.  
  360.     if (checkLines()) {
  361.  
  362.  
  363.  
  364.  
  365.  
  366.       curr = null;
  367.  
  368.  
  369.  
  370.  
  371.  
  372.       animateCount = 0;
  373.  
  374.  
  375.  
  376.  
  377.  
  378.     } else
  379.  
  380.  
  381.  
  382.  
  383.  
  384.       loadNext();
  385.  
  386.  
  387.  
  388.  
  389.  
  390.   }
  391.  
  392.  
  393.  
  394.  
  395.  
  396.    
  397.  
  398.  
  399.  
  400.  
  401.  
  402.   boolean checkLines() {
  403.  
  404.  
  405.  
  406.  
  407.  
  408.     clearedRows.clear();
  409.  
  410.  
  411.  
  412.  
  413.  
  414.     for (int j = 0; j < rows; ++j) {
  415.  
  416.  
  417.  
  418.  
  419.  
  420.       int count = 0;
  421.  
  422.  
  423.  
  424.  
  425.  
  426.       for (int i = 0; i < cols; ++i)
  427.  
  428.  
  429.  
  430.  
  431.  
  432.         if (isOccupied(i, j))
  433.  
  434.  
  435.  
  436.  
  437.  
  438.           count++;
  439.  
  440.  
  441.  
  442.  
  443.  
  444.       if (count >= cols)
  445.  
  446.  
  447.  
  448.  
  449.  
  450.         clearedRows.add(j);
  451.  
  452.  
  453.  
  454.  
  455.  
  456.     }
  457.  
  458.  
  459.  
  460.  
  461.  
  462.     if (clearedRows.isEmpty())
  463.  
  464.  
  465.  
  466.  
  467.  
  468.       return false;
  469.  
  470.  
  471.  
  472.  
  473.  
  474.        
  475.  
  476.  
  477.  
  478.  
  479.  
  480.     if (lines/10 < (lines + clearedRows.size())/10) {
  481.  
  482.  
  483.  
  484.  
  485.  
  486.       level++;
  487.  
  488.  
  489.  
  490.  
  491.  
  492.       timer -= SPEED_DECREASE;
  493.  
  494.  
  495.  
  496.  
  497.  
  498.     }
  499.  
  500.  
  501.  
  502.  
  503.  
  504.     lines += clearedRows.size();
  505.  
  506.  
  507.  
  508.  
  509.  
  510.     score += (1 << clearedRows.size() - 1)*100;
  511.  
  512.  
  513.  
  514.  
  515.  
  516.     return true;
  517.  
  518.  
  519.  
  520.  
  521.  
  522.   }
  523.  
  524.  
  525.  
  526.  
  527.  
  528.    
  529.  
  530.  
  531.  
  532.  
  533.  
  534.   void eraseCleared() {
  535.  
  536.  
  537.  
  538.  
  539.  
  540.     for (int row : clearedRows) {
  541.  
  542.  
  543.  
  544.  
  545.  
  546.       for (int j = row - 1; j > 0; --j) {
  547.  
  548.  
  549.  
  550.  
  551.  
  552.         int[] rowCopy = new int[cols];
  553.  
  554.  
  555.  
  556.  
  557.  
  558.         for (int i = 0; i < cols; ++i)
  559.  
  560.  
  561.  
  562.  
  563.  
  564.           rowCopy[i] = colors[i][j];
  565.  
  566.  
  567.  
  568.  
  569.  
  570.         for (int i = 0; i < cols; ++i)
  571.  
  572.  
  573.  
  574.  
  575.  
  576.           colors[i][j + 1] = rowCopy[i];
  577.  
  578.  
  579.  
  580.  
  581.  
  582.       }
  583.  
  584.  
  585.  
  586.  
  587.  
  588.     }
  589.  
  590.  
  591.  
  592.  
  593.  
  594.   }
  595.  
  596.  
  597.  
  598.  
  599.  
  600.    
  601.  
  602.  
  603.  
  604.  
  605.  
  606.   boolean isOccupied(int x, int y) {
  607.  
  608.  
  609.  
  610.  
  611.  
  612.     if (y < 0 && x < cols && x >= 0) // allow movement/flipping to spaces above the board
  613.  
  614.  
  615.  
  616.  
  617.  
  618.       return false;
  619.  
  620.  
  621.  
  622.  
  623.  
  624.     return (x >= cols || x < 0 || y >= rows || colors[x][y] != 0);
  625.  
  626.  
  627.  
  628.  
  629.  
  630.   }
  631.  
  632.  
  633.  
  634.  
  635.  
  636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement