tictactoe游戏没有运营商" =="匹配这些操作数

时间:2012-07-10 16:12:14

标签: c++ string printf iostream tic-tac-toe

它可能是非常低效和凌乱,但这是我的问题 - 它说“zz == defo&& zo == defo”之类的东西等于用红色加下划线说错误:没有运算符“==”匹配这些操作数,当我运行时,我得到的错误太长,无法在此处发布 - http://pastebin.com/KTEM0MZK

我做错了什么?提前致谢

这是我的代码 -

#include <iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
using namespace std;

string random1(){
    srand(((time(0) - 23) * time(0)) - (9*time(0)));
  if (rand() % 2 == 0){
    return "1";
        } else {
            return "0";
        }
}
string random2(){
    srand((((time(0) - 89) * time(0)) - (9*time(0)) / 3) - 99);
  if (rand() % 2 == 0){
    return "1";
        } else {
            return "0";
        }
}
int main(){
    string zz = "-";
    string zo = "-";
    string zt = "-";
    string oz = "-";
    string oo = "-";
    string ot = "-";
    string tz = "-";
    string to = "-";
    string tt = "-";
    for(int a = 1;a<=9;a++){
        srand ( time(NULL) - 8 );
        int ran1 = rand() % 3;  
        srand ( time(NULL) * 2);
        int ran2 = rand() % 3;
        int tote = (ran1 * 10) + ran2;
        cout << endl << "format- 0 = zero, 1 = one, 2 = two" << endl;
        int input;
        cin >> input;
        if(input == 00){
            zz = "X";
        } else if(input == 01){
            zo = "X";
        } else if(input == 02){
            zt = "X";
        } else if(input == 10){
            oz = "X";
        } else if(input == 11){
            oo = "X";
        } else if(input == 12){
            ot = "X";
        } else if(input == 20){
            tz = "X";
        } else if(input == 21){
            to = "X";
        } else if(input == 22){
            tt = "X";
        }
        // now for the computers part
        if(tote == 00){
            zz = "O";
        } else if(tote == 01){
            zo = "O";
        } else if(tote == 02){
            zt = "O";
        } else if(tote == 10){
            oz = "O";
        } else if(tote == 11){
            oo = "O";
        } else if(tote == 12){
            ot = "O";
        } else if(tote == 20){
            tz = "O";
        } else if(tote == 21){
            to = "O";
        } else if(tote == 22){
            tt = "O";
        }
        printf ("|%d|%d|%d", zz, zo, zt);
        cout << endl;
        printf ("|%d|%d|%d", oz, oo, ot);
        cout << endl;
        printf ("|%d|%d|%d", tz, to, tt);

        if(zz == "X" && zo == "X" && zt == "X" || oz == "X" && oo == "X" && ot == "X" || tz == "X" && to == "X" && tt == "X" || zz == "X" && oz == "X" && tz == "X" || zo == "X" && oo == "X" && to == "X" || zt == "X" && ot == "X" && tt == "X" || zz == "X" && oo == "X" && tt == "X"){
            cout << endl << "X WINNER";
        break;
        }
        if(zz == "O" && zo == "O" && zt == "O" || oz == "O" && oo == "O" && ot == "O" || tz == "O" && to == "O" && tt == "O" || zz == "O" && oz == "O" && tz == "O" || zo == "O" && oo == "O" && to == "O" || zt == "O" && ot == "O" && tt == "O" || zz == "O" && oo == "O" && tt == "O"){
            cout << endl << "O WINNER";
            break;
        }
    }
    system("pause");
    return 0;
}

3 个答案:

答案 0 :(得分:1)

您需要#include <string>,其中operator==已定义。

并且,正如jrok所述,您正在将std::string传递给printf():改为使用cout(正如您已经使用过的那样)。

答案 1 :(得分:0)

您正试图将std::string传递给printf,这是一个C库函数。此外,格式字符串表示您将传递整数("|%d|%d|%d")。

printf只会采用c风格的字符串,所以如果这是你想要的,你可以这样说:

printf("|%s|%s|%s", ot.c_str(), zz.c_str(), ...);

答案 2 :(得分:0)

也与您的代码相关

if(zz == "X" && zo == "X" && zt == "X" || oz == "X" && oo == "X" && ot == "X" || tz == "X" && to == "X" && tt == "X" || zz == "X" && oz == "X" && tz == "X" || zo == "X" && oo == "X" && to == "X" || zt == "X" && ot == "X" && tt == "X" || zz == "X" && oo == "X" && tt == "X"){
        cout << endl << "X WINNER";

应该是

if((zz == "X" && zo == "X" && zt == "X") || (oz == "X" && oo == "X" && ot == "X") || (tz == "X" && to == "X" && tt == "X") || (zz == "X" && oz == "X" && tz == "X") || (zo == "X" && oo == "X" && to == "X") || (zt == "X" && ot == "X" && tt == "X") || (zz == "X" && oo == "X" && tt == "X")){
        cout << endl << "X WINNER";

您的比较是查看每个单独的标记,而不是寻找标记的组合。每组3周围的()将为您提供所需的结果。