Advertisement
Hinski2

Untitled

May 30th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //enumy
  5. enum animal_enum {NO_ANIMAL, RAT, CAT, DOG, WOLF, JAGUAR, TIGER, LION, ELEPHANT};
  6. enum type_enum {FIELD, TRAP, RIVER, DEN_P1, DEN_P2};
  7. enum player_enum {P1, P2, NO_PLAYER};
  8.  
  9. //skróty
  10. #define fi first
  11. #define se second
  12.  
  13. //typy danych
  14. typedef pair<short, short> pss;
  15.  
  16. //consty
  17. const short n = 9, m = 7;
  18. const short tile_type[n][m] = {
  19.     {FIELD, FIELD, TRAP, DEN_P1, TRAP, FIELD, FIELD},
  20.     {FIELD, FIELD, FIELD, TRAP, FIELD, FIELD, FIELD},
  21.     {FIELD, FIELD, FIELD, FIELD, FIELD, FIELD, FIELD},
  22.     {FIELD, RIVER, RIVER, FIELD, RIVER, RIVER, FIELD},
  23.     {FIELD, RIVER, RIVER, FIELD, RIVER, RIVER, FIELD},
  24.     {FIELD, RIVER, RIVER, FIELD, RIVER, RIVER, FIELD},
  25.     {FIELD, FIELD, FIELD, FIELD, FIELD, FIELD, FIELD},
  26.     {FIELD, FIELD, FIELD, TRAP, FIELD, FIELD, FIELD},
  27.     {FIELD, FIELD, TRAP, DEN_P2, TRAP, FIELD, FIELD}};
  28. const pss shift[] = {{-2, 0}, {1, 0}, {0, -1}, {0, 1}};
  29.  
  30. //struktury
  31. struct pon {
  32.     animal_enum animal;
  33.     player_enum player;
  34. };
  35.  
  36. struct move_struct {
  37.     pss from;
  38.     pss shift;
  39. };
  40.  
  41. //funcje pomocjicze
  42. inline bool valid_coord(pss p);
  43.  
  44. //klasa przedstawiająca stan gry
  45. class State {
  46. public:
  47.     short player;                       // który gracz wykonuje ruch
  48.     vector<vector<pon>> board;          // nasza plansza
  49.     vector<vector<pss>> animal_poss;    // dokładne pozycje zwierząt
  50.  
  51.     //konstruktor
  52.     State(short player, vector<vector<pon>> board, vector<vector<pss>> animal_poss) : player(player), board(board), animal_poss(animal_poss) {}
  53.  
  54.     //destruktor
  55.     ~State() {
  56.     }
  57.  
  58.     // wypisanie planszy
  59.     void print() {
  60.         for(int i = 0; i < n; i++) {
  61.             for(int j = 0; j < m; j++) {
  62.                 char c;
  63.                 if(board[i][j].animal == NO_ANIMAL) c = '.';
  64.                 else if(board[i][j].animal == RAT) c = 'R' | ((board[i][j].player == P1) * 32);
  65.                 else if(board[i][j].animal == CAT) c = 'C' | ((board[i][j].player == P1) * 32);
  66.                 else if(board[i][j].animal == CAT) c = 'D' | ((board[i][j].player == P1) * 32);
  67.                 else if(board[i][j].animal == CAT) c = 'W' | ((board[i][j].player == P1) * 32);
  68.                 else if(board[i][j].animal == CAT) c = 'J' | ((board[i][j].player == P1) * 32);
  69.                 else if(board[i][j].animal == CAT) c = 'T' | ((board[i][j].player == P1) * 32);
  70.                 else if(board[i][j].animal == CAT) c = 'L' | ((board[i][j].player == P1) * 32);
  71.                 else if(board[i][j].animal == CAT) c = 'E' | ((board[i][j].player == P1) * 32);
  72.  
  73.                 cout << c;
  74.             }
  75.             cout << endl;
  76.         }
  77.         cout << endl;
  78.     }
  79.  
  80.     vector<move_struct> generate_possible_moves() {
  81.         vector<move_struct> possible_moves;
  82.         pss my_den = (player == P1 ? make_pair(0, 3) : make_pair(8, 3));
  83.         for(int animal = 1; animal < 9; animal++) {
  84.             if(animal_poss[player][animal] == (pss) {-1, -1}) continue; //jeśli to zwierze jest zbite
  85.  
  86.             for(auto s: shift){
  87.                 pss poss = {animal_poss[player][animal].fi + s.fi, animal_poss[player][animal].se + s.se};
  88.  
  89.                 //początkowe / ogólne sprawdzenia
  90.                 if(!valid_coord(poss)) continue; //jeśli wychodzimy poza plansza
  91.                 if(board[poss.fi][poss.se].animal != NO_ANIMAL && board[poss.fi][poss.se].player == player) continue; // jeśli w poss znajduje się zwierze i to nasze zwierze
  92.                 if(poss == my_den) continue; //jeśli whodzimy do swojej jamy
  93.                 if(animal != RAT && animal != TIGER && animal != LION && tile_type[poss.fi][poss.se] == RIVER) continue; // tylko szczur może wchodzić do wody,a tygrys i lew mogą nad nią skakać
  94.  
  95.                 switch(animal) {
  96.                     case RAT:
  97.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > RAT && board[poss.fi][poss.se].animal != ELEPHANT) continue;
  98.                         if(tile_type[animal_poss[player][animal].fi][animal_poss[player][animal].se] == RIVER && tile_type[poss.fi][poss.se] != RIVER && board[poss.fi][poss.se].animal) continue; //jeśli szczur był w wodzi i z niej wychodzi to nie może bić
  99.                         possible_moves.push_back({animal_poss[player][animal], s});
  100.                         break;
  101.  
  102.                     case CAT:
  103.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > CAT) continue;
  104.                         possible_moves.push_back({animal_poss[player][animal], s});
  105.                         break;
  106.  
  107.                     case DOG:
  108.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > DOG) continue;
  109.                         possible_moves.push_back({animal_poss[player][animal], s});
  110.                         break;
  111.  
  112.                     case WOLF:
  113.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > WOLF) continue;
  114.                         possible_moves.push_back({animal_poss[player][animal], s});
  115.                         break;
  116.  
  117.                     case JAGUAR:
  118.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > JAGUAR) continue;
  119.                         possible_moves.push_back({animal_poss[player][animal], s});
  120.                         break;
  121.  
  122.                     case TIGER:
  123.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > TIGER) continue;
  124.                         if(tile_type[poss.fi][poss.se] == RIVER) poss = make_jump_over_water(poss, animal, s);
  125.                         if(poss == (pss){-1, -1}) continue;
  126.                         possible_moves.push_back({animal_poss[player][animal], s});
  127.                         break;
  128.  
  129.                     case LION:
  130.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > LION) continue;
  131.                         possible_moves.push_back({animal_poss[player][animal], s});
  132.                         break;
  133.  
  134.                     case ELEPHANT:
  135.                         if(tile_type[poss.fi][poss.se] != TRAP && board[poss.fi][poss.se].animal > ELEPHANT) continue;
  136.                         possible_moves.push_back({animal_poss[player][animal], s});
  137.                         break;                        
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.     inline pss make_jump_over_water(pss poss, int animal, pss s){
  144.         while(tile_type[poss.fi][poss.se] == RIVER){
  145.             if(board[poss.fi][poss.se].animal > 0){ //trafiliśmy na szczura
  146.                 poss = {-1, -1};
  147.                 break;
  148.             }
  149.  
  150.             poss.fi += s.fi, poss.se += s.se; //lecimy dalej jak Jordan
  151.         }
  152.  
  153.         if(board[poss.fi][poss.se].player == player || board[poss.fi][poss.se].animal > animal) poss = {-1, -1}; //przeskoczyliśmy wode ale wskakujemy na nasze inne zwierze albo na zwirze przeciwnika o większej mocy
  154.         return poss;
  155.     }
  156.  
  157.     inline bool end_game(){
  158.         return board[0][3].animal || board[8][3].animal;
  159.     }
  160. };
  161.  
  162. void make_move(State &s) {
  163.  
  164. }
  165.  
  166. int main(){
  167.     //ustawianie gry
  168.     vector<vector<pon>> init_board(n, vector<pon>(m, {NO_ANIMAL, NO_PLAYER}));
  169.     short player = P1;
  170.     init_board[0][0] = {LION, P1}, init_board[0][6] = {TIGER, P1}, init_board[1][1] = {DOG, P1}, init_board[1][5] = {CAT, P1}, init_board[2][0] = {RAT, P1}, init_board[2][2] = {JAGUAR, P1}, init_board[2][4] = {WOLF, P1}, init_board[2][6] = {ELEPHANT, P1};
  171.     init_board[8][6] = {LION, P2}, init_board[8][0] = {TIGER, P2}, init_board[7][5] = {DOG, P2}, init_board[7][1] = {CAT, P2}, init_board[7][6] = {RAT, P2}, init_board[7][4] = {JAGUAR, P2}, init_board[7][2] = {WOLF, P2}, init_board[7][0] = {ELEPHANT, P2};
  172.  
  173.     vector<vector<pss>> animal_poss(2, vector<pss>(9));
  174.     animal_poss[P1][LION] = {0, 0}, animal_poss[P1][TIGER] = {0, 6}, animal_poss[P1][DOG] = {1, 1}, animal_poss[P1][CAT] = {1, 5}, animal_poss[P1][RAT] = {2, 0}, animal_poss[P1][JAGUAR] = {2, 2}, animal_poss[P1][WOLF] = {2, 4}, animal_poss[P1][ELEPHANT] = {2, 6};
  175.     animal_poss[P2][LION] = {8, 6}, animal_poss[P2][TIGER] = {8, 0}, animal_poss[P2][DOG] = {7, 5}, animal_poss[P2][CAT] = {7, 1}, animal_poss[P2][RAT] = {7, 6}, animal_poss[P2][JAGUAR] = {7, 4}, animal_poss[P2][WOLF] = {7, 2}, animal_poss[P2][ELEPHANT] = {7, 0};
  176.  
  177.     State s(player, init_board, animal_poss);
  178.  
  179.     //graj dopuki ktoś nie wygrał
  180.     while(s.end_game()){
  181.         make_move(s);
  182.     }
  183.  
  184.     //wypisz kto wygrał
  185. }
  186.  
  187. inline bool valid_coord(pss p){
  188.     return 0 <= p.fi && p.fi < n && 0 <= p.se && p.se < m;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement