Advertisement
mickypinata

PROG-T1034: Reversi

Mar 24th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef struct pos{
  6.     int row;
  7.     int col;
  8.     pos operator = (const pos &rhs){
  9.         row = rhs.row;
  10.         col = rhs.col;
  11.     }
  12. }pos;
  13.  
  14. /// U UR R DR D DL L UL
  15. pos ctrl[8] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
  16. vector<vector<int>> table(9, vector<int>(9, 0)); /// B = 1 W = 2
  17.  
  18. void PrintTable(){
  19.     for(int i = 1; i <= 8; ++i){
  20.         for(int j = 1; j <= 8; ++j){
  21.             switch(table[i][j]){
  22.             case 0:
  23.                 cout << ".";
  24.                 break;
  25.             case 1:
  26.                 cout << "X";
  27.                 break;
  28.             case 2:
  29.                 cout << "O";
  30.                 break;
  31.             }
  32.         }
  33.         cout << "\n";
  34.     }
  35.     return;
  36. }
  37.  
  38. int main(){
  39.  
  40.     int r, p, row;
  41.     char col;
  42.     bool found;
  43.     pos tr, c;
  44.  
  45.     table[4][5] = 1;
  46.     table[5][4] = 1;
  47.     table[4][4] = 2;
  48.     table[5][5] = 2;
  49.  
  50.     scanf("%d", &r);
  51.     for(int i = 1; i <= r; ++i){
  52.         if(i % 2 == 0){
  53.             p = 2;
  54.         } else {
  55.             p = 1;
  56.         }
  57.         scanf(" %c", &col);
  58.         scanf("%d", &row);
  59.         tr = {row, col - 'a' + 1};
  60.         table[tr.row][tr.col] = p;
  61.         for(int j = 0; j < 8; ++j){
  62.             c = {tr.row + ctrl[j].row, tr.col + ctrl[j].col};
  63.             found = false;
  64.             while(c.row > 0 && c.row <= 8 && c.col > 0 && c.col <= 8){
  65.                 if(table[c.row][c.col] == 0){
  66.                     break;
  67.                 } else if(table[c.row][c.col] == p){
  68.                     found = true;
  69.                     break;
  70.                 }
  71.                 c.row += ctrl[j].row;
  72.                 c.col += ctrl[j].col;
  73.             }
  74.             if(found){ /// Reverse
  75.                 c.row -= ctrl[j].row;
  76.                 c.col -= ctrl[j].col;
  77.                 while(c.row != tr.row || c.col != tr.col){
  78.                     if(table[c.row][c.col] == 3 - p){
  79.                         table[c.row][c.col] = p;
  80.                     }
  81.                     c.row -= ctrl[j].row;
  82.                     c.col -= ctrl[j].col;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     PrintTable();
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement