comp.lang.ada
 help / color / mirror / Atom feed
From: Austin Obyrne <austin.obyrne@hotmail.com>
Subject: Re: How to shuffle/jumble letters of a given word?
Date: Mon, 29 Jun 2015 13:21:29 -0700 (PDT)
Date: 2015-06-29T13:21:29-07:00	[thread overview]
Message-ID: <1fad5a33-a5c2-45ae-95c0-74c90f00157f@googlegroups.com> (raw)
In-Reply-To: <24d183ce-77b6-4fe1-a95e-0ff5ef72c7bd@googlegroups.com>

On Sunday, June 28, 2015 at 3:06:29 PM UTC+1, Trish Cayetano wrote:
> I am creating a "text twist game" where you guess the words in a jumbled manner. How do I set the code to shuffle the letters? 
> 
> Example: word to be guessed is HELLO
> 
> What should be displayed is: LOLEH or LELHO
> 
> Thanks in advance!

Hi Trish,

This may not be sophisticated enough for what you want but it works.
Try it with the word "Milestone"
Note: You must set the 'wordlength' and the scrambling parameters ('Step' and 'Repeats') to suit - *the product of those must not exceed the 'Wordlength' other wise they will fall off the end of the arrays.

I can do better if you really want to include sentences or groups of letters as words also.

Cheers - Austin

With Ada.Text_IO;
With Ada.Integer_Text_IO;
Procedure Scramble_Letters is
-------------------------------------------------------------------------
--| The program reads in a prescribed word of declared length 
--| from the key board stores the characters and scrambles them.
------------------------------------------------------------------------- 
   X    : Integer;
  Total : Integer;
 
  WordLength: Constant Integer:= 9; -- set the wordlength here
  
  Step    : Constant Integer := 4;
  Repeats : Constant Integer := 2;
  -- Step x Repeats =< WordLength --

  SUBTYPE Index_1 IS Positive RANGE 1 .. 100;
  Type ReadInArray IS ARRAY (Index_1) OF Character;
  NextChar: ReadInArray:= (others => ' ');  -- Stores the plaintext 'NextChar' items being read in.

  SUBTYPE Index_2 IS Positive RANGE 1 .. 100000;
  TYPE UnscrambledNextChars IS ARRAY (Index_2) OF Character;
  Alpha :  UnscrambledNextChars := (others => ' '); -- before scrambling

  SUBTYPE Index_3 IS Positive RANGE 1 .. 100000;
  TYPE ScrambledNextChars_Array IS ARRAY (Index_3) OF Character;
  Beta : ScrambledNextChars_Array := (others => ' '); --after scrambling

          ----------------------------------------------------------

  PROCEDURE Load_n_Scramble_Letters IS
  -- Pre : nil
  -- Post: nil

BEGIN  -- Load_n_Scramble_Letters

  FOR I IN 1 .. Total LOOP
   Alpha(I):= NextChar(I);
   Beta (I):= NextChar(I);
  END LOOP;
  Ada.Text_IO.New_Line(2);
  Ada.Text_IO.Put(Item => "                                Before    After ");--
    X := 0;
  FOR Repeater IN 1 .. Repeats LOOP
  FOR Count IN REVERSE X+1 .. X+Step LOOP
    X:=X+1;
  Beta(X) := Alpha(Count);
  Ada.Text_IO.New_Line(1);  --
  Ada.Text_IO.Put(Item => "                                  ");--
  Ada.Text_IO.Put(Item => Alpha(X));  --
  Ada.Text_IO.Put(Item => "         ");--
  Ada.Text_IO.Put(Item => Beta(X));  --
  END LOOP;
    Ada.Text_IO.New_Line;--
    Ada.Text_IO.Put(item => "                                ---------------");--
  END LOOP;
End Load_n_Scramble_Letters;

              ----------------------------------------------------------------------------

BEGIN  -- Scramble_Letters
    Total:=0;
  FOR I in 1 .. WordLength LOOP
    Total := Total +1;
    Ada.Text_IO.Put(Item => "Enter the character number");
    Ada.Integer_Text_IO.Put(Item => I, Width => 2);
    Ada.Text_IO.Put(Item => " > " );
    Ada.Text_IO.Get(Item => NextChar(Total));
  END LOOP;
    Ada.Text_IO.New_Line;
    Ada.Text_IO.Put(Item => " That's it - the word for scrambling is > ");
  FOR I in 1 .. WordLength LOOP
    Ada.Text_IO.Put(Item => NextChar(I));
  END LOOP;
    Load_n_Scramble_Letters;  -- goes to the scrambling procedure from here
END Scramble_Letters;
            
             ----------------------------------------------------------------

  parent reply	other threads:[~2015-06-29 20:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-28 14:06 How to shuffle/jumble letters of a given word? Trish Cayetano
2015-06-28 15:52 ` David Botton
2015-06-28 16:01   ` Anh Vo
2015-06-28 21:07 ` Simon Wright
2015-06-28 21:23   ` Jeffrey R. Carter
2015-06-29  8:05     ` Simon Wright
2015-06-29  8:13       ` Dmitry A. Kazakov
2015-06-29 18:04       ` Jeffrey R. Carter
2015-07-02 20:56         ` Randy Brukardt
2015-06-29 13:42 ` darkestkhan
2015-06-29 20:21 ` Austin Obyrne [this message]
2015-06-29 21:45 ` MM
2015-06-30 12:29 ` Austin Obyrne
2015-07-01 10:40   ` darkestkhan
2015-08-09 21:50     ` David Thompson
2015-08-10 15:20       ` darkestkhan
2015-07-11 15:29 ` Trish Cayetano
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox