Sign in to follow this  
Followers 0
quick_sliver007

How do I rotate an array.

5 posts in this topic

Here is the array

Dim $label[5]
$label[0] = GUICtrlCreateLabel("0", 200, 200, 10, 10)
$label[1] = GUICtrlCreateLabel("1", 200, 210, 10, 10)
$label[2] = GUICtrlCreateLabel("2", 200, 220, 10, 10)
$label[3] = GUICtrlCreateLabel("3", 200, 230, 10, 10)
$label[4] = GUICtrlCreateLabel("4", 200, 240, 10, 10)

First I want to move the the last array with unbound because the array will grow later on. This is why i need to rotate the array.

$pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] - 10)

This the result of moving the last array on top of the first array.

$label[4] = GUICtrlCreateLabel("4", 200, 190, 10, 10); take note of the 190
$label[0] = GUICtrlCreateLabel("0", 200, 200, 10, 10)
$label[1] = GUICtrlCreateLabel("1", 200, 210, 10, 10)
$label[2] = GUICtrlCreateLabel("2", 200, 220, 10, 10)
$label[3] = GUICtrlCreateLabel("3", 200, 230, 10, 10)

I want to take that and turn it to in this, this where I need the help most.

$label[0] = GUICtrlCreateLabel("4", 200, 190, 10, 10); take note of the 190
$label[1] = GUICtrlCreateLabel("0", 200, 200, 10, 10)
$label[2] = GUICtrlCreateLabel("1", 200, 210, 10, 10)
$label[3] = GUICtrlCreateLabel("2", 200, 220, 10, 10)
$label[4] = GUICtrlCreateLabel("3", 200, 230, 10, 10)

I tried this and had no luck..

Func up()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] - 10)
        $a = UBound($label) - 1
        $b = 0
        Do
            _ArraySwap($label[$a], $label[$b])
            $a = $a - 1
            $b = $b + 1
        Until $a = 0
        Sleep(1000)
    WEnd
EndFunc  ;==>up

What I tried is not working. I been tring and tring but for some reason It is not coming to me. Can anyone help me with this? If So you have my thanks.

P.S. if it helps any this is suppose to be the movement of the snake in a that classic snake game.

Well while I am at it. maybe this will help too.

//---------------------------------------------------------------------------
// SNAKE
// revised 27.05.2002
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstopFlag

#include "Unit1.h"
#include <stdlib.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}

// ------------------------------------------------- your program begins here
bool stopFlag=false;
bool stepFlag=false;
bool pause=false;
bool ateFood = false;
// the world
int world[25][25];
// these take care of the toroidal world
int east, south, relativeE, relativeS, wrappedE, wrappedS;
// the positions of each segment in the snake
int snakeE[650], snakeS[650];
int snakeHeadE, snakeHeadS;
int snakeTailE, snakeTailS;
int snakeLength;
int segment;
int direction, lastDirection, reverseDirection;
int span;
int maxSize = 25;
int iterations = 0;




//------------------------------------------------------------------- Render
void render(void) {
   for (east=0; east<maxSize; east++) {
      for (south=0; south<maxSize; south++) {
         switch (world[east][south]) {
            case 0:
               Form1->PaintBox1->Canvas->Pen->Color=clRed;
               Form1->PaintBox1->Canvas->Brush->Color=clRed;
               Form1->PaintBox1->Canvas->Rectangle
                  (east*20,south*20,east*20+20,south*20+20);
               break;
            case 1:
               Form1->PaintBox1->Canvas->Pen->Color=clLime;
               Form1->PaintBox1->Canvas->Brush->Color=clLime;
               Form1->PaintBox1->Canvas->Rectangle
                  (east*20,south*20,east*20+20,south*20+20);
               break;
            case 2:
               Form1->PaintBox1->Canvas->Pen->Color=clBlue;
               Form1->PaintBox1->Canvas->Brush->Color=clBlue;
               Form1->PaintBox1->Canvas->Rectangle
                  (east*20,south*20,east*20+20,south*20+20);
               break;
            case 3:
               Form1->PaintBox1->Canvas->Pen->Color=clFuchsia;
               Form1->PaintBox1->Canvas->Brush->Color=clFuchsia;
               Form1->PaintBox1->Canvas->Rectangle
                  (east*20,south*20,east*20+20,south*20+20);
               break;
         }
      }
   }
}

//---------------------------------------------------------------- Game Over
void gameOver (void) {
   Form1->Timer1->Enabled = false;
   Form1->Color = clBlack;
   render();
   Application->ProcessMessages();
}

//----------------------------------------------------------------- Clear All
void clearAll (void) {
   for (east=0; east<maxSize; east++) {
      for (south=0; south<maxSize; south++) {
         world[east][south]=false;
      }
   }
      for (int i = 0; i < 650; i++) {
         snakeE[i] = -1;
         snakeS[i] = -1;
      }
   render();      
}

//---------------------------------------------------------- make random food
void makeRandomFood (void) {
   // make 30 random food
   for (int i = 0; i < 30; i++) {
      world[random(25)][random(25)] = 2;
   }
}

//-------------------------------------------------------- make random poison
void makeRandomPoison (void) {
   // make 20 random poison
   for (int i = 0; i < 20; i++) {
      world[random(25)][random(25)] = 3;
   }
}

//--------------------------------------------------------- make random snake
// always start snake horizontal
// turned up or down, away from the edges
void makeRandomSnake (void) {
   Randomize();
   // begin with direction either up or down
   lastDirection = 1;
   int flip = random(2);
   switch (flip) {
      case 0:
         direction = 0;
         break;
      case 1:
         direction = 2;
         break;
   }
   // begin with random head position
   snakeHeadE = 1 + random(15);
   snakeHeadS = 1 + random(23);
   // begin with random snake length
   snakeLength = 3 + random(7);
   // lay out the snake horizontally
   for (int span = 0; span < snakeLength; span++) {
      world[snakeHeadE + span][snakeHeadS] = true;
      snakeE[span] = snakeHeadE + span;
      snakeS[span] = snakeHeadS;
   }
   snakeTailE = snakeHeadE + snakeLength - 1;
   snakeTailS = snakeHeadS;
   Form1->Timer1->Enabled = true;
}

//---------------------------------------------------------------------- STEP
void step (void) {
   Application->ProcessMessages();
   stepFlag=true;
   pause=false;
   // cannot fold back on yourself
   reverseDirection = direction + 2;
   if (reverseDirection > 3) reverseDirection = reverseDirection - 4;
   if (lastDirection == reverseDirection) direction = lastDirection;
   // figure out where new head will be
      switch (direction) {
         case 0: // up
            snakeHeadS--;
            if (snakeHeadS < 0) snakeHeadS = 24;
            break;
         case 1: // right
            snakeHeadE++;
            if (snakeHeadE > 24) snakeHeadE = 0;
            break;
         case 2: // down
            snakeHeadS++;
            if (snakeHeadS > 24) snakeHeadS = 0;
            break;
         case 3: // left
            snakeHeadE--;
            if (snakeHeadE  <0) snakeHeadE = 24;
            break;
      }
   // if you eat poison or eat yourself then the game is over
   if (world[snakeHeadE][snakeHeadS] == 3) gameOver();
   if (world[snakeHeadE][snakeHeadS] == 1) gameOver();   
   // if there is food there remember to add to length
   if (world[snakeHeadE][snakeHeadS] == 2) ateFood = true;
   world[snakeHeadE][snakeHeadS] = true;
   // move each segment's E and S position down the shake length
   for (segment = snakeLength + 1; segment > 0; segment --) {
      snakeE[segment] = snakeE[segment - 1];
      snakeS[segment] = snakeS[segment - 1];
   }
   // and the snake head at the front
   snakeE[segment] = snakeHeadE;
   snakeS[segment] = snakeHeadS;
   // remove the snake's last tail position if it hasn't eaten
   if (!ateFood) {
      snakeTailE = snakeE[snakeLength ];
      snakeTailS = snakeS[snakeLength ];
   }
   if (ateFood) {
      snakeLength++;
      ateFood = false;
   }
   lastDirection = direction;  // snake cannot double back on self
   world[snakeTailE][snakeTailS] = false;
   render();
   // show some statistics
   Form1->EditSnakeHeadEast->Text = snakeHeadE;
   Form1->EditSnakeHeadSouth->Text = snakeHeadS;
   Form1->EditSnakeTailEast->Text = snakeE[snakeLength-1];
   Form1->EditSnakeTailSouth->Text = snakeS[snakeLength-1];
   Form1->EditLength->Text = snakeLength;
}

//-------------------------------------------------------------- Begin Button
void __fastcall TForm1::ButtonbeginClick(TObject *Sender)
{
   Form1->Color = clAqua;
   clearAll();
   makeRandomFood();
   makeRandomPoison();
   makeRandomSnake();
   render();
}

//--------------------------------------------------------------- Step Button
void __fastcall TForm1::ButtonStepClick(TObject *Sender)
{
   step();
}

//-------------------------------------------------------------- Pause Button
void __fastcall TForm1::ButtonpauseClick(TObject *Sender)
{
   Timer1->Enabled = !Timer1->Enabled;
   if (Timer1->Enabled == true) {
      Buttonpause->Caption = "Pause";
      PanelPause->Color = clRed;
      }
   else {
      Buttonpause->Caption = "Resume";
      PanelPause->Color = clLime;
   }   
}

