Scripting a Code Cracker

Started by Ali, Mon 14/08/2006 15:30:37

Previous topic - Next topic

Ali

Hello everyone,

I've been struggling with this for a few days now and I can't get my head around it. I want to script a Code Cracker that works in the following way. A GUI label called Code displays a message encoded using a subsitution cypher (A=B, C=Q or whatever). Below, 26 GUI buttons with ID numbers 1-26 display the letters A-Z. I the player to be able to re-arrange the buttons, and have the new alphabet they create re-work the lettering in the Code label.

I've created two character arrays, Alphabet1[27] and [/b]Alphabet2[27][/b] for storing old and new versions of the button alphabet in slots 1-26.

I've managed to scipt a button-swapping script, but only using outdated code. I've added strings First and Second to store the labels of the clicked buttons and integers FirstID and SecondID to store the ID.

My idea was to alter the array Alphabet2 by replacing the character at FirstID with the first character the string Second and vice versa. Then the script should go through the GUI label checking each letter based on Alphabet1 and changing it to the corresponding character in Alphabet2. Afterwards, Alphabet 1 has its characters replaced by Alphabet2.

This doesn't work properly and I'm too dim to work out why. Is there perhaps a better way of approaching this. I'd be grateful for any input.

For reference, here is an example of the on-click code for the A button.

Code: ags

if (FirstID==0){ //If is the first button clicked store its ID, loc & text in the appropriate variables
Ã,  FirstID=A.ID;
Ã,  FirstXpos=A.X;
Ã,  First=A.Text;
}
else if (FirstID!=0){ //If is the second button clicked store its ID, loc & text in the appropriate variables
Ã,  if (FirstID!=SecondID){
Ã,  SecondID=A.ID;
Ã,  SecondXpos=A.X;
Ã,  Second=A.Text;
Ã,  SetGUIObjectPosition (0, FirstID, SecondXpos, A.Y);
Ã,  SetGUIObjectPosition (0, SecondID, FirstXpos, A.Y);//Swap the first and second button positions
Ã,  Display ("Clicked %c then %c",Ã,  First.Chars[0],Ã,  Second.Chars[0]);
Ã,  Alphabet2[FirstID]=Second.Chars[0];
Ã,  Alphabet2[SecondID]=First.Chars[0];//Swap the 2 letters in the Alphabet character array
Ã,  Display ("Replace %c with %c.",Ã,  Alphabet2[FirstID],Ã,  Alphabet2[SecondID]);
Ã,  FirstID=0;
Ã,  SecondID=0;
Ã,  CodeA=Code.Text;// Code is a GUI Label containing a string whose letters I want to alter
Ã,  while (StringPos<(CodeA.Length-1)){//Go through every letter in the string replacing it with
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[1])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[1]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[2])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[2]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[3])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[3]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[4])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[4]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[5])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[5]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[6])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[6]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[7])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[7]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[8])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[8]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[9])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[9]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[10])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[10]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[11])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[11]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[12])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[12]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[13])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[13]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[14])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[14]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[15])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[15]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[16])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[16]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[17])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[17]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[18])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[18]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[19])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[19]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[20])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[20]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[21])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[21]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[22])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[22]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[23])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[23]);
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[24])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[24]); 
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[25])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[25]); 
Ã,  Ã,  if (CodeA.Chars[StringPos]==Alphabet1[26])Code.Text=CodeA.ReplaceCharAt(StringPos, Alphabet2[26]);Ã,  Ã,  Ã, 
Ã,  Ã,  StringPos=StringPos+1;
Ã,  Ã,  CodeA=Code.Text;
}
StringPos=0;

