SHARE
TWEET

Untitled

a guest Oct 21st, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "peg_solitaire.h"
  2.  
  3. const std::vector<std::pair<int, int>> lab4::peg_solitaire::moves = { {0,2}, {0,-2}, {2,0}, {-2,0} };
  4.  
  5. bool lab4::peg_solitaire::is_move_possible(int x_from, int y_from, int x_to, int y_to) const
  6. {
  7.     if (x_from < 0 || x_from >= width || x_to < 0 || x_to >= width)
  8.         return false;
  9.     if (y_from < 0 || y_from >= height || y_to < 0 || y_to >= height)
  10.         return false;
  11.     if (!((abs(x_from - x_to) == 2 && y_from == y_to) || (abs(y_from - y_to) == 2 && x_from == x_to)))
  12.         return false;
  13.     if (board[y_from*width + x_from] == 0 || board[y_to*width + x_to] != 0 || board[(y_from + y_to)/2*width + (x_from + x_to)/2] == 0)
  14.         return false;
  15.     return true;
  16. }
  17.  
  18. int lab4::peg_solitaire::make_move(int x_from, int y_from, int x_to, int y_to)
  19. {
  20.     int& from = board[y_from*width + x_from];
  21.     int& over = board[(y_from + y_to) / 2 * width + (x_from + x_to) / 2];
  22.     int score = from * over;
  23.     board[y_to*width + x_to] = from;
  24.     from = 0;
  25.     over = 0;
  26.     --stone_count;
  27.     return score;
  28. }
  29.  
  30. void lab4::peg_solitaire::undo_move(int x_from, int y_from, int x_to, int y_to, int score)
  31. {
  32.     int& to = board[y_to*width + x_to];
  33.     board[(y_from + y_to) / 2 * width + (x_from + x_to) / 2] = score / to;
  34.     board[y_from*width + x_from] = to;
  35.     to = 0;
  36.     ++stone_count;
  37. }
  38.  
  39. bool lab4::can_win(peg_solitaire game)
  40. {
  41.     if (game.stone_count == 1)
  42.         return true;
  43.     bool canwin = false;
  44.     int score = 0;
  45.     for (int y = 0; y < game.height; ++y)
  46.     {
  47.         for (int x = 0; x < game.width; ++x)
  48.         {
  49.             for (const std::pair<int, int>& pair : game.moves)
  50.             {
  51.                 if (game.is_move_possible(x, y, x + pair.first, y + pair.second))
  52.                 {
  53.                     score = game.make_move(x, y, x + pair.first, y + pair.second);
  54.                     canwin = canwin || can_win(game);
  55.                     if (canwin) return canwin;
  56.                     game.undo_move(x, y, x + pair.first, y + pair.second, score);
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     return canwin;
  62. }
  63.  
  64. int lab4::best_win_score(peg_solitaire game)
  65. {
  66.     if (game.stone_count == 1)
  67.         return 0;
  68.     int best_score = -1;
  69.     int score;
  70.     int this_best_score;
  71.     for (int y = 0; y < game.height; ++y)
  72.     {
  73.         for (int x = 0; x < game.width; ++x)
  74.         {
  75.             for (const std::pair<int, int>& pair : game.moves)
  76.             {
  77.                 if (game.is_move_possible(x, y, x + pair.first, y + pair.second))
  78.                 {
  79.                    
  80.                     score = game.make_move(x, y, x + pair.first, y + pair.second);
  81.                     this_best_score = best_win_score(game);
  82.                     if (this_best_score != -1)
  83.                         best_score = std::max(this_best_score + score, best_score);
  84.                     game.undo_move(x, y, x + pair.first, y + pair.second, score);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     return best_score;
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top