forked from Advanced-Programming-Course/Midterm-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicMachine.java
More file actions
139 lines (124 loc) · 5.06 KB
/
Copy pathMagicMachine.java
File metadata and controls
139 lines (124 loc) · 5.06 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
139
import java.util.Random;
import java.util.Scanner;
public class MagicMachine {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of the magic machine (n): ");
int n = scanner.nextInt();
scanner.nextLine();
Random random = new Random();
int[][] machine = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
machine[i][j] = random.nextInt(5) + 1;
}
}
printMachine(machine);
System.out.println("Enter the input string (length between 5 and 20 characters): ");
String input = scanner.nextLine();
if (input.length() < 5 || input.length() > 20) {
System.out.println("Input string length must be between 5 and 20 characters.");
return;
}
String output = magicMachineFunction(n, machine, input);
System.out.println("Output: " + output);
}
public static String magicMachineFunction(int n, int[][] array, String input) {
String[][] grid = new String[n][n];
grid[0][0] = applyBlackFunction(array[0][0], input);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 && j == 0) continue;
int funcType = array[i][j];
if (i == 0) {
grid[i][j] = applyBlackFunction(funcType, grid[i][j - 1]);
} else if (j == 0) {
grid[i][j] = applyBlackFunction(funcType, grid[i - 1][j]);
} else {
String fromLeft = grid[i][j - 1];
String fromTop = grid[i - 1][j];
if (i == n - 1 || j == n - 1) {
grid[i][j] = applyWhiteFunction(funcType, fromLeft, fromTop);
} else {
String bottomResult = applyBlackFunction(funcType, fromTop);
grid[i][j] = bottomResult;
String rightResult = applyBlackFunction(funcType, fromLeft);
if (j < n - 1) grid[i][j + 1] = rightResult;
}
}
}
}
return grid[n - 1][n - 1];
}
private static String applyBlackFunction(int func, String input) {
switch (func) {
case 1:
return new StringBuilder(input).reverse().toString();
case 2:
String doubled = "";
for (char c : input.toCharArray()) {
doubled += c;
doubled += c;
}
return doubled;
case 3:
return input + input;
case 4:
return input.charAt(input.length() - 1) + input.substring(0, input.length() - 1);
case 5:
String replaced = "";
for (char c : input.toCharArray()) {
replaced += (char) ('z' - (c - 'a'));
}
return replaced;
default:
return null;
}
}
private static String applyWhiteFunction(int func, String str1, String str2) {
switch (func) {
case 1:
String merged = "";
int maxLength = Math.max(str1.length(), str2.length());
for (int i = 0; i < maxLength; i++) {
if (i < str1.length()) merged += str1.charAt(i);
if (i < str2.length()) merged += str2.charAt(i);
}
return merged;
case 2:
return str1 + new StringBuilder(str2).reverse().toString();
case 3:
String interleave = "";
int length = Math.max(str1.length(), str2.length());
for (int i = 0; i < length; i++) {
if (i < str1.length()) interleave += str1.charAt(i);
if (i < str2.length()) interleave += str2.charAt(str2.length() - 1 - i);
}
return interleave;
case 4:
return (str1.length() % 2 == 0) ? str1 : str2;
case 5:
String sumString = "";
int minLength = Math.min(str1.length(), str2.length());
for (int i = 0; i < minLength; i++) {
char newChar = (char) ((str1.charAt(i) - 'a' + str2.charAt(i) - 'a') % 26 + 'a');
sumString += newChar;
}
if (str1.length() > minLength) sumString += str1.substring(minLength);
if (str2.length() > minLength) sumString += str2.substring(minLength);
return sumString;
default:
return null ;
}
}
private static void printMachine(int[][] machine) {
System.out.println("Magic Machine Grid:");
for (int[] row : machine) {
for (int func : row) {
System.out.print(func + " ");
}
System.out.println();
}
System.out.println();
}
}