プニグマ punigma Java
import java.io.*;
class Roter
{
protected int[] mWire = new int[26];
protected int mPosition;
protected void Connect(int n1, int n2)
{
mWire[n1] = n2;
mWire[n2] = n1;
}
public Roter()
{
Connect(16, 6);
Connect(15, 20);
Connect(0, 21);
Connect(17, 22);
Connect(7, 2);
Connect(5, 11);
Connect(12, 23);
Connect(10, 1);
Connect(8, 24);
Connect(3, 25);
Connect(18, 9);
Connect(13, 4);
Connect(14, 19);
mPosition = 0;
}
public int GetPosition()
{
return mPosition;
}
public char Encode(char ch)
{
int n = (ch - 'A' + mPosition) % 26;
int encoded = 'A' + ((mWire[n] - mPosition + 26) % 26);
mPosition++;
return (char)encoded;
}
public void Rotate(int n)
{
mPosition = (mPosition + n) % 26;
}
}
public class Punigma
{
static void ShowPunigma(Roter roter, String input, String encoded)
{
String str0 = "------------------------";
String str1 = "Q W E R T Y U I O P |||";
String str2 = " A S D F G H J K L |*|";
String str3 = " Z X C V B N M |||";
str2 = str2.replace('*', (char)('A' + roter.GetPosition()));
System.out.println(str0);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println("\n" + input + " " + encoded);
}
public static void main( String[] args ) throws IOException
{
Roter roter = new Roter();
String input = "";
String encoded = "";
boolean reset = false;
System.out.println("1~9の数字を入力するとローターが回転します。");
System.out.println("A~Zを入力すると右側に変換した文字を表示します。");
System.out.println("0を入力するとプログラムを終了します。");
BufferedReader br = new BufferedReader(
new InputStreamReader( System.in ) );
while (input.length() < 30)
{
ShowPunigma(roter, input, encoded);
String line = br.readLine();
if( line.length() == 0 )
continue;
char ch = line.toUpperCase().charAt(0);
if ('A' <= ch && ch <= 'Z')
{
if (reset)
{
input = "";
encoded = "";
reset = false;
}
input += ch;
encoded += roter.Encode(ch);
}
else if ('1' <= ch && ch <= '9')
{
roter.Rotate(ch - '0');
reset = true;
}
else
{
break;
}
}
}
}