自動走迷宮小遊戲~

Gaowaly發表於2024-11-04

標頭檔案

MyMazer.h

#pragma once
#include<iostream>
#include"MyMazeMap.h"

using namespace std;

class MyMazer
{
public:
    MyMazer();
    MyMazeMap map;
    void setPersonPosition(int x, int y);
    void setPersonSpeed(int speed);
    void setPersonChar(char personchar);
    void start();
private:
    int LastPersonX;
    int LastPersonY;
    int PersonX;
    int PersonY;
    int SleepTime;
    char PersonChar;
    int direction;
private:
    void gotoxy(int x, int y);
    void direction_up();
    void direction_down();
    void direction_left();
    void direction_right();
    int turnDirection();

};

MyMazeMap.h

#pragma once
#include<iostream>

using namespace std;

class MyMazeMap
{
public:
    MyMazeMap();
    MyMazeMap(const MyMazeMap& C);
    void setMazeMap(int* mazeArr, int x, int y);
    void setMazeWall(const char wallSign);
    void setMazeWay(const char waySign);
    void showFactor();
    void drawMap();
    bool checkWall(int x, int y);
    int getmazeX();
    int getmazeY();
    char m_wallSign;
    char m_waySign;
private:
    int m_mazeArr[10][10];
    int m_mazeX;
    int m_mazeY;
};

主檔案:

MyMateMap.cpp

#include<iostream>
#include"MyMazeMap.h"

using namespace std;

MyMazeMap::MyMazeMap()
{
    m_wallSign = '+';
    m_waySign = ' ';
}

MyMazeMap::MyMazeMap(const MyMazeMap& b)
{
    m_wallSign = b.m_wallSign;
    m_waySign = b.m_waySign;
    m_mazeX = b.m_mazeX;
    m_mazeY = b.m_mazeY;
    for (int i = 0; i < m_mazeX; i++)
    {
        for (int j = 0; j < m_mazeY; j++) {
            m_mazeArr[i][j] = b.m_mazeArr[i][j];
        }
    }


}

void
MyMazeMap::setMazeMap(int* mazeArr, int x, int y)
{
    m_mazeArr[0][0] = *mazeArr;

    m_wallSign = '+';
    m_waySign = ' ';

    int k = 0;

    if (x > 10) {
        x = 10;

    }
    if (y > 10) {
        y = 10;
        k = y - 10;
    }
    m_mazeX = x;
    m_mazeY = y;
    for (int i = 0; i < m_mazeX; i++)
    {
        for (int j = 0; j < m_mazeY; j++) {
            m_mazeArr[i][j] = *mazeArr;
            *mazeArr++;
        }
        *mazeArr = *mazeArr + k;
    }
}

void
MyMazeMap::setMazeWall(const char wallSign)
{
    m_wallSign = wallSign;
}
void
MyMazeMap::setMazeWay(const char waySign)
{
    m_waySign = waySign;
}

void
MyMazeMap::showFactor()
{
    cout << m_mazeArr[0][0] << endl;
    cout << m_mazeX << endl;
    cout << m_mazeY << endl;
    cout << m_wallSign << " " << m_waySign << endl;
}
void
MyMazeMap::drawMap()
{
    for (int i = 0; i < m_mazeX; i++)
    {
        for (int j = 0; j < m_mazeY; j++) {
            if (m_mazeArr[i][j] == 1)
                cout << m_wallSign;
            else
                cout << m_waySign;
        }
        cout << endl;
    }
}
bool
MyMazeMap::checkWall(int x, int y)
{
    if (x == -1 || y == -1 || x == getmazeX() || y == getmazeY())
    {
        return false;
    }
    else if (m_mazeArr[x][y] == 1)
    {
        return true;
    }
    else return false;
}

int
MyMazeMap::getmazeX()
{
    return m_mazeX;
}
int
MyMazeMap::getmazeY()
{
    return m_mazeY;
}

MyMazer.cpp

#include<iostream>
#include <windows.h>
#include<stdio.h>
#include"MyMazer.h"
#include"MyMazeMap.h"

using namespace std;

const int UP = 0;
const int LEFT = 1;
const int DOWN = 2;
const int RIGHT = 3;

void
MyMazer::gotoxy(int x, int y)
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = y;
    pos.Y = x;
    SetConsoleCursorPosition(hOut, pos);
}

MyMazer::MyMazer()
{
    direction = UP;
}

void
MyMazer::setPersonPosition(int x, int y)
{
    PersonX = x;
    PersonY = y;
    gotoxy(PersonX, PersonY);
}
void
MyMazer::setPersonSpeed(int speed)
{
    SleepTime = speed * 500;
}

