プニグマ punigma C#
using System;
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 Position
{
get { 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;
}
}
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.Position));
Console.WriteLine(str0);
Console.WriteLine(str1);
Console.WriteLine(str2);
Console.WriteLine(str3);
Console.WriteLine("\n{0} {1}", input, encoded);
}
static void Main(string[] args)
{
Roter roter = new Roter();
string input = "";
string encoded = "";
bool reset = false;
Console.WriteLine("1~9の数字を入力するとローターが回転します。");
Console.WriteLine("A~Zを入力すると右側に変換した文字を表示します。");
Console.WriteLine("0を入力するとプログラムを終了します。");
while (input.Length < 30)
{
ShowPunigma(roter, input, encoded);
ConsoleKeyInfo cki = Console.ReadKey();
char ch = cki.KeyChar;
ch = Char.ToUpper(ch);
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;
}
}
}
}