Reputation: 1
I'm learning c++ right now and my assignment was to create a tic tac toe game that reads the moves off a .txt file that would look something like this: 2 3 1 3 2 2 3 2 etc.
So this is the code I built:
#include <iostream>
#include <fstream>
using namespace std;
char my_board[3][3]{0,0,0,
0,0,0,
0,0,0,};
int i = 0, j = 0;
int line = 0;
int X = 0, O = 0, N = 0;
void loadSquaresFromStream(int my_board[3][3], int i, int j);
void getgameStatus (int X, int O, int N, int i, int j, int my_board[3][3]);
void displayTotals(int X, int O, int N);
int main(){
cout << loadSquaresFromStream(int my_board[3][3], int i, int j) << endl;
return 0;
}
void loadSquaresFromStream(int my_board[3][3], int i, int j){
ifstream inputStream;
inputStream.open("games.txt");
//take all numbers from txt and input into an array
int moves[] = inputStream;
for (moves[i] != 0 && moves[j] != 0 ){
switch(line){
case %2:
my_board[i][j] = x;
i++; j++;
break;
case %1:
my_board[i][j] = o;
i++; j++;
break;
}
}
else if ((moves[i] == 0) && (moves[j] == 0)){
cout << getgameStatus(X, O, N, i,j, my_board[3][3]) << endl;
}
else{
cout << displayTotals(X, O, N) << endl;
}
}
void getgameStatus (int X, int O, int N, int i, int j, int my_board[3][3]){
if (my_board[1][1] == x && my_board[1][2] == x && my_board[1][3] == x){ //All possible games where x wins
cout << "x wins" << endl;
X++;
}
else if (my_board[2][1] == x && my_board[2][2] == x && my_board[2][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[3][1] == x && my_board[3][2] == x && my_board[3][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][1] == x && my_board[2][1] == x && my_board[3][1] == x){
cout << "x wins" << endl;
X++;
}if (my_board[1][1] == x && my_board[1][2] == x && my_board[1][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[2][1] == x && my_board[2][2] == x && my_board[2][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[3][1] == x && my_board[3][2] == x && my_board[3][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][1] == x && my_board[2][1] == x && my_board[3][1] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][2] == x && my_board[2][2] == x && my_board[3][2] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][3] == x && my_board[2][3] == x && my_board[3][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][1] == x && my_board[2][2] == x && my_board[3][3] == x){cout << "Total games won by x: " << X << endl;
}
cout << "x wins" << endl;
X++;
}
else if (my_board[1][3] == x && my_board[2][2] == x && my_board[3][1] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][2] == x && my_board[2][2] == x && my_board[3][2] == x){
cout << "x wins" << endl;
X++;
}void displayTotals(X, O, N)
else if (my_board[1][3] == x && my_board[2][3] == x && my_board[3][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][1] == x && my_board[2][2] == x && my_board[3][3] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][3] == x && my_board[2][2] == x && my_board[3][1] == x){
cout << "x wins" << endl;
X++;
}
else if (my_board[1][1] == o && my_board[1][2] == o && my_board[1][3] == o){ //All possible games where o wins
cout << "o wins" << endl;
O++;
}
else if (my_board[2][1] == o && my_board[2][2] == o && my_board[2][3] == o){
cout << "o wins" << endl;
O++;
}displayTotals(X, O, N)
else if (my_board[3][1] == o && my_board[3][2] == o && my_board[3][3] == o){
cout << "o wins" << endl;
O++;
}
else if (my_board[1][1] == o && my_board[2][1] == o && my_board[3][1] == o){
cout << "o wins" << endl;
O++;cout << "Total games won by x: " << X << endl;
}
}
else if (my_board[1][2] == o && my_board[2][2] == o && my_board[3][2] == o){
cout << "o wins" << endl;
O++;
}
else if (my_board[1][3] == o && my_board[2][3] == o && my_board[3][3] == o){
cout << "o wins" << endl;
O++;
}
else if (my_board[1][1] == o && my_board[2][2] == o && my_board[3][3] == o){
cout << "o wins" << endl;
O++;
}
else if (my_board[1][3] == o && my_board[2][2] == o && my_board[3][1] == o){
cout << "o wins" << endl;
O++;
}
else{
cout << "game incomplete" << endl; //All incomplete games
N++;
}
}
void displayTotals(int X, int O, int N){
cout << "Total games won by x: " << X << endl;
cout << "Total games won by o: " << O << endl;
cout << "Total games incomplete: " << N << endl;
}
And I have several of the same "expected primary expression before..." errors throughout. I can't figure out what's causing them. I'm new to this, so sorry if it's verbose. Thanks for any help you guys have.
Upvotes: 0
Views: 79
Reputation: 15916
Ok there are several things wrong with the code you've posted.
For starters, you should know that C++ is a case sensitive language. In multiple places you've done the following:
if (my_board[1][1] == x && my_board[1][2] == x && my_board[1][3] == x)
The problem is your variables are named X
and O
not x
and o
. Change those.
You're also trying to stream the return value of functions with a void
return type. You simply cannot do that. void
as its name implies is nothing, so you're trying to stream nothing to the console.
For your switch
statement, you wrote:
case %2:
That is simply invalid. I'm not sure what you were trying to do there, so I can't really point you in the right direction.
Finally I noticed you have global variables that have the same name and type as your function parameters. There's really no point in having both. Either you have global variables to avoid passing them around (this is not good practice, don't do that) or you have function parameters; don't do both.
Upvotes: 3