You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Goal: In this step, we’ll finish our game. Randomize the initial state of the game board. Also, detect when user wins, so after that we can print a win message and quit the game.
5
+
6
+
We need to be careful about how we randomize our puzzle, because not every puzzle is solvable. For example, there is no way to solve this puzzle:
7
+
8
+
1 2 3 4
9
+
5 6 7 8
10
+
9 10 11 12
11
+
13 15 14
12
+
13
+
If we just blindly randomize the numbers in the puzzle, there is a chance that we will generate such an unsolvable puzzle. With a physical version of the puzzle, we’d randomize the puzzle by sliding tiles in random directions until the tiles were sufficiently mixed. The solution for such a randomized puzzle is to slide each tile in the opposite direction that it was slid to randomize it in the first place. Thus, randomizing puzzles this way always generates a solvable puzzle.
14
+
15
+
We can have our program randomize the board in the same way.
16
+
17
+
Once the user has solved the puzzle, the program should print "\n\nYou won!\n\n" and then exit normally.
18
+
19
+
*/
20
+
21
+
#include<iostream>
22
+
#include"user_input.h"
23
+
#include"board.h"
24
+
25
+
26
+
intmain()
27
+
{
28
+
std::cout << "Welcome to Puzzle 15!\n";
29
+
30
+
Board winning_board{};
31
+
32
+
Board board{};
33
+
board.randomise();
34
+
std::cout << board;
35
+
36
+
char ch{};
37
+
38
+
while(true)
39
+
{
40
+
std::cout << "\nGive me a direction with the commands '" << Konstants::up << "' '" << Konstants::right << "' '" << Konstants::down <<"' '" << Konstants::left << "' to move the empty tile around!\n";
0 commit comments