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