#include "board.h"
Board::Board()
{
sPosTopLeft = " ";
sPosTopMid = " ";
sPosTopRight = " ";
sPosMidLeft = " ";
sPosMidMid = " ";
sPosMidRight = " ";
sPosBotLeft = " ";
sPosBotMid = " ";
sPosBotRight = " ";
sWinningPlayer = " ";
}
int Board::SetSquare(int nPos, string XorO)
{
if ((XorO == "X") || (XorO == "O"))
{
switch(nPos)
{
case 1:
{
if(sPosTopLeft == " ")
{
sPosTopLeft = XorO;
return 0;
}
else
{
return 1;
}
}
case 2:
{
if(sPosTopMid == " ")
{
sPosTopMid = XorO;
return 0;
}
else
{
return 1;
}
}
case 3:
{
if(sPosTopRight == " ")
{
sPosTopRight = XorO;
return 0;
}
else
{
return 1;
}
}
case 4:
{
if(sPosMidLeft == " ")
{
sPosMidLeft = XorO;
return 0;
}
else
{
return 1;
}
}
case 5:
{
if(sPosMidMid == " ")
{
sPosMidMid = XorO;
return 0;
}
else
{
return 1;
}
}
case 6:
{
if(sPosMidRight == " ")
{
sPosMidRight = XorO;
return 0;
}
else
{
return 1;
}
}
case 7:
{
if(sPosBotLeft == " ")
{
sPosBotLeft = XorO;
return 0;
}
else
{
return 1;
}
}
case 8:
{
if(sPosBotMid == " ")
{
sPosBotMid = XorO;
return 0;
}
else
{
return 1;
}
}
case 9:
{
if(sPosBotRight == " ")
{
sPosBotRight = XorO;
return 0;
}
else
{
return 1;
}
}
default:
{
return 2;
}
}
}
}
void Board::ResetBoard()
{
sPosTopLeft = " ";
sPosTopMid = " ";
sPosTopRight = " ";
sPosMidLeft = " ";
sPosMidMid = " ";
sPosMidRight = " ";
sPosBotLeft = " ";
sPosBotMid = " ";
sPosBotRight = " ";
sWinningPlayer = " ";
return;
}
void Board::PrintBoard()
{
using std::cout;
using std::endl;
cout << string( 100, '\n' );
cout << " | | " << endl;
cout << " " << sPosTopLeft << " | " << sPosTopMid << " | " << sPosTopRight << " " << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << sPosMidLeft << " | " << sPosMidMid << " | " << sPosMidRight << " " << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << sPosBotLeft << " | " << sPosBotMid << " | " << sPosBotRight << " " << endl;
cout << " | | " << endl;
cout << "\n\n";
return;
}
bool Board::CheckWinCondition()
{
if (sPosTopLeft == "X")
{
if (sPosTopMid == "X")
{
if (sPosTopRight == "X")
{
sWinningPlayer = "X";
return true;
}
}
if (sPosMidLeft == "X")
{
if (sPosBotLeft == "X")
{
sWinningPlayer = "X";
return true;
}
}
if (sPosMidMid == "X")
{
if (sPosBotRight == "X")
{
sWinningPlayer = "X";
return true;
}
}
}
if (sPosTopMid == "X")
{
if (sPosMidMid == "X")
{
if (sPosBotMid == "X")
{
sWinningPlayer = "X";
return true;
}
}
}
if (sPosTopRight == "X")
{
if (sPosMidMid == "X")
{
if (sPosBotLeft == "X")
{
sWinningPlayer = "X";
return true;
}
}
if (sPosMidRight == "X")
{
if (sPosBotRight == "X")
{
sWinningPlayer = "X";
return true;
}
}
}
if (sPosMidLeft == "X")
{
if (sPosMidMid == "X")
{
if (sPosMidRight == "X")
{
sWinningPlayer = "X";
return true;
}
}
}
if (sPosBotLeft == "X")
{
if (sPosBotMid == "X")
{
if(sPosBotRight == "X")
{
sWinningPlayer = "X";
return true;
}
}
}
///////////////////////////////////
if (sPosTopLeft == "O")
{
if (sPosTopMid == "O")
{
if (sPosTopRight == "O")
{
sWinningPlayer = "O";
return true;
}
}
if (sPosMidLeft == "O")
{
if (sPosBotLeft == "O")
{
sWinningPlayer = "O";
return true;
}
}
if (sPosMidMid == "O")
{
if (sPosBotRight == "O")
{
sWinningPlayer = "O";
return true;
}
}
}
if (sPosTopMid == "O")
{
if (sPosMidMid == "O")
{
if (sPosBotMid == "O")
{
sWinningPlayer = "O";
return true;
}
}
}
if (sPosTopRight == "O")
{
if (sPosMidMid == "O")
{
if (sPosBotLeft == "O")
{
sWinningPlayer = "O";
return true;
}
}
if (sPosMidRight == "O")
{
if (sPosBotRight == "O")
{
sWinningPlayer = "O";
return true;
}
}
}
if (sPosMidLeft == "O")
{
if (sPosMidMid == "O")
{
if (sPosMidRight == "O")
{
sWinningPlayer = "O";
return true;
}
}
}
if (sPosBotLeft == "O")
{
if (sPosBotMid == "O")
{
if(sPosBotRight == "O")
{
sWinningPlayer = "O";
return true;
}
}
}
if ((sPosTopLeft != " ") && (sPosTopMid != " "))
{
if ((sPosTopRight != " ") && (sPosMidLeft != " "))
{
if ((sPosMidMid != " ") && (sPosMidRight != " "))
{
if ((sPosBotLeft != " ") && (sPosBotMid != " "))
{
if (sPosBotRight != " ")
{
sWinningPlayer = "Draw";
return true;
}
}
}
}
}
return false;
}
string Board::GetWinningPlayer()
{
return sWinningPlayer;
}