Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. void Puzzle15::creatRandom15Puzzle(){
  2.     int possibleNumbers[4][4] = {
  3.             {0, 1, 2, 3},
  4.             {4, 5, 6, 7},
  5.             {8, 9, 10, 11},
  6.             {12, 13, 14, 15}
  7.         };
  8.     int puzzleGenerated[4][4] = {};
  9.     int randomNum;
  10.     bool validNum;
  11.     bool validPuzzle = false;
  12.     int x = 0;
  13.     int y = 0;
  14.    
  15.     while(y < 4){
  16.         if(y >=4 ){
  17.             break;
  18.         }
  19.         x = 0;
  20.  
  21.         while(x < 4){
  22.             if(y > 3) {
  23.                 break;
  24.             }
  25.             randomNum = rand() % 16 + 0;
  26.             validNum = true;
  27.  
  28.             for(int i=0; i < 4; i++){
  29.                 for(int j=0; j < 4; j++){
  30.                     if(puzzleGenerated[i][j] == randomNum){
  31.                         validNum = false;
  32.                        // cout << "RandomNum: " << randomNum << " puzzleGenerated: " << puzzleGenerated[i][j] << "\n";
  33.                     }
  34.                 }
  35.             }
  36.             if(validNum == true){
  37.                 puzzleGenerated[y][x] = randomNum;
  38.                 x++;
  39.                 cout << "X: " << x << "\n";
  40.             }  
  41.         }
  42.  
  43.         y++;
  44.         cout << "Y: " << y << "\n";
  45.        
  46.         if(y == 4){
  47.             // validPuzzle = isPuzzleSolvable(puzzleGenerated);
  48.             copyPuzzle(puzzleGenerated);
  49.             displayPuzzle();
  50.             break;
  51.             // if(!validPuzzle){
  52.             //     copyPuzzle(puzzleGenerated);
  53.             //     displayPuzzle();
  54.             //     loop = false;
  55.             // }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement