I have this code for a tic tac toe game, it`s not done yet, and what I have done isn`t working like I want it to.
The screan prints and promps player 1 and 2 for inputs one at a time, but my while loop in main is probably wrong, the loop continues even after the grid if fool of X and O, I want the program to end when it`s done, and then I`ll finish my checkWinner function.
somebody told me to change my functions to a bool, but I`m not sure how to do that.
Can anybody help me?
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::setw;
//global variables
const int row = 3; //size of array
const int column = 3; // size of array
string BLANK = " ";
string ticTac[row][column];
void ticTacToe(const string ticTacToe[][column]); //Function prototypes
void player1(string ticTacToe[][column]);
void player2(string ticTac[][column]);
void checkWinner(string ticTac[][column]);
// program execution begins
int main()
{
string ticTac[row][column] = {{BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}}; //number display for rows and columns
cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
ticTacToe( ticTac ); // function call
while(ticTac[row][column] != BLANK)
{
player1(ticTac);
player2(ticTac);
}
cout << endl;
return 0;
}
//Function ticTacToe
void ticTacToe(const string ticTacToe[][column])
{
cout << setw(5)<<"|"<<setw(5)<< "|" <<endl;
//for loop-continuation condition and increment
for (int i = 0; i < row; i ++)
{
for (int j = 0; j < column; j++)
//check to output '|' only in between the numbers, not at the end
if ( j < column - 1)
{
cout << setw(3)<< ticTacToe[i][j] << setw(2)<<"|";
}
else
cout << setw(3)<<ticTacToe[i][j];
cout << endl;
// check to make sure the horizontal line in only printed in between
// the 3 rows, not on the bottom fo the picture
if ( i < row -1)
{
cout << "____|____|____"<<endl;
cout << setw(5) << "|" << setw(5) << "|" << endl;
}
else
cout << setw(5) << "|" << setw(5) << "|" << endl;
}
}
void player1(string ticTac[][column])
{
int x = 0;
int y = 0;
string p1 = "X";
cout << "Player one can enter your coordinates for X." << endl;
cin >> x >> y ;
x--, y--;
while (x >= row || y >= column)
{
cout << "Invalit entry, try a number from 1 to 3.";
cin >> x >> y;
x--, y--;
}
if (ticTac[x][y] != BLANK)
{
cout << "That spot is taken." << "\nEnter different coordinates for 'X'." << endl;
player1(ticTac);
}
else
ticTac[x][y] = p1;
ticTacToe( ticTac );
}
void player2(string ticTac[][column])
{
int x = 0;
int y = 0;
string p2 = "O";
cout << "Player two can enter you coordinates for 'O'. ";
cin >> x >> y;
x--,y--;
while (x >= row || y >= column)
{
cout << "Invalit entry, try a number from 1 to 3.";
cin >> x >> y;
x--, y--;
}
if (ticTac[x][y] != BLANK)
{
cout << "That spot is taken." << "\nEnter different coordinates for 'O'." << endl;
player2(ticTac);
}
else
ticTac[x][y] = p2;
ticTacToe( ticTac );
}
/*void checkWinner(const string ticTac[][column])
{
for (int i = 0; i > row; i++)
{
for (int j = 0; j > column; j++)
{
//check rows for winner
if (ticTac[0][0] == ticTac[1][0] && ticTac[1][0] == ticTac[2][0])
{
cout << "YOU ARE THE WINNER!";
}
else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1])
{
cout << "YOU ARE THE WINNER!";
}
else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][1] == ticTac[2][2])
{
cout << "YOU ARE THE WINNER!";
}
//check for winner diagnally
else if (ticTac[2][0] == ticTac[1][1] && ticTac[1][1] == ticTac[0][2])
{
cout << "YOU ARE THE WINNER!";
}
else if ([0][0] == ticTac[1][1] && ticTac[1][2] == ticTac[2][2])
{
cout << "YOU ARE THE WINNER!";
}
//check columns for winner
else if ([0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2])
{
cout << "YOU ARE THE WINNER!";
}
else if ([1][0] == ticTac[1][1] && ticTac[1][1] == ticTac[1][2])
{
cout << "YOU ARE THE WINNER!";
}
else if ([2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2])
{
cout << "YOU ARE THE WINNER!";
}
}
}
}*/
I commented this last function out because I want to get just what`s in main working properly first before finishing it