1. #include "board.h"
  2.  
  3. Board::Board()
  4. {
  5.     sPosTopLeft  = " ";
  6.     sPosTopMid   = " ";
  7.     sPosTopRight = " ";
  8.     sPosMidLeft  = " ";
  9.     sPosMidMid   = " ";
  10.     sPosMidRight = " ";
  11.     sPosBotLeft  = " ";
  12.     sPosBotMid   = " ";
  13.     sPosBotRight = " ";
  14.     sWinningPlayer = " ";
  15. }
  16.  
  17. int Board::SetSquare(int nPos, string XorO)
  18. {
  19.     if ((XorO == "X") || (XorO == "O"))
  20.     {
  21.         switch(nPos)
  22.         {
  23.         case 1:
  24.             {
  25.                 if(sPosTopLeft == " ")
  26.                 {
  27.                     sPosTopLeft = XorO;
  28.                     return 0;
  29.                 }
  30.                 else
  31.                 {
  32.                     return 1;
  33.                 }
  34.             }
  35.         case 2:
  36.             {
  37.                 if(sPosTopMid == " ")
  38.                 {
  39.                     sPosTopMid = XorO;
  40.                     return 0;
  41.                 }
  42.                 else
  43.                 {
  44.                     return 1;
  45.                 }
  46.             }
  47.         case 3:
  48.             {
  49.                 if(sPosTopRight == " ")
  50.                 {
  51.                     sPosTopRight = XorO;
  52.                     return 0;
  53.                 }
  54.                 else
  55.                 {
  56.                     return 1;
  57.                 }
  58.             }
  59.         case 4:
  60.             {
  61.                 if(sPosMidLeft == " ")
  62.                 {
  63.                     sPosMidLeft = XorO;
  64.                     return 0;
  65.                 }
  66.                 else
  67.                 {
  68.                     return 1;
  69.                 }
  70.             }
  71.         case 5:
  72.             {
  73.                 if(sPosMidMid == " ")
  74.                 {
  75.                     sPosMidMid = XorO;
  76.                     return 0;
  77.                 }
  78.                 else
  79.                 {
  80.                     return 1;
  81.                 }
  82.             }
  83.         case 6:
  84.             {
  85.                 if(sPosMidRight == " ")
  86.                 {
  87.                     sPosMidRight = XorO;
  88.                     return 0;
  89.                 }
  90.                 else
  91.                 {
  92.                     return 1;
  93.                 }
  94.             }
  95.         case 7:
  96.             {
  97.                 if(sPosBotLeft == " ")
  98.                 {
  99.                     sPosBotLeft = XorO;
  100.                     return 0;
  101.                 }
  102.                 else
  103.                 {
  104.                     return 1;
  105.                 }
  106.             }
  107.         case 8:
  108.             {
  109.                 if(sPosBotMid == " ")
  110.                 {
  111.                     sPosBotMid = XorO;
  112.                     return 0;
  113.                 }
  114.                 else
  115.                 {
  116.                     return 1;
  117.                 }
  118.             }
  119.         case 9:
  120.             {
  121.                 if(sPosBotRight == " ")
  122.                 {
  123.                     sPosBotRight = XorO;
  124.                     return 0;
  125.                 }
  126.                 else
  127.                 {
  128.                     return 1;
  129.                 }
  130.             }
  131.         default:
  132.             {
  133.                 return 2;
  134.             }
  135.         }
  136.     }
  137. }
  138.  
  139. void Board::ResetBoard()
  140. {
  141.     sPosTopLeft    = " ";
  142.     sPosTopMid     = " ";
  143.     sPosTopRight   = " ";
  144.     sPosMidLeft    = " ";
  145.     sPosMidMid     = " ";
  146.     sPosMidRight   = " ";
  147.     sPosBotLeft    = " ";
  148.     sPosBotMid     = " ";
  149.     sPosBotRight   = " ";
  150.     sWinningPlayer = " ";
  151.     return;
  152. }
  153.  
  154. void Board::PrintBoard()
  155. {
  156.     using std::cout;
  157.     using std::endl;
  158.  
  159.     cout << string( 100, '\n' );
  160.     cout << "     |     |     " << endl;
  161.     cout << "  " << sPosTopLeft << "  |  " << sPosTopMid << "  |  " << sPosTopRight << "  " << endl;
  162.     cout << "_____|_____|_____" << endl;
  163.     cout << "     |     |     " << endl;
  164.     cout << "  " << sPosMidLeft << "  |  " << sPosMidMid << "  |  " << sPosMidRight << "  " << endl;
  165.     cout << "_____|_____|_____" << endl;
  166.     cout << "     |     |     " << endl;
  167.     cout << "  " << sPosBotLeft << "  |  " << sPosBotMid << "  |  " << sPosBotRight << "  " << endl;
  168.     cout << "     |     |     " << endl;
  169.     cout << "\n\n";
  170.     return;
  171. }
  172.  
  173. bool Board::CheckWinCondition()
  174. {
  175.     if (sPosTopLeft == "X")
  176.     {
  177.         if (sPosTopMid == "X")
  178.         {
  179.             if (sPosTopRight == "X")
  180.             {
  181.                 sWinningPlayer = "X";
  182.                 return true;
  183.             }
  184.         }
  185.         if (sPosMidLeft == "X")
  186.         {
  187.             if (sPosBotLeft == "X")
  188.             {
  189.                 sWinningPlayer = "X";
  190.                 return true;
  191.             }
  192.         }
  193.         if (sPosMidMid == "X")
  194.         {
  195.             if (sPosBotRight == "X")
  196.             {
  197.                 sWinningPlayer = "X";
  198.                 return true;
  199.             }
  200.         }
  201.     }
  202.     if (sPosTopMid == "X")
  203.     {
  204.         if (sPosMidMid == "X")
  205.         {
  206.             if (sPosBotMid == "X")
  207.             {
  208.                 sWinningPlayer = "X";
  209.                 return true;
  210.             }
  211.         }
  212.     }
  213.     if (sPosTopRight == "X")
  214.     {
  215.         if (sPosMidMid == "X")
  216.         {
  217.             if (sPosBotLeft == "X")
  218.             {
  219.                 sWinningPlayer = "X";
  220.                 return true;
  221.             }
  222.         }
  223.         if (sPosMidRight == "X")
  224.         {
  225.             if (sPosBotRight == "X")
  226.             {
  227.                 sWinningPlayer = "X";
  228.                 return true;
  229.             }
  230.         }
  231.     }
  232.     if (sPosMidLeft == "X")
  233.     {
  234.         if (sPosMidMid == "X")
  235.         {
  236.             if (sPosMidRight == "X")
  237.             {
  238.                 sWinningPlayer = "X";
  239.                 return true;
  240.             }
  241.         }
  242.     }
  243.     if (sPosBotLeft == "X")
  244.     {
  245.         if (sPosBotMid == "X")
  246.         {
  247.             if(sPosBotRight == "X")
  248.             {
  249.                 sWinningPlayer = "X";
  250.                 return true;
  251.             }
  252.         }
  253.     }
  254.     ///////////////////////////////////
  255.     if (sPosTopLeft == "O")
  256.     {
  257.         if (sPosTopMid == "O")
  258.         {
  259.             if (sPosTopRight == "O")
  260.             {
  261.                 sWinningPlayer = "O";
  262.                 return true;
  263.             }
  264.         }
  265.         if (sPosMidLeft == "O")
  266.         {
  267.             if (sPosBotLeft == "O")
  268.             {
  269.                 sWinningPlayer = "O";
  270.                 return true;
  271.             }
  272.         }
  273.         if (sPosMidMid == "O")
  274.         {
  275.             if (sPosBotRight == "O")
  276.             {
  277.                 sWinningPlayer = "O";
  278.                 return true;
  279.             }
  280.         }
  281.     }
  282.     if (sPosTopMid == "O")
  283.     {
  284.         if (sPosMidMid == "O")
  285.         {
  286.             if (sPosBotMid == "O")
  287.             {
  288.                 sWinningPlayer = "O";
  289.                 return true;
  290.             }
  291.         }
  292.     }
  293.     if (sPosTopRight == "O")
  294.     {
  295.         if (sPosMidMid == "O")
  296.         {
  297.             if (sPosBotLeft == "O")
  298.             {
  299.                 sWinningPlayer = "O";
  300.                 return true;
  301.             }
  302.         }
  303.         if (sPosMidRight == "O")
  304.         {
  305.             if (sPosBotRight == "O")
  306.             {
  307.                 sWinningPlayer = "O";
  308.                 return true;
  309.             }
  310.         }
  311.     }
  312.     if (sPosMidLeft == "O")
  313.     {
  314.         if (sPosMidMid == "O")
  315.         {
  316.             if (sPosMidRight == "O")
  317.             {
  318.                 sWinningPlayer = "O";
  319.                 return true;
  320.             }
  321.         }
  322.     }
  323.     if (sPosBotLeft == "O")
  324.     {
  325.         if (sPosBotMid == "O")
  326.         {
  327.             if(sPosBotRight == "O")
  328.             {
  329.                 sWinningPlayer = "O";
  330.                 return true;
  331.             }
  332.         }
  333.     }
  334.  
  335.     if ((sPosTopLeft != " ") && (sPosTopMid != " "))
  336.     {
  337.         if ((sPosTopRight != " ") && (sPosMidLeft != " "))
  338.         {
  339.             if ((sPosMidMid != " ") && (sPosMidRight != " "))
  340.             {
  341.                 if ((sPosBotLeft != " ") && (sPosBotMid != " "))
  342.                 {
  343.                     if (sPosBotRight != " ")
  344.                     {
  345.                         sWinningPlayer = "Draw";
  346.                         return true;
  347.                     }
  348.                 }
  349.             }
  350.         }
  351.     }
  352.  
  353.     return false;
  354. }
  355.  
  356. string Board::GetWinningPlayer()
  357. {
  358.     return sWinningPlayer;
  359. }