PDA

View Full Version : C++ coding help


Zechariah
15th October 2009, 22:06
I have a friend who needs help on the assignment. Would apprecite the help, he is not a member here!

His question:

Its a coding game...

That game includes sorting of numbers from one to nine in a 2D array of size 4 x 3 with one space and last element that should be constant and can't be used. Only space can be swapped up, left or right but can't swap left in 0 column and can't swap up in0 row.. and can't swap down in 3 row and can't swap right on 2 column. Only space should be moved and the game should end only when numbers are sorted or a sentinel Value is passed .. that is Q .. else W A S D should be used to move the space Up, left, Down, Right respectively

how to put a check of sorting in 2D array? In Ascending Order

mindless slogging
15th October 2009, 22:10
Are you, by any chance, applying the word 'assignment' loosely in the sentence?

zimmz
15th October 2009, 22:19
This could be similar to a game called 8-puzzle, often part of the university courses for AI.

www.8puzzle.com

http://en.wikipedia.org/wiki/Fifteen_puzzle

I doubt if I can find the code I wrote several years ago for above game but it should not be difficult and your friend should try it himself. By copying code he is not gonna learn programming.

Zechariah
15th October 2009, 22:25
Thanks Zimmz, I forward that to him.

He was just asking "if he can tell me how to put a sorting check on 2D array"

zimmz
15th October 2009, 23:17
Not sure what "sorting check on 2D array" means in the context of game he explained (not so clearly). If he can graphically explained the game (couple of steps) then it might be easier to give some hints. From the text in OP I understand that there are 9 elements (1-9) stored in the array of size 12 (4x3). But then what he meant by only one space? and also last element which is constant? In 4x3 array there will be two empty spaces assuming that the last element which is stored permanently at position [3][2] and is any thing but 1-9.

Check with him if he can describe the problem in a clear (graphical) way.

12thMan
15th October 2009, 23:33
It is probably one dimensional array. You can imagine it to be 2 or 3 arrays. I don't understand the question - there are 12 entries (4 x 3 metrix/array) - there are 9 numbers, a space and a constant (or a block???) that is 11 entries and one is missing. And what kind of sorting is needed

Well he just has to make sure that the space doesn't pass certain numbers when a user moves it. He can either do calculations or arrange the array in certain way like
123456789_x <-- this is a 4 x 3 array but one dimesional and what someone sees doesn't matter and can be shown as
1234
4567
89_x
you keep track of the space. The rest I am just typing here without any test or much thinking.
If there is a move up then you check if (array width - current position) might be (array width - current postion + current position div array width) is correct in limits then move all the number down by one. If it moves left then array width - current postion is not array width - current postion div array width + current position mod array width. The number have to be adjusted and fall in certain range. Add all these checks for up down left right. Formula needs checking. Best to draw on paper and do some calculations. After every legitimate move make a copy of the array starting at number 1 (if number 1 is at #3 index that will be at #1 in new array. fill the array and see if it is sorted.

From what I see you don''t solve the puzzle. User does and you just make sure space goes in valid block and numbers are sorted somehow. the sorting I don't understand but it might be a formula where you fill up the array with sequential numbers and see if they are sorted

What zimmz and I have posted should be enough hints for your friend to start. He should post the code if he needs more help or work with someone at school so thoes two can learn together. Otherwise it is no help for him to learn. These are the only fun things in classes at school

zimmz
16th October 2009, 00:04
Actually the conditions in OP won't allow to move the space any where and no sorting would be possible i.e game is stuck ;).

Here is the depiction of OP's description (S for space, X for constant element):

4 rows X 3 columns
------------
| 1 | 2 | 3 |
------------
| 4 | S | 5 |
------------
| 6 | S | 7 |
------------
| 8 | 9 | X |
------------