Move the @ from lbf file across the file (up, down, left, right and toggle)

Hi!

I am new to coding in C++, and I'm getting frustrated at this program. The objective is to upload a lbf file from within the file and move the '@' character around the 'level'. 0 to move forward, 1 to rotate right, 2 to move backwards, 3 to rotate left and 4 to 'toggle' (change the ? character into a * character). I am aware that I would need a new case etc, just any help!

From the main source code, I have created more cpp and header files, that store the voids, chars etc. But due to the limited length, I can't post it.

Any help would be appreciated. Thanks!

note: I wouldn't mind if any help is connected to the project or new code, just an example of what I need.

Source.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <list>

//Include the Header files, which will connect amongst all the files.
#include "Level1.h"
#include "CommandQueue.h"
#include "executeCommand.h"

using namespace std;

#define TRUE 1
#define FALSE 0

char currentLevel[64][64];

int main()
{
int height, width, posX, posY, menuRepeat;
int userSelection;

//The list of classes, which are created in the header files, which store various chars and void.
Level1 level;
CommandQueue Command;

//Allows the menu to loop, unless option 3 is entered.
menuRepeat = TRUE;

do
{
cout << "\nWhat do you want to do? \n1. Load Level \n2. Print Level (Only use after option 1!)\n3. Quit \n4. Display Commands\n5. Display Current Queue (Only use after Option 4!)\nYour option: ";
cin >> userSelection;

//The menu, with the only option not to loop the menu is case 3.
switch (userSelection)
{
case 1: menuRepeat = level.Loadlevel(height, width, posX, posY);
break;
case 2: level.Printlevel(height, width, posX, posY);
break;
case 3: menuRepeat = FALSE();
break;
case 4: Command.ArrayList();
break;
case 5: Command.Displayed();
break;
}
} while (menuRepeat == TRUE);
//The function which loops the menu.
return 0;

}

int Level1::Loadlevel(int& height, int& width, int& posX, int& posY)

{
int row, col, result;
char currentChar;
//currentChar is @, row is Y (down) and col is X (across).
string input;

cout << "\nPlease enter file name, level1.lbf or level2.lbf: ";
cin >> input;
ifstream file;
//level1.lbf and level2.lbf are both stored in the Project file.
file.open(input, ifstream::in);

if (file.is_open())
{
file >> height;
file >> width;
//File's row and col is stored in an Array
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
file >> currentChar;

if (currentChar == '@')

{
posX = row;
posY = col;
currentChar = '.';
}
currentLevel[row][col] = currentChar;
}
}
result = TRUE;

file.close();
}

else
//If the user inputs the incorrect file name.
{
cout << "Unable to open file";
result = FALSE;
}

return result;
}

void Level1::Printlevel(int& height, int& width, int& posX, int& posY)

{
//row is up until 3 for level1 and up until 2 for level2
for (int row = 0; row < height; row++)
{
//col is up until 3 for level1 and up until 2 for level2
for (int col = 0; col < width; col++)
{
if (posX == row && posY == col)
{
cout << ("@");
}
else
{
cout << currentLevel[row][col];
}
}
cout << endl;
}
}

int Level1::Updateposition()
{
return 0;
}

void CommandQueue::ArrayList()
{
//The menu option which the user inputs 10 commands.
cout << "\n";
cout << "List of Actions (10 moves required!):" << endl;
cout << "_____________________________________" << endl;
std::cout << "Move Forward: " << anArray[0] << endl;
std::cout << "Move Left: " << anArray[1] << endl;
std::cout << "Move Right: " << anArray[2] << endl;
std::cout << "Toggle Light: " << anArray[3] << endl;
cout << "\n";
//Various commands that the user needs to Input for the UserInput to work.
do
{
cout << "Enter 1st Command: ";
cin >> UserInput;
//An if statement if the User inputs the incorrect command. The menu will break if the user inputs a wrong command.
if(UserInput != anArray[0] && UserInput != anArray[1] && UserInput != anArray[2] && UserInput != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 2nd Command: ";
cin >> UserInput1;

if(UserInput1 != anArray[0] && UserInput1 != anArray[1] && UserInput1 != anArray[2] && UserInput1 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 3rd Command: ";
cin >> UserInput2;

if(UserInput2 != anArray[0] && UserInput2 != anArray[1] && UserInput2 != anArray[2] && UserInput2 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 4th Command: ";
cin >> UserInput3;

if(UserInput3 != anArray[0] && UserInput3 != anArray[1] && UserInput3 != anArray[2] && UserInput3 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 5th Command: ";
cin >> UserInput4;

if(UserInput4 != anArray[0] && UserInput4 != anArray[1] && UserInput4 != anArray[2] && UserInput4 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 6th Command: ";
cin >> UserInput5;

if(UserInput5 != anArray[0] && UserInput5 != anArray[1] && UserInput5 != anArray[2] && UserInput5 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 7th Command: ";
cin >> UserInput6;

if(UserInput6 != anArray[0] && UserInput6 != anArray[1] && UserInput6 != anArray[2] && UserInput6 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 8th Command: ";
cin >> UserInput7;

if(UserInput7 != anArray[0] && UserInput7 != anArray[1] && UserInput7 != anArray[2] && UserInput7 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 9th Command: ";
cin >> UserInput8;

if(UserInput8 != anArray[0] && UserInput8 != anArray[1] && UserInput8 != anArray[2] && UserInput8 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}

cout << "Enter 10th Command: ";
cin >> UserInput9;

if(UserInput9 != anArray[0] && UserInput9 != anArray[1] && UserInput9 != anArray[2] && UserInput9 != anArray[3])
{
std::cout << "Incorrect Input. Returning to Menu.\n";
break;
}
}while(UserInput9 != UserInput9);

cout << "\n";
cout << "You can now display your commands.";
cout << "\n";
return;
}

void CommandQueue::Displayed()
{
//The menu option which allows the user to display their previous queue.
cout << "\n";
cout << "1." << UserInput << " ";
cout << "2." << UserInput1 << " ";
cout << "3." << UserInput2 << " ";
cout << "4." << UserInput3 << " ";
cout << "5." << UserInput4 << " ";
cout << "6." << UserInput5 << " ";
cout << "7." << UserInput6 << " ";
cout << "8." << UserInput7 << " ";
cout << "9." << UserInput8 << " ";
cout << "10." << UserInput9 << " ";
cout << "\n";
}

note: Can't move past/into X

level1.lbf (when displayed on exe file)

. . @
. X X
. . ?

level2.lbf(when displayed on exe file)

. .
@ ?
Last edited on
Topic archived. No new replies allowed.