EnigmAndroid/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java

200 lines
6.2 KiB
Java
Raw Normal View History

package de.vanitasvitae.enigmandroid.enigma;
2015-02-04 20:51:31 +01:00
import android.app.Activity;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import de.vanitasvitae.enigmandroid.MainActivity;
import de.vanitasvitae.enigmandroid.R;
/**
* Plugboard of the enigma
* Copyright (C) 2015 Paul Schaub
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author vanitasvitae
2015-02-04 20:51:31 +01:00
*/
public class Plugboard
{
Integer[] plugs;
public Plugboard()
{
plugs = new Integer[26];
}
public Plugboard(int[][] configuration)
{
setConfiguration(configuration);
}
public Plugboard(String configuration)
{
setConfiguration(parseConfigurationString(configuration));
}
2015-02-04 20:51:31 +01:00
/**
* Configure the plugboard according to the given array.
*
* @param configuration two dimensional array of plugs
*/
public boolean setConfiguration(int[][] configuration)
{
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];
return false;
}
return true;
}
else
{
plugs = new Integer[26];
return false;
}
}
2015-02-04 20:51:31 +01: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
*/
public static int[][] parseConfigurationString(String input)
{
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[] 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;
}
}
public String getConfigurationString()
{
String out = "";
Set<Integer> remaining = new HashSet<>();
for(int i=0; i<26; i++)
{
remaining.add(i);
}
while(!remaining.isEmpty())
{
Integer x = remaining.iterator().next();
if(!x.equals(encrypt(x)))
{
out = out + (char) (x+65) + (char) (encrypt(x)+65) +",";
remaining.remove(encrypt(x));
}
remaining.remove(x);
}
if(out.length() != 0) out = out.substring(0,out.length()-1);
return out;
}
2015-02-04 20:51:31 +01: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
*/
public boolean setPlugs(int a, int b)
{
Activity activity = MainActivity.ActivitySingleton.getInstance().getActivity();
if(a==b)
2015-02-04 20:51:31 +01: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
}
if((plugs[a] != null || plugs[b] != null))
2015-02-04 20:51:31 +01:00
{
if (!plugs[a].equals(b) || !plugs[b].equals(a)) {
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;
}
return true;
2015-02-04 20:51:31 +01:00
}
else
{
plugs[a] = b;
plugs[b] = a;
return true;
}
}
2015-02-04 20:51:31 +01:00
/**
* Encrypt input via plugboard connections
* @param input input symbol to encryptString
* @return encrypted symbol
*/
public int encrypt(int input)
2015-02-04 20:51:31 +01:00
{
if(plugs[input]==null) return input;
else return plugs[input];
2015-02-04 20:51:31 +01:00
}
}