Ok I am ready to ask my next question which has to do with loading data from a text file.. first, I'll post the code for the entire 'Maze' game I created while messing around with arrays.
#include <iostream>const int tileWide = 23;const int tileHigh = 10;bool gameOver = false;void printBoard(int[tileHigh][tileWide]);void locateOne(int& , int& , int[tileHigh][tileWide]);void move(const char direction, int[tileHigh][tileWide]);int iaBoard[tileHigh][tileWide] = { {1,2,2,2,2,2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,2,0,2,0,0,0,2,0,2,0,2,2,2,2,2,0,2,2,2,2,2,0}, {0,0,0,0,0,2,0,2,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0}, {2,0,2,2,2,2,0,2,0,2,0,2,0,2,0,2,2,0,2,2,0,2,0}, {0,0,0,0,2,2,0,2,0,0,0,0,0,2,0,0,0,0,0,2,0,0,0}, {0,2,2,0,2,2,0,0,0,0,0,0,0,2,2,2,2,2,2,0,0,2,2}, {0,0,2,0,2,0,0,2,0,2,0,2,0,0,0,0,0,2,0,2,0,0,0}, {2,0,0,2,2,2,0,2,0,2,0,0,2,2,2,0,0,0,0,2,0,2,0}, {2,0,2,0,0,0,0,2,0,2,2,2,2,0,2,0,0,2,0,2,0,2,0}, {2,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,2,0,2,2,0,0} };int main(){ do{ system("cls"); std::cout << "find your way to the bottom right corner.... "; printBoard(iaBoard); std::cout << "enter a direction (w = up, s = down, a = left, d = right):"; char cNextMove; if(iaBoard[tileHigh - 1][tileWide - 1] == 1) { gameOver = true; } else { std::cin >> cNextMove; } move(cNextMove,iaBoard); } while(!gameOver); std::cout << std::endl << " You made did it! you made it to the corner ! \n "; system("pause"); std::cout << "sorry you can't restart this game :) \n" << std::endl; return 0;}void printBoard(int iaBoard[tileHigh][tileWide]){ std::cout << " \n\n "; for(int i=0;i<tileHigh;++i){ std::cout << "\t"; for(int j=0;j<tileWide;++j){ std::cout << iaBoard[j]; } std::cout << std::endl; }}void locateOne(int& irRow, int& irCol, int iaBoard[tileHigh][tileWide]){ for(int iRow = 0;iRow < tileHigh ;++iRow) { for(int iCol = 0;iCol < tileWide ;++iCol) { if(iaBoard[iRow][iCol] == 1) { irRow = iRow; irCol = iCol; } } }}void move(const char direction, int iaBoard[tileHigh][tileWide]){ int iRowOne = 0; int iColOne = 0; locateOne(iRowOne, iColOne, iaBoard); int iOldRow = iRowOne; int iOldCol = iColOne; switch(direction) { case 'w': { --iRowOne; break; } case 's': { ++iRowOne; break; } case 'a': { --iColOne; break; } case 'd': { ++iColOne; break; } } if (iRowOne >= 0 && iRowOne < tileHigh && iColOne >= 0 && iColOne < tileWide) { if(iaBoard[iRowOne][iColOne] != 2) { iaBoard[iRowOne][iColOne] = 1; iaBoard[iOldRow][iOldCol] = 0; } }}
the concept is simple.. there is a multi-dimensional array of numbers, 0's and 2's and a 1, you control the one starting in the top left and have to make it to the bottom right spot. you cannot move through 2's, only 0's.
My question is about the array itself, for learning purposes I would like to be able to load the array (the level) in from a text file. The way I visualize this happening is in the text file there is something like this:
then somehow sort the numbers delimited by the spaces / new lines into a multi dimensional array.