Advertisement
Guest User

Untitled

a guest
Jan 29th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <time.h>
  2. #include <vector>
  3. #include <string>
  4. #include <iostream>
  5. #include <stdlib.h>
  6. class p15 {
  7. public :
  8.     void play() {
  9.         bool p = true;
  10.         std::string a;
  11.         while( p ) {
  12.             createBrd();
  13.             while( !isDone() ) { drawBrd();getMove(); }
  14.             drawBrd();
  15.             std::cout << "\n\nCongratulations!\nPlay again (Y/N)?";
  16.             std::cin >> a; if( a != "Y" && a != "y" ) break;
  17.         }
  18.     }
  19. private:
  20.     void createBrd() {
  21.         int i = 1; std::vector<int> v;
  22.         for( ; i < 16; i++ ) { brd[i - 1] = i; }
  23.         brd[15] = 0; x = y = 3;
  24.         for( i = 0; i < 1000; i++ ) {
  25.             getCandidates( v );
  26.             move( v[rand() % v.size()] );
  27.             v.clear();
  28.         }
  29.     }
  30.     void move( int d ) {
  31.         int t = x + y * 4;
  32.         switch( d ) {
  33.             case 1: y--; break;
  34.             case 2: x++; break;
  35.             case 4: y++; break;
  36.             case 8: x--;
  37.         }
  38.         brd[t] = brd[x + y * 4];
  39.         brd[x + y * 4] = 0;
  40.     }
  41.     void getCandidates( std::vector<int>& v ) {
  42.         if( x < 3 ) v.push_back( 2 ); if( x > 0 ) v.push_back( 8 );
  43.         if( y < 3 ) v.push_back( 4 ); if( y > 0 ) v.push_back( 1 );
  44.     }
  45.     void drawBrd() {
  46.         int r; std::cout << "\n\n";
  47.         for( int y = 0; y < 4; y++ ) {
  48.             std::cout << "+----+----+----+----+\n";
  49.             for( int x = 0; x < 4; x++ ) {
  50.                 r = brd[x + y * 4];
  51.                 std::cout << "| ";
  52.                 if( r < 10 ) std::cout << " ";
  53.                 if( !r ) std::cout << "  ";
  54.                 else std::cout << r << " ";
  55.             }
  56.             std::cout << "|\n";
  57.         }
  58.         std::cout << "+----+----+----+----+\n";
  59.     }
  60.     void getMove() {
  61.         std::vector<int> v; getCandidates( v );
  62.         std::vector<int> p; getTiles( p, v ); unsigned int i;
  63.         while( true ) {
  64.             std::cout << "\nPossible moves: ";
  65.             for( i = 0; i < p.size(); i++ ) std::cout << p[i] << " ";
  66.             int z; std::cin >> z;
  67.             for( i = 0; i < p.size(); i++ )
  68.                 if( z == p[i] ) { move( v[i] ); return; }
  69.         }
  70.     }
  71.     void getTiles( std::vector<int>& p, std::vector<int>& v ) {
  72.         for( unsigned int t = 0; t < v.size(); t++ ) {
  73.             int xx = x, yy = y;
  74.             switch( v[t] ) {
  75.                 case 1: yy--; break;
  76.                 case 2: xx++; break;
  77.                 case 4: yy++; break;
  78.                 case 8: xx--;
  79.             }
  80.             p.push_back( brd[xx + yy * 4] );
  81.         }
  82.     }
  83.     bool isDone() {
  84.         for( int i = 0; i < 15; i++ ) {
  85.             if( brd[i] != i + 1 ) return false;
  86.         }
  87.         return true;
  88.     }
  89.     int brd[16], x, y;
  90. };
  91. int main( int argc, char* argv[] ) {
  92.     srand( ( unsigned )time( 0 ) );
  93.     p15 p; p.play(); return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement