user13053792
user13053792

Reputation:

Domineering C++ game

I am making a domineering game for my coursework, I have been trying to do something new by adding a lot of void functions and now for some weird reason my board isn't working as it is saying identifier "board" is undefined but I have never really had a problem with my board until now and I haven't figured it out yet. The error is in the void playAt() function.

#include <iostream>
#include <Windows.h>
#include <vector>
using namespace std;
int horizontal = 0;
int vertical = 0;
int row = 0;
int column = 0;
int x = row = 0;
int y = column = 0;
bool gameOver;
bool player = horizontal && vertical;
bool islegal;

void Setup()
{
    gameOver = false;
}
void Draw()
{
    // specifty default value to fill the vector elements
    int mapSize = 0;

    cout << "Enter the size of board => ";
    cin >> mapSize;

    vector<vector<char> > board(mapSize, vector<char>(mapSize, 'e'));
    for (int i = 0; i < mapSize; i++) {
        for (int j = 0; j < mapSize; j++) {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
}
void play()
{
    if (player == horizontal) {
        cout << ("Horizontal to play");
    }
    else {
        cout << ("Vertical to play");
    }
}
void playAt(int row, int column, bool player)
{
    board[row][column] = true;
    if (player == horizontal) {
        board[x][y + 1] = true;
        board[x][y + 2] = true;
    }
    else {
        board[x + 1][y] = true;
        board[x + 2][y] = true;
    }
}
void Input()
{
}
void Logic()
{
}
int main()
{
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
    }
}

Upvotes: 1

Views: 267

Answers (2)

Ron
Ron

Reputation: 15531

When we declare a name, such as your board, we do so in a certain scope. Outside that scope, the name is inaccessible. Your vector<vector<char>> board name is local to function Draw() and its scope, since it is defined and declared only there. Outside that function's scope it is not visible to other portions of your code.

One quick (but debatable) solution to your problem is to declare the board name in a global scope, where it is visible to everyone, for example before the void Setup() function definition:

std::vector<std::vector<char>> board;

A better solution is to declare and initialize it inside the main() function and pass it as a parameter to other functions in which case you would need to change the function signatures.

That being said, the use of using namespace std; is best avoided.

Upvotes: 1

Object object
Object object

Reputation: 2054

Your issue is that board is indeed undefined within playAt(). The board variable is local to Draw(), outside the scope of Draw() it doesn't exist!

You need to either pass it (from a scope in which it exists like Draw()) to the functions you want to be able to use it or elevate its scope (i.e to the global level like with horizontal and everything else declared at the top). Generally prefer passing it or even better put all those functions inside an object with a class member board (it is C ++ after all).

Upvotes: 0

Related Questions