Advertisement
H0_0H

Untitled

Apr 28th, 2022 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. typedef pair<int, int> PII;
  5. typedef pair<LL, LL> PLL;
  6.  
  7.  
  8. class piece{
  9.     public:
  10.         void set_x(int x0){
  11.             x = x0;
  12.         }
  13.        
  14.         void set_y(int y0){
  15.             y = y0;
  16.         }
  17.  
  18.         void set_type(int t){
  19.             type = t;
  20.         }
  21.  
  22.         void set_show(){
  23.             if (type == 1) show = "b";
  24.             else if (type == 2) show = "w";
  25.             else show = "+";
  26.         }
  27.  
  28.         int x, y, type;
  29.         string show;
  30. };
  31.  
  32. class player{
  33.     public:
  34.         void set_name(string s){
  35.             name = s;
  36.         }
  37.  
  38.         string name;
  39. };
  40.  
  41. class board{
  42.     public:
  43.         void init_pieces(){
  44.             for (int i = 0; i < 15; i++){
  45.                 for (int j = 0; j < 15; j++){
  46.                     pieces[i][j].set_x(i);
  47.                     pieces[i][j].set_y(j);
  48.                     pieces[i][j].set_type(0);
  49.                     pieces[i][j].set_show();
  50.                 }
  51.             }
  52.         }
  53.  
  54.         void showboard(){
  55.             for (int i = 0; i < 15; i++){
  56.                 for (int j = 0; j < 15; j++){
  57.                     cout << pieces[i][j].show << ' ';
  58.                 }
  59.                 cout << '\n';
  60.             }
  61.         }
  62.  
  63.         piece pieces[15][15];
  64. };
  65.  
  66. class game{
  67.     public:
  68.         void init_players(player p1, player p2){
  69.             players[0] = p1;
  70.             players[1] = p2;
  71.         }
  72.        
  73.         void set_B(board b){
  74.             B = b;
  75.         }
  76.  
  77.         void set_turn(bool t){
  78.             turn = t;
  79.         }
  80.  
  81.         void showRule(){
  82.             cout << "Rule: " << endl;
  83.         }
  84.  
  85.         void showB(){
  86.             B.showboard();
  87.         }
  88.  
  89.         void judge(int x, int y){
  90.             int cnt = 1;
  91.             for (int i = 1; i < 15; i++){
  92.                 if (B.pieces[i][y].type != 0 && B.pieces[i][y].type == B.pieces[i - 1][y].type){
  93.                     cnt ++;
  94.                     if (cnt == 5){
  95.                         showB();
  96.                         if (turn) cout << players[0].name + " won!" << endl;
  97.                         else cout << players[1].name + " won!" << endl;
  98.  
  99.                         check = false;
  100.                         return;
  101.                     }
  102.                 } else cnt = 1;
  103.             }
  104.  
  105.             cnt = 1;
  106.             for (int j = 1; j < 15; j++){
  107.                 if (B.pieces[x][j].type != 0 && B.pieces[x][j].type == B.pieces[x][j - 1].type){
  108.                     cnt ++;
  109.                     if (cnt == 5){
  110.                         showB();
  111.                         if (turn) cout << players[0].name + " won!" << endl;
  112.                         else cout << players[1].name + " won!" << endl;
  113.  
  114.                         check = false;
  115.                         return;
  116.                     }
  117.                 } else cnt = 1;
  118.             }
  119.  
  120.  
  121.             int x0 = x;
  122.             int y0 = y;
  123.             while (x0 > 0 && y0 > 0){
  124.                 x0 --;
  125.                 y0 --;
  126.             }
  127.  
  128.             x0 ++;
  129.             y0 ++;
  130.             cnt = 1;
  131.             while (x0 < 14 && y0 < 14){
  132.                 if (B.pieces[x0][y0].type != 0 && B.pieces[x0][y0].type == B.pieces[x0 - 1][y0 - 1].type){
  133.                     cnt ++;
  134.                     if (cnt == 5){
  135.                         showB();
  136.                         if (turn) cout << players[0].name + " won!" << endl;
  137.                         else cout << players[1].name + " won!" << endl;
  138.  
  139.                         check = false;
  140.                         return;        
  141.                     }
  142.                 } else cnt = 1;
  143.  
  144.                 x0 ++;
  145.                 y0 ++;
  146.             }
  147.  
  148.  
  149.             x0 = x;
  150.             y0 = y;
  151.             while (x0 > 0 && y0 < 14){
  152.                 x0 --;
  153.                 y0 ++;
  154.             }
  155.  
  156.             x0 ++;
  157.             y0 --;
  158.             cnt = 1;
  159.             while (x0 < 14 && y0 > 0){
  160.                 if (B.pieces[x0][y0].type != 0 && B.pieces[x0][y0].type == B.pieces[x0 - 1][y0 + 1].type){
  161.                     cnt ++;
  162.                     if (cnt == 5){
  163.                         showB();
  164.                         if (turn) cout << players[0].name + " won!" << endl;
  165.                         else cout << players[1].name + " won!" << endl;
  166.                    
  167.                         check = false;
  168.                         return;
  169.                     }
  170.                 }
  171.                 else cnt = 1;
  172.                 x0 ++;
  173.                 y0 --;
  174.             }
  175.  
  176.         }
  177.        
  178.        
  179.         void begin(){
  180.            showRule();
  181.            cout << "The game is started" << endl;
  182.            cout << "Please input player1's name:  " << endl;
  183.            string n1, n2;
  184.            cin >> n1;
  185.            cout << "Please input player2's name:  "  << endl;
  186.            cin >> n2;
  187.            player p1, p2;
  188.            p1.set_name(n1);
  189.            p2.set_name(n2);
  190.            init_players(p1, p2);
  191.         }
  192.  
  193.         void begingame(){
  194.             int t, x, y;
  195.             while (check){
  196.                 showB();
  197.                 if (turn){
  198.                     cout << "Now it's " + players[0].name + "'s turn" << endl;
  199.                     t = 1;
  200.                 } else{
  201.                     cout << "Now it's " + players[1].name + "'s turn" << endl;
  202.                     t = 2;
  203.                 }
  204.            
  205.                 cout << "Please input position:  " << endl;
  206.                 cin >> x >> y;
  207.                 piece p;
  208.                 p.set_x(x);
  209.                 p.set_y(y);
  210.                 p.set_type(t);
  211.                 p.set_show();
  212.                 B.pieces[x - 1][y - 1] = p;
  213.                 judge(x - 1, y - 1);
  214.                 set_turn(not (turn));
  215.             }
  216.  
  217.             cout << "Game over!";
  218.         }
  219.  
  220.  
  221.         player players[2];
  222.         board B;
  223.         bool turn, check = true;
  224. };
  225.  
  226. int main(){
  227.     ios::sync_with_stdio(false);
  228.     cin.tie(0);
  229.  
  230.  
  231.     board b1;
  232.     b1.init_pieces();
  233.     game G;
  234.     G.set_B(b1);
  235.     G.set_turn(true);
  236.     G.begin();
  237.     G.begingame();
  238.  
  239.     return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement