需要帮助了解C++中"15 Puzzle Game"的某些部分
Need help understanding some parts of "15 Puzzle Game" in C++
我正在阅读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();
调用一次?
三.代码中数字1
、2
、4
和8
(用于开关块(的意义是什么?
D.getCandidates()
功能如何工作?
A.棋盘正在洗牌,所以 1000 意味着"很多,但不是那么多,你真的必须开始等待棋盘被洗牌">
二. getCandidates()
通过在向量中填充候选项来返回候选项,v.clear()
为新候选项重置矢量。
三.这里的1,2,4,8只是表示向上,向下,向左,向右(不是按特定顺序(。 这是由getCandidate()
填写的。这些特定值似乎没有理由,它们可以用作字节中的位,但这里似乎并非如此。
D. getCandidate()
根据空白点是否靠近哪个边框在矢量中向上、向下、向左、向右填充。
相关文章:
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 如何将部分流作为参数传递
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- 我能做些什么来跟踪矢量元素?
- 如何删除部分类?
- 我可以做些什么来改进指针向量中的此搜索?
- 使用 c++/WinRT Direct3D UWP Game DR 模板 Direct X11 的错误消息
- 我需要做些什么才能使用C++构建茧
- 如何只链接到柯南包中的部分库?
- 无法打印用户填充的部分数组的反面
- 使用类型特征的部分类专用化
- 错误 LNK2001:未解析的外部符号"private: static class Game Game::game_"
- 我可以做些什么来提高内存搜索速度?
- 将 CRTP 与部分类专用化结合使用?
- 关于函数模板中定义的 lambda 闭包类型可以说些什么?
- 为什么 C++17 标准没有带来部分类模板参数扣除?
- 在本书的示例代码中"introduction to 3d game programming with directx 11"
- C++17 中的部分类模板参数推导
- "Color Game" - 从数组 C++ 调用随机
- 是否可以保证输入<Type>实数时复杂对象的虚部设置为零?