I feel that I have all the parts I need and I feel very close, however I cannot get my functions to be used in void main. Any help is appreciated! Thanks!
CODE
#include<iostream>
using namespace std;
class TTTBoard {
public:
void PrintBoard();
char board[9] = { '1','2','3','4','5','6','7','8','9' };
void WhosMove(char Turn);
void Update();
void UserSelectASlot(int Spot);
void WinX();
void WinY();
};
void TTTBoard::WinX()
{
if (board[0] == board[1] && board[1] == board[2])
{
if (board[0] == 'X')
goto Win;
else
goto NoWin;
}
else
if (board[3] == board[4] && board[4] == board[5])
{
if (board[3] == 'X')
goto Win;
else
goto NoWin;
}
else
if (board[6] == board[7] && board[7] == board[8])
{
if (board[6] == 'X')
goto Win;
else
goto NoWin;
}
else
if (board[0] == board[3] && board[3] == board[6])
{
if (board[0] == 'X')
goto Win;
else
goto NoWin;
}
else
if (board[1] == board[4] && board[4] == board[7])
{
if (board[1] == 'X')
goto Win;
else
goto NoWin;
}
else
if (board[2] == board[5] && board[5] == board[8])
{
if (board[2] == 'X')
goto Win;
else
goto NoWin;
}
else
if (board[0] == board[4] && board[4] == board[8])
{
if (board[0] == 'X')
goto Win;
else
goto NoWin;
}
else
if (board[2] == board[4] && board[4] == board[6])
{
if (board[2] == 'X')
goto Win;
else
goto NoWin;
}
else
goto Tie;
Win:
cout << "Congratulations! You Won!";
NoWin:
cout << "You didnt win :(";
Tie:
cout << "You tied with a computer?";
}
void TTTBoard::WinY()
{
if (board[0] == board[1] && board[1] == board[2])
{
if (board[0] == 'Y')
goto Win;
else
goto NoWin;
}
else
if (board[3] == board[4] && board[4] == board[5])
{
if (board[3] == 'Y')
goto Win;
else
goto NoWin;
}
else
if (board[6] == board[7] && board[7] == board[8])
{
if (board[6] == 'Y')
goto Win;
else
goto NoWin;
}
else
if (board[0] == board[3] && board[3] == board[6])
{
if (board[0] == 'Y')
goto Win;
else
goto NoWin;
}
else
if (board[1] == board[4] && board[4] == board[7])
{
if (board[1] == 'Y')
goto Win;
else
goto NoWin;
}
else
if (board[2] == board[5] && board[5] == board[8])
{
if (board[2] == 'Y')
goto Win;
else
goto NoWin;
}
else
if (board[0] == board[4] && board[4] == board[8])
{
if (board[0] == 'Y')
goto Win;
else
goto NoWin;
}
else
if (board[2] == board[4] && board[4] == board[6])
{
if (board[2] == 'Y')
goto Win;
else
goto NoWin;
}
else
{
goto Tie;
}
Win:
cout << "Sorry you lost pal!";
NoWin :
cout << "I'll beat you next time!";
Tie:
cout << "Your lucky we tied!";
}
void TTTBoard::UserSelectASlot(int Spot)
{
cout << "\nSelect the space you would like to place your marker in\n";
cin >> Spot;
//All Available Options For Open Spots
if (Spot == 1)
{
board[0] = Spot;
}
else if (Spot == 2)
{
board[1] = Spot;
}
else if (Spot == 3)
{
board[2] = Spot;
}
else if (Spot == 4)
{
board[3] = Spot;
}
else if (Spot == 5)
{
board[4] = Spot;
}
else if (Spot == 6)
{
board[5] = Spot;
}
else if (Spot == 7)
{
board[6] = Spot;
}
else if (Spot == 8)
{
board[7] = Spot;
}
else if (Spot == 9)
{
board[8] = Spot;
}
}
void TTTBoard::PrintBoard()
{
//Designing the board
cout <<" "<< board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << " _ _ _ _ _ \n" << endl;
cout <<" "<< board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << " _ _ _ _ _ \n" << endl;
cout <<" "<< board[6] << " | " << board[7] << " | " << board[8] << endl;
}
void TTTBoard::WhosMove(char Turn) {
//Players Turn
if (Turn == 'X')
{
Turn = 'O';
cout << "It's my turn now!";
}
//Computers Turn
else if(Turn == 'O')
{
Turn = 'X';
cout << "It's your turn now!";
}
}
void TTTBoard::Update()
{
for (int x = 0; x < 9; x++)
{
cout << board[x] << "\t";
if (x == 2 || x == 5 || x == 8)
cout << "\n";
}
}
int main()
{
TTTBoard game;
cout << "You have loaded a Tic-Tac-Toe Program!";
cout << "\nIn this program you will be playing against a computer!";
cout << "\nThe board will be printed with a series of numbers";
cout << "\nYou will type that number in to choose that spot...if you go first";
cout << "\n\n##################\nLet The Game Begin\n##################\n\n";
cout << "\nYou are X and the Computer is O\n";
class TTTBoard
game.PrintBoard();
game.WhosMove(Turn);
game.UserSelectASlot(Spot);
game.Update();
game.WinX();
game.WinY();
}
I have been reading for a few hours on this, and I understand I can call to the function using &? I also found out that placing my integer or char in the game.function in main just sends that to my function, if I understood that correct.
I know there may be some small errors in other places, but for the most part I feel pretty good about it. If you can help me learn what's going on I would appreciate it, Thanks!
Aucun commentaire:
Enregistrer un commentaire