Tic Tac Toe之前预期的初步表达

时间:2014-03-05 04:42:49

标签: c++

我正在学习c ++,我的任务是创建一个tic tac toe游戏,它读取.txt文件中的移动,看起来像这样: 2 3 1 3 2 2 3 2 等

所以这是我建立的代码:

#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;

}

我有几个相同的“预期的主要表达式......之前......”错误。我无法弄清楚是什么导致了他们。我是新手,对不起,如果它很冗长。谢谢你们的任何帮助。

1 个答案:

答案 0 :(得分:3)

好的,你发布的代码有几个问题。

对于初学者,您应该知道C ++是一种区分大小写的语言。在多个地方,您已完成以下操作:

if (my_board[1][1] == x && my_board[1][2] == x && my_board[1][3] == x)

问题是您的变量名为XO不是xo。改变那些。

您还尝试使用void返回类型来传递函数的返回值。你根本做不到。 void顾名思义就是什么都没有,所以你试图不向控制台传输任何内容。

对于您的switch声明,您写道:

case %2:

这简直无效。我不确定你在那里做什么,所以我不能指出你正确的方向。

最后,我注意到你的全局变量与你的函数参数具有相同的名称和类型。两者都没有意义。要么你有全局变量要避免传递它们(这不是好习惯,不要这样做)你有函数参数;不要两者兼顾。