tetrisolve

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

verify.cpp (1923B)


      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cassert>
      4 #include<vector>
      5 #include "common_tetris.cpp"
      6 
      7 using namespace std;
      8 
      9 // the graph information
     10 vector<int> config[7], rotation[7], column[7];
     11 vector<int> lastpiece;
     12 
     13 using namespace std;
     14 
     15 bool wins(int vertex, int depth);
     16 
     17 bool winsp(int vertex, int depth, int p) {
     18   // check that we win at vertex with piece p
     19   int r = rotation[p][vertex];
     20   int c = column[p][vertex];
     21   int n = config[p][vertex];
     22 
     23   // trace the run
     24   for (unsigned int i = 0; i < lastpiece.size(); i++)
     25     printf("%d", lastpiece[i]);
     26   printf(" vertex %d piece %d => r %d c %d to get to %d\n", vertex, p, r, c, n);
     27 
     28   // check we can actually put the piece
     29   assert(fits(p, r, c, 0));
     30   int l = 0;
     31   while(fits(p, r, c, l))
     32     l++;
     33   l--;
     34   // piece drops at row l
     35   blit(p, r, c, l, depth);
     36   // recursive call on the result (going at the new vertex)
     37   bool win = wins(n, depth+1);
     38   assert(win);
     39   unblit(p, r, c, l);
     40 
     41   return true;
     42 }
     43 
     44 bool wins(int vertex, int depth) {
     45   // check winning strategy at vertex
     46   
     47   // if vertex is winning then check that current board state is winning
     48   if (vertex == -1) {
     49     assert(line());
     50     return true;
     51   }
     52 
     53   // otherwise, check that each move has a winning strategy
     54   for (int p = 0; p < 7; p++) {
     55     lastpiece.push_back(p);
     56     bool wins = winsp(vertex, depth, p);
     57     lastpiece.pop_back();
     58     assert(wins);
     59   }
     60   return true;
     61 }
     62 
     63 int main(int argc, char **argv) {
     64   init_pieces();
     65 
     66   // read strategy from stdin
     67   bool reading = true;
     68   while (reading) {
     69     for (int p = 0; p < 7; p++) {
     70       int r, c, t;
     71       int ret = scanf("%d%d%d", &r, &c, &t); 
     72       if (ret != 3) {
     73         assert (p == 0);
     74         reading = false;
     75         break;
     76       }
     77       column[p].push_back(c);
     78       rotation[p].push_back(r);
     79       config[p].push_back(t);
     80     }
     81   }
     82   int start = config[0].size()-1;
     83 
     84   bool winn = wins(start, 0);
     85   assert(winn);
     86   return 0;
     87 }
     88