[text] game

Viewer

  1. #ifndef GAME_H
  2. #define GAME_H
  3.  
  4. #include "Square.h"
  5. #include <iostream>
  6. #include "Square.h"
  7. #include "Button.h"
  8.  
  9. class Game { 
  10.     Square **r;
  11.     // Rectangle r[5];
  12.     int count;
  13.     bool gameStarted;
  14.     bool tie;
  15.     bool horizontal;
  16.     bool vertical;
  17.     bool diagonal1;
  18.     bool diagonal2;
  19.     
  20.  
  21.     Button button3;
  22.     Button button4;
  23.     Button button5;
  24.     Button buttonTie;
  25.     Button player;
  26.     Button computer;
  27.     Button play;
  28.     Button player1Wins;
  29.     Button player2Wins;
  30.     Button playAgain; 
  31.     Button quit;
  32.  
  33.     // Square board[9];
  34.     bool playerX;
  35.     bool AI;
  36.     bool end;
  37.  
  38.       void AIMove() {
  39.         if (!playerX && !end) {
  40.             for (int i = 0; i < count; i++) {
  41.                 for (int j = 0; j < count; j++) {
  42.                     if (r[i][j].isEmpty()) {
  43.                         r[i][j].playO();
  44.                         playerX = !playerX;
  45.                         return;
  46.                     }
  47.                 }
  48.             }   
  49.             
  50.         }
  51.     }
  52.  
  53.     void init(){
  54.  
  55.       
  56.  
  57.         r = new Square*[count];
  58.         for (int i = 0; i < count; i++){
  59.             r[i] = new Square[count];
  60.         }
  61.  
  62.         float x = -0.9;
  63.         float y = 0.9;
  64.         float size = 1.8 / count;
  65.  
  66.         float w = 1.8 / count;
  67.         float h = 1.5 / count;
  68.         // Initialize your state variables
  69.         for (int i = 0; i < count; i++){
  70.             x = -0.9;
  71.             for (int j = 0; j < count; j++){
  72.                 r[i][j] = Square(x, y, size);
  73.                 x += size;
  74.             }
  75.             y -= size;
  76.         }
  77.     }
  78.  
  79.   
  80.     
  81.    
  82. public:
  83.     Game() {
  84.         button3 = Button("3 x 3", -0.8, 0.0);
  85.         button4 = Button("4 x 4", -0.2, 0.0);
  86.         button5 = Button("5 x 5", 0.4, 0.0);
  87.         player = Button("Human vs Human", -0.8, 0.3);
  88.         computer = Button("Human vs AI", 0.2, 0.3);
  89.         play = Button("Start", -0.2, -0.3);
  90.         player1Wins = Button("Player O wins", -0.5, 0.3);
  91.         player2Wins = Button("Player X wins", -0.5, 0.3);
  92.         playAgain = Button("Play Again", -0.8, -0.3);
  93.         quit = Button("Quit", 0.2, -0.3);
  94.         buttonTie = Button("Game was a Draw", -0.5, 0.3);
  95.         
  96.  
  97.         count = 3;
  98.         init();
  99.         // board[0] = Square(-0.9f, 0.9f, 0.6f);
  100.         // board[1] = Square(-0.3f, 0.9f, 0.6f);
  101.         // board[2] = Square(0.3f, 0.9f, 0.6f);
  102.  
  103.         // board[3] = Square(-0.9f, 0.3f, 0.6f);
  104.         // board[4] = Square(-0.3f, 0.3f, 0.6f);
  105.         // board[5] = Square(0.3f, 0.3f, 0.6f);
  106.  
  107.         // board[6] = Square(-0.9f, -0.3f, 0.6f);
  108.         // board[7] = Square(-0.3f, -0.3f, 0.6f);
  109.         // board[8] = Square(0.3f, -0.3f, 0.6f);
  110.  
  111.         playerX = true;
  112.         AI = false;
  113.         gameStarted = false;
  114.         end = false;
  115.         tie = false;
  116.         
  117.         
  118.     }
  119.  
  120.     void checkWin(){
  121.         for(int i = 0; i < count; i++){
  122.             if(r[i][0].getPlayer() != EMPTY){
  123.                 bool win = true;
  124.                 for(int j = 1; j < count; j++){
  125.                     if(r[i][j].getPlayer() != r[i][j - 1].getPlayer()){
  126.                         win = false;
  127.                         break;
  128.                     }
  129.                 }
  130.                 if(win){
  131.                     end = true;
  132.                     return;
  133.  
  134.                 }
  135.             }
  136.         }
  137.       
  138.  
  139.         for(int j = 0; j < count; j++){
  140.             if(r[0][j].getPlayer() != EMPTY){
  141.                 bool win = true;
  142.                 for(int i = 1; i < count; i++){
  143.                     if(r[i][j].getPlayer() != r[i - 1][j].getPlayer()){
  144.                         win = false;
  145.                         break;
  146.                     }
  147.                 }
  148.                 if(win){
  149.                     end = true;
  150.                     return;
  151.                 }
  152.             }
  153.         }
  154.  
  155.         if(r[0][0].getPlayer() != EMPTY){
  156.             bool win = true;
  157.             for(int i = 1; i < count; i++){
  158.                 if(r[i][i].getPlayer() != r[0][0].getPlayer()){
  159.                     win = false;
  160.                     break;
  161.                 }
  162.             }
  163.             if(win){
  164.                 end = true;
  165.                 return;
  166.             }
  167.         }
  168.  
  169.  
  170.          if(r[0][count - 1].getPlayer() != EMPTY){
  171.             bool win = true;
  172.             for(int i = 1; i < count; i++){
  173.                 if(r[i][count - 1 - i].getPlayer() != r[0][count - 1].getPlayer()){
  174.                     win = false;
  175.                     break;
  176.                 }
  177.             }
  178.             if(win){
  179.                 end = true;
  180.                 return;
  181.             }
  182.         }
  183.  
  184.  
  185.         tie = true;
  186.  
  187.         for(int i = 0; i < count; i++){
  188.             for(int j = 0; j < count; j++){
  189.                 if(r[i][j].isEmpty()){
  190.                     tie = false;
  191.                     break;
  192.                 }
  193.             }
  194.             if(!tie){
  195.                 break;
  196.             }
  197.         }
  198.  
  199.         if(tie){
  200.             end = true;
  201.             return;
  202.         }
  203.  
  204.     }
  205.     
  206.    
  207.  
  208.    
  209.     void AIOn() {
  210.         AI = true;
  211.         AIMove();
  212.     }
  213.  
  214.     void AIOff() {
  215.         AI = false;
  216.     }
  217.  
  218.     void playerXFirst() {
  219.         playerX = true;
  220.     }
  221.  
  222.     void playerOFirst() {
  223.         playerX = false;
  224.     }
  225.  
  226.     void start() {
  227.         if (AI) {
  228.             AIMove();
  229.         }
  230.     }
  231.  
  232.     //  void leftMouseDown( float x, float y ){
  233.     //     // Respond to left mouse down events
  234.     //     if (button3.contains(x, y)){
  235.     //         std::cout << "Will change to 3x3" << std::endl;
  236.     //         for (int i = 0; i < count; i++){
  237.     //             delete[] r[i];
  238.     //         }
  239.     //         delete[] r;
  240.     //         count = 3;
  241.     //         init();
  242.     //     }
  243.     //     else if (button4.contains(x, y)){
  244.     //         std::cout << "Will change to 4x4" << std::endl;
  245.     //         for (int i = 0; i < count; i++){
  246.     //             delete[] r[i];
  247.     //         }
  248.     //         delete[] r;
  249.     //         count = 4;
  250.     //         init();
  251.     //     }
  252.     //     else if (button5.contains(x, y)){
  253.     //         std::cout << "Will change to 5x5" << std::endl;
  254.     //         for (int i = 0; i < count; i++){
  255.     //             delete[] r[i];
  256.     //         }
  257.     //         delete[] r;
  258.     //         count = 5;
  259.     //         init();
  260.     //     }
  261.     //     else if(play.contains(x, y)){
  262.     //         std::cout << "Game Started" << std::endl;
  263.     //         gameStarted = true;
  264.             
  265.     //     }
  266.     // }
  267.  
  268.     void handleMouseClick(float x, float y) {
  269.         
  270.           if(gameStarted){
  271.  
  272.             for (int i = 0; i < count; i++) {
  273.                 for (int j = 0; j < count; j++) {
  274.                      if (r[i][j].contains(x, y)) {
  275.                         if (r[i][j].isEmpty() && !end) {
  276.                             if (playerX) {
  277.                                 r[i][j].playX();
  278.                             } else {
  279.                                 r[i][j].playO();
  280.                             }
  281.                             playerX = !playerX;
  282.                             checkWin();
  283.                             break;
  284.                         }
  285.                   
  286.                 }
  287.                
  288.             }
  289.         }
  290.       
  291.  
  292.         if (AI) {
  293.             AIMove();
  294.             checkWin();
  295.         if(AI && end){
  296.         if (playAgain.contains(x, y)){
  297.             // init();
  298.             std::cout << "New Game" << std::endl;
  299.             gameStarted = false;
  300.             end = false;
  301.             
  302.             
  303.             
  304.              
  305.         }       
  306.           else if(quit.contains(x, y)){
  307.             std::cout << "Game Over" << std::endl;  
  308.             //referenced from ChatGPT
  309.             exit(0);    
  310.     
  311.         }
  312.         }
  313.       
  314.             
  315.         //     if(end && !gameStarted){
  316.         //     if (playAgain.contains(x, y)){
  317.         //     init();
  318.         //     std::cout << "New Game" << std::endl;
  319.         //     gameStarted = false;
  320.         //     end = false;
  321.         //     AIOff();
  322.             
  323.             
  324.              
  325.         // }       
  326.         //   else if(quit.contains(x, y)){
  327.         //     std::cout << "Game Over" << std::endl;  
  328.         //     //referenced from ChatGPT
  329.         //     exit(0);    
  330.     
  331.         // }
  332.         //     }
  333.             
  334.         }
  335.         else if(end){
  336.          if (playAgain.contains(x, y)){
  337.             // init();
  338.             std::cout << "New Game" << std::endl;
  339.             gameStarted = false;
  340.             end = false;
  341.             
  342.             
  343.             
  344.              
  345.         }       
  346.           else if(quit.contains(x, y)){
  347.             std::cout << "Game Over" << std::endl;  
  348.             //referenced from ChatGPT
  349.             exit(0);    
  350.     
  351.         }
  352.         
  353.         }
  354.         
  355.         } 
  356.        
  357.         else if(computer.contains(x,y)){
  358.             std::cout << "Human vs AI" << std::endl;
  359.            AIOn();
  360.            
  361.         }
  362.         else if(player.contains(x, y)){
  363.             std::cout << "Human vs Human" << std::endl;
  364.             AIOff();
  365.         }
  366.         else if (button3.contains(x, y)){
  367.             std::cout << "Will change to 3x3" << std::endl;
  368.             for (int i = 0; i < count; i++){
  369.                 delete[] r[i];
  370.             }
  371.             delete[] r;
  372.             count = 3;
  373.             init();
  374.         }
  375.         else if (button4.contains(x, y)){
  376.             std::cout << "Will change to 4x4" << std::endl;
  377.             for (int i = 0; i < count; i++){
  378.                 delete[] r[i];
  379.             }
  380.             delete[] r;
  381.             count = 4;
  382.             init();
  383.         }
  384.         else if (button5.contains(x, y)){
  385.             std::cout << "Will change to 5x5" << std::endl;
  386.             for (int i = 0; i < count; i++){
  387.                 delete[] r[i];
  388.             }
  389.             delete[] r;
  390.             count = 5;
  391.             init();
  392.         }
  393.         else if(play.contains(x, y)){
  394.             std::cout << "Game Started" << std::endl;
  395.             gameStarted = true;
  396.             
  397.         }
  398.         
  399.     
  400.         
  401. }
  402.  
  403.     void draw() {
  404.         
  405.         if(!gameStarted && !end){
  406.         button3.draw();
  407.         button4.draw();
  408.         button5.draw();
  409.         player.draw();
  410.         computer.draw();
  411.         play.draw();
  412.         }
  413.  
  414.         else if(gameStarted && !end){
  415.             for (int i = 0; i < count; i++){
  416.                 for(int j = 0; j < count; j++){
  417.                     r[i][j].draw();
  418.                             
  419.                 } 
  420.         
  421.             }
  422.             
  423.         }
  424.         
  425.         
  426.             
  427.         else if(end){ 
  428.             if(tie){
  429.                 buttonTie.draw();
  430.             }
  431.             else if(playerX){
  432.                     player1Wins.draw(); 
  433.             }   
  434.             else if(!playerX){
  435.                     player2Wins.draw();
  436.                 }
  437.         
  438.             std::cout << "Game ended" << std::endl;
  439.             playAgain.draw();
  440.             quit.draw();
  441.         }    
  442.             
  443.          
  444.  
  445.         
  446.          
  447.         // if(gameStarted){
  448.         //     for (int i = 0; i < 9; i++) {
  449.         //         r[i]->draw();
  450.         //     }
  451.  
  452.         // }
  453.     }
  454. };
  455.  
  456. #endif

Editor

You can edit this paste and save as new:


File Description
  • game
  • Paste Code
  • 20 Apr-2024
  • 11.18 Kb
You can Share it: