Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <ctime>
- using namespace std;
- struct Tile
- {
- bool up;
- bool right;
- bool down;
- bool left;
- int x; // just for save points, the save points cordinate
- int y; // just for save points, the save points cordinate
- int k; // just for save points, how many steps at that save point
- vector<string> path;
- };
- struct Player
- {
- int num_player_quests;
- int x;
- int y;
- Tile player_tile;
- };
- struct Item
- {
- string name;
- int x;
- int y;
- int player_id;
- };
- struct Quest
- {
- string name;
- int x;
- int y;
- };
- bool can_up(Tile me, vector<vector<Tile> > line) // can it go one step up
- {
- if(me.y == 0)return false;
- else if(line[me.y][me.x].up == true && line[me.y - 1][me.x ].down == true)return true;
- else return false;
- }
- bool can_right(Tile me, vector<vector<Tile> > line) // can it go one step right
- {
- if(me.x == 6)return false;
- else if(line[me.y][me.x].right == true && line[me.y][me.x + 1].left == true)return true;
- else return false;
- }
- bool can_down(Tile me, vector<vector<Tile> > line) // can it go one step down
- {
- if(me.y == 6)return false;
- else if(line[me.y][me.x].down == true && line[me.y + 1][me.x].up == true)return true;
- else return false;
- }
- bool can_left(Tile me, vector<vector<Tile> > line) // can it go one step left
- {
- if(me.x == 0)return false;
- else if(line[me.y][me.x].left == true && line[me.y][me.x - 1].right == true)return true;
- else return false;
- }
- int countVertexes(vector<vector<Tile> > line, Tile me)
- {
- int n = 0;
- if(can_up(me, line)) n++;
- if(can_down(me, line)) n++;
- if(can_left(me, line)) n++;
- if(can_right(me, line)) n++;
- return n;
- }
- bool find_available_path(vector<vector<Tile> > &line, Tile item, Player me)
- {
- vector<Tile*> save_points;
- int k = 0; // number of steps, limit is 20
- int save_index = -1; // which save point is which
- Tile copy_me; // the one that moves around to look if there is an available path
- copy_me.x = me.x;
- copy_me.y = me.y;
- cerr << me.x << " " << me.y << endl;
- cerr << endl;
- while((copy_me.x != item.x || copy_me.y != item.y) && k < 20)
- {
- cerr<<"X Y : " << copy_me.x << " " << copy_me.y << endl;
- if(copy_me.x == item.x && copy_me.y == item.y)return true;
- if(can_up(copy_me, line) == true)
- {
- if(countVertexes(line, copy_me) > 1)
- {
- line[copy_me.y][copy_me.x].k = k;
- save_points.push_back(&line[copy_me.y][copy_me.x]);
- }
- line[copy_me.y][copy_me.x].up = false;
- Tile current_pos = copy_me;
- copy_me.x = copy_me.x;
- copy_me.y = copy_me.y - 1;
- k++;
- line[copy_me.y][copy_me.x].down = false;
- line[copy_me.y][copy_me.x].path = line[current_pos.y][current_pos.x].path;
- line[copy_me.y][copy_me.x].path.push_back({"UP"});
- }
- else if(can_right(copy_me, line) == true)
- {
- if(countVertexes(line, copy_me) > 1)
- {
- line[copy_me.y][copy_me.x].k = k;
- save_points.push_back(&line[copy_me.y][copy_me.x]);
- }
- line[copy_me.y][copy_me.x].right = false;
- Tile current_pos = copy_me;
- copy_me.x = copy_me.x + 1;
- copy_me. y = copy_me.y;
- k++;
- line[copy_me.y][copy_me.x].left = false;
- line[copy_me.y][copy_me.x].path = line[current_pos.y][current_pos.x].path;
- line[copy_me.y][copy_me.x].path.push_back({"RIGHT"});
- }
- else if(can_down(copy_me, line) == true)
- {
- if(countVertexes(line, copy_me) > 1)
- {
- line[copy_me.y][copy_me.x].k = k;
- save_points.push_back(&line[copy_me.y][copy_me.x]);
- }
- line[copy_me.y][copy_me.x].down = false;
- Tile current_pos = copy_me;
- copy_me.x = copy_me.x;
- copy_me.y = copy_me.y+1;
- k++;
- line[copy_me.y][copy_me.x].up = false;
- line[copy_me.y][copy_me.x].path = line[current_pos.y][current_pos.x].path;
- line[copy_me.y][copy_me.x].path.push_back({"DOWN"});
- }
- else if(can_left(copy_me, line) == true)
- {
- if(countVertexes(line, copy_me) > 1)
- {
- line[copy_me.y][copy_me.x].k = k;
- save_points.push_back(&line[copy_me.y][copy_me.x]);
- }
- line[copy_me.y][copy_me.x].left = false;
- Tile current_pos = copy_me;
- copy_me.x = copy_me.x-1;
- copy_me.y = copy_me.y;
- k++;
- line[copy_me.y][copy_me.x].right = false;
- line[copy_me.y][copy_me.x].path = line[current_pos.y][current_pos.x].path;
- line[copy_me.y][copy_me.x].path.push_back({"LEFT"});
- }
- else if(can_up(copy_me, line) == false && can_right(copy_me, line) == false && can_down(copy_me, line) == false && can_left(copy_me, line) == false)
- {
- if(save_points.size() > 0)
- {
- while(save_points.size() > 0 && countVertexes(line, *save_points[save_points.size() - 1])<= 0)
- {
- save_points.erase(save_points.end() - 1);
- }
- if(save_points.size() == 0) return false;
- copy_me = *save_points[save_points.size() - 1];
- k = save_points[save_points.size() - 1]->k;
- }
- else return false;
- }
- if(copy_me.x == item.x && copy_me.y == item.y)return true;
- }
- return false;
- }
- void push_turn (vector<vector<Tile> > line, Player me, Item my_item)
- {
- if(me.x != 0 && me.x != 6 && me.y != 0 && me.y != 6)
- {
- if(me.x < 3)cout << "PUSH " << me.y << " LEFT" << endl;
- else cout << "PUSH " << me.y << " RIGHT" << endl;
- }
- else if(my_item.x != -1 && my_item.x != -2)
- {
- if(my_item.x != me.x)
- {
- if(my_item.y < 3)cout << "PUSH " << my_item.x << " UP" << endl;
- else cout << "PUSH " << my_item.x << " DOWN" << endl;
- }
- else
- {
- if(my_item.x < 3)cout << "PUSH " << my_item.y << " LEFT" << endl;
- else cout << "PUSH " << my_item.y << " RIGHT" << endl;
- }
- }
- else if(my_item.x == -1)
- {
- if(me.x == 0)cout << "PUSH " << me.y << " LEFT" << endl;
- else if(me.x == 6)cout << "PUSH " << me.y << " RIGHT" << endl;
- else if(me.y == 0)cout << "PUSH " << me.x << " UP" << endl;
- else cout << "PUSH " << me.x << " DOWN" << endl;
- }
- else cout << "PUSH " << 3 << " LEFT";
- }
- int main()
- {
- //srand(time(0));
- // game loop
- while (1)
- {
- vector<vector<Tile> > line;
- Player me;
- Player enemy;
- vector<Item> my_items;
- vector<Item> enemy_items;
- vector<Quest> my_quests;
- vector<Quest> enemy_quests;
- line.resize(7);
- int turn_type;
- cin >> turn_type; cin.ignore();
- for (int i = 0; i < line.size(); i++) // ivedu duomenis zaidimo lentos
- {
- for (int j = 0; j < 7; j++)
- {
- string tile;
- cin >> tile; cin.ignore();
- line[i].push_back({tile[0] - '0', tile[1] - '0', tile[2] - '0', tile[3] - '0'});
- line[i][j].x = j;
- line[i][j].y = i;
- line[i][j].path.clear();
- }
- }
- string a;string b;Tile A;Tile B;// cancer tik duomenu uzsaugojimui
- cin >> me.num_player_quests >> me.x >> me.y >> a; cin.ignore(); // ivedu duomenis apie save
- A.up = a[0] - '0'; A.right = a[1] - '0'; A.down = a[2] - '0'; A.left = a[3] - '0';
- me.player_tile = A;
- cin >> enemy.num_player_quests >> enemy.x >> enemy.y >> b; cin.ignore(); // ivedu duomenis apie priesa
- B.up = b[0] - '0'; B.right = b[1] - '0'; B.down = b[2] - '0'; B.left = b[3] - '0';
- enemy.player_tile = B;
- int num_of_items;
- cin >> num_of_items; cin.ignore();// the total number of items available on board and on player tiles
- for (int i = 0; i < num_of_items; i++)
- {
- Item A;
- cin >> A.name >> A.x >> A.y >> A.player_id; cin.ignore();
- if(A.player_id == 0)my_items.push_back(A);
- else enemy_items.push_back(A);
- }
- int num_of_quests;
- cin >> num_of_quests; cin.ignore(); // the total number of revealed quests for both players
- for (int i = 0; i < num_of_quests; i++)
- {
- Quest A;
- int id;
- cin >> A.name >> id; cin.ignore();
- if(id == 0)
- {
- my_quests.push_back(A);
- for(int j = 0; j < my_items.size(); j++)
- {
- if(my_items[j].name == my_quests[i].name)
- {
- my_quests[i].x = my_items[j].x;
- my_quests[i].y = my_items[j].y;
- }
- }
- }
- else enemy_quests.push_back(A);
- }
- // cerr << "how many items: " << my_items.size() << endl;
- //bool canReach = false;
- //if(my_quests.size() > 0 && my_quests[0].y > 0)
- //{
- //cerr << "quest 0 x y : " << my_quests[0].x << " " << my_quests[0].y << endl;
- // canReach = find_available_path(line, line[my_quests[0].y][my_quests[0].x], me) ;
- //cerr << "can it reach or not: " << canReach << endl;
- //}
- if(turn_type == 0) // push
- {
- bool shook = false; // for breaking cycle
- for(int i = 0; i < my_items.size(); i++)
- {
- for(int j = 0; j < my_quests.size(); j++)
- {
- if(my_items[i].name == my_quests[j].name)
- {
- push_turn(line, me, my_items[i]);
- shook = true;
- break;
- }
- }
- if(shook)break;
- }
- if(shook == false)cout << "PUSH " << 3 << " LEFT" << endl;
- }
- if(turn_type == 1) // move
- {
- //&& my_quests[0].y > 0
- //for(int )
- if(my_quests.size() > 0)
- {
- Tile copyLines[7][7];
- for(int i = 0; i < 7; i++)
- {
- for(int j = 0; j < 7; j++)
- {
- copyLines[i][j] = line[i][j];
- }
- }
- bool canReach = false;
- for(int i = 0; i < my_quests.size(); i++)
- {
- if(my_quests[i].x < 0) continue;
- for(int i1 = 0; i1 < 7; i1++)
- {
- for(int j = 0; j < 7; j++)
- {
- line[i1][j] = copyLines[i1][j];
- }
- }
- cerr << "does it work hmmm xdd " << endl;
- canReach = find_available_path(line, line[my_quests[i].y][my_quests[i].x], me);
- if(my_quests[i].x > 0 && canReach)
- {
- cout << "MOVE ";
- for(int j = 0; j < line[my_quests[i].y][my_quests[i].x].path.size(); j++)
- {
- if(j == line[my_quests[i].y][my_quests[i].x].path.size() - 1) cout << line[my_quests[i].y][my_quests[i].x].path[j] << endl;
- else cout << line[my_quests[i].y][my_quests[i].x].path[j] << " ";
- }
- break; // later i will not break but check if i can move elsewhere
- }
- }
- if(!canReach)
- {
- cout << "PASS" << endl;
- }
- }
- else cout << "PASS" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement