2015-02-04 20:51:31 +01:00
|
|
|
package de.vanitasvitae.enigmandroid;
|
|
|
|
|
2015-08-15 03:53:02 +02:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.widget.Toast;
|
2015-03-14 00:29:43 +01:00
|
|
|
|
2015-08-15 03:53:02 +02:00
|
|
|
import java.util.ArrayList;
|
2015-03-14 00:29:43 +01:00
|
|
|
|
2015-08-15 03:53:02 +02:00
|
|
|
/**
|
|
|
|
* Created by vanitas on 12.08.15.
|
2015-02-04 20:51:31 +01:00
|
|
|
*/
|
|
|
|
public class Plugboard
|
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
Integer[] plugs;
|
2015-02-18 21:51:40 +01:00
|
|
|
|
|
|
|
public Plugboard()
|
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
plugs = new Integer[26];
|
|
|
|
}
|
|
|
|
|
|
|
|
public Plugboard(int[][] configuration)
|
|
|
|
{
|
|
|
|
setConfiguration(configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Plugboard(String configuration)
|
|
|
|
{
|
|
|
|
setConfiguration(parseConfigurationString(configuration));
|
2015-02-18 21:51:40 +01:00
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-02-18 21:51:40 +01:00
|
|
|
/**
|
2015-08-15 03:53:02 +02:00
|
|
|
* Configure the plugboard according to the given array.
|
2015-02-18 21:51:40 +01:00
|
|
|
*
|
2015-08-15 03:53:02 +02:00
|
|
|
* @param configuration
|
2015-02-18 21:51:40 +01:00
|
|
|
*/
|
2015-08-15 03:53:02 +02:00
|
|
|
public void setConfiguration(int[][] configuration)
|
2015-02-18 21:51:40 +01:00
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
if(configuration != null) {
|
|
|
|
boolean validConfiguration = true;
|
|
|
|
plugs = new Integer[26];
|
|
|
|
for (int[] p : configuration) {
|
|
|
|
if (!setPlugs(p[0], p[1])) {
|
|
|
|
validConfiguration = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!validConfiguration) plugs = new Integer[26];
|
|
|
|
}
|
|
|
|
else plugs = new Integer[26];
|
2015-02-18 21:51:40 +01:00
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-02-18 21:51:40 +01:00
|
|
|
/**
|
2015-08-15 03:53:02 +02:00
|
|
|
* Parse configuration from input string
|
|
|
|
* input must have the following form: "" or "XY" or "XY,VW" or "XY,...,AB"
|
|
|
|
* A character must not be inside the input multiple times. Exception is ','
|
|
|
|
* This is not catched here!
|
|
|
|
* @param input String that codes the configuration
|
|
|
|
* @return two dimensional array of plugged symbols
|
2015-02-18 21:51:40 +01:00
|
|
|
*/
|
2015-08-15 03:53:02 +02:00
|
|
|
public static int[][] parseConfigurationString(String input)
|
2015-02-18 21:51:40 +01:00
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
Activity activity = MainActivity.ActivitySingleton.getInstance().getActivity();
|
|
|
|
//If length != 0,2,5,8... ( ,XY, XY-VW, ...)
|
|
|
|
if(((input.length()+1)%3)!=0&&input.length()!=0)
|
|
|
|
{
|
|
|
|
Toast.makeText(activity.getApplicationContext(), R.string.error_parsing_plugs,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input = input.toUpperCase();
|
|
|
|
ArrayList<int[]> plugList = new ArrayList<int[]>();
|
|
|
|
int[] plug = new int[2];
|
|
|
|
for (int i = 0; i < input.length(); i++) {
|
|
|
|
int c = input.charAt(i) - 65;
|
|
|
|
if (c < 0 || c > 25) {
|
|
|
|
if (i % 3 != 2) {
|
|
|
|
Toast.makeText(activity.getApplicationContext(), R.string.error_parsing_plugs,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (i % 3 == 0) {
|
|
|
|
plug = new int[2];
|
|
|
|
plug[0] = c;
|
|
|
|
}
|
|
|
|
if (i % 3 == 1) {
|
|
|
|
plug[1] = c;
|
|
|
|
plugList.add(plug);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int[][] parsedConfiguration = new int[plugList.size()][2];
|
|
|
|
for(int i=0; i<plugList.size(); i++)
|
|
|
|
{
|
|
|
|
parsedConfiguration[i] = plugList.get(i);
|
|
|
|
}
|
|
|
|
return parsedConfiguration;
|
|
|
|
}
|
|
|
|
|
2015-02-18 21:51:40 +01:00
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-02-11 20:56:43 +01:00
|
|
|
/**
|
2015-08-15 03:53:02 +02:00
|
|
|
* Set the given plugs (connect a and b)
|
|
|
|
* Return false, if something goes wrong (plugs already used somehow)
|
|
|
|
* @param a first plug
|
|
|
|
* @param b second plug
|
|
|
|
* @return success
|
2015-02-11 20:56:43 +01:00
|
|
|
*/
|
2015-08-15 03:53:02 +02:00
|
|
|
public boolean setPlugs(int a, int b)
|
2015-02-18 21:51:40 +01:00
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
Activity activity = MainActivity.ActivitySingleton.getInstance().getActivity();
|
|
|
|
if(a==b)
|
2015-02-04 20:51:31 +01:00
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
Toast.makeText(activity.getApplication().getApplicationContext(),
|
|
|
|
activity.getResources().getText(R.string.error_unable_to_plug_a_b).toString()
|
|
|
|
+" "+(char)(a+65)+","+(char) (b+65),Toast.LENGTH_LONG).show();
|
|
|
|
return false;
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|
2015-08-15 03:53:02 +02:00
|
|
|
if(plugs[a] != null || plugs[b] != null)
|
2015-02-04 20:51:31 +01:00
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
Toast.makeText(activity.getApplication().getApplicationContext(),
|
|
|
|
activity.getResources().getText(R.string.error_plug_already_in_use).toString()
|
|
|
|
+" "+(char) (a+65)+","+(char)(b +65), Toast.LENGTH_SHORT).show();
|
|
|
|
return false;
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|
2015-02-18 21:51:40 +01:00
|
|
|
else
|
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
plugs[a] = b;
|
|
|
|
plugs[b] = a;
|
|
|
|
return true;
|
2015-02-18 21:51:40 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-08-15 03:53:02 +02:00
|
|
|
/**
|
|
|
|
* Encrypt input via plugboard connections
|
|
|
|
* @param input input symbol to encrypt
|
|
|
|
* @return encrypted symbol
|
|
|
|
*/
|
|
|
|
public int encrypt(int input)
|
2015-02-04 20:51:31 +01:00
|
|
|
{
|
2015-08-15 03:53:02 +02:00
|
|
|
if(plugs[input]==null) return input;
|
|
|
|
else return plugs[input];
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|
|
|
|
}
|