需要帮助在c ++中编辑2d向量

时间:2015-11-20 22:03:20

标签: c++ c++11 vector

我目前正在用c ++制作一个简单的tic tac toe游戏。我将游戏板存储为2d向量,std::vector<std::vector<char>> _gameBoard;并且有另一个向量,用于创建_gameBoard std::vector<char> _gamePieces;。我用这个函数来设置矢量值

void Board::createBoard(int boardSize){
    for(int i = 0; i < boardSize; i++){
        for(int j = 0; j < boardSize; j++){
            _gamePieces.push_back(' ');
        }
        _gameBoard.push_back(_gamePieces);
    }
}

我现在正尝试通过执行此操作来编辑带有玩家角色的矢量

void Board::editGameBoard(int players){
    Players player;
    char playerSymbol;
    for(int i = 0; i < players; i++){
        std::cout << "Player " << (i+1) << ": Where do you want to go(e.g 1 2)? ";
        int x, y;
        std::cin >> x >> y;
        playerSymbol = player.getPlayerSymbol(i);
        _gameBoard[x-1][y-1] = playerSymbol;
        printBoard();
    }

}

但我一直收到错误,我能想到的另一个修复是使用push_back交换值然后使用pop_back但是有没有办法通过像数组一样访问它来实现它?

这是错误error,它说我无法嵌入图片,因此它创建了一个链接。

下面我将发布代码。

的main.cpp

#include <iostream>
#include "Board.h"
#include "Input.h"
#include "Players.h"

int main(){

    bool isRunning = true;
    char endGame;

    std::cout << "Welcome to Tic Tac Toe!\n" << std::endl ;

    while(isRunning){
        Players player;
        Board gameBoard;
        player.setPlayers();
        player.setPlayerSymbol();
        gameBoard.setBoardSize(player.getPlayers());
        gameBoard.createBoard(gameBoard.getBoardSize());
        gameBoard.printBoard();
        gameBoard.editGameBoard(player.getPlayers());

        std::cout << "Do you want to end the game(Y or N): ";
        std::cin >> endGame;

        if(endGame == 'y' || endGame == 'Y')
            isRunning = false;

    }

    system("pause");
    return 0;
}

players.h

#pragma once
#include <vector>

class Players
{
    public:
        Players();
        void setPlayers();
        int getPlayers();
        void setPlayerSymbol();
        char getPlayerSymbol(int players);

    private: 
        int _players;
        std::vector<char> _playerSymbol;


};

players.cpp

#include "Players.h"
#include <iostream>



Players::Players(){
}

void Players::setPlayers(){
    std::cout << "How many players are there(2-4): ";
    std::cin >> _players;

    while(_players < 1 && _players > 5){
        std::cout << "Invalid entry, try again: ";
        std::cin >> _players;
    }
}

int Players::getPlayers(){
    return _players;
}

void Players::setPlayerSymbol(){
    for(int i = 0; i < _players; i++){
        char symbol;
        std::cout << "What symbol do you want for player " << (i+1) << ": ";
        std::cin >> symbol;
        _playerSymbol.push_back(symbol);
    }
}

char Players::getPlayerSymbol(int player){
    return _playerSymbol[player-1];
}

board.h

#pragma once
#include <vector>

class Board
{
    public:
        Board();
        void createBoard(int boardSize);
        void setBoardSize(int players);
        int getBoardSize();
        void printBoard();
        void editGameBoard(int players);


    private:
        std::vector<std::vector<char>> _gameBoard;
        std::vector<char> _gamePieces;
        int _boardSize;


};

board.cpp

#include "Board.h"
#include "Players.h"
#include <iostream>


Board::Board(){

}

void Board::createBoard(int boardSize){
    for(int i = 0; i < boardSize; i++){
        for(int j = 0; j < boardSize; j++){
                _gamePieces.push_back(' ');
        }
        _gameBoard.push_back(_gamePieces);
    }
}

void Board::setBoardSize(int players){
    std::cout << "How big do you want the game board to be";
    if(players == 2){
        std::cout << "(3x3 - 15x15(3 = 3x3, 4 = 4x4 etc)): ";
        std::cin >> _boardSize;

        while(_boardSize < 3 && _boardSize > 15){
            std::cout << "Invalid entry, try again: ";
            std::cin >> _boardSize;
        }
    }else if(players == 3){
        std::cout << "(4x4 - 15x15(4 = 4x4, 5 = 5x5 etc)): ";
        std::cin >> _boardSize;

        while(_boardSize < 4 && _boardSize > 15){
            std::cout << "Invalid entry, try again: ";
            std::cin >> _boardSize;
        }
    }else if(players == 4){
        std::cout << "(5x5 - 15x15(5 = 5x5, 6 = 6x6 etc)): ";
        std::cin >> _boardSize;

        while(_boardSize < 5 && _boardSize > 15){
            std::cout << "Invalid entry, try again: ";
            std::cin >> _boardSize;
        }
    }
    std::cout << std::endl;
}

int Board::getBoardSize(){
    return _boardSize;
}

void Board::printBoard(){
    std::cout << " ";
    for(int j = 0; j < _boardSize*2; j++){
        if((j%2) == 0)
            std::cout << " ";
        else
            std::cout << (j+1)/2;
    }
    std::cout << std::endl;
    for(int i = 0; i < _boardSize; i++){
        std::cout << " ";
        for(int j = 0; (j-1) < _boardSize*2; j++){
            std::cout << "-";
        }
        std::cout << std::endl;
        std::cout << i+1;
        std::cout << "|";
        for(int j = 0; j < _boardSize; j++){
            std::cout << _gameBoard[i][j];
            std::cout << "|";
        }
        std::cout << std::endl;
    }
    std::cout << " ";
    for(int i = 0; (i-1) < _boardSize*2; i++){
            std::cout << "-";
    }
    std::cout << std::endl << std::endl;
}

void Board::editGameBoard(int players){
    Players player;
    char playerSymbol;
    for(int i = 0; i < players; i++){
        std::cout << "Player " << (i+1) << ": Where do you want to go(e.g 1 2)? ";
        int x, y;
        std::cin >> x >> y;
        playerSymbol = player.getPlayerSymbol(i);
        _gameBoard[x-1][y-1] = playerSymbol;
        printBoard();
    }

}

4 个答案:

答案 0 :(得分:0)

首先,你具体是什么错误?你是否逐行完成了代码?另外,什么是Player类,你在代码中使用它,但没有显示它是什么。它是用数据初始化的吗?你验证了x和y变量实际上是否正在获得值?尝试使用int x = 0,y = 0;养成初始化变量的习惯从来都不是坏事。

现在至于其他答案,为什么我们在C ++ 11中使用'new'时他所做的事情是完全安全和理智的?我只会改变使用std :: vector&lt; char&gt;而不是std :: vector&lt;的std ::矢量&lt; char&gt;取代。您可以使用简单的数学方法轻松找到2D位置。你只需要知道电路板的宽度。 Position_in_vector =(y * width)+ x;

编辑:您发布的链接设置了我的公司防火墙,因此我看不到它,请更新您的原始帖子。

答案 1 :(得分:-1)

我认为您可以创建一个临时矢量,使用push_back将playerSymbol添加到该矢量,然后引用您的_gameBoard[x-1][y-1]并将新创建的矢量分配到该位置。

答案 2 :(得分:-2)

您使用的是错误的数据结构。如果要创建具有预定义大小的矩阵,请使用2D数组。

int** ary = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
    ary[i] = new int[colCount];

看看这篇文章: How do I declare a 2d array in C++ using new?

它将更具性能,并将避免动态分配。

答案 3 :(得分:-2)

向量主要用于扩展和缩小数据集合。您的游戏板可能会保持不变,因此固定内存就足够了。这意味着二维阵列可能更适合于任务。 如果需要动态内存,请使用new关键字探索将指针用作数组。另外,请发布错误,以便我们帮助解释发生的事情。