Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <Windows.h>
- #include <conio.h>
- #include <chrono>
- #include <thread>
- #include <random>
- using namespace std;
- string player = "O";
- int playerX = 1;
- int playerY = 0;
- void characterMove(string** labyrinth, int& x, int& y, int dx, int dy)
- {
- if (labyrinth[x + dx][y + dy] == " " || labyrinth[x + dx][y + dy] == "E")
- {
- x += dx;
- y += dy;
- }
- }
- void playerMove(string** labyrinth)
- {
- char key = _getch();
- if (key == 72) // The ASCII CODE FOR THE UP ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, -1, 0);
- }
- else if (key == 75) // The ASCII CODE FOR THE LEFT ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, 0, -1);
- }
- else if (key == 80) // The ASCII CODE FOR THE DOWN ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, 1, 0);
- }
- else if (key == 77) // The ASCII CODE FOR THE RIGHT ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, 0, 1);
- }
- }
- // Function to display the labyrinth
- void display(string** labyrinth, int rowsCount, int columnsCount)
- {
- bool breakAfterRender = false;
- while (true)
- {
- system("cls");
- int colour = rand() % 15 + 1;
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < columnsCount; j++)
- {
- if (i == playerX && j == playerY)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colour);
- cout << player;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- }
- else
- {
- cout << labyrinth[i][j];
- }
- }
- cout << "\n";
- }
- if (breakAfterRender)
- {
- cout << "\n";
- cout << "Congratulations! You have reached the end of the level!" << "\n";
- cin.get();
- break;
- }
- labyrinth[playerX][playerY] = " ";
- playerMove(labyrinth);
- if (labyrinth[playerX][playerY] == "E")
- {
- breakAfterRender = true;
- }
- labyrinth[playerX][playerY] = player;
- this_thread::sleep_for(chrono::milliseconds(10));
- }
- }
- void openFile(string fileName)
- {
- int rowsCount = 1;
- int columnsCount = 0;
- int startAt = 0;
- int endAt = 0;
- ifstream myfile;
- myfile.open(fileName);
- if (myfile.is_open())
- {
- char mychar;
- while (myfile)
- {
- mychar = myfile.get();
- if (mychar == '\n')
- {
- rowsCount++;
- }
- else if ( (mychar != '\n') && (rowsCount == 1) )
- {
- columnsCount++;
- }
- else if (mychar == 'S')
- {
- startAt = rowsCount - 1;
- }
- else if (mychar == 'E')
- {
- endAt = rowsCount - 1;
- }
- }
- }
- else
- {
- cout << "Error: Unable to open file " << fileName << "." << "\n";
- }
- myfile.close();
- // Dynamically allocating row space in the heap
- string** labyrinth = new string * [rowsCount];
- // Dynamically allocating column space in the heap
- for (int i = 0; i < rowsCount; i++)
- {
- labyrinth[i] = new string[columnsCount];
- }
- int currentRow = 0;
- int currentColumn = 0;
- myfile.open(fileName);
- if (myfile.is_open())
- {
- char mychar;
- while (myfile.get(mychar))
- {
- if ( (currentRow < rowsCount) && (currentColumn < columnsCount) )
- {
- labyrinth[currentRow][currentColumn] = mychar;
- }
- if (mychar == '\n')
- {
- currentRow++;
- currentColumn = 0;
- }
- else
- {
- currentColumn++;
- }
- }
- }
- myfile.close();
- // Displaying the labyrinth
- display(labyrinth, rowsCount, columnsCount);
- // Free up the space after the use of the array
- for (int i = 0; i < rowsCount; i++)
- {
- delete[] labyrinth[i];
- }
- delete[] labyrinth;
- }
- void levels()
- {
- int level = 1;
- while (level < 11)
- {
- if (level == 1)
- {
- string fileName = "maps/map_0_s.txt";
- // Opening the text file
- openFile(fileName);
- level++;
- }
- }
- }
- int main()
- {
- ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
- SendMessage(GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x2000000);
- levels();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement