Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<SFML\Graphics.hpp>
- #include<iostream>
- #include<iterator>
- #include<map>
- #include<set>
- #include<queue>
- #include<string>
- #include<fstream>
- #include<cmath>
- #include<iterator>
- #include<stack>
- using namespace sf;
- using namespace std;
- char prevchoice='p';
- struct Astar
- {
- char pixel,AI=' ';
- int h, g=0, f;
- pair< int, int> parent;
- int id;
- };
- pair<int,pair<int, int>> Min_F(map<int, pair<int, pair<int, int>>> openst)
- {
- pair<int, int>min = make_pair(-1, 1e9);
- map<int, pair<int, pair<int, int>>>::iterator it;
- for (auto i = openst.begin(); i != openst.end(); i++)
- {
- if (i->second.first < min.second)
- {
- it = i;
- min.first = i->first;
- min.second = i->second.first;
- }
- }
- return make_pair(it->first,make_pair(it->second.second.first,it->second.second.second));
- }
- stack<pair<int, int>>path,path2;
- void PathFind(Astar ary[36][28], int ghost_start_row, int ghost_start_col, int target_row, int target_col,char y)
- {
- pair<int, int>pathfinder;
- int w;
- pathfinder.first = target_row, pathfinder.second = target_col;
- w = pathfinder.first;
- pathfinder.first = ary[pathfinder.first][pathfinder.second].parent.first;
- pathfinder.second = ary[w][pathfinder.second].parent.second;
- if (y == 'R')
- {
- ary[w][pathfinder.second].AI = 'R';
- path.push(make_pair(pathfinder.first, pathfinder.second));
- }
- else
- {
- ary[w][pathfinder.second].AI = 'B';
- path2.push(make_pair(pathfinder.first, pathfinder.second));
- }
- if (pathfinder.first != ghost_start_row || pathfinder.second != ghost_start_col)
- PathFind(ary, ghost_start_row, ghost_start_col, pathfinder.first, pathfinder.second, y);
- }
- set <int > close_set;
- map<int, pair<int, pair<int, int>>>open_set;
- pair<int*, int*>ghost_real_pos,ghost2_real_pos;
- void AI(Astar ary[36][28], int ghost_position_row, int ghost_position_col, int target_first_row, int target_first_col,char x)
- {
- char y='R';
- if (x == 'R')
- y = 'B';
- // Up
- if (ary[ghost_position_row - 1][ghost_position_col].pixel != '0' &&ary[ghost_position_row - 1][ghost_position_col].AI != y && close_set.count(ary[ghost_position_row - 1][ghost_position_col].id) == 0)
- {
- if (open_set.count(ary[ghost_position_row - 1][ghost_position_col].id) == 0)
- {
- ary[ghost_position_row - 1][ghost_position_col].h = (abs(ghost_position_row - 1 - target_first_row) + abs(ghost_position_col - target_first_col));
- ary[ghost_position_row - 1][ghost_position_col].g = ary[ghost_position_row][ghost_position_col].g + 1;
- ary[ghost_position_row - 1][ghost_position_col].f = ary[ghost_position_row - 1][ghost_position_col].h + ary[ghost_position_row - 1][ghost_position_col].g;
- //setting pairent
- ary[ghost_position_row - 1][ghost_position_col].parent.first = ghost_position_row;
- ary[ghost_position_row - 1][ghost_position_col].parent.second = ghost_position_col;
- open_set[ary[ghost_position_row - 1][ghost_position_col].id] = make_pair(ary[ghost_position_row - 1][ghost_position_col].f, make_pair(ghost_position_row - 1, ghost_position_col));
- }
- }
- //down
- if (ary[ghost_position_row + 1][ghost_position_col].pixel != '0' && ary[ghost_position_row + 1][ghost_position_col].AI != y &&close_set.count(ary[ghost_position_row + 1][ghost_position_col].id) == 0)
- {
- if (open_set.count(ary[ghost_position_row + 1][ghost_position_col].id) == 0)
- {
- ary[ghost_position_row + 1][ghost_position_col].h = (abs(ghost_position_row + 1 - target_first_row) + abs(ghost_position_col - target_first_col));
- ary[ghost_position_row + 1][ghost_position_col].g = ary[ghost_position_row][ghost_position_col].g + 1;
- ary[ghost_position_row + 1][ghost_position_col].f = ary[ghost_position_row + 1][ghost_position_col].h + ary[ghost_position_row + 1][ghost_position_col].g;
- //setting parent
- ary[ghost_position_row + 1][ghost_position_col].parent.first = ghost_position_row;
- ary[ghost_position_row + 1][ghost_position_col].parent.second = ghost_position_col;
- open_set[ary[ghost_position_row + 1][ghost_position_col].id]= make_pair(ary[ghost_position_row + 1][ghost_position_col].f,make_pair(ghost_position_row + 1, ghost_position_col));
- }
- }
- // right
- if (ary[ghost_position_row ][ghost_position_col + 1].pixel != '0' &&ary[ghost_position_row][ghost_position_col + 1].AI !=y && close_set.count(ary[ghost_position_row ][ghost_position_col+1].id) == 0)
- {
- if (open_set.count(ary[ghost_position_row ][ghost_position_col+1].id) == 0)
- {
- ary[ghost_position_row][ghost_position_col + 1].h = (abs(ghost_position_row - target_first_row) + abs(ghost_position_col + 1 - target_first_col));
- ary[ghost_position_row][ghost_position_col + 1].g = ary[ghost_position_row][ghost_position_col].g + 1;
- ary[ghost_position_row][ghost_position_col + 1].f = ary[ghost_position_row][ghost_position_col+1].h + ary[ghost_position_row][ghost_position_col+1].g;
- //setting parent
- ary[ghost_position_row ][ghost_position_col+1].parent.first = ghost_position_row;
- ary[ghost_position_row ][ghost_position_col+1].parent.second =ghost_position_col;
- open_set[ary[ghost_position_row ][ghost_position_col + 1].id]= make_pair(ary[ghost_position_row][ghost_position_col + 1].f,make_pair(ghost_position_row, ghost_position_col + 1));
- }
- }
- //left
- if (ary[ghost_position_row][ghost_position_col - 1].pixel != '0' &&ary[ghost_position_row][ghost_position_col - 1].AI != y && close_set.count(ary[ghost_position_row ][ghost_position_col-1].id) == 0)
- {
- if (open_set.count(ary[ghost_position_row][ghost_position_col-1].id) == 0)
- {
- ary[ghost_position_row][ghost_position_col - 1].h = (abs(ghost_position_row - target_first_row) + abs(ghost_position_col - 1 - target_first_col));
- ary[ghost_position_row][ghost_position_col - 1].g = ary[ghost_position_row][ghost_position_col].g + 1;
- ary[ghost_position_row][ghost_position_col - 1].f = ary[ghost_position_row][ghost_position_col - 1].h + ary[ghost_position_row][ghost_position_col - 1].g;
- //setting parent
- ary[ghost_position_row][ghost_position_col - 1].parent.first = ghost_position_row;
- ary[ghost_position_row][ghost_position_col - 1].parent.second = ghost_position_col;
- open_set[ary[ghost_position_row ][ghost_position_col-1].id] =make_pair(ary[ghost_position_row][ghost_position_col - 1].f,make_pair(ghost_position_row, ghost_position_col - 1));
- }
- }
- // Erasing from open_set
- close_set.insert(ary[ghost_position_row][ghost_position_col].id);
- open_set.erase(ary[ghost_position_row][ghost_position_col].id);
- //next position
- pair<int, int> Next_Ghost_pos;
- Next_Ghost_pos.first = Min_F(open_set).second.first;
- Next_Ghost_pos.second = Min_F(open_set).second.second;
- //check if target reached
- if (Next_Ghost_pos.first != target_first_row || Next_Ghost_pos.second != target_first_col)
- AI(ary, Next_Ghost_pos.first, Next_Ghost_pos.second, target_first_row, target_first_col,x);
- else
- {
- pair<int, int>pathfinder;
- pathfinder.first = target_first_row, pathfinder.second = target_first_col;
- if (x == 'R')
- {
- path.push(make_pair(pathfinder.first, pathfinder.second));
- ary[pathfinder.first][pathfinder.second].AI = 'R';
- }
- else
- {
- path2.push(make_pair(pathfinder.first, pathfinder.second));
- ary[pathfinder.first][pathfinder.second].AI = 'B';
- }
- if(x=='R')
- PathFind(ary, *ghost_real_pos.first, *ghost_real_pos.second, target_first_row, target_first_col,x);
- else
- PathFind(ary, *ghost2_real_pos.first, *ghost2_real_pos.second, target_first_row, target_first_col, x);
- }
- }
- //moving the ghost
- void moving (RectangleShape &ghost, pair<int, int>last, pair<int,int>now)
- {
- if (last.first == now.first&&last.second < now.second)
- ghost.move(25.0 / 10.0, 0);
- if (last.first == now.first&&last.second > now.second)
- ghost.move(-25.0 / 10.0, 0);
- if (last.first < now.first&&last.second == now.second)
- ghost.move(0, 25.0 / 10.0);
- if (last.first > now.first&&last.second == now.second)
- ghost.move(0, -25.0 / 10.0);
- }
- //next astar search loc
- pair<int,int> next (pair<int, int>last, pair<int, int>now,pair<int,int>red)
- {
- if (last.first == now.first&&last.second < now.second)
- return make_pair(red.first, red.second + 1);
- if (last.first == now.first&&last.second > now.second)
- return make_pair(red.first, red.second - 1);
- if (last.first < now.first&&last.second == now.second)
- return make_pair(red.first+ 1, red.second );
- if (last.first > now.first&&last.second == now.second)
- return make_pair(red.first-1, red.second );
- }
- //drawing function of the map
- bool CheckFront = 1, CheckBack = 1;
- bool pickupschecker[36][28] = { { 0 } };
- void drawing(RenderWindow &w, Astar ary[36][28], RectangleShape box, CircleShape pac, RectangleShape pickup, bool pickupschecker[36][28],RectangleShape ghost1)
- {
- w.draw(box);
- w.draw(pac);
- w.draw(ghost1);
- for (int i = 0; i < 28; i++)
- {
- for (int j = 0; j < 36; j++)
- {
- if (ary[j][i].pixel != '0')
- {
- pickup.setPosition(i * 25, j * 25);
- pickup.setOrigin(-12.5, -12.5);
- if (pac.getGlobalBounds().intersects(pickup.getGlobalBounds()))
- pickupschecker[j][i] = 1;
- if (!pickupschecker[j][i])
- w.draw(pickup);
- }
- }
- }
- w.display();
- w.clear();
- }
- char pacman_movement(Astar map[36][28], char x,int locmap1,int locmap2 )
- {
- if (Keyboard::isKeyPressed(Keyboard::Right))
- {
- prevchoice='d';
- if( (map[locmap1][locmap2].pixel == '+' || map[locmap1][locmap2].pixel == '-' || map[locmap1][locmap2].pixel == 'u' || map[locmap1][locmap2].pixel == 'd' || map[locmap1][locmap2].pixel == '<' || map[locmap1][locmap2].pixel == '*'))
- {
- if (map[locmap1][locmap2].pixel != '>')
- x = 'd'; // will change the column
- }
- }
- else if (Keyboard::isKeyPressed(Keyboard::Left))
- {
- prevchoice='a';
- if ( (map[locmap1][locmap2].pixel == '+' || map[locmap1][locmap2].pixel == '-' || map[locmap1][locmap2].pixel == 'u' || map[locmap1][locmap2].pixel == 'd' || map[locmap1][locmap2].pixel == '>' || map[locmap1][locmap2].pixel == '/' ) && map[locmap1][locmap2].pixel != '<')
- {
- if (map[locmap1][locmap2].pixel != '<')
- x = 'a';
- }
- }
- else if (Keyboard::isKeyPressed(Keyboard::Up))
- {
- prevchoice='w';
- if ( (map[locmap1][locmap2].pixel == '+' || map[locmap1][locmap2].pixel == '|' || map[locmap1][locmap2].pixel == '>' || map[locmap1][locmap2].pixel == 'd' || map[locmap1][locmap2].pixel == '<' ) && map[locmap1][locmap2].pixel != 'u')
- {
- if (map[locmap1][locmap2].pixel != 'u')
- x = 'w';
- }
- }
- else if (Keyboard::isKeyPressed(Keyboard::Down))
- {
- prevchoice='s';
- if( (map[locmap1][locmap2].pixel == '+' || map[locmap1][locmap2].pixel == '|' || map[locmap1][locmap2].pixel == 'u' || map[locmap1][locmap2].pixel == '>' || map[locmap1][locmap2].pixel == '<') && map[locmap1][locmap2].pixel != 'd')
- {
- if (map[locmap1][locmap2].pixel != 'd')
- x = 's';
- }
- }
- if (prevchoice == 'a' && (map[locmap1][locmap2].pixel=='+'||map[locmap1][locmap2].pixel=='>'||map[locmap1][locmap2].pixel=='u'||map[locmap1][locmap2].pixel=='d'))
- {
- x=prevchoice;
- prevchoice='p';
- }
- else if(prevchoice == 'd' && (map[locmap1][locmap2].pixel=='+'||map[locmap1][locmap2].pixel=='<'||map[locmap1][locmap2].pixel=='u'||map[locmap1][locmap2].pixel=='d'))
- {
- x=prevchoice;
- prevchoice='p';
- }
- else if (prevchoice == 'w' && (map[locmap1][locmap2].pixel=='+'||map[locmap1][locmap2].pixel=='>'||map[locmap1][locmap2].pixel=='<'||map[locmap1][locmap2].pixel=='d'))
- {
- x=prevchoice;
- prevchoice='p';
- }
- else if (prevchoice == 's' && (map[locmap1][locmap2].pixel=='+'||map[locmap1][locmap2].pixel=='>'||map[locmap1][locmap2].pixel=='u'||map[locmap1][locmap2].pixel=='<'))
- {
- x=prevchoice;
- prevchoice='p';
- }
- return x;
- }
- int main()
- {
- bool switcher = 0;
- //ghost
- RectangleShape ghost(Vector2f(25.0f, 25.0f));
- ghost.setPosition(26*25, 4*25);
- ghost.setFillColor(Color::Red);
- //ghost 2
- RectangleShape ghost2(Vector2f(25.0f, 25.0f));
- ghost2.setPosition(26 * 25, 4 * 25);
- ghost2.setFillColor(Color::Blue);
- pair<int, int>current_ghost2_pos=make_pair(5,26),current_ghost1_pos = make_pair(4, 26);
- //ghost position
- pair<int, int>Blueghost_pos=make_pair(5,26),Redghost_pos = make_pair(4 , 26); //first row , sec column
- ghost_real_pos.first = &Redghost_pos.first, ghost_real_pos.second = &Redghost_pos.second;
- ghost2_real_pos.first = &Blueghost_pos.first, ghost2_real_pos.second = &Blueghost_pos.second;
- //disabling the ai at portals
- pair<int, int>rightportal_ai = make_pair(5, 17);
- pair<int, int>leftportal_ai = make_pair(22, 17);
- bool disable_ai_at_left_portal = 0;
- bool disable_ai_at_righ_portal = 0;
- //pair of ai movement right and form back
- pair< int, int > target_front = make_pair(4, 26);
- pair< int, int > target_back = make_pair(4, 1);
- bool target_front_arrived = 0;
- bool target_back_arrived = 0;
- int locmap1 = 4, locmap2 = 1;
- char x = 'p';
- RenderWindow gameplay(VideoMode(700, 750), "GamePlay", Style::Default);
- Astar map[36][28];
- ifstream pac;
- // Map
- Texture layout;
- RectangleShape Maptexture(Vector2f(750, 800));
- Maptexture.setPosition(-22, 53);
- layout.loadFromFile("Test1.png");
- Maptexture.setTexture(&layout);
- // pickups
- RectangleShape pickup(Vector2f(5.0f, 5.0f));
- pickup.setFillColor(Color::White);
- //make sure to use the new map file
- pac.open("Pac2.txt");
- int r = 0, id = 0;
- while (!pac.eof())
- {
- for (int i = 0; i < 28; i++)
- {
- pac >> map[r][i].pixel;
- id++;
- }
- r++;
- }
- pac.close();
- //setting the id
- r = 0;
- for (int i = 0; i < 36; i++)
- {
- for (int j = 0; j < 28; j++)
- {
- map[i][j].id =r ;
- cout << map[i][j].pixel << " ";
- r++;
- }
- cout << endl;
- }
- cout << endl << endl;
- //just tracing
- /*for (int i = 0; i < 36; i++)
- {
- for (int j = 0; j < 28; j++)
- {
- cout << j << " ";
- }
- cout << endl;
- }
- */
- // wall unit
- RectangleShape Wallunit;
- float Wallunit_x = 25.0f, Wallunit_y = 25.0f;
- Wallunit.setSize(Vector2f(Wallunit_x, Wallunit_y));
- // player
- CircleShape player(17);
- player.setFillColor(Color::Yellow);
- player.setPosition(25, 100);
- player.setOrigin(0, 1);
- drawing(gameplay, map, Maptexture, player, pickup, pickupschecker,ghost);
- //path
- int size = path.size();
- while (gameplay.isOpen())
- {
- AI(map, Redghost_pos.first, Redghost_pos.second, locmap1, locmap2,'R');
- size = path.size();
- Event e;
- while (gameplay.pollEvent(e))
- if (e.type == e.Closed)
- gameplay.close();
- // check the movement
- x = pacman_movement(map,x,locmap1,locmap2);
- // portals working successfully
- if (map[locmap1][locmap2].pixel == '/'&&x == 'a') {
- locmap2 = 28;
- player.setPosition(Vector2f(float(28 * 25), float(17 * 25)));
- }
- else if (map[locmap1][locmap2].pixel == '*'&&x == 'd')
- {
- locmap2 = -1;
- player.setPosition(Vector2f(float(-25), float(17 * 25)));
- }
- //
- pair<int, int>last,cur,last2,cur2;
- if (path.size()!=1)
- {
- last = make_pair(path.top().first, path.top().second);
- path.pop();
- cur = make_pair(path.top().first, path.top().second);
- }
- /*if (path2.size() != 1)
- {
- last2 = make_pair(path2.top().first, path2.top().second);
- path2.pop();
- cur2 = make_pair(path2.top().first, path2.top().second);
- }*/
- bool ghost_left = 0;
- //movement in all directions
- if (x == 'd'&&map[locmap1][locmap2 + 1].pixel != '0')
- {
- locmap2++;
- Redghost_pos = next(last, cur, Redghost_pos);
- // Blueghost_pos = next(last2, cur2, Blueghost_pos);
- for (int i = 0; i < 10; i++)
- {
- player.move(25.0 / 10.0, 0);
- moving( ghost, last, cur);
- // moving (ghost2, last2, cur2);
- drawing(gameplay, map, Maptexture, player, pickup, pickupschecker, ghost);
- }
- }
- else if (x == 's'&&map[locmap1 + 1][locmap2].pixel != '0')
- {
- locmap1++;
- Redghost_pos = next(last, cur, Redghost_pos);
- for (int i = 0; i < 10; i++)
- {
- player.move(0, 25.0 / 10.0);
- moving(ghost, last, cur);
- drawing(gameplay, map, Maptexture, player, pickup, pickupschecker,ghost);
- }
- }
- else if (x == 'a'&&map[locmap1][locmap2 - 1].pixel != '0')
- {
- locmap2--;
- Redghost_pos = next(last, cur, Redghost_pos);
- for (int i = 0; i < 10; i++)
- {
- player.move(-(25.0 / 10.0), 0);
- moving( ghost, last, cur);
- drawing(gameplay, map, Maptexture, player, pickup, pickupschecker,ghost);
- }
- }
- else if (x == 'w'&&map[locmap1 - 1][locmap2].pixel != '0')
- {
- locmap1--;
- Redghost_pos = next(last, cur, Redghost_pos);
- for (int i = 0; i < 10; i++)
- {
- player.move(0, -(25.0 / 10.0));
- moving( ghost, last, cur);
- drawing(gameplay, map, Maptexture, player, pickup, pickupschecker, ghost);
- }
- }
- else
- {
- Redghost_pos = next(last, cur, Redghost_pos);
- for (int i = 0; i < 10; i++)
- {
- moving(ghost, last, cur);
- drawing(gameplay, map, Maptexture, player, pickup, pickupschecker, ghost);
- }
- }
- target_front.first = 26;
- target_front.second = 4;
- //clearing old data
- while (1)
- {
- if (!path.empty())
- path.pop();
- else
- break;
- }
- open_set.clear();
- close_set.clear();
- /*for (int i = 0; i < 36; i++)
- {
- for (int j = 0; j < 28; j++)
- {
- cout << map[i][j].AI << " ";
- }
- cout << endl;
- }*/
- //make sure to use the new map file
- pac.open("Pac2.txt");
- int r = 0;
- while (!pac.eof())
- {
- for (int i = 0; i < 28; i++)
- {
- pac >> map[r][i].pixel;
- }
- r++;
- }
- pac.close();
- //setting the id
- r = 0;
- for (int i = 0; i < 36; i++)
- {
- for (int j = 0; j < 28; j++)
- {
- map[i][j].id = r;
- r++;
- }
- }
- gameplay.setFramerateLimit(90);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement