0

i am currently making a simple tic tac toe game in c++. I have stored the gameboard as a 2d vector which is std::vector<std::vector<char>> _gameBoard; and have another vector which is used to create the _gameBoard which is std::vector<char> _gamePieces;. I used this function to set the vector values

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);
    }
}

I am now trying to edit the vector with the players character by doing this

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();
    }

}

but i keep getting an error, the other fix i can think of is to use push_back swap the values then use pop_back but would there be a way to do it by accessing it like an array?

Here is the error, error, it said i couldn't embed the image so it created a link.

Below i will post the code.

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();
    }

}
Mykola
  • 3,343
  • 6
  • 23
  • 39

4 Answers4

0

First off, what error are you getting specifically? Have you stepped through the code line by line? Also, what is the Player class, you use it in the code, but didn't show what it was. Is it initialized with data? Have you verified the x and y variables are in fact getting values? Try using int x = 0, y = 0; Getting in the habit of initializing variables is never a bad thing.

Now as for the other answers, why are we using 'new' in C++11 when what he was doing was perfectly safe and sane? Only change I would make is using std::vector< char > instead of std::vector< std::vector< char > >. You can easily find the 2D location using simple math. You just need to know the width of the board. Position_in_vector = (y * width) + x;

Edit: The link you posted set off my company firewall, so I cannot see it, please update your original post.

MikeL
  • 59
  • 2
-1

I think that you could create a temporary vector, use push_back to add playerSymbol to that vector, then refer to your _gameBoard[x-1][y-1]and assign your newly created vector to that position.

-2

You are using the wrong data structure. If you want to create a matrix with pre-defined size use a 2D array.

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

Look at this post: How do I declare a 2d array in C++ using new?

It is going to be more performatic and will avoid dynamic allocation.

Community
  • 1
  • 1
p.magalhaes
  • 7,595
  • 10
  • 53
  • 108
-2

Vectors are primarily for expanding and shrinking data collections. Your game board will likely remain the same so fixed memory will suffice. This means a 2 dimensional array may be better suited to the task. If you require dynamic memory, the explore the use of pointers as arrays using the new keyword. In addition, please post the error so we can help to explain what is happening.