需要帮助了解C++中"15 Puzzle Game"的某些部分

Need help understanding some parts of "15 Puzzle Game" in C++

本文关键字:Game 些部 Puzzle 了解 帮助 C++      更新时间:2023-10-16

我正在阅读C++中实现的"15 Puzzle Game"的代码(链接(。

法典

#include <time.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iostream>
class p15 {
public :
    void play() {
        bool p = true;
        std::string a;
        while( p ) {
            createBrd();
            while( !isDone() ) { drawBrd();getMove(); }
            drawBrd();
            std::cout << "nnCongratulations!nPlay again (Y/N)?";
            std::cin >> a; if( a != "Y" && a != "y" ) break;
        }
    }
private:
    void createBrd() {
        int i = 1; std::vector<int> v;
        for( ; i < 16; i++ ) { brd[i - 1] = i; }
        brd[15] = 0; x = y = 3;
        for( i = 0; i < 1000; i++ ) {
            getCandidates( v );
            move( v[rand() % v.size()] );
            v.clear();
        }
    }
    void move( int d ) {
        int t = x + y * 4;
        switch( d ) {
            case 1: y--; break;
            case 2: x++; break;
            case 4: y++; break;
            case 8: x--;
        }
        brd[t] = brd[x + y * 4];
        brd[x + y * 4] = 0;
    }
    void getCandidates( std::vector<int>& v ) {
        if( x < 3 ) v.push_back( 2 ); if( x > 0 ) v.push_back( 8 );
        if( y < 3 ) v.push_back( 4 ); if( y > 0 ) v.push_back( 1 );
    }
    void drawBrd() {
        int r; std::cout << "nn";
        for( int y = 0; y < 4; y++ ) {
            std::cout << "+----+----+----+----+n";
            for( int x = 0; x < 4; x++ ) {
                r = brd[x + y * 4];
                std::cout << "| ";
                if( r < 10 ) std::cout << " ";
                if( !r ) std::cout << "  ";
                else std::cout << r << " ";
            }
            std::cout << "|n";
        }
        std::cout << "+----+----+----+----+n";
    }
    void getMove() {
        std::vector<int> v; getCandidates( v );
        std::vector<int> p; getTiles( p, v ); unsigned int i;
        while( true ) {
            std::cout << "nPossible moves: ";
            for( i = 0; i < p.size(); i++ ) std::cout << p[i] << " ";
            int z; std::cin >> z;
            for( i = 0; i < p.size(); i++ )
                if( z == p[i] ) { move( v[i] ); return; }
        }
    }
    void getTiles( std::vector<int>& p, std::vector<int>& v ) {
        for( unsigned int t = 0; t < v.size(); t++ ) {
            int xx = x, yy = y;
            switch( v[t] ) {
                case 1: yy--; break;
                case 2: xx++; break;
                case 4: yy++; break;
                case 8: xx--;
            }
            p.push_back( brd[xx + yy * 4] );
        }
    }
    bool isDone() {
        for( int i = 0; i < 15; i++ ) {
            if( brd[i] != i + 1 ) return false;
        }
        return true;
    }
    int brd[16], x, y;
};
int main( int argc, char* argv[] ) {
    srand( ( unsigned )time( 0 ) );
    p15 p; p.play(); return 0;
}
/*
Possible Output:
+----+----+----+----+
| 11 |  5 | 12 |  3 |
+----+----+----+----+
| 10 |  7 |  6 |  4 |
+----+----+----+----+
| 13 |    |  2 |  1 |
+----+----+----+----+
| 15 | 14 |  8 |  9 |
+----+----+----+----+
Possible moves: 2 13 14 7
*/

虽然我理解了大部分代码,但有些部分对我来说似乎不清楚:

一个。为什么 for 循环迭代 1000 次?为什么选择数字 1000?

for( i = 0; i < 1000; i++ )

二.在上面的循环中,为什么每次迭代v.clear();调用一次?

三.代码中数字1248(用于开关块(的意义是什么?

D.getCandidates()功能如何工作?

A.棋盘正在洗牌,所以 1000 意味着"很多,但不是那么多,你真的必须开始等待棋盘被洗牌">

二. getCandidates()通过在向量中填充候选项来返回候选项,v.clear()为新候选项重置矢量。

三.这里的1,2,4,8只是表示向上,向下,向左,向右(不是按特定顺序(。 这是由getCandidate()填写的。这些特定值似乎没有理由,它们可以用作字节中的位,但这里似乎并非如此。

D. getCandidate()根据空白点是否靠近哪个边框在矢量中向上、向下、向左、向右填充。