Advertisement
Zekrommaster110

Simple C++ Puzzle Game

Mar 15th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. // THIS IS ONLY A MIRROR POST
  2. // ORIGINAL: https://zekro.de/gist/puzzle
  3.  
  4. /*
  5.     Simple Puzzle Game v.1.5
  6.     © 2018 Ringo Hoffmann
  7.  
  8.     IDEAS:
  9.     ✔ debug start argument
  10.     - entering seed before start
  11.     ✔ saving high score
  12.     - better randomizer
  13.     - exclude unsolvable cases of
  14.       start field
  15.     - customizable size
  16. */
  17.  
  18. #include <iostream>
  19. #include <fstream>
  20. #include <string>
  21. #include <algorithm>
  22. #include <math.h>
  23.  
  24. using namespace std;
  25.  
  26.  
  27. const char *VERSION = "1.5";
  28. const char *HS_FILENAME = "highscore.dat";
  29.  
  30. // moves counter
  31. int moves = 0, highscore, debug_mode = 0;
  32.  
  33. // Initializing 2-dimensional field
  34. int f[3][3];
  35.  
  36. // clear display help function
  37. void cls() {
  38. #ifdef _WIN32 || _WIN64
  39.     system("cls");
  40. #else
  41.     system("clear");
  42. #endif
  43. }
  44.  
  45. // printing field with moves count
  46. void print_field() {
  47.     cls();
  48.     if (debug_mode)
  49.         cout << "DEBUG MODE ENABLED\n\n";
  50.     cout << "Puzzle Game by Ringo Hoffmann\n© 2018\n"
  51.         << "v." << VERSION << "\n\n"
  52.         << "[Start with argument '-r' to reset highscore.]\n\n"
  53.         << "Move:      " << moves + 1 << "\n"
  54.         << "Highscore: ";
  55.     if (highscore == -1)
  56.         cout << "not set yet\n\n";
  57.     else
  58.         cout << highscore << "\n\n";
  59.  
  60.     cout << " +-------+\n";
  61.     for (int i = 0; i < 3; i++) {
  62.         cout << " | ";
  63.         for (int j = 0; j < 3; j++) {
  64.             if (f[i][j] == 0)
  65.                 cout << "  ";
  66.             else
  67.                 cout << f[i][j] << " ";
  68.         }
  69.         cout << "|\n";
  70.     }
  71.     cout << " +-------+\n";
  72. }
  73.  
  74. // get position of number in field
  75. void get_pos(int n, int *pos) {
  76.     for (int i = 0; i < 3; i++) {
  77.         for (int j = 0; j < 3; j++) {
  78.             if (f[i][j] == n) {
  79.                 pos[0] = i;
  80.                 pos[1] = j;
  81.                 return;
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. // check if finish sequenze is reached
  88. bool is_finished() {
  89.     int last = -1;
  90.     for (int i = 0; i < 2; i++) {
  91.         for (int j = 0; j < 3; j++) {
  92.             if (f[i][j] > last)
  93.                 last = f[i][j];
  94.             else
  95.                 return false;
  96.         }
  97.         return (f[2][0] = 7 && f[2][1] == 8 && f[2][2] == 0);
  98.     }
  99. }
  100.  
  101. // getting highscore from file
  102. // if file is not existent, it returns -1
  103. // (= not set)
  104. int get_highscore() {
  105.     ifstream fr(HS_FILENAME);
  106.     int out = -1;
  107.     if (fr.good())
  108.         fr >> out;
  109.     return out;
  110. }
  111.  
  112. // write highscore to file
  113. void set_highscore(int m) {
  114.     ofstream fw(HS_FILENAME);
  115.     if (fw.good()) {
  116.         fw << m;
  117.         fw.close();
  118.     }
  119. }
  120.  
  121. // reset highscore by writing -1
  122. // (= not set) to the high score file
  123. void reset_highscore() {
  124.     set_highscore(-1);
  125. }
  126.  
  127. // just a function to check if an argument is
  128. // set as start argument
  129. bool arg_exists(char** begin, char** end, const string& option)
  130. {
  131.     return find(begin, end, option) != end;
  132. }
  133.  
  134.  
  135. int main(int argc, char *argv[]) {
  136.  
  137.     // checking for debug and reset start argument
  138.     if (arg_exists(argv, argv + argc, "-d"))
  139.         debug_mode = 1;
  140.     if (arg_exists(argv, argv + argc, "-r"))
  141.         reset_highscore();
  142.  
  143.     // get highscore
  144.     highscore = get_highscore();
  145.  
  146.     int i = 0;
  147.  
  148.     // filling field with numbers randomly
  149.     // if debug_mode is set, it will be filled
  150.     // with a prepared structure which can be solved
  151.     // with only one move
  152.     if (!debug_mode) {
  153.         while (i < 9) {
  154.             int numbs[] = { 1,2,3,4,5,6,7,8,0 };
  155.             int c_r[] = { rand() % 3, rand() % 3 };
  156.             int *e = &f[c_r[0]][c_r[1]];
  157.             if (*e == 0) {
  158.                 *e = numbs[i++];
  159.             }
  160.         }
  161.     }
  162.     else {
  163.         int count = 1;
  164.         for (int i = 0; i < 2; i++)
  165.             for (int j = 0; j < 3; j++)
  166.                 f[i][j] = count++;
  167.         f[2][0] = 7;
  168.         f[2][1] = 0;
  169.         f[2][2] = 8;
  170.     }
  171.  
  172.  
  173.     int inpt;
  174.  
  175.     do {
  176.         print_field();
  177.  
  178.         cout << "\nEnter number (0 for exit) > ";
  179.         cin >> inpt;
  180.  
  181.         // check if input is in bounds
  182.         // else: ignore input
  183.         if (inpt > 8 || inpt < 0)
  184.             continue;
  185.  
  186.         int pos[2], free[2];
  187.         // get grid position of input number
  188.         // and free slot (0)
  189.         get_pos(inpt, pos);
  190.         get_pos(0, free);
  191.         // calculate difference between the two points
  192.         float diff = sqrt(
  193.             (free[0] - pos[0]) * (free[0] - pos[0]) +
  194.             (free[1] - pos[1]) * (free[1] - pos[1])
  195.         );
  196.  
  197.         // If the difference is more than 1,
  198.         // ignore input
  199.         if (diff > 1)
  200.             continue;
  201.  
  202.         // swap value of input position with
  203.         // value of empty (0) position and
  204.         // count up moves after
  205.         int tmp = f[pos[0]][pos[1]];
  206.         f[pos[0]][pos[1]] = f[free[0]][free[1]];
  207.         f[free[0]][free[1]] = tmp;
  208.         moves++;
  209.     }
  210.     // cancel with input 0 or if finished
  211.     while (inpt != 0 && !is_finished());
  212.  
  213.     print_field();
  214.  
  215.     // just a check to dont print won text if
  216.     // canceled with 0-input
  217.     // else print won text, moves and save as
  218.     // high score, if moves are less then the set
  219.     // high score or if high score was not set before
  220.     if (inpt != 0) {
  221.         cout << "\nWon!\nCount of moves: " << moves << endl;
  222.         if (moves < highscore || highscore == -1) {
  223.             set_highscore(moves);
  224.             cout << "Congratulation! You set a new highscore!\n";
  225.         }
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement