【问题标题】:How to use the data type char in multidimensional arrays如何在多维数组中使用数据类型char
【发布时间】:2014-09-18 22:40:01
【问题描述】:

我正在开发一个程序,该程序允许用户与另一个人类玩家玩井字游戏。到目前为止,我已经创建了一个“板”和一系列选项,允许用户选择他们想要放置“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;
    }

每当我尝试运行此程序时,我都会遇到错误,可能还有其他方法可以在屏幕上打印出“板”,但到目前为止,这是我所知道的。

【问题讨论】:

标签: c++ arrays multidimensional-array


【解决方案1】:

经过一些修复后,它现在可以工作了,请参阅 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多