gpt4 book ai didi

c++ - Tic Tac Toe 预期主要表达之前

转载 作者:行者123 更新时间:2023-11-28 00:32:45 25 4
gpt4 key购买 nike

我现在正在学习 C++,我的任务是创建一个 tic tac toe 游戏,该游戏读取 .txt 文件的 Action ,看起来像这样:2 31 32 23 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;

}

我有几个相同的“预期的主表达式之前......”错误。我不知道是什么原因造成的。我是新手,很抱歉,如果它很冗长。感谢你们提供的任何帮助。

最佳答案

好的,您发布的代码有几处错误。

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

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

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

您还尝试流式传输具有 void 返回类型的函数的返回值。你根本不能那样做。 void 顾名思义什么都不是,因此您试图将任何内容流式传输到控制台。

对于您的 switch 语句,您写道:

case %2:

那简直是无效的。我不确定您在那里尝试做什么,所以我无法真正为您指明正确的方向。

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

关于c++ - Tic Tac Toe 预期主要表达之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com