tetrisolve

make a line in adversarial tetris
git clone https://a3nm.net/git/tetrisolve/
Log | Files | Refs | README | LICENSE

common_tetris.cpp (3250B)


      1 #include<cassert>
      2 #include<cstdio>
      3 
      4 // dimensions of playing field (LINES should be sufficiently large relative to
      5 // MAXHEIGHT to have the space to move pieces before dropping them)
      6 #define COLS 10
      7 #define LINES 16
      8 
      9 // the tetris pieces
     10 bool pieces[7][4][4] = {
     11   {
     12     {1, 0, 0, 0},
     13     {1, 0, 0, 0},
     14     {1, 0, 0, 0},
     15     {1, 0, 0, 0}
     16   },
     17   {
     18     {0, 1, 0, 0},
     19     {1, 1, 0, 0},
     20     {1, 0, 0, 0},
     21     {},
     22   },
     23   {
     24     {0, 1, 1, 0},
     25     {1, 1, 0, 0},
     26     {},
     27     {},
     28   },
     29   {
     30     {1, 1, 1, 0},
     31     {0, 1, 0, 0},
     32     {},
     33     {}
     34   },
     35   {
     36     {1, 1, 1, 0},
     37     {1, 0, 0, 0},
     38     {},
     39     {}
     40   },
     41   {
     42     {1, 1, 1, 0},
     43     {0, 0, 1, 0},
     44     {},
     45     {}
     46   },
     47   {
     48     {1, 1, 0, 0},
     49     {1, 1, 0, 0},
     50     {},
     51     {}
     52   }};
     53 
     54 // the rotated tetris pieces
     55 bool piecesr[7][4][4][4];
     56 
     57 // the playing field (global)
     58 int field[LINES][COLS] = {};
     59 
     60 void init_pieces() {
     61   // initialize array of rotated pieces
     62   for (int p = 0; p < 7; p++)
     63     for (int i = 0; i < 4; i++)
     64       for (int j = 0; j < 4; j++)
     65         piecesr[p][0][i][j] = pieces[p][i][j];
     66 
     67   // do the 3 rotations
     68   for (int p = 0; p < 7; p++)
     69     for (int r = 1; r < 4; r++)
     70       for (int i = 0; i < 4; i++)
     71         for (int j = 0; j < 4; j++)
     72           piecesr[p][r][i][j] = piecesr[p][r-1][j][3-i];
     73 }
     74 
     75 bool fits(int p, int r, int c, int l) {
     76   // does piece p rot r fit at col c line l?
     77   for (int i = 0; i < 4; i++)
     78     for (int j = 0; j < 4; j++)
     79       if (piecesr[p][r][i][j]) {
     80         if (c + j < 0)
     81           return false;
     82         if (c + j >= COLS)
     83           return false; // outside the screen right
     84         if (l + i >= LINES)
     85           return false; // outside the screen bottom
     86         if (field[l + i][c + j])
     87           return false; // blocked
     88       }
     89   return true;
     90 }
     91 
     92 bool stopped(int p, int r, int c, int l) {
     93   // is piece p rot r at col c line l stopped by something below it?
     94   for (int i = 0; i < 4; i++)
     95     for (int j = 0; j < 4; j++)
     96       if (piecesr[p][r][i][j]) {
     97         assert(c + j >= 0);
     98         assert(c + j < COLS);
     99         assert(l + i < LINES);
    100         assert(!field[l + i][c + j]);
    101         if (l + i == LINES-1)
    102           return true; // screen bottom
    103         if (field[l+i+1][c+j])
    104           return true; // filled square
    105       }
    106   return false;
    107 }
    108 
    109 void blit(int p, int r, int c, int l, int x) {
    110   // put piece p rot r at col c line l of field
    111   // writing it as integer 1+'x' which can be more convenient for printing
    112   for (int i = 0; i < 4; i++)
    113     for (int j = 0; j < 4; j++)
    114       if (piecesr[p][r][i][j]) {
    115         assert(c + j < COLS);
    116         assert(l + i < LINES);
    117         assert(!field[l + i][c + j]);
    118         field[l + i][c + j] = 1+x;
    119       }
    120 }
    121 
    122 void unblit(int p, int r, int c, int l) {
    123   // un piece p rot r at col c line l
    124   for (int i = 0; i < 4; i++)
    125     for (int j = 0; j < 4; j++)
    126       if (piecesr[p][r][i][j]) {
    127         assert(c + j < COLS);
    128         assert(l + i < LINES);
    129         assert(field[l + i][c + j]);
    130         field[l + i][c + j] = 0;
    131       }
    132 }
    133 
    134 int line() {
    135   // check if there is a line in the current configuration
    136   for (int i = 0; i < LINES; i++) {
    137     bool ok = true;
    138     for (int j = 0; j < COLS; j++)
    139       if (!field[i][j]) {
    140         ok = false; break;
    141       }
    142     if (ok) {
    143       return true;
    144     }
    145   }
    146   return false;
    147 }
    148