# James Evans - 1910316
# Statistical Programming
# Assignment 1

# Task 2

# Question 1

# Create 9 vectors, that will each contain the information for each corresponding column in the Polybius matrix.

v0<-c(" ","V"," ","L")
v1<-c("Z"," ","J"," ")
v2<-c("M","A","I"," ")
v3<-c("D"," ","S","C")
v4<-c(" ","R","B"," ")
v5<-c("K"," ","P","U")
v6<-c("W","E","Y","H")
v7<-c("Q"," "," ","N")
v8<-c(" ","T","F"," ")
v9<-c("G","O"," ","X")

# Create a matrix names Polybius:-
Polybius<-matrix()

# Using the column bind function, create the matrix using each of the 10 vectors already created.
Polybius<-cbind(v0,v1,v2,v3,v4,v5,v6,v7,v8,v9)

# Display Polybius:-
Polybius

# Rename the columns and rows of Polybius to match tose in the question:-
colnames(Polybius)<-c("0","1","2","3","4","5","6","7","8","9")
rownames(Polybius)<-c("0","1","2","3")

# Display Polybius:-
Polybius


# Question 2

# Set the eventual ciphertext vector to NULL.

ciphertext<-NULL

# Create an initial message vector "imsg" to contain the individual characters that need to be encoded:-

imsg<-"CAPTURETHEHILL"

# Change imsg from a vector with one character element to a vector containing an element for each character:-

# Set the message vector to contain NULL:-

message<-NULL

# Use the nchar function to determine the length of the message.
# For each individual character in imsg; a new element is added to the "message" vector.

for (i in 1:nchar(imsg,type = "chars")){
  
  message<-c(message,substring(imsg,i,i))
  
}

# For each element in the message vector; go through each element in the Polypius matrix until a match is found.
# When a match is found:- 
  # First add the row number minus 1 (to correlate with the matrix row names) to the ciphertext vector.
  # Then add the column number minus 1 (to correlate with the matrix row names) to the ciphertext vector.

for (i in seq(message)){
  
  for(j in seq(Polybius[,1])){
    
    for(k in seq(Polybius[1,])){
      
      if(message[i]==Polybius[j,k]) ciphertext<-c(ciphertext,j-1,k-1)
      
      
    }
  }
}

# Use 5 minus the modulated length of the cipher text (to 5), to determine the number of characters (elements) which need to be added to the end of the vector.

# For each additional character which is required; add an element whose value randomly generated between 1 and 9:-
  # To generate the random numbers, the "Runif" function is used, generating a single number between 1 and 9. 
  # This is then surrounded by the "floor" function, which converts that single randomly generated number to an integer.

# This ensures that the length of the vector is perfectly divisible by 5, and is "padded out" by somewhat random numbers.

for (i in 1:(5-length(ciphertext)%%5))
  
  #ciphertext<-c(ciphertext,9-length(ciphertext)%%5)
  
  ciphertext<-c(ciphertext,floor(runif(1,0,9)))

# Show ciphertext

ciphertext


#Question 3

# Create a vector called finaltext, and assign a NULL value:-

finaltext<-NULL

# We must first create a vector which includes the required space marks:-

# Loop through each element in ciphertext:-
# If the character in question is the final element of ciphertext, simply add the character to finaltext.
# If the iteration is perfectly divisible by 5, insert the element followed by a space character to finaltext.
# If the character is neither the final element nor is the iteration perfectly divisible by 5; add the character to finaltext..

for (i in seq(ciphertext)){
  
  ifelse(i==length(ciphertext),finaltext<-c(finaltext,ciphertext[i]),
         ifelse(i%%5==0,finaltext<-c(finaltext,ciphertext[i]," "),
          finaltext<-c(finaltext,ciphertext[i])))
  
}

# Show finaltext

finaltext

# Use the paste function, collapsed without any separating characters, to turn the vector of multiple elements to a vector containing a single element.

finaltext<-paste(finaltext,collapse = "")


# Show finaltext

finaltext