# TASK 2 (50%)  - ASHLEIGH JOSEPH SIMONS - 265050

# 1. ---------------------------------------------------

# To create the Polybius matrix we need to have a vector containing all of
# the values that will be put within the matrix. charData is the character data

charData<-c(" ", "Z", "M", "D", " ", "K", "W", "Q", " ", "G", 
            "V", " ", "A", " ", "R", " ", "E", " ", "T", "O",
            " ", "J", "I", "S", "B", "P", "Y", " ", "F", " ",
            "L", " ", " ", "C", " ", "U", "H", "N", " ", "X") 


# Now that we have the character data: charData we can create the Polybius 
# matrix. it required 4 rows and 10 columns and we want the data being entered
# row by row so the data and positions are correct

Polybius<-matrix(charData, nrow = 4, ncol = 10, byrow = TRUE) 


# Now we need to change the column names and row names
# this can be done using colnames() and rownames()

colnames(Polybius)<-c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")

rownames(Polybius)<-c("0", "1", "2", "3")

Polybius # Display the matrix 


# 2. ---------------------------------------------------

# To encrypt the message we first need the message to be split up into 
# characters, this can be done using a sub string. 

message<-"CAPTURETHEHILL " # setting the message as 'CAPTURETHEHILL'

message<-substring(message, 1:15, 1:15) # split into characters 

message # display the message with its new format


# Now we need to create a loop that will go through the message letter by 
# letter and look for it within the matrix. Then when it finds the letter,
# the values of the column and row are put into letterCode, 1 for each letter
# of the alphabet has a unique code

for (i in message)
{
  if (i == Polybius[2,3]) # A
  {
    letterCode<-"12"
  }
  if (i == Polybius[3,5]) # B
  {
    letterCode<-"24"
  }
  if (i == Polybius[4,4]) # C
  {
    letterCode<-"22"
  }
  if (i == Polybius[1,4]) # D
  {
    letterCode<-"03"
  }
  if (i == Polybius[2,7]) # E
  {
    letterCode<-"16"
  }
  if (i == Polybius[3,9]) # F
  {
    letterCode<-"28"
  }
  if (i == Polybius[1,10]) # G
  {
    letterCode<-"09"
  }
  if (i == Polybius[4,7]) # H
  {
    letterCode<-"36"
  }
  if (i == Polybius[3,3]) # I
  {
    letterCode<-"22"
  }
  if (i == Polybius[3,2]) # J
  {
    letterCode<-"21"
  }
  if (i == Polybius[1,6]) # K
  {
    letterCode<-"05"
  }
  if (i == Polybius[4,1]) # L
  {
    letterCode<-"30"
  }
  if (i == Polybius[1,3]) # M
  {
    letterCode<-"02"
  }
  if (i == Polybius[4,8]) # N
  {
    letterCode<-"37"
  }
  if (i == Polybius[2,10]) # O
  {
    letterCode<-"19"
  }
  if (i == Polybius[3,6]) # P
  {
    letterCode<-"25"
  }
  if (i == Polybius[1,8]) # Q
  {
    letterCode<-"07"
  }
  if (i == Polybius[2,5]) # R
  {
    letterCode<-"14"
  }
  if (i == Polybius[3,4]) # S
  {
    letterCode<-"23"
  }
  if (i == Polybius[2,9]) # T
  {
    letterCode<-"18"
  }
  if (i == Polybius[4,6]) # U
  {
    letterCode<-"35"
  }
  if (i == Polybius[2,1]) # V
  {
    letterCode<-"10"
  }
  if (i == Polybius[1,7]) # W
  {
    letterCode<-"06"
  }
  if (i == Polybius[4,10]) # X
  {
    letterCode<-"39"
  }
  if (i == Polybius[3,7]) # Y
  {
    letterCode<-"26"
  }
  if (i == Polybius[1,2]) # Z
  {
    letterCode<-"01"
  }
  if (i == Polybius[1,9]) # any space values, e.g " ". 
  {
    letterCode<-("09")
  }


  # when each time the loop is run, the i in message will change, each time
  # the loop is used, the letter for each loop iteration will be put into the 
  # corresponding Codes 
  
  if (i == message[1])
  {
    Code1<-letterCode # 1st time
  }
  if (i == message[2])
  {
    Code2<-letterCode # 2nd time
  }
  if (i == message[3])
  {
    Code3<-letterCode # 3rd time
  }
  if (i == message[4])
  {
    Code4<-letterCode # 4th time
  }
  if (i == message[5])
  {
    Code5<-letterCode # 5th time
  }
  if (i == message[6])
  {
    Code6<-letterCode # 6th time
  }
  if (i == message[7])
  {
    Code7<-letterCode # 7th time
  }
  if (i == message[8])
  {
    Code8<-letterCode # 8th time
  }
  if (i == message[9])
  {
    Code9<-letterCode # 9th time
  }
  if (i == message[10])
  {
    Code10<-letterCode # 10th time
  }
  if (i == message[11])
  {
    Code11<-letterCode # 11th time
  }
  if (i == message[12])
  {
    Code12<-letterCode # 12th time
  }
  if (i == message[13])
  {
    Code13<-letterCode # 13th time
  }
  if (i == message[14])
  {
    Code14<-letterCode # 14th time
  }
  if (i == message[15])
  {
    Code15<-letterCode # 15th time
  }
}


# Now that all of the letters have been encrypted we can create one large
# vector of them all 

AllCodes<-c(Code1, Code2, Code3, Code4,
            Code5, Code6, Code7, Code8,
            Code9, Code10, Code11, Code12,
            Code13, Code14, Code15)

AllCodes # display AllCodes


# All of the codes can now be put into one large vector and this can be done
# using paste() and then making sure the 'sep = "" ' as these are what seperate 
# the codes

AllCodes<-paste(Code1, Code2, Code3, Code4,
                Code5, Code6, Code7, Code8,
                Code9, Code10, Code11, Code12,
                Code13, Code14, Code15, sep = "")

ciphertext<-AllCodes # put all of the codes into vector ciphertext

ciphertext # display ciphertext

# Now the characters can be put into seperate elements in the vector ciphertext.
# Substring will be used again to do this this time putting the vector 
# into seperate elements 

ciphertext<-substring(ciphertext, 1:30, 1:30) # split into characters 

ciphertext # display the encrypted message

# 3. ---------------------------------------------------

# To display all of the codes in one vector we can first put all of the 
# values from the first 5 in cipher text and put them into ft1, finaltext1

# We can use this in another vector later on to call the rest with 
# this first vector


ft1<-paste(ciphertext[1:5], ciphertext[2:5], 
           ciphertext[3:5], ciphertext[4:5], 
           ciphertext[5:5])

ft1[1] # Display the first 5 elements in ciphertext


ft2<-paste(ciphertext[6:10], ciphertext[7:10], 
           ciphertext[8:10], ciphertext[9:10], 
           ciphertext[10:10]) 

ft2[1] # Display the 6th to 10th elements in ciphertext


ft3<-paste(ciphertext[11:15], ciphertext[12:15], 
           ciphertext[13:15], ciphertext[14:15], 
           ciphertext[15:15]) 

ft3[1]# Display the 11th to 15th elements in ciphertext


ft4<-paste(ciphertext[16:20], ciphertext[17:20], 
           ciphertext[18:20], ciphertext[18:20], 
           ciphertext[20:20]) 

ft4[1] # Display the 16th to 20th elements in ciphertext


ft5<-paste(ciphertext[21:25], ciphertext[22:25], 
           ciphertext[23:25], ciphertext[24:25], 
           ciphertext[25:25]) 

ft5[1] # Display the 21st to 25th elements in ciphertext

ft6<-paste(ciphertext[26:30], ciphertext[27:30], 
           ciphertext[28:30], ciphertext[29:30], 
           ciphertext[30:30]) 

ft6[1] # Display the 26th to 30th elements in ciphertext


# Now we can take these vectors and put them into final text so that all 
# of the elements are in groups of 5 numbers

finaltext<-c(ft1[1], ft2[1], ft3[1], ft4[1], ft5[1], ft6[1])

finaltext # display the final text