void
MyMazer::setPersonChar(char personchar)
{
    PersonChar = personchar;
    cout << PersonChar;
}
void
MyMazer::start()
{
    while (true)
    {
        switch (MyMazer::turnDirection())
        {
        case UP:direction = UP; direction_up(); break;
        case RIGHT:direction = RIGHT; direction_right(); break;
        case LEFT:direction = LEFT; direction_left(); break;
        case DOWN:direction = DOWN; direction_down(); break;
        }
    }
}
void
MyMazer::direction_up()
{

    LastPersonX = PersonX;
    LastPersonY = PersonY;
    PersonX = PersonX - 1;
    gotoxy(LastPersonX, LastPersonY);
    cout << map.m_waySign;
    gotoxy(PersonX, PersonY);
    cout << PersonChar;
    Sleep(SleepTime);

}
void
MyMazer::direction_down()
{

    LastPersonX = PersonX;
    LastPersonY = PersonY;
    PersonX = PersonX + 1;
    gotoxy(LastPersonX, LastPersonY);
    cout << map.m_waySign;
    gotoxy(PersonX, PersonY);
    cout << PersonChar;
    Sleep(SleepTime);

}
void
MyMazer::direction_left()
{
    LastPersonX = PersonX;
    LastPersonY = PersonY;
    PersonY = PersonY - 1;
    gotoxy(LastPersonX, LastPersonY);
    cout << map.m_waySign;
    gotoxy(PersonX, PersonY);
    cout << PersonChar;
    Sleep(SleepTime);
}
void
MyMazer::direction_right()
{
    LastPersonX = PersonX;
    LastPersonY = PersonY;
    PersonY = PersonY + 1;
    gotoxy(LastPersonX, LastPersonY);
    cout << map.m_waySign;
    gotoxy(PersonX, PersonY);
    cout << PersonChar;
    Sleep(SleepTime);
}

int
MyMazer::turnDirection()
{
    if (direction == UP)
    {
        if (PersonX == 0)
        {
            cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
            cout << "successful" << endl;
            system("pause");
            exit(0);
        }

        if (map.checkWall(PersonX, PersonY - 1))
        {
            if (!map.checkWall(PersonX - 1, PersonY))

                return UP;
            else if (!map.checkWall(PersonX, PersonY + 1))
                return RIGHT;
            else
                return DOWN;
        }
        else
            return LEFT;
    }
    if (direction == RIGHT)
    {
        if (PersonY == map.getmazeY())
        {
            cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
            cout << "successful" << endl;
            system("pause");
            exit(0);
        }

        if (map.checkWall(PersonX - 1, PersonY))
        {
            if (!map.checkWall(PersonX, PersonY + 1))

                return RIGHT;
            else if (!map.checkWall(PersonX + 1, PersonY))
                return DOWN;
            else
                return LEFT;
        }
        else
            return UP;
    }

    if (direction == DOWN)
    {
        if (PersonX == map.getmazeX())
        {
            cout << endl << endl << endl;
            cout << "successful" << endl;
            system("pause");
            exit(0);
        }

        if (map.checkWall(PersonX, PersonY + 1))
        {
            if (!map.checkWall(PersonX + 1, PersonY))

                return DOWN;
            else if (!map.checkWall(PersonX, PersonY - 1))
                return LEFT;
            else
                return UP;
        }
        else
            return RIGHT;
    }
    if (direction == LEFT)
    {
        if (PersonY == 0)
        {
            cout << endl << endl << endl;
            cout << "successful" << endl;
            system("pause");
            exit(0);
        }

        if (map.checkWall(PersonX + 1, PersonY))
        {
            if (!map.checkWall(PersonX, PersonY - 1))
                return LEFT;
            else if (!map.checkWall(PersonX - 1, PersonY))
                return UP;
            else
                return RIGHT;
        }
        else
            return DOWN;
    }

}

Main.cpp

#include <iostream>
#include <stdlib.h>
#include "MyMazeMap.h"
#include "MyMazer.h"

using namespace std;

const int WALL = 1;
const int ROAD = 0;
const int SLOW = 3;
const int MEDIUM = 2;
const int FAST = 1;

const int SUCCESS = 0;
int main()
{
    int map[8][9] = {
                    {WALL,WALL,WALL,WALL,WALL,WALL,WALL,ROAD,WALL},
                    {WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
                    {WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
                    {WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
                    {WALL,WALL,ROAD,ROAD,ROAD,ROAD,WALL,ROAD,WALL},
                    {WALL,WALL,ROAD,WALL,WALL,ROAD,ROAD,ROAD,WALL},
                    {WALL,ROAD,ROAD,WALL,WALL,WALL,WALL,WALL,WALL},
                    {WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,WALL}
    };
    MyMazeMap maze;

    maze.setMazeMap(&map[0][0], 8, 9);
    maze.setMazeWall('*');
    //maze.showFactor();
    maze.drawMap();
    MyMazer mazer;
    mazer.map = maze;
    mazer.setPersonPosition(7, 1);
    mazer.setPersonSpeed(FAST);
    mazer.setPersonChar('T');
    mazer.start();

    cout << endl;
    system("pause");
    return SUCCESS;
}

執行程式碼

可自行修改地圖

相關文章