Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Moving the hole

Moving the hole

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasegame-dev
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eivanlo
    wrote on last edited by
    #1

    Like a puzzle game, how can I swap the 'hole' with the 'target element'? I use 'switch' and 'pointer' but failed!! Pls advise!! //Moving the hole. void Move(int& holeidx,int move) { int tar_row; //The row of the target element int tar_col; //The column of the target element int hrow; //The row of the hole int hcol; //The column of the hole int temp=0; //A temporary variable for swapping int number; //The index number of the target element hrow = holeidx / size; //Calculate the row number of the hole hcol = holeidx % size; //Calculate the column number of the hole int* phole = 0; //The pointer of the hole switch(move) { case 49: //(Press 1) phole = &holeidx; tar_row = ++hrow; tar_col = --hcol; break; case 50: //(Press 2) phole = &holeidx; tar_row = ++hrow; tar_col = hcol; break; case 51: //(Press 3) phole = &holeidx; tar_row = ++hrow; tar_col = ++hcol; break; case 52: //(Press 4) phole = &holeidx; tar_row = hrow; tar_col = --hcol; break; case 53: //(Press 5) phole = &holeidx; tar_row = hrow; tar_col = hcol; break; case 54: //(Press 6) phole = &holeidx; tar_row = hrow; tar_col = ++hcol; break; case 55: //(Press 7) phole = &holeidx; tar_row = --hrow; tar_col = --hcol; break; case 56: //(Press 8) phole = &holeidx; tar_row = --hrow; tar_col = hcol; break; case 57: //(Press 9) phole = &holeidx; tar_row = --hrow; tar_col = ++hcol; break; case 113: //(Press q) case 81: //(Press Q) exit(1); //If Press Q to quit, exit the game directly. break; default: break; }

    B 1 Reply Last reply
    0
    • E eivanlo

      Like a puzzle game, how can I swap the 'hole' with the 'target element'? I use 'switch' and 'pointer' but failed!! Pls advise!! //Moving the hole. void Move(int& holeidx,int move) { int tar_row; //The row of the target element int tar_col; //The column of the target element int hrow; //The row of the hole int hcol; //The column of the hole int temp=0; //A temporary variable for swapping int number; //The index number of the target element hrow = holeidx / size; //Calculate the row number of the hole hcol = holeidx % size; //Calculate the column number of the hole int* phole = 0; //The pointer of the hole switch(move) { case 49: //(Press 1) phole = &holeidx; tar_row = ++hrow; tar_col = --hcol; break; case 50: //(Press 2) phole = &holeidx; tar_row = ++hrow; tar_col = hcol; break; case 51: //(Press 3) phole = &holeidx; tar_row = ++hrow; tar_col = ++hcol; break; case 52: //(Press 4) phole = &holeidx; tar_row = hrow; tar_col = --hcol; break; case 53: //(Press 5) phole = &holeidx; tar_row = hrow; tar_col = hcol; break; case 54: //(Press 6) phole = &holeidx; tar_row = hrow; tar_col = ++hcol; break; case 55: //(Press 7) phole = &holeidx; tar_row = --hrow; tar_col = --hcol; break; case 56: //(Press 8) phole = &holeidx; tar_row = --hrow; tar_col = hcol; break; case 57: //(Press 9) phole = &holeidx; tar_row = --hrow; tar_col = ++hcol; break; case 113: //(Press q) case 81: //(Press Q) exit(1); //If Press Q to quit, exit the game directly. break; default: break; }

      B Offline
      B Offline
      Bob Ciora
      wrote on last edited by
      #2

      I'm not sure what you're doing with phole, but it's being assigned the same value in every case of the switch statement. This is inefficient. Most of all, though, you don't need phole at all. In addition, you don't need tar_row and tar_col either. Your switch statement can simply increment or decrement hrow and/or hcol directly. When you're finished, just convert these values back into holeidx. You can stop here. This will solve your problem. Also... You can remove the switch statement altogether and use a lookup table. Translate the '1' through '9' values into an index into this table. Table entries would be simple structs holding the modification to the row and column variables. You only need special traps for the 'q' key. This table could be simplified even further by storing the modifier to holeidx. But this table would have to be built at run-time, since the value of size isn't known until then. Bob Ciora -- modified at 18:04 Friday 27th January, 2006

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups