JosepRivaille

X55865: Carambola (1)

Jun 3rd, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. typedef vector< vector<char> > Tauler;
  7.  
  8. struct Bola {
  9.     int x_ant, y_ant;   //Posicio anterior de la bola
  10.     int x_act, y_act;   //Posicio actual de la bola
  11.     int x_seg, y_seg;   //Posicio seguent de la bola
  12. };
  13.  
  14.  
  15.  
  16. //Actualitza el tauler amb les noves posicions de la bola ø
  17. void new_tauler(Tauler& M, Bola& b) {
  18.     M[b.x_ant][b.y_ant] = '=';
  19.     M[b.x_act][b.y_act] = 'B';
  20. }
  21.  
  22.  
  23. //Diu si la bola està a una cantonada
  24. //Pre: la bola està dins del tauler
  25. bool corner(const Bola& b, int f, int c) {
  26.     if ((b.x_act == 0 || b.x_act == f-1) && (b.y_act == 0 || b.y_act == c-1)) return true;
  27.     return false;
  28. }
  29.  
  30.  
  31. //Diu si la bola es pot seguir movent o si s'ha de parar
  32. //ja sigui cantonada o que troba la vermella
  33. bool move(const Tauler& t, Bola& b, int f, int c) {
  34.     if (corner(b, f, c)) {
  35.         if (b.x_act == 0) b.x_seg = 1;
  36.         if (b.x_act == f - 1) b.x_seg = f - 2;
  37.         if (b.y_act == 0) b.y_seg = 1;
  38.         if (b.y_act == c - 1) b.y_seg = c - 2;
  39.     }
  40.     //Actualitzacio posicions
  41.     b.x_ant = b.x_act;
  42.     b.y_ant = b.y_act;
  43.     b.x_act = b.x_seg;
  44.     b.y_act = b.y_seg;
  45.     b.x_seg = 2*b.x_act - b.x_ant;
  46.     b.y_seg = 2*b.y_act - b.y_ant;
  47.     bool edge_f = false;
  48.     bool edge_c = false;
  49.     if (b.x_act == 0 || b.x_act == f-1) edge_f = true;
  50.     if (b.y_act == 0 || b.y_act == c-1) edge_c = true;
  51.     if (edge_f && edge_c) {
  52.         b.x_seg = b.x_act;
  53.         b.y_seg = b.y_act;
  54.     }
  55.     else if (edge_f) b.x_seg = b.x_ant;
  56.     else if (edge_c) b.y_seg = b.y_ant;
  57.     if (edge_f || edge_c || t[b.x_act][b.y_act] == 'b') return true;
  58.     else return false;
  59. }
  60.  
  61.  
  62. //Mou la bola fins que xoca
  63. void moure_fins_xocar(Tauler& t, Bola& b) {
  64.     int f = t.size();
  65.     int c = t[0].size();
  66.     while (!move(t, b, f, c)) new_tauler(t, b);
  67. }
  68.  
  69.  
  70. //Retorna la funcio fins a 4 possibles rebots per a que segueixi el joc
  71. //Pre: xocs = 0
  72. bool play(Tauler& t, Bola& b, int xocs, int f, int c) {
  73.     if (xocs < 4) {
  74.         moure_fins_xocar(t, b);
  75.         bool xoc = false;
  76.         if (t[b.x_act][b.y_act] == 'b') xoc = true;
  77.         new_tauler(t, b);
  78.         if (xoc) {
  79.             return xocs == 3;
  80.         }
  81.         if (corner(b, f, c)) return false;
  82.         else cout << "(" << b.x_act << "," << b.y_act << ")";
  83.         return play(t, b, xocs+1, f, c);
  84.     }
  85.     return false;
  86. }
  87.  
  88.  
  89. //Escriu el tauler resultant ø
  90. void write_matrix(const Tauler& T) {
  91.     int f = T.size();
  92.     int c = T[0].size();
  93.     for (int i = 0; i < f; ++i) {
  94.         for (int j = 0; j < c; ++j) {
  95.             cout << T[i][j];
  96.         }
  97.         cout << endl;
  98.     }
  99.     cout << endl;
  100. }
  101.  
  102.  
  103. //Pre: Llegeix un seguit de Taulers tamany fxc i la posicio de 2 boles ø
  104. //Post: Per a cada cas escriu els punts on rebota, si ho fa, si fa carambola
  105. //      i el nou tauler modificat
  106. int main() {
  107.     int f, c;
  108.     while (cin >> f >> c) {
  109.         Bola b;
  110.         int x, y;
  111.         cin >> b.x_act >> b.y_act >> x >> y;
  112.         Tauler M(f, vector<char> (c, '.'));
  113.         M[b.x_act][b.y_act] = 'B';
  114.         M[x][y] = 'b';
  115.         if (play(M, b, 0, f, c)) cout << ": si" << endl;
  116.         else cout << ": no" << endl;
  117.         new_tauler(M, b);
  118.         write_matrix(M);
  119.     }
  120. }
  121.  
  122. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment