[Programme C ++] Jeu de plaque à pousser numérique (15 puzzles)

Je pense que ce jeu est un test de contenu de réflexion, après tout, il est sur la scène du cerveau le plus puissant.
Cependant, l'écriture de ce programme est également fatigante (principalement il y a un gros bug non trouvé, voir l'analyse plus loin).

programme

//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

Exemple de sortie

1
2

une analyse

  • Permettez-moi d'abord de parler du bogue le plus gênant! L'erreur suivante a été:
    punaise

Ensuite, j'ai constaté que le problème était dans la move()fonction. Après un long moment, j'ai trouvé qu'elle move()était surchargée et que le changement change()était toujours erroné. J'ai travaillé dessus pendant longtemps (plus de deux heures au total). Après avoir examiné diverses situations, je ne savais pas combien après l'avoir examiné. Encore et encore, j'ai soudainement découvert que tout était unsignedcausé. Bien que les coordonnées de vacance ne puissent pas être négatives, mais change()pour déterminer s'il existe une fonction légitime de l'entrée lorsqu'elle est en -1fonctionnement (en fait déplacée vers la droite à droite), car unsignedavec le 0temps, -1elle tournera autour est une très grande valeur , Le jugement doit être établi, il y en aura donc vector subscript out of range.

  • Notez que la sortie de 151 lignes "doit utiliser des caractères d'échappement \.
  • D'autres problèmes sont similaires à ceux du jeu du serpent .

TOUS DROITS RÉSERVÉS © 2020 Teddy van Jerry
Bienvenue à réimprimer, veuillez indiquer la source.


Voir également

Page de navigation de Teddy van Jerry
[Programme C ++] Jeu Tic-Tac-Toe (Humain VS Humain)
[Programme C ++] Jeu Tic-Tac-Toe (Ordinateur humain VS Lv1)
[Programme C ++] Jeu Tic-Tac-Toe (Ordinateur humain VS Lv2)
[Programme C ++ 】 Jeu Tic-Tac-Toe (Ordinateur humain VS Lv3)
[Programme C ++] Jeu Tic-Tac-Toe (Ordinateur humain VS Lv3) (Édition statistique)
[Programme C ++] Jeu Gobang (Humain VS Humain)
[Programme C ++] Jeu de labyrinthe mobile
[C ++ Programme] Nombre aléatoire
[programme C ++] Jeu de serpent
[programme C ++] Jeu 2048
[ programme C ++] Jeu Tic-tac-toe (humain VS humain) (interface graphique EasyX) [programme C ++] Jeu Tic -tac-toe
(ordinateur VS humain Lv3) (Enregistrer la version des statistiques) (Interface graphique EasyX)


Avez-vous trouvé des bugs sur l'image? C'est en fait dans Snake Game , mais je l'ai changé avec 3D Paint .

Je suppose que tu aimes

Origine blog.csdn.net/weixin_50012998/article/details/108446155
conseillé
Classement