プニグマ punigma VB.NET
Module Punigma
Class Roter
Protected mWire As Integer() = New Integer(25) {}
Protected mPosition As Integer
Protected Sub Connect(n1 As Integer, n2 As Integer)
mWire(n1) = n2
mWire(n2) = n1
End Sub
Public Sub New()
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
End Sub
Public ReadOnly Property Position() As Integer
Get
Return mPosition
End Get
End Property
Public Function Encode(ch As Char) As Char
Dim n As Integer = (Asc(ch) - Asc("A"c) + mPosition) Mod 26
Dim encoded As Integer = Asc("A"c) + ((mWire(n) - mPosition + 26) Mod 26)
mPosition += 1
Return ChrW(encoded)
End Function
Public Sub Rotate(n As Integer)
mPosition = (mPosition + n) Mod 26
End Sub
End Class
Private Sub ShowPunigma(roter As Roter, input As String, encoded As String)
Dim str0 As String = "------------------------"
Dim str1 As String = "Q W E R T Y U I O P |||"
Dim str2 As String = " A S D F G H J K L |*|"
Dim str3 As String = " Z X C V B N M |||"
str2 = str2.Replace("*"c, ChrW(Asc("A"c) + roter.Position))
Console.WriteLine(str0)
Console.WriteLine(str1)
Console.WriteLine(str2)
Console.WriteLine(str3)
Console.WriteLine(vbLf & "{0} {1}", input, encoded)
End Sub
Sub Main()
Dim roter As New Roter()
Dim input As String = ""
Dim encoded As String = ""
Dim reset As Boolean = False
Console.WriteLine("1~9の数字を入力するとローターが回転します。")
Console.WriteLine("A~Zを入力すると右側に変換した文字を表示します。")
Console.WriteLine("0を入力するとプログラムを終了します。")
While input.Length < 30
ShowPunigma(roter, input, encoded)
Dim cki As ConsoleKeyInfo = Console.ReadKey()
Dim ch As Char = cki.KeyChar
ch = Char.ToUpper(ch)
If "A"c <= ch AndAlso ch <= "Z"c Then
If reset Then
input = ""
encoded = ""
reset = False
End If
input += ch
encoded += roter.Encode(ch)
ElseIf "1"c <= ch AndAlso ch <= "9"c Then
roter.Rotate(Asc(ch) - Asc("0"c))
reset = True
Else
Exit While
End If
End While
End Sub
End Module