[size=2]Example:
8 7 2
4 3 5
6 1
Supposed to end up like this...
1 2 3
4 5 6
7 8
I need help developing a game that puts the numbers in random order in a two dimensional array and then allows the user to arrange the numbers in order to win the game by only moving a number to a empty box.
Comments
:
: 8 7 2
: 4 3 5
: 6 1
:
: Supposed to end up like this...
:
: 1 2 3
: 4 5 6
: 7 8
:
: I need help developing a game that puts the numbers in random order
: in a two dimensional array and then allows the user to arrange the
: numbers in order to win the game by only moving a number to a empty
: box.
This is a start:
[code]
USES Crt, Dos;
CONST
BoxSize = 3;
VAR
BoxInfo : Array[1..BoxSize,1..BoxSize] Of Byte;
BoxDone : Boolean;
Choice : Char;
{ Used for swapping two boxes }
PROCEDURE SwapBox(X1,Y1,X2,Y2 : Byte);
VAR
TempB : Byte;
Begin
TempB := BoxInfo[Y1,X1];
BoxInfo[Y1,X1] := BoxInfo[Y2,X2];
BoxInfo[Y2,X2] := TempB;
End;
{ Inserts the numbers and randomizes them }
PROCEDURE InitializeBox;
VAR
X, Y : Byte;
Begin
{ First off, fill our boxes in the right order }
For Y := 1 to BoxSize Do
For X := 1 to BoxSize Do
BoxInfo[Y,X] := X + (Y-1)*BoxSize;
BoxInfo[Y,X] := 0; { Zero will define our empty box, the last one }
{ Now let's mix them up }
Randomize; { Truely random pattern }
For Y := 1 to BoxSize Do
For X := 1 to BoxSize Do
Begin
{ Select a random box to swap with }
SwapBox(X,Y,Random(BoxSize)+1,Random(BoxSize)+1);
End;
End;
{ Displays the box on the screen }
PROCEDURE ShowBox;
Begin
{ All this does is display the box on the screen in the same
location each time. Look at "GotoXY(WhereX,WhereY);" }
End;
{ Gets the users input and swaps the boxes }
PROCEDURE AllowMove;
Begin
{ Write the code to allow user to move the block }
End;
{ Searches through the boxes and sees if they are all in order }
FUNCTION CheckBox : Boolean;
Begin
{ You need to see if all numbers are in order here }
{ This is here so that the program doesn't go into an endless loop
if run right now }
CheckBox := True;
End;
{ Our main procedure. Very basic & calls oter procedures to do the work }
Begin
Repeat
InitializeBox;
Repeat
ShowBox;
AllowMove;
BoxDone := CheckBox;
Until BoxDone = True;
Write('Play Again [Y/N]?');
Choice := UpCase(ReadKey);
Until Choice = 'N';
WriteLn;
End.
[/code]
This is obviously not the only setup and I'm sure some would think to do it differently, but it is straight forward. All you need to do is fill in the remaining three procedures (SHOWBOX, ALLOWMOVE, CHECKBOX). The first and last should be fairly simple and ALLOWMOVE will be somewhat more complicated. If you need help, just ask. I'm not going to write the rest of the code for you as it won't help you learn, but I'm more than willing to help you if you get stuck.
Phat Nat
: dimensional array and ensure that the random number generator
: doesn't put a duplicate number in the array?
First fill a 1-d array with the numbers. For example:
[code]
BoardSizeX := 4;
BoardSizeY := 3;
for i := 0 to BoardSizeX*BoardSizeY-1 do
TempNumbers[i] := i+1;
[/code]
Then use a "slow" sort algorithm to randomize the numbers:
[code]
{ Bubblesort to randomize }
for i := 0 to BoardSizeX*BoardSizeY-1 do
for j := 0 to BoardSizeX*BoardSizeY-2 do
if rnd() < 0.5 then begin
t := TempNumbers[j];
TempNumbers[j] := TempNumbers[j+1];
TempNumbers[j+1] := t;
end;
[/code]
Finally fill the 2-d array from the 1-d array:
[code]
for i := 0 to BoardSizeX*BoardSizeY-1 do
Numbers[i mod BoardSizeX, i div BoardSizeX] := TempNumbers[i];
[/code]
Note: all arrays are 0-indexed. The TempNumbers must be at least BoardSizeX*BoardSizeY in length (i.e.: TempNumbers[0..BoardSizeX*BoardSizeY]).
The Numbers array must be a 2-D array with at least BoardSizeX by BoardSizeY number of elements (i.e. Numbers[0..BoardSizeX-1, 0..BoardSizeY-1]).