diff --git a/1.cpp b/1.cpp index 898198d..27ed8fa 100644 --- a/1.cpp +++ b/1.cpp @@ -1,11 +1,16 @@ // Разверните строку. Указатель reverse_string должен // указывать на развернутую строку. -char* string = "The string!"; +char* string = “The string!”; int main() { char* reverse_string; - + int sz = strlen(string); + reverse_string= new char[sz+1]; + for(int i=0; i m[i][j]) temp=m[i][j]; + return temp; } int _tmain(int argc, _TCHAR* argv[]) { Matrix matrix; + for(int i=0; i + + + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.3.0 MinGW 32bit + Desktop Qt 5.3.0 MinGW 32bit + qt.53.win32_mingw482_kit + 0 + 0 + 0 + + D:/Qt_Projects/build-Pacman-Desktop_Qt_5_3_0_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + D:/Qt_Projects/build-Pacman-Desktop_Qt_5_3_0_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 2 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy locally + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Pacman + + Qt4ProjectManager.Qt4RunConfiguration:D:/Qt_Projects/Pacman/Pacman.pro + + Pacman.pro + false + false + + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.EnvironmentId + {1ef6cdab-f21e-4980-a51d-da4ecd6e5454} + + + ProjectExplorer.Project.Updater.FileVersion + 15 + + diff --git a/Pacman/Source_files.qrc b/Pacman/Source_files.qrc new file mode 100644 index 0000000..2398d39 --- /dev/null +++ b/Pacman/Source_files.qrc @@ -0,0 +1,11 @@ + + + finish.png + gold.png + level1.txt + level2.txt + monster.png + pacman.png + wall.png + + diff --git a/Pacman/ThreadPool.h b/Pacman/ThreadPool.h new file mode 100644 index 0000000..d15fd87 --- /dev/null +++ b/Pacman/ThreadPool.h @@ -0,0 +1,98 @@ +#ifndef THREAD_POOL_H +#define THREAD_POOL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +class ThreadPool { +public: + ThreadPool(size_t); + void joinAll(); + template + auto enqueue(F&& f, Args&&... args) + -> std::future::type>; + ~ThreadPool(); +private: + // need to keep track of threads so we can join them + std::vector< std::thread > workers; + // the task queue + std::queue< std::function > tasks; + + // synchronization + std::mutex queue_mutex; + std::condition_variable condition; + bool stop; +}; + +// the constructor just launches some amount of workers +inline ThreadPool::ThreadPool(size_t threads) + : stop(false) +{ + for(size_t i = 0;i lock(this->queue_mutex); + while(!this->stop && this->tasks.empty()) + this->condition.wait(lock); + if(this->stop && this->tasks.empty()) + return; + std::function task(this->tasks.front()); + this->tasks.pop(); + lock.unlock(); + task(); + } + } + ); +} + + +// add new work item to the pool +template +auto ThreadPool::enqueue(F&& f, Args&&... args) + -> std::future::type> +{ + typedef typename std::result_of::type return_type; + + // don't allow enqueueing after stopping the pool + if(stop) + throw std::runtime_error("enqueue on stopped ThreadPool"); + + auto task = std::make_shared< std::packaged_task >( + std::bind(std::forward(f), std::forward(args)...) + ); + + std::future res = task->get_future(); + { + std::unique_lock lock(queue_mutex); + tasks.push([task](){ (*task)(); }); + } + condition.notify_one(); + return res; +} + +// the destructor joins all threads +inline ThreadPool::~ThreadPool() +{ + { + std::unique_lock lock(queue_mutex); + stop = true; + } + condition.notify_all(); + for(size_t i = 0;isetPixmap(m_creature_image); + m_label->setScaledContents(true); + m_label->resize(CELL_SIZE,CELL_SIZE); + m_label->show(); + +} + +Creature::~Creature(){} + +void Creature::move(const int dx,const int dy) +{ + if(m_pmap->getCell(m_x+dx,m_y-dy) != WALL ) + { + m_x += dx; + m_y -= dy; + } +} + +void Creature::move(const Point &point) +{ + move(point.x - m_x, m_y - point.y ); +} + +void Creature::setPosition(const size_t x,const size_t y) +{ + m_x = x; + m_y = y; +} + +void Creature::setPosition(const Point &point) +{ + m_x = point.x; + m_y = point.y; +} + +void Creature::drawCreature() +{ + m_label->move(m_x*CELL_SIZE, m_y*CELL_SIZE); +} + +Point Creature::getPosition() const +{ + return Point(m_x,m_y); +} + diff --git a/Pacman/creature.h b/Pacman/creature.h new file mode 100644 index 0000000..b4faca1 --- /dev/null +++ b/Pacman/creature.h @@ -0,0 +1,33 @@ +#ifndef CREATURE_H +#define CREATURE_H + +#include "gamemap.h" +#include +#include +#include + +typedef std::shared_ptr QLabelPtr; +// Creature class to handle move-events and containing +//data for displaying on main window +class Creature +{ +public: + const size_t CELL_SIZE = 25; + Creature(GameMapPtr pmap, QPixmap image, QWidget* parent ); + virtual ~Creature(); + void move(const int dx,const int dy); + void move(const Point& point); + void setPosition(const size_t x,const size_t y); + void setPosition(const Point& point); + virtual void drawCreature(); + Point getPosition() const; + +protected: + size_t m_x; + size_t m_y; + GameMapPtr m_pmap; + QPixmap m_creature_image; + QLabelPtr m_label; +}; + +#endif // CREATURE_H diff --git a/Pacman/finish.png b/Pacman/finish.png new file mode 100644 index 0000000..c0c2de8 Binary files /dev/null and b/Pacman/finish.png differ diff --git a/Pacman/gamemap.cpp b/Pacman/gamemap.cpp new file mode 100644 index 0000000..476c37c --- /dev/null +++ b/Pacman/gamemap.cpp @@ -0,0 +1,103 @@ +#include "gamemap.h" +#include + +#include +#include + +GameMap::GameMap() : m_level(0) +{ + m_wall_icon = QPixmap(":/wall.png"); + m_gold_icon = QPixmap(":/gold.png"); + m_finish_icon = QPixmap(":/finish.png"); + + levelFiles.push_back(":/level1.txt"); + levelFiles.push_back(":/level2.txt"); +} + +void GameMap::loadMap() +{ + if(map.size()) map.clear(); + std::vector vecStr; + QString levelString(levelFiles[m_level]); + QFile file(levelString); + file.open(QIODevice::ReadOnly | QIODevice::Text); + + QTextStream in(&file); + QString line = in.readLine();; + while (!line.isNull()) + { + vecStr.push_back(line.toStdString()); + line = in.readLine(); + } + + map.resize(vecStr.size()); + for(size_t i = 0; i < vecStr.size(); i++) + for(size_t j = 0; j < vecStr[i].size(); j++) + { + map[i].push_back(MAP_OBJECTS((int)(vecStr[i][j] - '0'))); + switch(map[i][j]) + { + case START: m_startPoint.x=j; + m_startPoint.y=i; + break; + case FINISH: m_finishPoint.x=j; + m_finishPoint.y=i; + break; + case MONSTER: m_mnstrPoints.push(Point(j,i)); + break; + default: break; + } + } +} + +MAP_OBJECTS GameMap::getCell(const size_t j,const size_t i) const +{ + return map[i][j]; +} + +void GameMap::setCell(const size_t j,const size_t i, MAP_OBJECTS val) +{ + map[i][j] = val; +} + +size_t GameMap::mapSizeY() const +{ + return map.size(); +} + + +size_t GameMap::mapSizeX() const +{ + return map[0].size(); +} + +size_t GameMap::monstersAmount() const +{ + return m_mnstrPoints.size(); +} + +Point GameMap::startPoint() const +{ + return m_startPoint; +} + + +Point GameMap::finishPoint() const +{ + return m_finishPoint; +} + +Point GameMap::startPointMonster() +{ + Point temp = m_mnstrPoints.top(); + m_mnstrPoints.pop(); + return temp; +} + +GameMap::~GameMap(){} + +void GameMap::levelUp() +{ + if(m_level < (levelFiles.size()-1)) + m_level++; +} diff --git a/Pacman/gamemap.h b/Pacman/gamemap.h new file mode 100644 index 0000000..13c9145 --- /dev/null +++ b/Pacman/gamemap.h @@ -0,0 +1,65 @@ +#ifndef GAMEMAP_H +#define GAMEMAP_H + +#include +#include +#include +#include + +// types of map containment +enum MAP_OBJECTS {SPACE=0,WALL,GOLD, START,FINISH,MONSTER}; + +struct Point +{ + size_t x; + size_t y; + Point() :x(0),y(0){} + Point(size_t x,size_t y) : x(x),y(y){} + Point(const Point& obj) + { + x=obj.x; + y=obj.y; + } + Point& operator=(const Point& obj) + { + x=obj.x; + y=obj.y; + return *this; + } + bool operator ==(const Point& obj) + { + return (obj.x==this->x && obj.y==this->y); + } +}; + +// Game map containing its elements for filling cells +// and displaying on main window +class GameMap +{ +public: + GameMap(); + MAP_OBJECTS getCell(const size_t i,const size_t j) const; //returns map cell containment + void loadMap(); + size_t mapSizeY() const; + size_t mapSizeX() const; + size_t monstersAmount() const; //call once when initializing monsters + void setCell(const size_t i,const size_t j, MAP_OBJECTS); //used by pacman creature to change cell + Point startPoint() const;// returns start and finish + Point finishPoint() const;// coordinates + Point startPointMonster();// returns st.point from stack to monster obj + ~GameMap(); + void levelUp(); +private: + std::vector > map; + Point m_startPoint; + Point m_finishPoint; + std::stack m_mnstrPoints; + QPixmap m_wall_icon; + QPixmap m_gold_icon; + QPixmap m_finish_icon; + size_t m_level; + std::vector levelFiles; +}; + +typedef std::shared_ptr GameMapPtr; +#endif // GAMEMAP_H diff --git a/Pacman/gold.png b/Pacman/gold.png new file mode 100644 index 0000000..a1e50ba Binary files /dev/null and b/Pacman/gold.png differ diff --git a/Pacman/level1.txt b/Pacman/level1.txt new file mode 100644 index 0000000..6f9c331 --- /dev/null +++ b/Pacman/level1.txt @@ -0,0 +1,16 @@ +1111111111111111 +1000000014021051 +1001101012100001 +1030111012112101 +1010120001100101 +1000110100110121 +1001000100120021 +1001010100120121 +1000010200020121 +1000000000000121 +1011111110010121 +1222222100010021 +1222221000100021 +1051111101000021 +1000000000000001 +1111111111111111 diff --git a/Pacman/level2.txt b/Pacman/level2.txt new file mode 100644 index 0000000..f4d0816 --- /dev/null +++ b/Pacman/level2.txt @@ -0,0 +1,16 @@ +1111111111111111 +1000000300000021 +1011101122211101 +1012200000000001 +1010111111111101 +1000000000001221 +1011011111001221 +1010012221001001 +1010025252000001 +1010012221001001 +1010011111001001 +1010000000001001 +1010111111101221 +1000122222101221 +1000000000050241 +1111111111111111 \ No newline at end of file diff --git a/Pacman/log.txt b/Pacman/log.txt new file mode 100644 index 0000000..b0ff38d --- /dev/null +++ b/Pacman/log.txt @@ -0,0 +1 @@ +future_error :No associated state \ No newline at end of file diff --git a/Pacman/log2.txt b/Pacman/log2.txt new file mode 100644 index 0000000..c63e94a --- /dev/null +++ b/Pacman/log2.txt @@ -0,0 +1,3 @@ +2 11 +(2,11) +End funccc \ No newline at end of file diff --git a/Pacman/main.cpp b/Pacman/main.cpp new file mode 100644 index 0000000..baa9e84 --- /dev/null +++ b/Pacman/main.cpp @@ -0,0 +1,12 @@ +#include "pacman.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Pacman w; + + w.show(); + + return a.exec(); +} diff --git a/Pacman/monster.png b/Pacman/monster.png new file mode 100644 index 0000000..adfd424 Binary files /dev/null and b/Pacman/monster.png differ diff --git a/Pacman/monster_creature.cpp b/Pacman/monster_creature.cpp new file mode 100644 index 0000000..a0c4795 --- /dev/null +++ b/Pacman/monster_creature.cpp @@ -0,0 +1,73 @@ +#include "monster_creature.h" +#include + +enum GRAPH_OBJ {BAD =-1 , BLANK =-2}; + +Monster_creature::Monster_creature(GameMapPtr pmap, QPixmap image, QWidget* parent) + : Creature(pmap, image , parent ) , flag(false) +{ + m_movePoint = Point(m_pmap->startPointMonster()); + setPosition(m_movePoint); +} + + +Point Monster_creature::persuitPacman(const Point &point) +{ + + size_t SIZE = m_pmap->mapSizeY(); + std::vector > graph(m_pmap->mapSizeY()); + std::vector path; + Point start(m_x,m_y) ,end(point); + Point current(point); + + if(start==end) return end; + //graph init + for(size_t i=0; i < m_pmap->mapSizeY(); ++i) + for(size_t j=0; j < m_pmap->mapSizeX(); ++j) + { + if(m_pmap->getCell(j,i) == WALL) + graph[i].push_back(BAD); + else graph[i].push_back(BLANK); + } + + //wave expansion + int dx[4] = {1, 0, -1, 0};//shifts + int dy[4] = {0, 1, 0, -1};//up,down,left,right + int d = 0; + bool stop=false; + + graph[start.y][start.x] = 0; + while(!stop && graph[point.y][point.x] == BLANK) + { + stop=true; + for(size_t y=0; y < SIZE; ++y) + for(size_t x=0; x < SIZE; ++x) + if(graph[y][x] == d) + for(size_t k = 0; k < 4; ++k) //marking all unmarked neighbours + if(graph[y + dy[k]][x + dx[k]] == BLANK) + { + graph[y+dy[k]][x+dx[k]] = d+1; //wave expansion + stop = false; + } + ++d; + } + if (graph[point.y][point.x] == BLANK) return Point(); + + //writing backpath + while(d>0) + { + path.push_back(current); + d--; + + + for(size_t k=0; k < 4; ++k) + if(graph[current.y + dy[k]][current.x + dx[k]] == d) + { + current.x += dx[k]; + current.y += dy[k]; + break; + } + } + //path.push_back(start); //not using in this impl + return path.back(); +} diff --git a/Pacman/monster_creature.h b/Pacman/monster_creature.h new file mode 100644 index 0000000..47952b3 --- /dev/null +++ b/Pacman/monster_creature.h @@ -0,0 +1,15 @@ +#ifndef MONSTER_CREATURE_H +#define MONSTER_CREATURE_H +#include "creature.h" + +class Monster_creature: public Creature +{ +public: + Monster_creature(GameMapPtr pmap, QPixmap image, QWidget* parent); + Point persuitPacman(const Point &point);//finds path to point, and returns its first point + virtual ~Monster_creature(){} + Point m_movePoint; + bool flag; +}; + +#endif // MONSTER_CREATURE_H diff --git a/Pacman/pacman.cpp b/Pacman/pacman.cpp new file mode 100644 index 0000000..9e38764 --- /dev/null +++ b/Pacman/pacman.cpp @@ -0,0 +1,278 @@ +#include "pacman.h" +#include "ui_pacman.h" +#include "pacman_creature.h" +#include "gamemap.h" +#include "monster_creature.h" +#include +#include + +Pacman::Pacman(QWidget *parent) + :QMainWindow(parent) + ,ui(new Ui::Pacman) + ,m_pmap(new GameMap) + ,m_timer(new QTimer(this)) + ,m_monsterTimer(new QTimer(this)) + ,m_count(20) + ,m_keyIsPressed(false) + ,m_isUpdated(true) + ,m_startPause(true) + ,m_lostGameFlag(false) + ,m_key(Qt::Key_0) + ,m_score(0) + ,m_speed(5) + ,m_pool(4) +{ + + ui->setupUi(this); + + m_pmap->loadMap(); + m_pacman = PacmanPtr (new Pacman_creature( + m_pmap + ,QPixmap(":/pacman.png") + ,this)); + + size_t tempSize = m_pmap->monstersAmount(); + for(size_t i=0; idrawCreature(m_pacman->rotateFaceDirection(Qt::Key_0)); + + for(size_t i=0; idrawCreature(); + + //Setting timer, so speed of pacman and mosters are equal + connect(m_timer, SIGNAL(timeout()), this, SLOT(updateGame())); + connect(m_monsterTimer, SIGNAL(timeout()), this, SLOT(monsterBehaviourTimer()) ); +} + +void Pacman::drawMap() +{ + size_t size_x = m_pmap->mapSizeX(); + size_t size_y = m_pmap->mapSizeY(); + + for(size_t i=0; i < size_x; ++i) + for(size_t j=0; j< size_y; ++j) + switch(m_pmap->getCell(j,i)) + { + case WALL: + m_mapLabels[i][j]->setPixmap(QPixmap(":/wall.png")); + break; + case FINISH: + m_mapLabels[i][j]->setPixmap(QPixmap(":/finish.png")); + break; + case GOLD: + m_mapLabels[i][j]->setPixmap(QPixmap(":/gold.png")); + break; + case SPACE: + m_mapLabels[i][j]->clear(); + default: break; + } +} + +void Pacman::setLabels() +{ + + size_t size_x = m_pmap->mapSizeX(); + size_t size_y = m_pmap->mapSizeY(); + if(m_mapLabels.size()) + m_mapLabels.clear(); + m_mapLabels.resize(size_x); + for(size_t i=0; i < size_y; ++i) + for(size_t j=0; j< size_x; ++j) + { + m_mapLabels[i].push_back( QLabelPtr(new QLabel(this))); + m_mapLabels[i][j]->resize(CELL_SIZE,CELL_SIZE); + m_mapLabels[i][j]->move(j*CELL_SIZE,i*CELL_SIZE); + m_mapLabels[i][j]->show(); + } +} + +void Pacman::keyPressEvent(QKeyEvent * event) +{ + m_keyIsPressed = true; + m_isUpdated = false; + m_key = (Qt::Key)event->key(); +} + +void Pacman::keyReleaseEvent(QKeyEvent * event) +{ + m_keyIsPressed = false; +} + +void Pacman::interaction() +{ + int x = m_pacman->getPosition().x; + int y = m_pacman->getPosition().y; + //monster interaction + for(size_t i=0; i < m_monsters.size(); ++i) + if( m_pacman->getPosition() == m_monsters[i]->getPosition()) + { + m_timer->stop(); + m_lostGameFlag = true; + ui->startButton->setDisabled(true); + QMessageBox::information(this, "GAME INFO", "YOU LOST!!!"); + break; + } + //pacman-map interaction + switch(m_pmap->getCell(x,y)) + { + case GOLD: + m_pmap->setCell(x,y,SPACE); + m_score+=50; + ui->ScoreBox->setText(QString::number(m_score,10)); + break; + case FINISH: + if(m_score > 1000) + { + m_timer->stop(); + QMessageBox::information(this, "GAME INFO", "YOU WON!!!"); + m_pmap->levelUp(); + } + break; + default: break; + } +} + +void Pacman::setInterface() +{ + size_t size_x = m_pmap->mapSizeX(); + size_t size_y = m_pmap->mapSizeY(); + setMaximumSize(size_x*CELL_SIZE, size_y*CELL_SIZE+50); + ui->label_2->move(10,CELL_SIZE*size_x+10); + ui->difficulty->move(60,CELL_SIZE*size_x+10); + ui->startButton->move(100,CELL_SIZE*size_x+10); + ui->restartButton->move(160,CELL_SIZE*size_x+10); + ui->label->move(270,CELL_SIZE*size_x+10); + ui->ScoreBox->move(340,CELL_SIZE*size_x+10); +} + +void Pacman::updateGame() +{ + if(m_keyIsPressed || !m_isUpdated) + switch (m_key) + { + case Qt::Key_Right: + m_pacman->move(1,0); + break; + case Qt::Key_Left: + m_pacman->move(-1,0); + break; + case Qt::Key_Up: + m_pacman->move(0,1); + break; + case Qt::Key_Down: + m_pacman->move(0,-1); + break; + default: break; + } + m_isUpdated = true; + m_pacman->drawCreature(m_pacman->rotateFaceDirection(m_key)); + + //monster behaviour logic + Point temp , pacmanPoint; + + std::vector > nextPoints; //results of next points + pacmanPoint = m_pacman->getPosition(); + + if(m_count) + { + for(size_t i=0; i < m_monsters.size(); ++i) + nextPoints.push_back( m_pool.enqueue( &Monster_creature::persuitPacman, m_monsters[i], pacmanPoint )); + m_count--; + } + else + { + for(size_t i=0; i < m_monsters.size(); ++i) + { + if(m_monsters[i]->getPosition() == m_monsters[i]->m_movePoint || m_monsters[i]->flag) + { + nextPoints.push_back( m_pool.enqueue( &Monster_creature::persuitPacman, m_monsters[i], pacmanPoint )); + m_monsters[i]->flag = true; + } + else + nextPoints.push_back(m_pool.enqueue( &Monster_creature::persuitPacman, m_monsters[i], m_monsters[i]->m_movePoint )); + } + } + + + for(size_t i=0; i < m_monsters.size(); ++i) + m_monsters[i]->move(nextPoints[i].get() ); + + for(size_t i=0; i < m_monsters.size(); ++i) + m_monsters[i]->drawCreature(); + + + interaction(); + drawMap(); +} + +void Pacman::monsterBehaviourTimer() +{ + m_count = size_t(m_pmap->mapSizeX()*1.8); + for(size_t i = 0; i < m_monsters.size(); ++i) + m_monsters[i]->flag = false; +} + +Pacman::~Pacman() +{ + delete ui; +} + + + + +void Pacman::on_startButton_clicked() +{ + if(m_startPause) + { + m_timer->start(500-m_speed*50); + m_monsterTimer->start(12000); + ui->startButton->setText("PAUSE"); + m_startPause = false; + } + else + { + m_timer->stop(); + m_monsterTimer->stop(); + ui->startButton->setText("START"); + m_startPause = true; + } + +} + +void Pacman::on_difficulty_valueChanged(int arg1) +{ + m_speed = arg1; +} + +void Pacman::on_restartButton_clicked() +{ + m_timer->stop(); + m_monsterTimer->stop(); + m_count = size_t(m_pmap->mapSizeX()*1.4); + m_lostGameFlag = false; + ui->startButton->setDisabled(false); + m_pmap->loadMap(); + + m_monsters.clear(); + size_t tempSize = m_pmap->monstersAmount(); + for(size_t i=0; isetPosition(m_pmap->startPoint()); + + m_score = 0; + ui->ScoreBox->setText(QString::number(m_score,10)); + m_timer->start(500-m_speed*50); + m_monsterTimer->start(12000); +} diff --git a/Pacman/pacman.h b/Pacman/pacman.h new file mode 100644 index 0000000..784b77a --- /dev/null +++ b/Pacman/pacman.h @@ -0,0 +1,69 @@ +#ifndef PACMAN_H +#define PACMAN_H + +#include +#include +#include +#include +#include +#include +#include +#include "ThreadPool.h" + +class GameMap; +class Pacman_creature; +class Monster_creature; + +typedef std::shared_ptr MonsterPtr; +typedef std::shared_ptr PacmanPtr; +typedef std::shared_ptr QLabelPtr; + +namespace Ui { +class Pacman; +} + +class Pacman : public QMainWindow +{ + Q_OBJECT +public: + + explicit Pacman(QWidget *parent = 0); + ~Pacman(); +private: + void drawMap(); + void setLabels(); + void keyPressEvent(QKeyEvent *); + void keyReleaseEvent(QKeyEvent *); + void interaction(); + void setInterface(); +private slots: + void updateGame(); + void monsterBehaviourTimer(); + void on_startButton_clicked(); + void on_difficulty_valueChanged(int arg1); + void on_restartButton_clicked(); + +private: + const size_t CELL_SIZE = 25; + const size_t WIDGET_SIZE_X = 600; + const size_t WIDGET_SIZE_Y = 600; + Ui::Pacman *ui; + std::shared_ptr m_pmap; + std::shared_ptr m_pacman; + std::vector m_monsters; + std::vector > m_mapLabels; + QTimer *m_timer; + QTimer *m_monsterTimer; + size_t m_count; + bool m_keyIsPressed; + bool m_isUpdated; + bool m_startPause; + bool m_lostGameFlag; + Qt::Key m_key; + size_t m_score; + size_t m_speed; + ThreadPool m_pool; + +}; + +#endif // PACMAN_H diff --git a/Pacman/pacman.png b/Pacman/pacman.png new file mode 100644 index 0000000..fee9e5b Binary files /dev/null and b/Pacman/pacman.png differ diff --git a/Pacman/pacman.ui b/Pacman/pacman.ui new file mode 100644 index 0000000..572a6c1 --- /dev/null +++ b/Pacman/pacman.ui @@ -0,0 +1,179 @@ + + + Pacman + + + + 0 + 0 + 419 + 457 + + + + + 625 + 625 + + + + Qt::NoFocus + + + Pacman + + + + Qt::NoFocus + + + + + 270 + 390 + 61 + 21 + + + + + Showcard Gothic + 12 + 75 + true + false + + + + SCORE: + + + + + + 340 + 390 + 51 + 21 + + + + + Showcard Gothic + 12 + 75 + true + + + + + + + + + + 100 + 390 + 51 + 23 + + + + + Showcard Gothic + 10 + 75 + true + + + + Qt::NoFocus + + + START + + + + + + 60 + 390 + 31 + 31 + + + + + Showcard Gothic + 12 + 75 + true + + + + Qt::NoFocus + + + + + + 1 + + + 6 + + + 3 + + + + + + 10 + 390 + 46 + 31 + + + + + Showcard Gothic + 10 + 75 + true + + + + SPEED + + + + + + 160 + 390 + 71 + 23 + + + + + Showcard Gothic + 10 + 75 + true + + + + Qt::NoFocus + + + RESTART + + + + + + + + diff --git a/Pacman/pacman_creature.cpp b/Pacman/pacman_creature.cpp new file mode 100644 index 0000000..2baf196 --- /dev/null +++ b/Pacman/pacman_creature.cpp @@ -0,0 +1,38 @@ +#include "pacman_creature.h" + + +Pacman_creature::Pacman_creature(GameMapPtr pmap, QPixmap image, QWidget* parent) + : Creature(pmap, image , parent ) +{ + m_x = m_pmap->startPoint().x; + m_y = m_pmap->startPoint().y; +} + +QPixmap Pacman_creature::rotateFaceDirection(Qt::Key key) +{ + QTransform transform; + QPixmap tempPixmap(m_creature_image); + switch (key) + { + case Qt::Key_Left: + transform.rotate(180); + tempPixmap = tempPixmap.transformed(transform); + break; + case Qt::Key_Up: + transform.rotate(-90); + tempPixmap = tempPixmap.transformed(transform); + break; + case Qt::Key_Down: + transform.rotate(90); + tempPixmap = tempPixmap.transformed(transform); + break; + default: break; + } + return tempPixmap; +} + +void Pacman_creature::drawCreature(QPixmap image) +{ + m_label->setPixmap(image); + m_label->move(m_x*CELL_SIZE, m_y*CELL_SIZE); +} diff --git a/Pacman/pacman_creature.h b/Pacman/pacman_creature.h new file mode 100644 index 0000000..89701ed --- /dev/null +++ b/Pacman/pacman_creature.h @@ -0,0 +1,17 @@ +#ifndef PACMAN_CREATURE_H +#define PACMAN_CREATURE_H +#include "creature.h" + + + + +class Pacman_creature: public Creature +{ +public: + Pacman_creature(GameMapPtr pmap, QPixmap image, QWidget* parent ); + QPixmap rotateFaceDirection(Qt::Key); + virtual void drawCreature(QPixmap);//draws rotated face pixmap + virtual ~Pacman_creature(){} +}; + +#endif // PACMAN_CREATURE_H diff --git a/Pacman/wall.png b/Pacman/wall.png new file mode 100644 index 0000000..6beff25 Binary files /dev/null and b/Pacman/wall.png differ diff --git a/Polyline/Polyline.cpp b/Polyline/Polyline.cpp new file mode 100644 index 0000000..591bd77 --- /dev/null +++ b/Polyline/Polyline.cpp @@ -0,0 +1,25 @@ +// Polyline.cpp : Defines the entry point for the console application. +// + +#include "stdafx.h" +#include +#include +#include +#include "Polyline.h" + + + +int _tmain(int argc, _TCHAR* argv[]) +{ + using namespace std; + + Points points; + Handler handle; + handle.InputPoints(points); + handle.cycle(points); + handle.ShowCrossPoints(); + handle.fillNode(); + system("pause"); + return 0; +} + diff --git a/Polyline/Polyline.h b/Polyline/Polyline.h new file mode 100644 index 0000000..2f59665 --- /dev/null +++ b/Polyline/Polyline.h @@ -0,0 +1,231 @@ +#include +#include +#include +#include +#include + +using namespace std; +#define EPS 1.0E-6 +typedef vector>> Graph; +typedef std::vector> Points; + +struct Node +{ + double x; + double y; + vector pNodes; + + Node() : x(0), y(0) {} + bool operator==(pair pr) + { + return abs(pr.first-x) pr) + { + x=pr.first; + y=pr.second; + return *this; + } + + + friend ostream& operator<<(ostream& os, Node obj) + { + os << "("<< obj.x <<"," << obj.y << ") "; + return os; + } + +}; + +//class that handles vector of entered points +class Handler +{ + int size; // number of points + Points crossPts; // vector of cross points for all segments + Graph graph; // Nodes with connections to other nodes + vector node; +public: + Handler() {} + void InputPoints(Points & obj); + void ShowInput(const Points & obj) const; + void FindCrossPoint(const Points& obj, int s1, int s2, int s3, int s4); + void ShowCrossPoints() const; + void cycle(const Points& obj); + void showGraph() const; + void findNeibours(const Points& obj, int s1 , int s2); + void fillNode(); + void findFigure(); + friend ostream& operator<<(ostream& os, const pair & obj); + friend ostream& operator<<(ostream& os, const vector> & obj); +}; + +bool myfunction (pair i, pair j) + { return abs(i.first-j.first) i, pair j) +{ + if(abs(i.first-j.first)< EPS) + return i.second < j.second; + else return i.first < j.first; +} + +//user input-point-function +void Handler::InputPoints(Points & obj) +{ + + cout << "Input number of points > 3: "; + cin >> size; + obj.resize(size); + cout << "Enter " << size << " points in format: x y\n"; + for(int i=0; i> obj[i].first >> obj[i].second; + } +} + +//shows what did used input +void Handler::ShowInput(const Points & obj) const +{ + int i=1; + for(Points::const_iterator it = obj.begin(); it != obj.end();++it,++i) + cout << "\n#" << i << *it; +} + +ostream& operator<<(ostream& os,const pair & obj) +{ + os << "(" << obj.first << "," << obj.second << ") "; + return os; +} + +ostream& operator<<(ostream& os, const vector> & obj) +{ + int i=1; + for(Points::const_iterator it = obj.begin(); it != obj.end();++it,++i) + os << "(" << (*it).first << "," << (*it).second << ") ;"; + return os; +} + + +// looking for cross point of 2 segments and pushing into vector of cross points +void Handler::FindCrossPoint(const Points& obj, int s1 , int s2, int s3, int s4) +{ + double a1,a2,b1,b2,c1,c2; //coefs + double x,y; //cross point coordinates + + a1 = obj[s1].second - obj[s2].second; // ax+by+c=0 + a2 = obj[s3].second - obj[s4].second;// + b1 = obj[s2].first - obj[s1].first;// + b2 = obj[s4].first - obj[s3].first;// + c1 = obj[s1].first * obj[s2].second - obj[s2].first * obj[s1].second; + c2 = obj[s3].first * obj[s4].second - obj[s4].first * obj[s3].second; + + if( abs(a1-a2) < EPS && abs(b1-b2) < EPS ) + return; // paralel no points, even if segments have conjunction that point wont matter + + x = ( b1*c2 - b2*c1 ) / (a1*b2-a2*b1); + y = (a2*c1 - a1*c2) / (a1*b2 - a2*b1); + + //checking if point belongs the segments + if( x <= max(obj[s1].first, obj[s2].first) && x >= min(obj[s1].first, obj[s2].first) + && x <= max(obj[s3].first, obj[s4].first) && x >= min(obj[s3].first, obj[s4].first) + && y <= max(obj[s1].second, obj[s2].second) && y >= min(obj[s1].second, obj[s2].second) + && y <= max(obj[s3].second, obj[s4].second) && y >= min(obj[s3].second, obj[s4].second)) + crossPts.push_back(make_pair(x,y)); + +} + +void Handler::ShowCrossPoints() const +{ + int i=1; + cout << "\nCross points: "; + for(Points::const_iterator it = crossPts.begin(); it != crossPts.end(); ++it, ++i) + cout << "\n#"< indexCrossPts; // index for relation between CrossPts and tempVec: crossPts[indexCrossPts[i]] == tempVec[i]; + for(int i=0; i= min(obj[s1].first, obj[s2].first) + && crossPts[i].second <= max(obj[s1].second, obj[s2].second) + && crossPts[i].second >= min(obj[s1].second, obj[s2].second) ) + { + tempVec.push_back(make_pair(crossPts[i].first,crossPts[i].second)); + indexCrossPts.push_back(i);//index in crossPts is value of indexCrossPts + } + + for(int i=0; i