Advertisement
acMan3

c4

Feb 25th, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <unistd.h>
  5.  
  6. using namespace std;
  7.  
  8. static const int L_CHAR  = 97;  // a
  9. static const int D_CHAR  = 115; // s
  10. static const int R_CHAR = 100;  // d
  11.  
  12. static const int HORIZ = 0;
  13. static const int VERTI = 1;
  14. static const int FORWA = 2;
  15. static const int BACKW = 3;
  16.  
  17.  
  18. class board
  19. {
  20.     public:
  21.         board()
  22.         {
  23.             theBoard.resize(xMax, std::vector<int>(yMax, 0));
  24.         }
  25.  
  26.         void run()
  27.         {
  28.             while(!won)
  29.             {
  30.                 draw();
  31.                 sleep(1);
  32.                 readInput();
  33.                 won = checkWin();
  34.             }
  35.            
  36.             draw();
  37.             cout << endl << setw(12) << getSpace(winningPlayer) << " WINS!" << endl << endl;
  38.         }
  39.                    
  40.     private:
  41.         int xMax = 7;
  42.         int yMax = 6;
  43.         int cursor = 0;
  44.         int currPlayer = 1;
  45.         int winningPlayer = 0;
  46.         int total = 0;
  47.         int p1T = 0;
  48.         int p2T = 0;
  49.         bool won = false;
  50.         char player1Char = 'X';
  51.         char player2Char = 'O';
  52.         char blankChar   = ' ';        
  53.         vector<vector<int>> theBoard;
  54.  
  55.         void draw()
  56.         {
  57.             std::system("clear");
  58.             cout << setw(3 + (cursor * 4)) << getSpace(currPlayer) << endl;
  59.             for(int y = yMax-1; y >= 0; y--)          
  60.             {
  61.                 cout << "|-=-|-=-|-=-|-=-|-=-|-=-|-=-|" << endl;
  62.                 for(int x = 0; x < xMax; x++)
  63.                 {
  64.                     cout << "| " << getSpace(theBoard[x][y]) << " ";
  65.                 }
  66.                 cout << "|" << endl;
  67.             }
  68.             cout << "|-=-|-=-|-=-|-=-|-=-|-=-|-=-|" << endl;
  69.         }
  70.  
  71.         char getSpace(int p)
  72.         {
  73.             if (p == 1)
  74.                 return player1Char;
  75.             else if (p == 2)
  76.                 return player2Char;
  77.             else
  78.                 return blankChar;
  79.         }
  80.        
  81.         void readInput()
  82.         {
  83.             char in;
  84.             cin >> in;
  85.                
  86.             if (in == L_CHAR)
  87.             {
  88.                 cursor--;
  89.                 if (cursor < 0)
  90.                     cursor = xMax - 1;
  91.             }
  92.            
  93.             if (in == R_CHAR)
  94.             {
  95.                 cursor++;
  96.                 if (cursor > xMax - 1)
  97.                     cursor = 0;
  98.             }            
  99.            
  100.             if (in == D_CHAR)
  101.             {
  102.                 for(int y = 0; y < yMax; y++)          
  103.                 {
  104.                     if (theBoard[cursor][y] == 0)
  105.                     {
  106.                         theBoard[cursor][y] = currPlayer;
  107.                        
  108.                         total++;
  109.                         if(currPlayer == 1)
  110.                         {
  111.                            p1T++;
  112.                            currPlayer = 2;
  113.                         }  
  114.                         else
  115.                         {
  116.                            p2T++;
  117.                            currPlayer = 1;
  118.                         }
  119.                        
  120.                         break;
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.  
  126.         bool checkWin()
  127.         {
  128.             bool retVal = false;
  129.            
  130.             if ((retVal = (total == xMax*yMax)) == false)
  131.                 if ((retVal = check(xMax-3, yMax, HORIZ)) == false)
  132.                     if((retVal = check(xMax, yMax-3, VERTI)) == false)
  133.                         if((retVal = check(xMax-3, yMax-3, FORWA)) == false)
  134.                             retVal = check(xMax, yMax-3, BACKW);
  135.  
  136.             return retVal;
  137.         }
  138.  
  139.         bool check(int xIn, int yIn, int dir)
  140.         {
  141.             int xS = 0, yS = 0;
  142.             int x1 = 0, x2 = 0, x3 = 0;
  143.             int y1 = 0, y2 = 0, y3 = 0;
  144.  
  145.             switch(dir)
  146.             {
  147.                 case HORIZ:
  148.                     x1 = 1; x2 = 2; x3 = 3;
  149.                     y1 = 0, y2 = 0, y3 = 0;
  150.                     break;
  151.                 case VERTI:
  152.                     x1 = 0, x2 = 0, x3 = 0;
  153.                     y1 = 1; y2 = 2; y3 = 3;                
  154.                     break;
  155.                 case FORWA:
  156.                     x1 = 1, x2 = 2, x3 = 3;
  157.                     y1 = 1, y2 = 2, y3 = 3;    
  158.                     break;
  159.                 case BACKW:
  160.                     xS = 3;
  161.                     x1 = -1, x2 = -2, x3 = -3;                
  162.                     y1 = 1, y2 = 2, y3 = 3;            
  163.                     break;
  164.                 default:  
  165.                     return false;      
  166.             };
  167.            
  168.             for(int x = xS; x < xIn; x++)          
  169.             {
  170.                 for(int y = yS; y < yIn; y++)
  171.                 {
  172.                     for(int p = 1; p < 3; p++)
  173.                     {
  174.                         if((theBoard[x][y] == p) && (theBoard[x+x1][y+y1] == p) &&
  175.                            (theBoard[x+x2][y+y2] == p) && (theBoard[x+x3][y+y3] == p))
  176.                         {
  177.                             winningPlayer = p;
  178.                             return true;                        
  179.                         }
  180.                     }
  181.                 }
  182.             }
  183.            
  184.             return false;      
  185.         }
  186.  
  187. };
  188.  
  189.  
  190.  
  191. int main ()
  192. {
  193.     board b;
  194.     b.run();
  195.  
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement