Error message in tic tac toe problem please help
I keep getting an error message on line 23 using bloodshed dev-C++ that states:
"invalid conversion from '[*][3]' to 'int'"
I was wondering what the hell that meant.
here is my code:
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, int, 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, B, ROWS);
displayBoard(B, ROWS);
system("pause");
return 0;
}
void displayBoard ( int array[][COLS], int rows )
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << array[x][y] << " ";
}
cout << endl;
}
}
void player1 (int space, int array[][COLS], int rows)
{
cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
cin >> space ;
if (space == 1) array[0][0] = 'x' ;
if (space == 2) array[0][1] = 'x' ;
if (space == 3) array[0][2] = 'x' ;
if (space == 4) array[1][0] = 'x' ;
if (space == 5) array[1][1] = 'x' ;
if (space == 6) array[1][2] = 'x' ;
if (space == 7) array[2][0] = 'x' ;
if (space == 8) array[2][1] = 'x' ;
if (space == 9) array[2][2] = 'x' ;
else cout << "invalid entry" ;
}
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 ;
}
Winner function not working properly....I think
I can play the game just fine but I can't get the code to declare the winner after a winner is determined.
here is my code:
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(char [][COLS], int);
void player1 (int rowX, int colX, char array[][COLS], int rows );
void player2 (int rowO, int colO, char array[][COLS], int rows );
char winner (char array[][COLS]);
void playOneGame(char B[][COLS]);
int main ()
{
char yes;
char Y = 'Y' ;
char y = 'y' ;
char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
cout << "The playing board looks like:\n";
displayBoard(B, ROWS);
do
{
playOneGame(B);
cout << "Another game? Y/N" ;
cin >> yes;
}
while (yes == 'Y' || yes == 'y') ;
system("pause");
return 0;
}
void playOneGame (char B[][COLS])
{
int rowX = 0 ;
int colX = 0 ;
int rowO = 0 ;
int colO = 0 ;
for (int i = 0; i < 5 ; i++)
{
player1 (rowX, colX, B, ROWS);
winner (B);
displayBoard(B, ROWS);
player2 (rowO, colO, B, ROWS);
winner (B);
displayBoard(B, ROWS);
}
}
void displayBoard ( char array[][COLS], int rows )
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << array[x][y] << " ";
}
cout << endl;
}
}
void player1 (int rowX, int colX, char array[][COLS], int rows)
{
cout << "Please select a space on the board to place your 'x' by by entering the row first and then the column, pressing 'enter' after each entry\n";
cin >> rowX >> colX ;
if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
}
void player2 (int rowO, int colO, char array[][COLS], int rows )
{
cout << "Please select a space on the board to place your 'o' by entering the row first and then the column, pressing 'enter' after each entry\n";
cin >> rowO >> colO ;
if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
}
char winner ( char B[][COLS])
{
char answer = 'n' ;
if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
answer = B[0][0];
if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
answer = B[1][0];
if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
answer = B[2][0];
if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
answer = B[1][0];
if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
answer = B[1][1];
if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
answer = B[2][2];
if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
answer = B[2][2];
if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
answer = B[1][1];
return answer ;
}
Still trying to get the winner declared.....
Ok I think I fixed all the little things, but I still cannot figure out how to display a winner when a winner is determined. My program spits out a winner everytime it is ran.
Here is my code:
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(char [][COLS], int);
void player1 (int rowX, int colX, char array[][COLS] );
void player2 (int rowO, int colO, char array[][COLS] );
char winner (char array[][COLS]);
void playOneGame(char B[][COLS]);
int main ()
{
char yes;
char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
char answer = 'n' ;
cout << "The playing board looks like:\n";
displayBoard(B, ROWS);
cout << "To play the game enter the row in which you would like to place your marker and press enter. Then do the same for the column.\n";
do
{
playOneGame(B);
cout << "Another game? Y/N" ;
cin >> yes;
}
while (yes == 'Y' || yes == 'y') ;
system("pause");
return 0;
}
void playOneGame (char B[][COLS])
{
int rowX = 0 ;
int colX = 0 ;
int rowO = 0 ;
int colO = 0 ;
for (int i = 0; i < 6 ; i++)
{
player1 (rowX, colX, B);
winner (B) ;
displayBoard(B, ROWS);
player2 (rowO, colO, B);
winner (B);
displayBoard(B, ROWS);
}
}
void displayBoard ( char array[][COLS], int rows )
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << array[x][y] << " ";
}
cout << endl;
}
}
void player1 (int rowX, int colX, char array[][COLS])
{
cout << "Please select a space on the board to place your 'x'\n";
cin >> rowX >> colX ;
if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
}
void player2 (int rowO, int colO, char array[][COLS] )
{
cout << "Please select a space on the board to place your 'o'\n";
cin >> rowO >> colO ;
if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
}
char winner ( char B[][COLS])
{
char answer;
if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
answer = B[0][0] ;
if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
answer = B[1][0];
if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
answer = B[2][0];
if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
answer = B[1][0];
if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
answer = B[1][1];
if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
answer = B[2][2];
if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
answer = B[2][2];
if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
answer = B[1][1];
cout << answer ;
}