//=========================================================================== // CRYPTOLOGY: M-94 // Nicholas Gessler // 22 February 2005 //=========================================================================== #include #pragma hdrstop #include "Unit1.h" #include #include #include #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DoubleBuffered = true; } //--------------------------------------------------------------------------- //=========================================================================== // VARIABLES //=========================================================================== POINT from, to; int sequence; using namespace std; //=============================================================== ROTOR CLASS // Rotors are numbered from 1 to 25. // Rotors are lettered from B to Z. // Rotors contain an alphabet from A to Z, indexed from 0 to 25. // Rotors have a movable position on the axle given by their index. // Rotors have a movable position on the axle given by their position. class rotors { public: char letter[1]; int number; int position; char alphabet[26]; // displays the alphabet on one rotor void display(void) { Form1->PaintBox1->Canvas->TextOut(20 * position + 7, 10, letter); for (int i = 0; i < 26; i++) { Form1->PaintBox1->Canvas-> TextOut(20 * position + 7, 20 * i + 30, alphabet[i]); } } // rotates the alphabet on one rotor void rotaterotor(int clicks) { for (int i = 0; i < clicks; i++) { vector v(alphabet, alphabet + 26); rotate(v.begin(), v.begin()+1, v.end()); copy(v.begin(), v.end(), alphabet); } } } rotor[26], up, down; //=========================================================================== // FUNCTIONS //=========================================================================== //---------------------------------------------------------------- INITIALIZE void initialize (void) { // This is not a valid rotor. It is simply the alphabet used for testing. strcpy(rotor[0].letter, "A"); rotor[0].number = 0; rotor[0].position = 0; strcpy(rotor[0].alphabet, "abcdefghijklmnopqrstuvwxyz"); // Rotors 1 through 25 begin here: // Initially the rotors are positioned in alphabetical order. strcpy(rotor[1].letter, "B"); rotor[1].number = 1; rotor[1].position = 1; strcpy(rotor[1].alphabet, "ABCEIGDJFVUYMHTQKZOLRXSPWN"); strcpy(rotor[2].letter, "C"); rotor[2].number = 2; rotor[2].position = 2; strcpy(rotor[2].alphabet, "ACDEHFIJKTLMOUVYGZNPQXRWSB"); strcpy(rotor[3].letter, "D"); rotor[3].number = 3; rotor[3].position = 3; strcpy(rotor[3].alphabet, "ADKOMJUBGEPHSCZINXFYQRTVWL"); strcpy(rotor[4].letter, "E"); rotor[4].number = 4; rotor[4].position = 4; strcpy(rotor[4].alphabet, "AEDCBIFGJHLKMRUOQVPTNWYXZS"); strcpy(rotor[5].letter, "F"); rotor[5].number = 5; rotor[5].position = 5; strcpy(rotor[5].alphabet, "AFNQUKDOPITJBRHCYSLWEMZVXG"); strcpy(rotor[6].letter, "G"); rotor[6].number = 6; rotor[6].position = 6; strcpy(rotor[6].alphabet, "AGPOCIXLURNDYZHWBJSQFKVMET"); strcpy(rotor[7].letter, "H"); rotor[7].number = 7; rotor[7].position = 7; strcpy(rotor[7].alphabet, "AHXJEZBNIKPVROGSYDULCFMQTW"); strcpy(rotor[8].letter, "I"); rotor[8].number = 8; rotor[8].position = 8; strcpy(rotor[8].alphabet, "AIHPJOBWKCVFZLQERYNSUMGTDX"); strcpy(rotor[9].letter, "J"); rotor[9].number = 9; rotor[9].position = 9; strcpy(rotor[9].alphabet, "AJDSKQOIVTZEFHGYUNLPMBXWCR"); strcpy(rotor[10].letter, "K"); rotor[10].number = 10; rotor[10].position = 10; strcpy(rotor[10].alphabet, "AKELBDFJGHONMTPRQSVZUXYWIC"); strcpy(rotor[11].letter, "L"); rotor[11].number = 11; rotor[11].position = 11; strcpy(rotor[11].alphabet, "ALTMSXVQPNOHUWDIZYCGKRFBEJ"); strcpy(rotor[12].letter, "M"); rotor[12].number = 12; rotor[12].position = 12; strcpy(rotor[12].alphabet, "AMNFLHQGCUJTBYPZKXISRDVEWO"); strcpy(rotor[13].letter, "N"); rotor[13].number = 13; rotor[13].position = 13; strcpy(rotor[13].alphabet, "ANCJILDHBMKGXUZTSWQYVORPFE"); strcpy(rotor[14].letter, "O"); rotor[14].number = 14; rotor[14].position = 14; strcpy(rotor[14].alphabet, "AODWPKJVIUQHZCTXBLEGNYRSMF"); strcpy(rotor[15].letter, "P"); rotor[15].number = 15; rotor[15].position = 15; strcpy(rotor[15].alphabet, "APBVHIYKSGUENTCXOWFQDRLJZM"); strcpy(rotor[16].letter, "Q"); rotor[16].number = 16; rotor[16].position = 16; strcpy(rotor[16].alphabet, "AQJNUBTGIMWZRVLXCSHDEOKFPY"); strcpy(rotor[17].letter, "R"); rotor[17].number = 17; rotor[17].position = 17; strcpy(rotor[17].alphabet, "ARMYOFTHEUSZJXDPCWGQIBKLNV"); strcpy(rotor[18].letter, "S"); rotor[18].number = 18; rotor[18].position = 18; strcpy(rotor[18].alphabet, "ASDMCNEQBOZPLGVJRKYTFUIWXH"); strcpy(rotor[19].letter, "T"); rotor[19].number = 19; rotor[19].position = 19; strcpy(rotor[19].alphabet, "ATOJYLFXNGWHVCMIRBSEKUPDZQ"); strcpy(rotor[20].letter, "U"); rotor[20].number = 20; rotor[20].position = 20; strcpy(rotor[20].alphabet, "AUTRZXQLYIOVBPESNHJWMDGFCK"); strcpy(rotor[21].letter, "V"); rotor[21].number = 21; rotor[21].position = 21; strcpy(rotor[21].alphabet, "AVNKHRGOXEYBFSJMUDQCLZWTIP"); strcpy(rotor[22].letter, "W"); rotor[22].number = 22; rotor[22].position = 22; strcpy(rotor[22].alphabet, "AWVSFDLIEBHKNRJQZGMXPUCOTY"); strcpy(rotor[23].letter, "X"); rotor[23].number = 23; rotor[23].position = 23; strcpy(rotor[23].alphabet, "AXKWREVDTUFOYHMLSIQNJCPGBZ"); strcpy(rotor[24].letter, "Y"); rotor[24].number = 24; rotor[24].position = 24; strcpy(rotor[24].alphabet, "AYJPXMVKBQWUGLOSTECHNZFRID"); strcpy(rotor[25].letter, "Z"); rotor[25].number = 25; rotor[25].position = 25; strcpy(rotor[25].alphabet, "AZDNBUHYFWJLVGRCQMPSOEXTKI"); } //--------------------------------------------------------------- SHOW ROTORS void showRotors (void) { Form1->Shape1->Visible = true; Form1->Shape2->Visible = true; Form1->Shape3->Visible = true; Form1->Shape4->Visible = true; Form1->Shape5->Visible = true; Form1->Shape6->Visible = true; Form1->Image1->Visible = false; Form1->Image2->Visible = false; Form1->Refresh(); for (int position = 0; position < 26; position++) { rotor[position].display(); } } //=========================================================================== // EVENT HANDLERS //=========================================================================== //-------------------------------------------------------- SHOW PHOTOS BUTTON void __fastcall TForm1::ButtonShowPhotosClick(TObject *Sender) { Form1->Shape1->Visible = false; Form1->Shape2->Visible = false; Form1->Shape3->Visible = false; Form1->Shape4->Visible = false; Form1->Shape5->Visible = false; Form1->Shape6->Visible = false; Form1->Image1->Visible = true; Form1->Image2->Visible = true; Form1->Refresh(); } //---------------------------------------------------------SHOW ROTORS BUTTON void __fastcall TForm1::ButtonShowRotorsClick(TObject *Sender) { showRotors(); } //-------------------------------------------------------------- RESET BUTTON void __fastcall TForm1::ButtonResetClick(TObject *Sender) { initialize(); showRotors(); } //------------------------------------------------------------ ON FORM CREATE void __fastcall TForm1::FormCreate(TObject *Sender) { initialize(); } //------------------------------------------------------------- ON MOUSE MOVE void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { EditRotor->Text = String(X / 20); EditLetter->Text = String(Y / 20); } //------------------------------------------------------------- ON MOUSE DOWN void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { from.x = X / 20; from.y = Y / 20; Edit1->Text = from.x; Edit2->Text = from.y; } //--------------------------------------------------------------- ON MOUSE UP void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { to.x = X / 20; to.y = Y / 20; Edit3->Text = to.x; Edit4->Text = to.y; // if dragged upwards if (to.y < from.y) { rotor[from.x].rotaterotor(from.y - to.y); rotor[from.x].display(); } // if dragged downwards if (to.y > from.y) { rotor[from.x].rotaterotor(from.y - to.y + 26); rotor[from.x].display(); } // if dragged left or right if (to.x != from.x) { down = rotor[from.x]; up = rotor[to.x]; rotor[to.x] = down; rotor[to.x].position = to.x; rotor[from.x] = up; rotor[from.x].position = from.x; } showRotors(); } //---------------------------------------------------------- TRACK BAR CHANGE void __fastcall TForm1::TrackBar1Change(TObject *Sender) { Form1->Shape5->Top = TrackBar1->Position; Form1->Shape6->Top = TrackBar1->Position + 20; showRotors(); } //----------------------------------------------------------------------- END