简体   繁体   中英

How to make board one dimensional c++

I have previously created a board for a domineering game but now I am trying to make the board one dimensional for a game called toads and frogs and I don't know where to start, if it is possible that I don't have to include an array to make the one dimensional then that would be good.

#include<Windows.h>
#include<iostream>
#include<vector>

using namespace std;


int main()
{  
// specify default value to fill the vector elements
int mapSize = 8;
int x = 0;
int y = 0;
int horizontal = 0;
int vertical = 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;
}

}

Right now you have a two dimensional board created by use of a nested vector. You use vector<vector<char>> board(mapSize, vector<char>(mapSize, 'e')); which creates a list containing lists, using the vector constructor vector<E>(size_type n, const value_type& val) . This means the vector contains n elements of type E that are copies of val .

For example, in the case of vector<int>(5, 10) , the list created will look like this [10, 10, 10, 10, 10] In your code, it will become more like [['e', 'e', 'e'], ['e', 'e', 'e'], ['e', 'e', 'e']] for mapSize = 3 for example.

To make one dimensional, you simply need to make it an ordinary list rather than a list of lists. For example vector<char> board(mapSize, 'e') to make a simple ['e', 'e', 'e'] .

This also means when printing the code, access will be made in one dimension - Eg. board[i] will get the value at that space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM