Moving the hole
-
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; }
-
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; }
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 needphole
at all. In addition, you don't needtar_row
andtar_col
either. Your switch statement can simply increment or decrementhrow
and/orhcol
directly. When you're finished, just convert these values back intoholeidx
. 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 toholeidx
. But this table would have to be built at run-time, since the value ofsize
isn't known until then. Bob Ciora -- modified at 18:04 Friday 27th January, 2006