-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptGameTest.java
More file actions
138 lines (126 loc) · 4.4 KB
/
EncryptGameTest.java
File metadata and controls
138 lines (126 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
import java.util.Scanner;
import java.util.Random;
/**
*
* @author cjyen
*/
public class Assignment1 {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
String encode;
char name;
int Change = 0;
int option = 0;
String pText = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String cText = "qwermnblkjhzxcpoiuasdf ytgvQWERMNBLKJHZXCPOIUASDFYTGV";
//substitute letter into a temporary arrays
char Letters[] = new char[53];
for(int p = 52; p >= 0; p--)
{
Letters[p] = cText.charAt(p);
}
//randomly exchange the letters
//reason why dont use i++ because at the end the numbers will not be exchanged much.
for(int i =52; i>=0; i--)
{
int j =rand.nextInt(0,2); //when i =52, let j be the random numbers from 0~52
char temp = Letters[i];
Letters[i] = Letters[j];
Letters[j] = temp;
}
//change the cText to the new cText, because Letters is a char element, this line is also to change it to string element
cText = new String(Letters);
//menu select
while(option != 3)
{
boolean option1 = false;
String randomtext = "";
String reverse = "";
String finaltext = "";
String even = "",odd = "";
System.out.println("Menu:");
System.out.println("[1] Encrypt");
System.out.println("[2] Decrypt");
System.out.println("[3] Exist");
option = keyboard.nextInt();
//https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo
if(option == 1)
{
System.out.println("\nSystem Encrypt:");
option1 = true;
}
else if(option == 2)
{
System.out.println("\nSystem Decrypt:");
}
else if(option == 3)
{
System.out.println("\nThanks for playing");
}
else
{
System.out.println("Please enter option again!!!");
}
if(option !=3)
{
System.out.println("\nPlease enter the text you want");
encode = keyboard.nextLine();
}
else
{
encode = "";
}
if(option1)
{
//encrypt
for(int i = 0; i<encode.length();i++)
{
Change = pText.indexOf(encode.charAt(i));
name = cText.charAt(Change);
randomtext = randomtext + name;
}
}
else
{
//decrypt
for(int i = 0; i<encode.length();i++)
{
Change = cText.indexOf(encode.charAt(i));
name = pText.charAt(Change);
randomtext = randomtext + name;
}
}
//reverse section
for(int i = 1; i<=randomtext.length();i++)
{
reverse = reverse + randomtext.charAt(randomtext.length()-i);
}
//odd number or even rearrange
for(int i = 0; i<randomtext.length();i++)
{
if(i%2 == 0)
{
even = even + randomtext.charAt(i);
}
else
{
odd = odd + randomtext.charAt(i);
}
}
finaltext= even + odd;
//outprint
if(option != 3)
{
System.out.printf("\nEncrypt:\t\t%s\n", randomtext);
System.out.printf("Reverse:\t\t%s\n", reverse);
System.out.printf("Encrypted(odd):\t%s\n\n", finaltext);
}
}
}
}