I am writing a simple tic tac toe problem for my first C++ programming class that needs four functions, one to display the board, one for player1 to enter its postion on the board one for player 2 to enter its position on the board, and a function that decides when a winner has emerged. I also need the program to loop until there is a winner and to ask if the players want to play another round. Right now I am just trying to get the basics down seeing if I can get an X to actually show up when I call the displayBoard function for the second time after player 1 goes. If anyone can help please let me know.
Here is my code so far:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
//Global constants
const int COLS = 3; //Number of columns on the board
const int ROWS = 3; //Number of rows on the board
void displayBoard(int [][COLS], int);
void player1 (int);
void player2 (int);
int main ()
{
int space = 0 ;
int space2 = 0 ;
int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
cout << "The playing board looks like:\n";
displayBoard(B, ROWS);
player1 (space);
displayBoard(B, ROWS);
system("pause");
return 0;
}
void displayBoard ( int array[][COLS], int rows, int space, int space2 )
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << array[x][y] << " ";
}
cout << endl;
}
}
void player1 (int space)
{
cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
cin >> space ;
int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
if (space = 1) B[0][0] = 'x' ;
if (space = 2) B[0][1] = 'x' ;
if (space = 3) B[0][2] = 'x' ;
if (space = 4) B[1][0] = 'x' ;
if (space = 5) B[1][1] = 'x' ;
if (space = 6) B[1][2] = 'x' ;
if (space = 7) B[2][0] = 'x' ;
if (space = 8) B[2][1] = 'x' ;
else if (space =9) B[2][2] = 'x' ;
}
void player2 (int space2)
{
cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
cin >> space2 ;
int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
if (space2 = 1) B[0][0] = 'x' ;
if (space2 = 2) B[0][1] = 'x' ;
if (space2 = 3) B[0][2] = 'x' ;
if (space2 = 4) B[1][0] = 'x' ;
if (space2 = 5) B[1][1] = 'x' ;
if (space2 = 6) B[1][2] = 'x' ;
if (space2 = 7) B[2][0] = 'x' ;
if (space2 = 8) B[2][1] = 'x' ;
else if (space2 =9) B[2][2] = 'x' ;
}
char winner (char winner)
{
int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
winner = B[0][0];
if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
winner = B[1][0];
if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
winner = B[2][0];
if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
winner = B[1][0];
if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
winner = B[1][1];
if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
winner = B[2][2];
if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
winner = B[2][2];
if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
winner = B[1][1];
return winner ;
}