markyrocks

tictactoe update c++ raylib simple graphics

Dec 17th, 2021
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.87 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<vector>
  4. #include "raylib.h"
  5. #include<string>
  6.  
  7. #define WinHeight 600
  8. #define WinWidth 800
  9. #define BoardHeight 300
  10. #define BoardWidth 300
  11. #define fps 60
  12. #define X 1
  13. #define O -1
  14. #define BackgroundColor WHITE
  15. #define LineColor BLACK
  16. #define CellSize 100
  17. #define LineThickness 5
  18. #define WinCenterX WinWidth/2
  19. #define WinCenterY WinHeight/2
  20. using namespace std;
  21.  
  22. struct vec2 {
  23.     int x, y;
  24.     vec2():x(),y(){}
  25.     vec2(int x, int y) :x(x), y(y) {}
  26.  
  27. };
  28.  
  29. struct clickzone {
  30.  
  31.     int cx,cy,x1, x2, y1, y2,id,val;
  32.  
  33.     //shapes could be added to this, just does a square for now
  34.  
  35.     clickzone():cx(), cy(), x1(), x2(), y1(), y2(),id(),val(0) {}
  36.  
  37.     clickzone(int centerx, int centery,int size,int move):cx(centerx),cy(centery),x1(),x2(),y1(),y2(),val(0) {
  38.         id = move;
  39.         x1 = centerx - (size * .5);
  40.         x2 = x1 + size;
  41.         y1 = centery - (size * .5);
  42.         y2 = y1 + size;
  43.     }
  44.  
  45.     bool inzone(int x, int y) {
  46.         return x > x1 - 1 && x<x2 + 1 && y>y1 - 1 && y < y2 + 1 ? true : false;
  47.     }
  48.  
  49.     bool inzone(vec2 v) {
  50.         return inzone(v.x, v.y);
  51.     }
  52.  
  53.  
  54.  
  55. };
  56.  
  57. struct ttt {
  58.  
  59.     int board[3][3];//not used
  60.     vector<clickzone> zones;
  61.     bool player1, player2; // not used
  62.     int hwnd;// not used
  63.     ttt() : hwnd{}, player1{}, player2{}, board{}, zones{} {
  64.         InitWindow( WinWidth, WinHeight, "Tic-Tac-Toe");
  65.         SetTargetFPS(fps);
  66.        
  67.     }
  68.  
  69.     void getzones() {
  70.         int move = 1;
  71.         for (int fyz = WinCenterY - CellSize; fyz < WinCenterY + (CellSize * 2); fyz += CellSize) {
  72.             for (int fxz = WinCenterX - CellSize; fxz < WinCenterX + (CellSize * 2); fxz += CellSize) {
  73.                 zones.emplace_back(fxz, fyz, CellSize, move);
  74.                 move++;
  75.             }
  76.         }
  77.     }
  78.  
  79.     clickzone* click(vec2 pos) {
  80.         for (auto& i : zones) {
  81.             if (i.inzone(pos)) {
  82.                 return &i;
  83.             }
  84.         }
  85.         return nullptr;
  86.     }
  87.  
  88.     void drawParallelLines(int x, int y, int length, int line_thickness,int width,bool horizontal, Color linecolor) {
  89.        
  90.         for (int i = 0; i < 2; i++) {
  91.             for (int j = 0; j < line_thickness; j++) {
  92.                 if (horizontal) {
  93.                     if (i == 0) {
  94.                         DrawLine(x, y + j, x + length, y + j, linecolor);
  95.                     }
  96.                     else {
  97.                         DrawLine(x, y + width + j, x + length, y + width + j, linecolor);
  98.                     }
  99.                 }
  100.                 else {
  101.                     if (i == 0) {
  102.                         DrawLine(x + j, y, x + j, y + length, linecolor);
  103.                     }
  104.                     else {
  105.                         DrawLine(x + j + width, y, x + j + width, y + length, linecolor);
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     void grid() {
  113.        
  114.         drawParallelLines(WinCenterX-(BoardWidth*.5), WinCenterY-(CellSize*.5) , BoardWidth, LineThickness, CellSize, true, LineColor);
  115.         drawParallelLines(WinCenterX-(CellSize*.5), WinCenterY - (BoardHeight*.5), BoardHeight, LineThickness, CellSize, false, LineColor);
  116.     }
  117.  
  118.     void DrawO(int x, int y, int radius, int thickness, Color linecolor) {
  119.         DrawCircle(x, y, radius, linecolor);
  120.         DrawCircle(x, y, radius-thickness, BackgroundColor);
  121.     }
  122.  
  123.     void DrawX(int x, int y, int height, int thickness, Color linecolor) {
  124.         int len = sqrt((height * height) + (height * height));
  125.         int x1 = x - (height * .5);
  126.         int x2 = x1 + height;
  127.         int y1 = y - (height * .5);
  128.         int y2 = y1 + height;
  129.  
  130.         for (int i = 0; i < thickness; i++) {
  131.             DrawLine(x1 + i, y1, x2 + i, y2, linecolor);
  132.             DrawLine(x2 + i, y1, x1 + i, y2, linecolor);
  133.         }
  134.     }
  135.  
  136.     void DrawBoard() {
  137.         for (int i = 0; i < 9; i++) {
  138.             if (zones[i].val == X) {
  139.                 DrawX(zones[i].cx, zones[i].cy, CellSize * .8, LineThickness, LineColor);
  140.             }
  141.             else if (zones[i].val == O) {
  142.                 DrawO(zones[i].cx, zones[i].cy, CellSize * .4, LineThickness, LineColor);
  143.             }
  144.         }
  145.     }
  146.  
  147.     void play2() {
  148.         int win = 0;
  149.         while (!WindowShouldClose())
  150.         {   BeginDrawing();
  151.             ClearBackground(BackgroundColor);
  152.             DrawText("Two Players only", 100, 10, 50, LineColor);//change to player select
  153.  
  154.             DrawText("Press Enter to Continue", 125, 100, 30, LineColor);
  155.            
  156.             EndDrawing();
  157.        
  158.             if (GetKeyPressed() == 257) {
  159.                 break;
  160.             }
  161.         }
  162.  
  163.         getzones();
  164.        
  165.         bool flip = true;
  166.         while (!WindowShouldClose())
  167.         {  
  168.             BeginDrawing();
  169.             ClearBackground(BackgroundColor);
  170.             grid();
  171.             DrawBoard();
  172.             if (!win) {
  173.                 string s("Player ");
  174.                     s.append(flip ? "X" : "O");
  175.                     s.append("'s Turn");
  176.                     DrawText(s.c_str(), 200, 40, 45, LineColor);
  177.             }
  178.             else {
  179.                 string s("Player ");
  180.                 s.append(win == X ? "X" : "O");
  181.                 s.append(" WINS!!!!!");
  182.                 DrawText(s.c_str(), 200, 40, 45, LineColor);
  183.                 linewin();
  184.             }
  185.             EndDrawing();
  186.            
  187.             if ((IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) && !win) {
  188.                
  189.                 auto p = GetMousePosition();
  190.                 auto z = click(vec2((int)p.x, (int)p.y));
  191.                 if (z && z->val == 0) {
  192.                    
  193.                     z->val = flip ? X : O;
  194.                     flip = !flip;
  195.                     win = checkwin2();
  196.                 }
  197.             }
  198.         }
  199.     }
  200.  
  201.  
  202.  
  203.     void play() {  // no longer used
  204.         int numberPlayers = 2;
  205.         grid();
  206.         display();
  207.         int turns = 0;
  208.         bool flip = false;
  209.         while (checkwin() == ' ' && turns < 9 && !WindowShouldClose()) {
  210.             int move = 0;
  211.             if (numberPlayers == 2) {
  212.                 while (move == 0) {
  213.                     std::cout << "Player " << flip + 1 << " Enter A Move \n";
  214.                     scanf_s("%d", &move);
  215.                     if (checkmove(move)) {
  216.                         updateboard(move, flip + 1);
  217.                         display();
  218.                     }
  219.                     else {
  220.                         std::cout << "Incorrect input or square is already occupied Please Try again\n";
  221.                         move = 0;
  222.                     }
  223.                 }
  224.             }
  225.             else if (numberPlayers == 1) {
  226.  
  227.                 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  228.  
  229.  
  230.             }
  231.             flip = !flip;
  232.             turns++;
  233.         }
  234.  
  235.         if (checkwin() == ' ') {
  236.             std::cout << " ITS A TIE\n";
  237.         }
  238.         else {
  239.             std::cout << checkwin() << " WINS!!\n";
  240.         }
  241.         std::cout << "Game Over\n";
  242.  
  243.     }
  244.  
  245.     std::vector<int> copyboard() { // this was for the ai might still be used
  246.         std::vector<int> cc(9);
  247.         for (int i = 0; i < 9; i++) {
  248.             cc[i] = board[(int)floor(i / 3)][i % 3];
  249.         }
  250.         return cc;
  251.     }
  252.  
  253.     int aimove() {
  254.        
  255.         std::vector<int> cc = copyboard();
  256.         std::vector<int> pms = potentialmoves(cc);
  257.         if (pms.size() == 9) { return 5; }
  258.         std::vector<std::vector<int>> scorelist(pms.size());
  259.  
  260.         int move = 0;
  261.  
  262.  
  263.  
  264.         return move;
  265.     }
  266.     void aimove_recur(std::vector<int> cc, int* move,bool is_max) {
  267.  
  268.     }
  269.  
  270.     std::vector<int> potentialmoves(std::vector<int> cc) {
  271.         //basically empty squares
  272.         std::vector<int> v{};
  273.         for (int i = 0; i < 9; i++) {
  274.             if (cc[i] == 0) {
  275.                 v.push_back(i);
  276.             }
  277.         }
  278.         return v;
  279.     }
  280.  
  281.     //int checktwos(std::vector<int> cc,int opponent) {
  282.     //  int xscore = 0;
  283.     //  int yscore = 0;
  284.     //  int dscore = 0;
  285.     //  int xdscore = 0;
  286.     //  for (int i = 0; i < 3; i++) {
  287.     //      for (int j = 0; j < 3; j++) {
  288.     //          xscore += cc[ i * 3 + j ];
  289.     //          yscore += cc[ j * 3 + i];
  290.     //          if (i == j) {
  291.     //              dscore += cc[ (j + i) * 2];
  292.     //              xdscore += cc[j + i + 2];
  293.     //          }
  294.  
  295.     //      }
  296.     //      if (xscore == 2 || yscore == 2 || dscore == 2 || xdscore==2) {
  297.     //          return 'X';
  298.     //      }
  299.     //      xscore = 0;
  300.     //      yscore = 0;
  301.     //  }
  302.  
  303.     //}
  304.  
  305.  
  306.     void updateboard(int move, int player) { //no longer used
  307.         board[(int)floor((move - 1) / 3)][(move - 1) % 3] = player == 1 ? X : O;
  308.     }
  309.  
  310.     bool checkmove(int move) { //no longer used
  311.         auto b = board[(int)floor((move-1) / 3)][(move - 1) % 3];
  312.         if (b == 0) { return true; }
  313.         return false;
  314.     }
  315.  
  316.     void display() { //no longer used
  317.        
  318.         std::cout << "\n\n\n";
  319.         for (int i = 0; i < 9; i++) {
  320.             auto b = board[(int)floor(i / 3)][i % 3];
  321.             if (b == X) {
  322.                 std::cout << "X ";
  323.             }
  324.             else if (b == O) {
  325.                 std::cout << "O ";
  326.             }
  327.             else {
  328.                 std::cout << i + 1 << " ";
  329.             }
  330.            
  331.             if (!((i + 1) % 3)) {
  332.                 std::cout << '\n';
  333.             }
  334.         }
  335.         std::cout << "\n\n\n";
  336.     }
  337.  
  338.     void reset() { //pointless
  339.         for (int i = 0; i < 3;i++) {
  340.             for (int j = 0; j < 3; j++) {
  341.                 board[i][j] = 0;
  342.             }
  343.         }
  344.     }
  345.  
  346.     char checkwin() {  //no longer used
  347.         int xscore = 0;
  348.         int yscore = 0;
  349.         int dscore = 0;
  350.         int ddscore = 0;
  351.    
  352.         for (int i = 0; i < 3; i++) {
  353.             for (int j = 0; j < 3; j++) {
  354.                 xscore +=board[i][j];
  355.                 yscore += board[j][i];
  356.                 if (i == j) {
  357.                     dscore += board[i][j];
  358.                     if (i == 0) {
  359.                         ddscore += board[i + 2][j];
  360.                     }
  361.                     else if (i == 1) {
  362.                         ddscore += board[i][j];
  363.                     }
  364.                     else if (i == 2) {
  365.                         ddscore += board[i][j - 2];
  366.                     }
  367.                 }
  368.  
  369.             }
  370.             if (xscore == 3 || yscore==3 || dscore==3 || ddscore == 3) {
  371.                 return 'X';
  372.             }
  373.             else if (xscore == -3 || yscore == -3 || dscore == -3 || ddscore == -3) {
  374.             return 'O';
  375.             }
  376.             xscore = 0;
  377.             yscore = 0;
  378.         }
  379.    
  380.         return ' ';
  381.     }
  382.    
  383.     int checkwin2() {
  384.  
  385.         int h1 = zones[0].val + zones[1].val + zones[2].val;
  386.         int h2 = zones[3].val + zones[4].val + zones[5].val;
  387.         int h3 = zones[6].val + zones[7].val + zones[8].val;
  388.         int v1 = zones[0].val + zones[3].val + zones[6].val;
  389.         int v2 = zones[1].val + zones[4].val + zones[7].val;
  390.         int v3 = zones[2].val + zones[5].val + zones[8].val;
  391.         int d1 = zones[0].val + zones[4].val + zones[8].val;
  392.         int d2 = zones[2].val + zones[4].val + zones[6].val;
  393.  
  394.         if (h1 == 3 || h2 == 3 || h3 == 3 || v1 == 3 || v2 == 3 || v3 == 3 || d1 == 3 || d2 == 3) {
  395.                 return X;
  396.         }
  397.         else if (h1 == -3 || h2 == -3 || h3 == -3 || v1 == -3 || v2 == -3 || v3 == -3 || d1 == -3 || d2 == -3) {
  398.             return O;
  399.         }
  400.         return 0;
  401.     }
  402.  
  403.     void linewin() {
  404.         int h1 = zones[0].val + zones[1].val + zones[2].val;
  405.         int h2 = zones[3].val + zones[4].val + zones[5].val;
  406.         int h3 = zones[6].val + zones[7].val + zones[8].val;
  407.         int v1 = zones[0].val + zones[3].val + zones[6].val;
  408.         int v2 = zones[1].val + zones[4].val + zones[7].val;
  409.         int v3 = zones[2].val + zones[5].val + zones[8].val;
  410.         int d1 = zones[0].val + zones[4].val + zones[8].val;
  411.         int d2 = zones[2].val + zones[4].val + zones[6].val;
  412.  
  413.         if (h1 == 3 || h1 == -3) {
  414.             drawlinethick(zones[0].x1, zones[0].cy, zones[2].x2, zones[0].cy, LineThickness, RED);
  415.         }
  416.         else if (h2 == 3 || h2 == -3) {
  417.             drawlinethick(zones[3].x1, zones[3].cy, zones[5].x2, zones[3].cy, LineThickness, RED);
  418.         }
  419.         else if (h3 == 3 || h3 == -3) {
  420.             drawlinethick(zones[6].x1, zones[6].cy, zones[8].x2, zones[6].cy, LineThickness, RED);
  421.         }
  422.         else if (v1 == 3 || v1 == -3) {
  423.             drawlinethick(zones[0].cx, zones[0].y1, zones[0].cx, zones[6].y2, LineThickness, RED);
  424.         }
  425.         else if (v2 == 3 || v2 == -3) {
  426.             drawlinethick(zones[1].cx, zones[1].y1, zones[1].cx, zones[7].y2, LineThickness, RED);
  427.         }
  428.         else if (v3 == 3 || v3 == -3) {
  429.             drawlinethick(zones[2].cx, zones[2].y1, zones[2].cx, zones[8].y2, LineThickness, RED);
  430.         }
  431.         else if (d1 == 3 || d1 == -3) {
  432.             drawlinethick(zones[0].x1, zones[0].y1, zones[8].x2, zones[8].y2, LineThickness, RED);
  433.         }
  434.         else if (d2 == 3 || d2 == -3) {
  435.             drawlinethick(zones[2].x2, zones[2].y1, zones[6].x1, zones[6].y2, LineThickness, RED);
  436.         }
  437.     }
  438.  
  439.     void drawlinethick(int x1, int y1, int x2, int y2, int thickness, Color linecolor) {
  440.         for (int i = 0; i < thickness; i++) {
  441.             if (y1 == y2) {
  442.                 DrawLine(x1, y1 + i, x2, y2 + i, linecolor);
  443.             }
  444.             else {
  445.                 DrawLine(x1 +i, y1, x2+i, y2, linecolor);
  446.             }
  447.         }
  448.     }
  449.  
  450. };
  451.  
  452.  
  453. int main() {
  454.    
  455.     ttt a;
  456.     a.play2();
  457.  
  458. }
Advertisement
Add Comment
Please, Sign In to add comment