Advertisement
AGhaznawi

Untitled

Jul 30th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.58 KB | None | 0 0
  1. //====++++====++++====
  2. // game.h
  3. //====++++====++++====
  4. #pragma once
  5.  
  6. #include "Board.h"
  7. #include "Ai.h"
  8. #include <QDialog>
  9. #include <QPainter>
  10.  
  11. class Game : public QDialog
  12. {
  13.     Q_OBJECT
  14.  
  15. public:
  16.     Game(QWidget *parent = 0);
  17.     ~Game();
  18.  
  19. private:
  20.     bool userTurn;
  21.     int player;
  22.  
  23.     QPainter *painter;
  24.     QRect boardRects[9];
  25.  
  26.     void paintEvent(QPaintEvent *e);
  27.     void mousePressEvent(QMouseEvent *e);
  28.     GameState CheckGame();
  29.     void Reset();
  30.     void Tie();
  31.     void Lose();
  32.     void Win();
  33.     void PlayAi();
  34. };
  35.  
  36.  
  37. //====++++====++++====
  38. // game.cpp
  39. //====++++====++++====
  40. #include "game.h"
  41. #include "board.h"
  42. #include "ai.h"
  43. #include "globals.h"
  44. #include <QPainter>
  45. #include <QMouseEvent>
  46. #include <QMessageBox>
  47.  
  48. /***************************************
  49.  * Constructor                         *
  50.  ***************************************/
  51. Game::Game(QWidget *parent) : QDialog(parent)
  52. {
  53.     // Set window title and fixed size
  54.     this->setWindowTitle("Tic Tac Toe");
  55.     this->setFixedSize(300, 300);
  56.  
  57.     // Create the gameboard as a matrix of
  58.     // 9 rects each of 100 x 100 size
  59.     QSize size(100, 100);
  60.     boardRects[0] = QRect(QPoint(0, 0), size);
  61.     boardRects[1] = QRect(QPoint(0, 100), size);
  62.     boardRects[2] = QRect(QPoint(0, 200), size);
  63.     boardRects[3] = QRect(QPoint(100, 0), size);
  64.     boardRects[4] = QRect(QPoint(100, 100), size);
  65.     boardRects[5] = QRect(QPoint(100, 200), size);
  66.     boardRects[6] = QRect(QPoint(200, 0), size);
  67.     boardRects[7] = QRect(QPoint(200, 100), size);
  68.     boardRects[8] = QRect(QPoint(200, 200), size);
  69.  
  70.     // Set player symbol to X
  71.     player = X;
  72.  
  73. }
  74.  
  75. /***************************************
  76.  * Destructor                          *
  77.  ***************************************/
  78. Game::~Game()
  79. {
  80.  
  81. }
  82.  
  83. /***************************************
  84.  * paintEvent function                 *
  85.  ***************************************/
  86. void Game::paintEvent(QPaintEvent *e)
  87. {
  88.     painter = new QPainter(this);
  89.  
  90.     painter->fillRect(this->rect(), QColor(245, 245, 245));
  91.  
  92.     QPen borderPen(Qt::black);
  93.     borderPen.setWidth(2);
  94.  
  95.     painter->setPen(borderPen);
  96.     painter->drawRects(boardRects, 9);
  97.  
  98.     for(int i = 0; i < 9; i++)
  99.     {
  100.         QString path;
  101.         switch(displayBoard[i])
  102.         {
  103.         case EMPTY: path = ""; break;
  104.         case X: path = ":/images/X.png"; break;
  105.         case O: path = ":/images/O.png"; break;
  106.         }
  107.  
  108.         painter->drawPixmap(boardRects[i].x()+10, boardRects[i].y()+10,
  109.                             boardRects[i].width()-20, boardRects[i].height()-20,
  110.                             QPixmap(path));
  111.     }
  112. }
  113.  
  114. /***************************************
  115.  * mousePressEvent() is called when    *
  116.  * the player presses a mouse button.  *
  117.  * It contains the game loop           *
  118.  ***************************************/
  119. void Game::mousePressEvent(QMouseEvent *e)
  120. {
  121.     for(int i = 0; i < 9; i++)
  122.     {
  123.         if(boardRects[i].contains(e->pos()) && displayBoard[i] == EMPTY)
  124.         {
  125.             if(CheckGame() == PLAY)
  126.             {
  127.                 board.SetSquare(i, currentPly.player);
  128.                 displayBoard[i] = currentPly.player;
  129.                 QWidget::update();
  130.             }
  131.  
  132.             if(CheckGame() == WIN)
  133.             {
  134.                 if(currentPly.player == player)
  135.                     Win();
  136.                 else
  137.                     Lose();
  138.                 return;
  139.             }
  140.             if(CheckGame() == LOSE)
  141.             {
  142.                 if(currentPly.player == player)
  143.                     Lose();
  144.                 else
  145.                     Win();
  146.                 return;
  147.             }
  148.             if(CheckGame() == TIE)
  149.             {
  150.                 Tie();
  151.                 return;
  152.             }
  153.             if(CheckGame() == PLAY)
  154.             {
  155.                 currentPly.Toggle();
  156.  
  157.                 PlayAi();
  158.                 QWidget::update();
  159.             }
  160.  
  161.             if(CheckGame() == WIN)
  162.             {
  163.                 if(currentPly.player == player)
  164.                     Win();
  165.                 else
  166.                     Lose();
  167.                 return;
  168.             }
  169.             if(CheckGame() == LOSE)
  170.             {
  171.                 if(currentPly.player == player)
  172.                     Lose();
  173.                 else
  174.                     Win();
  175.                 return;
  176.             }
  177.             if(CheckGame() == TIE)
  178.             {
  179.                 Tie();
  180.                 return;
  181.             }
  182.  
  183.             if(CheckGame() == PLAY)
  184.             {
  185.                 currentPly.Toggle();
  186.                 return;
  187.             }
  188.  
  189.             break;
  190.         }
  191.     }
  192. }
  193.  
  194. /***************************************
  195.  * Shows "tie" message                 *
  196.  ***************************************/
  197. void Game::Tie()
  198. {
  199.     QMessageBox tie;
  200.     tie.setWindowTitle("Result");
  201.     tie.setText("Tie");
  202.     tie.setFixedSize(400, 400);
  203.     tie.exec();
  204.     Reset();
  205. }
  206.  
  207. /***************************************
  208.  * Shows "lose" message, when player   *
  209.  * loses the game                      *
  210.  ***************************************/
  211. void Game::Lose()
  212. {
  213.     QMessageBox lose;
  214.     lose.setWindowTitle("Result");
  215.     lose.setText("You lose");
  216.     lose.setFixedSize(400, 400);
  217.     lose.exec();
  218.     Reset();
  219. }
  220.  
  221. /***************************************
  222.  * Shows "win" message, when player    *
  223.  * wins the game. No way this function *
  224.  * will be called in a perfect search. *
  225.  ***************************************/
  226. void Game::Win()
  227. {
  228.     QMessageBox win;
  229.     win.setWindowTitle("Result");
  230.     win.setText("You win");
  231.     win.setFixedSize(400, 400);
  232.     win.exec();
  233.     Reset();
  234. }
  235.  
  236. /***************************************
  237.  * CheckGame() checks the game status  *
  238.  * using board.Evaluate() to control   *
  239.  * the game flow appropriately         *
  240.  ***************************************/
  241. GameState Game::CheckGame()
  242. {
  243.     return board.Evaluate(currentPly.player);
  244. }
  245.  
  246. /****************************************
  247.  * Resets the game for a new match      *
  248.  ****************************************/
  249. void Game::Reset()
  250. {
  251.     painter->end();
  252.     board.Init();
  253.     currentPly.player = X;
  254.     for(int i = 0; i < 9; i++)
  255.     {
  256.         displayBoard[i] = EMPTY;
  257.     }
  258. }
  259.  
  260. /****************************************
  261.  * plays AI turn                        *
  262.  ****************************************/
  263. void Game::PlayAi()
  264. {
  265.     Move bestMove = ai.Search(currentPly.player);
  266.     board.SetSquare(bestMove.location, currentPly.player);
  267.     displayBoard[bestMove.location] = currentPly.player;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement