Hi guys.. So I'm solving Problem 3 of Chapter 7 in Jumping into C++. Basically it's telling me to create a two player tic tac toe game. As you can see the code is far from finish and I probably will run into a ton of other problems later, but first I want to know why my function for background_work doesn't work. It won't even compile. : /
I have tested the code without the void function being called and it works fine if put in main. Also I tried declaring it's variables as global variables but it still didn't work (so i just put it back down to keep the code neater). Any help will be appreciated.
Code:
// Write a two-player tic-tac-toe game, allowing two humans to play against each other; use enums when possible to represent the values of the board.
#include <iostream>
using namespace std;
int x; // My clasic and favorite input variable
void Show_Board ();
void BackGround_Work ();
void UserInput ();
//Board Spaces as Global Variables
char Space_1 = ('1');
char Space_2 = ('2');
char Space_3 = ('3');
char Space_4 = ('4');
char Space_5 = ('5');
char Space_6 = ('6');
char Space_7 = ('7');
char Space_8 = ('8');
char Space_9 = ('9');
int main ()
{
// Intro and Intstructions...
cout << "Awesome Student Presents C++ Tic Tac Toe!!\n";
cout << "Intructions:\n";
cout << "Get 3 in a row to win on a 3x3 grid!\n";
cout << "Choose from squares 1 to 9 by inputing a number!\n\n";
cout << "The grid can be seen as followed..\n\n";
Show_Board();
cout << "Player 1 --> X\n";
cout << "Player 2 --> O\n\n";
// Game
for (int i = 0; i < 9; i++)
{
cout << "Player 1's Turn!\n";
UserInput();
BackGround_Work();
}
}
void Show_Board () // Shows the Tic Tac Toe Board
{
cout << "\t"<<Space_1<<"\t|\t"<<Space_2<<"\t|\t"<<Space_3<<"\n";
cout << "--------+-------+-------\n";
cout << "\t"<<Space_4<<"\t|\t"<<Space_5<<"\t|\t"<<Space_6<<"\n";
cout << "--------+-------+-------\n";
cout << "\t"<<Space_7<<"\t|\t"<<Space_8<<"\t|\t"<<Space_9<<"\n\n";
}
void Background_Work ()
{
// Variables Declared
enum TicTacToe {Empty, X_Filled, O_Filled};
int iSpace_1 = Empty;
int iSpace_2 = Empty;
int iSpace_3 = Empty;
int iSpace_4 = Empty;
int iSpace_5 = Empty;
int iSpace_6 = Empty;
int iSpace_7 = Empty;
int iSpace_8 = Empty;
int iSpace_9 = Empty;
// Visual Change & Background Work
bool done = false;
while (!done) {
switch (x) {
case 1:
if (iSpace_1 == Empty)
Space_1 = ('X'), iSpace_1 = X_Filled;
break;
case 2:
if (iSpace_2 == Empty)
Space_2 = ('X'), iSpace_2 = X_Filled;
break;
case 3:
if (iSpace_3 == Empty)
Space_3 = ('X'), iSpace_3 = X_Filled;
break;
case 4:
if (iSpace_4 == Empty)
Space_4 = ('X'), iSpace_4 = X_Filled;
break;
case 5:
if (iSpace_5 == Empty)
Space_5 = ('X'), iSpace_5 = X_Filled;
break;
case 6:
if (iSpace_6 == Empty)
Space_6 = ('X'), iSpace_6 = X_Filled;
break;
case 7:
if (iSpace_7 == Empty)
Space_7 = ('X'), iSpace_7 = X_Filled;
break;
case 8:
if (iSpace_8 == Empty)
Space_8 = ('X'), iSpace_8 = X_Filled;
break;
case 9:
if (iSpace_9 == Empty)
Space_9 = ('X'), iSpace_9 = X_Filled;
break;
default:
cout << "Invalid Move!\n";
break;
}
}
}
void UserInput () // So that letter inputs don't mess up the loops!
{
while (!(cin >> x))
{
cout << "A number please..\n";
cin.clear();
cin.ignore(100, '\n');
}
}