//------------------------------------------------------- PaintBox Mouse Down
void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   east = X / 20;
   if(east>=maxSize) goto End;
   if(east<0) goto End;
   snakeHeadS = Y / 20;
   if(snakeHeadS>=maxSize) goto End;
   if(snakeHeadS<0) goto End;
   world[east][snakeHeadS]=!world[east][snakeHeadS];
   switch (world[east][snakeHeadS]) {
      case 0:
         Form1->PaintBox1->Canvas->Pen->Color=clRed;
         Form1->PaintBox1->Canvas->Brush->Color=clRed;
         Form1->PaintBox1->Canvas->Rectangle
            (east*20,snakeHeadS*20,east*20+20,snakeHeadS*20+20);
         break;
      case 1:
         Form1->PaintBox1->Canvas->Pen->Color=clLime;
         Form1->PaintBox1->Canvas->Brush->Color=clLime;
         Form1->PaintBox1->Canvas->Rectangle
            (east*20,snakeHeadS*20,east*20+20,snakeHeadS*20+20);
         break;
   }
   End:
}

//---------------------------------------------------- NORTH panel mouse move
void __fastcall TForm1::PanelNorthMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
   direction = 0;
}

//----------------------------------------------------- EAST panel mouse move
void __fastcall TForm1::PanelEastMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
   direction = 1;
}

//----------------------------------------------------- SOUTH panel mouse move
void __fastcall TForm1::PanelSouthMouseMove(TObject *Sender,
   TShiftState Shift, int X, int Y)
{
   direction = 2;
}

//----------------------------------------------------- WEST panel mouse move
void __fastcall TForm1::PanelWestMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
   direction = 3;   
}

//------------------------------------------------------------------ On Timer
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
   step();
}

//--------------------------------------------------- Paint Box On Mouse Down
void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
   EditCursorEast->Text = X / 20;
   EditCursorSouth->Text = Y / 20;
}

//---------------------------------------------------- Turn L or R Mouse Down
void __fastcall TForm1::PanelMouseDownTopMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   switch (Button) {
      case 0:
         direction = direction - 1;
         if (direction == -1) direction = 3;
         break;
      case 1:
         direction = direction + 1;
         if (direction == 4) direction = 0;
         break;
   }
}
//---------------------------------------------------------------------------
That is the game in VB..

.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Haha, how's this:

#CS
_ArrayRotate( $aArray [, $iDir [, $iSteps ] ] )
By Rob Saunders
Params:
     $aArray   The Array to rotate.
     $iDir   The direction to rotate.
               True (not 0) steps forward.
               False (0) steps backward.
     $iSteps   How many steps forward/backward
               to rotate the items.
#CE

Func _ArrayRotate(ByRef $aArray, $iDir = 0, $iSteps = 1)
    Local $iLen = UBound($aArray) - 1
    Local $vStore
    Local $iSC; step counter
    Local $iAC; array counter
    For $iSC = 1 To $iSteps
        If $iDir Then
            $sStore = $aArray[0]
            For $iAC = 0 To $iLen
                If $iAC < $iLen Then
                    $aArray[$iAC] = $aArray[$iAC + 1]
                Else
                    $aArray[$iAC] = $sStore
                EndIf
            Next
        Else
            $sStore = $aArray[$iLen]
            For $iAC = $iLen To 0 Step -1
                If $iAC > 0 Then
                    $aArray[$iAC] = $aArray[$iAC - 1]
                Else
                    $aArray[$iAC] = $sStore
                EndIf
            Next
        EndIf
    Next
EndFunc
Edited by Saunders

Share this post


Link to post
Share on other sites

Haha, how's this:

CODE
#CS

_ArrayRotate( $aArray [, $iDir [, $iSteps ] ] )

By Rob Saunders

Params:

    $aArray  The Array to rotate.

    $iDir    The direction to rotate.

              True (not 0) steps forward.

              False (0) steps backward.

    $iSteps  How many steps forward/backward

              to rotate the items.

#CE

Func _ArrayRotate(ByRef $aArray, $iDir = 0, $iSteps = 1)

Local $iLen = UBound($aArray) - 1

Local $vStore

Local $iSC ; step counter

Local $iAC ; array counter

For $iSC = 1 To $iSteps

  If $iDir Then

  $sStore = $aArray[0]

  For $iAC = 0 To $iLen

    If $iAC < $iLen Then

    $aArray[$iAC] = $aArray[$iAC + 1]

    Else

    $aArray[$iAC] = $sStore

    EndIf

  Next

  Else

  $sStore = $aArray[$iLen]

  For $iAC = $iLen To 0 Step -1

    If $iAC > 0 Then

    $aArray[$iAC] = $aArray[$iAC - 1]

    Else

    $aArray[$iAC] = $sStore

    EndIf

  Next

  EndIf

Next

EndFunc

<{POST_SNAPBACK}>

could you repost that with out ths scroll bars, because the scroll bars causes it to be one line when I copy and past.

.

Share this post


Link to post
Share on other sites

Really??? How odd... I'll just edit my first post. Sorry about that.

Share this post


Link to post
Share on other sites

Really??? How odd... I'll just edit my first post. Sorry about that.

<{POST_SNAPBACK}>

Thank you. I believe this will do, well at least I hope so. Thank you again.

.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0