Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // THIS IS ONLY A MIRROR POST
- // ORIGINAL: https://zekro.de/gist/puzzle
- /*
- Simple Puzzle Game v.1.5
- © 2018 Ringo Hoffmann
- IDEAS:
- ✔ debug start argument
- - entering seed before start
- ✔ saving high score
- - better randomizer
- - exclude unsolvable cases of
- start field
- - customizable size
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <algorithm>
- #include <math.h>
- using namespace std;
- const char *VERSION = "1.5";
- const char *HS_FILENAME = "highscore.dat";
- // moves counter
- int moves = 0, highscore, debug_mode = 0;
- // Initializing 2-dimensional field
- int f[3][3];
- // clear display help function
- void cls() {
- #ifdef _WIN32 || _WIN64
- system("cls");
- #else
- system("clear");
- #endif
- }
- // printing field with moves count
- void print_field() {
- cls();
- if (debug_mode)
- cout << "DEBUG MODE ENABLED\n\n";
- cout << "Puzzle Game by Ringo Hoffmann\n© 2018\n"
- << "v." << VERSION << "\n\n"
- << "[Start with argument '-r' to reset highscore.]\n\n"
- << "Move: " << moves + 1 << "\n"
- << "Highscore: ";
- if (highscore == -1)
- cout << "not set yet\n\n";
- else
- cout << highscore << "\n\n";
- cout << " +-------+\n";
- for (int i = 0; i < 3; i++) {
- cout << " | ";
- for (int j = 0; j < 3; j++) {
- if (f[i][j] == 0)
- cout << " ";
- else
- cout << f[i][j] << " ";
- }
- cout << "|\n";
- }
- cout << " +-------+\n";
- }
- // get position of number in field
- void get_pos(int n, int *pos) {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (f[i][j] == n) {
- pos[0] = i;
- pos[1] = j;
- return;
- }
- }
- }
- }
- // check if finish sequenze is reached
- bool is_finished() {
- int last = -1;
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 3; j++) {
- if (f[i][j] > last)
- last = f[i][j];
- else
- return false;
- }
- return (f[2][0] = 7 && f[2][1] == 8 && f[2][2] == 0);
- }
- }
- // getting highscore from file
- // if file is not existent, it returns -1
- // (= not set)
- int get_highscore() {
- ifstream fr(HS_FILENAME);
- int out = -1;
- if (fr.good())
- fr >> out;
- return out;
- }
- // write highscore to file
- void set_highscore(int m) {
- ofstream fw(HS_FILENAME);
- if (fw.good()) {
- fw << m;
- fw.close();
- }
- }
- // reset highscore by writing -1
- // (= not set) to the high score file
- void reset_highscore() {
- set_highscore(-1);
- }
- // just a function to check if an argument is
- // set as start argument
- bool arg_exists(char** begin, char** end, const string& option)
- {
- return find(begin, end, option) != end;
- }
- int main(int argc, char *argv[]) {
- // checking for debug and reset start argument
- if (arg_exists(argv, argv + argc, "-d"))
- debug_mode = 1;
- if (arg_exists(argv, argv + argc, "-r"))
- reset_highscore();
- // get highscore
- highscore = get_highscore();
- int i = 0;
- // filling field with numbers randomly
- // if debug_mode is set, it will be filled
- // with a prepared structure which can be solved
- // with only one move
- if (!debug_mode) {
- while (i < 9) {
- int numbs[] = { 1,2,3,4,5,6,7,8,0 };
- int c_r[] = { rand() % 3, rand() % 3 };
- int *e = &f[c_r[0]][c_r[1]];
- if (*e == 0) {
- *e = numbs[i++];
- }
- }
- }
- else {
- int count = 1;
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < 3; j++)
- f[i][j] = count++;
- f[2][0] = 7;
- f[2][1] = 0;
- f[2][2] = 8;
- }
- int inpt;
- do {
- print_field();
- cout << "\nEnter number (0 for exit) > ";
- cin >> inpt;
- // check if input is in bounds
- // else: ignore input
- if (inpt > 8 || inpt < 0)
- continue;
- int pos[2], free[2];
- // get grid position of input number
- // and free slot (0)
- get_pos(inpt, pos);
- get_pos(0, free);
- // calculate difference between the two points
- float diff = sqrt(
- (free[0] - pos[0]) * (free[0] - pos[0]) +
- (free[1] - pos[1]) * (free[1] - pos[1])
- );
- // If the difference is more than 1,
- // ignore input
- if (diff > 1)
- continue;
- // swap value of input position with
- // value of empty (0) position and
- // count up moves after
- int tmp = f[pos[0]][pos[1]];
- f[pos[0]][pos[1]] = f[free[0]][free[1]];
- f[free[0]][free[1]] = tmp;
- moves++;
- }
- // cancel with input 0 or if finished
- while (inpt != 0 && !is_finished());
- print_field();
- // just a check to dont print won text if
- // canceled with 0-input
- // else print won text, moves and save as
- // high score, if moves are less then the set
- // high score or if high score was not set before
- if (inpt != 0) {
- cout << "\nWon!\nCount of moves: " << moves << endl;
- if (moves < highscore || highscore == -1) {
- set_highscore(moves);
- cout << "Congratulation! You set a new highscore!\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement