Tic Tac Toe游戏功能定义错误

时间:2014-03-19 18:50:26

标签: c++ function types definitions

尝试用于c ++练习的tic tac toe游戏。继续收到以下错误

167 [错误]在输入结束时预期'}'

102 [错误]在'{'标记

之前不允许使用函数定义

97 [错误]'gboard'不是类型

91 [错误]'c'的名称查找已更改为ISO'for'scoping [-fpermissive]

这些都在void Board :: draw()中。我试图纠正所有这些错误,但我知道如何最终产生更多错误。

完整代码:

#include <iostream> 
#include <string> 
using std::string; 

enum SquareState{ blank = ' ', X = 'X', O = 'O'};

class Board{
    private: 
        const int HEIGHT;
        const int WIDTH;
        int* gboard;
    public:
        Board() : WIDTH(3), HEIGHT(3)
        {
            gboard = new int[9];
            for (int i = 0; i < 9; i++)
                *(gboard+i) = blank;
        }

        //Board() {delete[] gboard;}
        void setX(int h, int w);
        void set0(int h, int w);
        bool isTaken(int h, int w);
        int isLine();
        void draw();
};

void Board::setX(int h, int w)
{
    *(gboard + h*HEIGHT + w) = X;
}
void Board::set0(int h, int w)
{
    *(gboard + h*HEIGHT + w) = O;
}
bool Board::isTaken(int h, int w)
{
    return *(gboard + h*HEIGHT + w) != ' ';
}
int Board::isLine()
{
    if(*gboard==X && *(gboard +1)==X && *(gboard +2)==X)
        return X;
    if(*gboard==O && *(gboard +1)==O && *(gboard +2)==O)
        return 0;
    if(*(gboard+3)==X && *(gboard +4)==X && *(gboard +5)==X)
        return X;
    if(*(gboard+3)==O && *(gboard +4)==O && *(gboard +5)==O)
        return 0;
    if(*(gboard+6)==X && *(gboard +7)==X && *(gboard +8)==X)
        return X;
    if(*(gboard+6)==O && *(gboard +7)==O && *(gboard +8)==O)
        return 0;
    if(*gboard==X && *(gboard +3)==X && *(gboard +6)==X)
        return X;
    if(*gboard==O && *(gboard +3)==O && *(gboard +6)==O)
        return 0;
    if(*(gboard+1)==X && *(gboard +4)==X && *(gboard +7)==X)
        return X;
    if(*(gboard+1)==O && *(gboard +4)==O && *(gboard +7)==O)
        return 0;
    if(*(gboard+2)==X && *(gboard +5)==X && *(gboard +8)==X)
        return X;
    if(*(gboard+2)==O && *(gboard +5)==O && *(gboard +8)==O)
        return 0;
    if(*gboard==X && *(gboard +4)==X && *(gboard +8)==X)
        return X;   
    if(*gboard==O && *(gboard +4)==O && *(gboard +8)==O)
        return 0;
    if(*(gboard+2)==X && *(gboard +4)==X && *(gboard +6)==X)
        return X;
    if(*(gboard+2)==O && *(gboard +4)==O && *(gboard +6)==O)
        return 0;

    return blank;
}
void Board::draw()
{
    int i;
    using std::cout;
cout << "\n";
for (int i = 0; i <HEIGHT; i++);
{
    cout << (char)*(gboard + i*HEIGHT);
    for (int c = 1; c < WIDTH; c++);
        cout << " | " << (char)*(gboard + i*WIDTH + c);
    cout << "\n" << "------" << "/n";
}
class Game
{
    public:
        Board* doInput(string player, gboard * gb);
        bool inRange (int test);
};

Board* Game::doInput(string player, Board* gb)
{
    using std::cout;
    using std::cin;

    gb->draw();
    string letter;
    if(player.compare("one")==0)
        letter = "X";
    else if (player.compare("two")==0)
        letter = "0";
    else return gb;

    int input1, input2;
    do{
        do{
            cout << "\nplayer" << player.c_str()
                << ", choose a row to put an "
                << letter.c_str() << ": ";
            cin >> input1;
        }while(!inRange(input1));
        do{
            cout << "\nplayer" << player.c_str()
                << ", choose a column to put an "
                << letter.c_str() << ": ";
            cin >> input2;  
        }while(!inRange(input2));
    }while (gb->isTaken(input1, input2));

    if (player.compare("one")==0)
        gb->setX(input1, input2);
    else
        gb->set0(input1, input2);
    return gb;
}

bool Game::inRange(int test)
{
    return test > -1 && test < 3;
}

int main(void)
{
    using std::cout;
    using std::cin;

    Board* gb = new Board;
    Game g;
    string player1, player2;
    cout<< "Let's play some Triple-T"
        <<"\nplayer one, introduce yourself: ";
    cin >> player1;
    cout << "\nplayer two, introduce yourself: ";
    cin >> player2;

    while (gb->isLine()== ' ')
    {
        gb = g.doInput("one", gb);
        gb = g.doInput("two", gb);
    } 
    gb->draw();
    if (gb->isLine()==X)
        cout<< "\nplayer one has prevailed\n";
    else 
        cout<< "\nplayer one has prevailed\n";
    return 0;
}//end main

0 个答案:

没有答案