Help cant call on function.
Author |
Message |
Al_
|
Posted: Sun May 24, 2009 11:45 am Post subject: Help cant call on function. |
|
|
Hi. to those who will have helped me, thank you in advance. Heres the code:
im trying to make a tic tac toe game with arrays.
code: | #include<iostream>
using namespace std;
//int get_input();
int check(int board[3][3], int p);
int main()
{
int board[3][3];
//Top Row
board[0][0] = 0;
board[0][1] = 0;
board[0][2] = 0;
//Middle Row
board[1][0] = 0;
board[1][1] = 0;
board[1][2] = 0;
//Bottom Row
board[2][0] = 0;
board[2][1] = 0;
board[2][2] = 0;
int p = 0;
while (p != 0){
// get_input();
check(board[3][3],p); //Line 30
}
}
int check(int board[3][3], int p){
//Player 1 Cases for winning
//Rows
if ((board[0][0] = 1) && (board[0][1] = 1) && (board[0][2] = 1)){
cout<<"Player 1 wins";
p = 1;
}
else if ((board[1][0] = 1) && (board[1][1] = 1) && (board[1][2] = 1)){
cout<<"Player 1 wins";
p = 1;
}
else if ((board[2][0] = 1) && (board[2][1] = 1) && (board[2][2] = 1)){
cout<<"Player 1 wins";
p = 1;
}
//Columns
else if ((board[0][0] = 1) && (board[1][0] = 1) && (board[2][0] = 1)){
cout<<"Player 1 wins";
p = 1;
}
else if ((board[0][1] = 1) && (board[1][1] = 1) && (board[2][1] = 1)){
cout<<"Player 1 wins";
p = 1;
}
else if ((board[0][2] = 1) && (board[1][2] = 1) && (board[2][2] = 1)){
cout<<"Player 1 wins";
p = 1;
}
//Diagonals
else if ((board[0][0] = 1) && (board[1][1] = 1) && (board[1][2] = 1)){
cout<<"Player 1 wins";
p = 1;
}
else if ((board[0][2] = 1) && (board[1][1] = 1) && (board[2][0] = 1)){
cout<<"Player 1 wins";
p = 1;
}
//Player 2 Cases for winning
//Rows
if ((board[0][0] = 2) && (board[0][1] = 2) && (board[0][2] = 2)){
cout<<"Player 2 wins";
p = 2;
}
else if ((board[1][0] = 2) && (board[1][1] = 2) && (board[1][2] = 2)){
cout<<"Player 2 wins";
p = 2;
}
else if ((board[2][0] = 2) && (board[2][1] = 2) && (board[2][2] = 2)){
cout<<"Player 2 wins";
p = 2;
}
//Columns
else if ((board[0][0] = 2) && (board[1][0] = 2) && (board[2][0] = 2)){
cout<<"Player 2 wins";
p = 2;
}
else if ((board[0][1] = 2) && (board[1][1] = 2) && (board[2][1] = 2)){
cout<<"Player 2 wins";
p = 2;
}
else if ((board[0][2] = 2) && (board[1][2] = 2) && (board[2][2] = 2)){
cout<<"Player 2 wins";
p = 2;
}
//Diagonals
else if ((board[0][0] = 2) && (board[1][1] = 2) && (board[1][2] = 2)){
cout<<"Player 2 wins";
p = 2;
}
else if ((board[0][2] = 2) && (board[1][1] = 2) && (board[2][0] = 2)){
cout<<"Player 2 wins";
p = 2;
}
}
|
Im using Dev-C++.
Error message:
in function 'int main()':
invalid conversion from 'int' to 'int(*)[3]' Line 30
initializing argument 1 of 'int check(int(*)[3])' Line 30 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DtY

|
Posted: Sun May 24, 2009 1:06 pm Post subject: RE:Help cant call on function. |
|
|
You want to pass a reference to the array, not the alement [3][3], the call should look like:
CPP: | check(board,p); //Line 30 |
Also, in a lot of your ifs, you have stuff like:
CPP: | if ((board[0][0] = 1) && (board[0][1] = 1) && (board[0][2] = 1)) |
The "=" in C++ means assignment, what you want here is comparison, which is "==". |
|
|
|
|
 |
Al_
|
Posted: Sun May 24, 2009 6:13 pm Post subject: RE:Help cant call on function. |
|
|
Thank you very much for your help. And thanks for noticing the "=" mistake, probably saved me an hour of headaches. |
|
|
|
|
 |
Al_
|
Posted: Sun May 24, 2009 6:17 pm Post subject: RE:Help cant call on function. |
|
|
Two quick question:
Once i return to the main, after the check() function, are the values for p and the values for each entry in the array saved?
And also, if none of the conditions in the if statement are met, then the compiler skips over the if statement and ultimately the check() function does nothing. Is this correct? |
|
|
|
|
 |
DtY

|
Posted: Sun May 24, 2009 6:28 pm Post subject: RE:Help cant call on function. |
|
|
First question: Yes, the new values will be saved. This is because arrays are pointers. If you haven't learned about pointers yet, don't worry about it, just know that if you pass an array to a function, and use that function to change a value, it will edit the original. It wont do this for other types though, unless you explicitly pass a pointer.
p wont be changed, but since it's a single value, you can return the value of p, rather than changing it in there (so you wont need to pass p at all)
Second: That is correct. If you want to make sure it does, add else at the end:
c++: | else if ((board[0][2] = 2) && (board[1][1] = 2) && (board[2][0] = 2)){
cout<<"Player 2 wins";
p = 2;
} else { /* Default condition here! */ } |
|
|
|
|
|
 |
|
|