Alphabet1[1]=Alphabet2[1];
Alphabet1[2]=Alphabet2[2];
Alphabet1[3]=Alphabet2[3];
Alphabet1[4]=Alphabet2[4];
Alphabet1[5]=Alphabet2[5];
Alphabet1[6]=Alphabet2[6];
Alphabet1[7]=Alphabet2[7];
Alphabet1[8]=Alphabet2[8];
Alphabet1[9]=Alphabet2[9];
Alphabet1[10]=Alphabet2[10];
Alphabet1[11]=Alphabet2[11];
Alphabet1[12]=Alphabet2[12];
Alphabet1[13]=Alphabet2[13];
Alphabet1[14]=Alphabet2[14];
Alphabet1[15]=Alphabet2[15];
Alphabet1[16]=Alphabet2[16];
Alphabet1[17]=Alphabet2[17];
Alphabet1[18]=Alphabet2[18];
Alphabet1[19]=Alphabet2[19];
Alphabet1[20]=Alphabet2[20];
Alphabet1[21]=Alphabet2[21];
Alphabet1[22]=Alphabet2[22];
Alphabet1[23]=Alphabet2[23];
Alphabet1[24]=Alphabet2[24];
Alphabet1[25]=Alphabet2[25];
Alphabet1[26]=Alphabet2[26];
}
else if (FirstID!=SecondID){
Ã,  FirstID=0;
Ã,  SecondID=0;
}
}


As I say, I don't really know what I'm doing and I'd appreciate any thoughts!

Thanks,

Ali.

SSH

In which way doesn't it work?

Also, you could use the String.Contains function to get the position of the letter in the alphabet, rather than a huge list of ifs

And you should probably keep a copy of the original coded message in a string and work from that, rather than keep copying it from the label test, becuase as it is, every time the player swaps 2 characters, the "decoding" process is applied to the already failed decode string again. e.g.

If the player swaps A and B and the coded message is

ABCDABCD

you would get

BACDBACD

then they swap E anf F which arenmt in the message you'd get

ABCDABCD again becuase the a-b swap transition is reapplied! You need to start from the original coded message again each time.
12

Ali

Thanks SSH!

I'll check out String.Contains, I knew that the ifs were pretty inelegant.

Sorry I wasn't clear about how it didn't work. The first swap works, but the second jumbles the letters in a way that doesn't make sense to me.

I think I see what you mean about reapplying the swap to the failed de-code, but I don't think keeping the original coded message in a string would work properly. The code cracker is supposed to worklike this:

Beginning:

Code: "ABABCD"

Buttons: A B C D E...


After I swap buttons A and B:

Code: "BABACD"

Buttons: B A C D E...


After I swap buttons C and D:

Code: "BABACD"

Buttons: B A D C E...

Would this be possible if I referred to the original code each time? Would it help at all if I uploaded the game?

Thanks for the feedback SSH, sorry for asking so much. I'm just not much of a scripter.

-Ali

Besh

I'm not sure what A is, but try to change:

Before:
Code: ags

else if (FirstID != 0){ //If is the second button clicked store its ID, loc & text in the appropriate variables
Ã,  if (FirstID != SecondID){
Ã,  Ã,  SecondID = A.ID;


After
Code: ags

else { //If is the second button clicked store its ID, loc & text in the appropriate variables
Ã,  if (FirstID != A.ID) {
Ã,  Ã,  Ã, SecondID = A.ID;


you don't know if secondID is equal (or not) to firstID since you put secondID = A.ID in the line below.
"Spread our codes to the stars,
You can rescue us all"
- Muse

Ali

A is GUI control 1, a button with "A" as its label. There are 25 more buttons labelled B-Z.

Thanks for the tip Besh. I'll make that change and have another go at getting my head around the crazy old work of scripting.

Besh

you have 25 parts of code like this :o

what version of AGS are you using? if you use a new one you can try pointers, in that case there is only one script for all the buttons.

If you need more help upload your game/gui or email.

"Spread our codes to the stars,
You can rescue us all"
- Muse

Ali

Having typed that last message, I opened up AGS and realised that my essential concept was flawed. I saw that pointers were the way to go and I've begun re-working the idea from the ground up.

Thanks for the offer of help Besh, I may take you up on that if I get stuck. Which I inevitably will.

Thanks again!

SMF spam blocked by CleanTalk