I think this game is a test of thinking content, after all, it is on the stage of the most powerful brain.
However, writing this program is also tiring (mainly there is a big bug not found, see the analysis later).
program
//This is a simple 15-puzzle game.
#include <iostream>
#include <vector>
#include <Windows.h>
#include <string>
#include <random>
#include <ctime>
#include <conio.h>
using namespace std;
int sx = 3, sy = 3; // int connot be replaced by unsigned
vector<vector<char>> p = {
{
'1','2','3','4'},
{
'5','6','7','8'},
{
'9','A','B','C'},
{
'D','E','F',' '}};
vector<vector<char>> p0 = p; // the finished state
void change(int n)
{
switch (n)
{
case 1: // up
if (sx + 1 <= 3)
{
p[sx][sy] = p[sx + 1][sy];
p[sx + 1][sy] = ' ';
++sx;
}
break;
case 2: // down
if (sx - 1 >= 0)
{
p[sx][sy] = p[sx - 1][sy];
p[sx - 1][sy] = ' ';
--sx;
}
break;
case 3: // left
if (sy + 1 <= 3)
{
p[sx][sy] = p[sx][sy + 1];
p[sx][sy + 1] = ' ';
++sy;
}
break;
case 4: // right
if (sy - 1 >= 0)
{
p[sx][sy] = p[sx][sy - 1];
p[sx][sy - 1] = ' ';
--sy;
}
break;
default:
break;
}
}
void input(char c1, char c2)
{
c1 = _getch();
c2 = _getch();
switch (c2)
{
case 72: // up
change(1);
break;
case 80: // down
change(2);
break;
case 75: // left
change(3);
break;
case 77: // right
change(4);
break;
default:
break;
}
}
void random_start() // create a random plate
{
default_random_engine random;
vector<int> ivec;
for (int i = 0; i != 400; i++)
ivec.push_back(random() % 4 + 1);
srand((unsigned)time(NULL));
int ran = rand() % 200; // generate a random number ranging from 0 to 199
for (int i = ran; i != ran + 200; i++) // make 200 moves
change(ivec[i]);
if (sx == 0) change(1);
if (sx == 1) change(1);
if (sx == 2) change(1);
if (sy == 0) change(3);
if (sy == 1) change(3);
if (sy == 2) change(3); // move the empty place to p[3][3]
}
void print(vector<vector<char>> q)
{
for (int i = 0; i != 4; i++)
{
for (int j = 0; j != 4; j++)
cout << "|" << q[i][j];
cout << "|" << endl;
}
}
int main()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
// The five lines above are used to hide the Console Cursor.
cout << "This is a simple 15-puzzle game.\nProgrammer:Teddy van Jerry" << endl;
random_start();
cout << "\nYou can move the number ranging from 1 to F using ↑,↓,←,→.\n" << endl;
print(p);
cout << "\nNow you have 10 seconds to get ready. After that, we will begin.\n" << endl;
for (int i = 10; i != 0; i--)
{
cout << i;
Sleep(1000); // wait for a second;
cout << " ";
}
cout << "Go!\n" << endl;
long time_start = clock(); // the starting point
unsigned cnt = 0;
while (p != p0) // when it's unfinshed
{
char a1 = '\0', a2 = '\0';
input(a1, a2);
print(p);
cout << "\n" << endl;
++cnt;
}
long total_time = clock() - time_start;
int min = total_time / 60000;
int second = (total_time % 60000) / 1000;
string zero = (second < 10) ? "0" : "";
cout << "You made it!" << endl;
cout << "You made " << cnt << " moves. ";
cout << "The total time is " << min << "'" << zero << second << "\"." << endl;
cout << "\nALL RIGHTS RESERVED (c) 2020 Teddy van Jerry" << endl;
char anything;
cin >> anything;
return 0;
}
//Copyright :2020 Teddy van Jerry
Sample output
analysis
- Let me talk about the most troublesome bug first! The following error has been:
Then I found that the problem was in the move()
function. After a long time, I found that it move()
was overloaded, and the change change()
was still wrong. I worked on it for a long time (more than two hours in total). After considering various situations, I didn’t know how much after I reviewed it. Over and over again, I suddenly discovered that everything was unsigned
caused. Although the vacancy coordinates can not be negative, but change()
to determine whether there is a legitimate function of the input when in -1
operation (in fact moved to the right on the right) because unsigned
in 0
time -1
it will turn up round is very big value , The judgment must be established, so there will be vector subscript out of range
.
- Note that the 151 line output
"
must use escape characters\
. - Other problems are similar to those in the snake game .
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
welcome to reprint, please indicate the source.
See also
Teddy van Jerry’s navigation page
[C++ Program] Tic-Tac-Toe Game (Human VS Human)
[C++ Program] Tic-Tac-Toe Game (Human VS Lv1 Computer)
[C++ Program] Tic-Tac-Toe Game (Human VS Lv2 Computer)
[C++ Program 】 Tic-Tac-Toe Game (Human VS Lv3 Computer)
[C++ Program] Tic-Tac-Toe Game (Human VS Lv3 Computer) (Statistics Edition)
[C++ Program] Gobang Game (Human VS Human)
[C++ Program] Moving Maze Game
[C++ Program] Random number
[C++ program] Snake game
[C++ program] 2048 game
[C++ program] Tic-tac-toe game (human VS human) (EasyX graphical interface) [C++ program] Tic- tac-toe
game (human VS Lv3 computer) (Record statistics version) (EasyX graphical interface)
Have you found any bugs in the picture? This is actually in Snake Game , but I changed it with 3D Paint .