如何在多维数组中使用数据类型char

时间:2014-09-18 22:40:01

标签: c++ arrays multidimensional-array

我正致力于一个程序,允许用户与另一个人类玩家玩tic tac toe。到目前为止,我已经创建了一个" board"一系列选项,允许用户选择他们想要放置" x"或" o"。但是,我的板是一个多维数组,数据类型为char。每当我尝试编辑这个数组时,我都会收到错误,我也无法在头文件中声明数组原型,然后在cpp文件中定义值。 我的问题是:我如何声明这个数组,以便能够在整个程序中修改它? 以下是我到目前为止的情况:

标题

class Game
{
    public:
        Game();
        void printBoard();
        void firstMove();
        int updateBoard(int m);
    private:
        char board[3][3];
        int userMove;
        int x;
        int y;
};

的.cpp

#include "Game.h"
#include <iostream>
#include <string>
using namespace std;

Game::Game()
{
board[3][3]= {{'o','o','o'},{'o','o','o'},{'o','o','o'}}
}

void Game::printBoard()
{
    for(x=0; x < 3; x++)
    {
        for(y =0; y<3; y++)
        {
            cout << board[x][y] << " " ;
        }
        cout << endl;
    }
}

void Game::firstMove()
{   cout << endl;
    cout << "Your move, enter where you want to place an 'x' " << endl;
    cout << endl;
    cout << "Top right (1)" << endl;
    cout << "Top left (2)" << endl;
    cout << "Center (3)" << endl;
    cout << "Center top (4) " << endl;
    cout << "Center bottom (5)" << endl;
    cout << "Center right (6) " << endl;
    cout << "Center left (7)" << endl;
    cout << "Bottom right (8)" << endl;
    cout << "Bottom left (9)" << endl;
    cin >> userMove;
    updateBoard(userMove);
}

int Game::updateBoard(int m)
{
    switch(userMove)
    case 1:
        board[1][3] = 'x';
        printBoard();

        return 0;
    }

每当我尝试运行此操作时,我都会遇到错误,可能还有其他方法可以打印出一个&#34; board&#34;在屏幕上,但到目前为止这是我所知道的。

1 个答案:

答案 0 :(得分:0)

经过一些修复后,现在可以看到http://codepad.org/B5YwnSx1 (虽然我不太确定你想要达到的目的)

#include <iostream>
#include <string>
using namespace std;
class Game
{
    public:
        Game();
        void printBoard();
        void firstMove();
        int updateBoard(int m);
    private:
        char board[3][3];
        int userMove;
        int x;
        int y;


};



Game::Game()
{

char b[3][3]= { {'o','o','o'},{'o','o','o'},{'o','o','o'}};
//copy from b to board
for(x=0; x < 3; x++) for(y =0; y<3; y++)board[x][y]=b[x][y];
}
//added main
int main()
{
Game* g = new Game();
g->updateBoard(1);
}

void Game::printBoard()
{

    for(x=0; x < 3; x++)
    {
        for(y =0; y<3; y++)
        {
            cout << board[x][y] << " " ;
        }
        cout << endl;
    }


}

void Game::firstMove()
{   cout << endl;
    cout << "Your move, enter where you want to place an 'x' " << endl;
    cout << endl;
    cout << "Top right (1)" << endl;
    cout << "Top left (2)" << endl;
    cout << "Center (3)" << endl;
    cout << "Center top (4) " << endl;
    cout << "Center bottom (5)" << endl;
    cout << "Center right (6) " << endl;
    cout << "Center left (7)" << endl;
    cout << "Bottom right (8)" << endl;
    cout << "Bottom left (9)" << endl;
    cin >> userMove;
    updateBoard(userMove);
}

int Game::updateBoard(int m)
{
    switch(m)  //where is usermove set? why was m not used?(userMove)
case 1:
board[0][2] = 'x';//board[1][3] is out of bounds 0..2
printBoard();

return 0;
}