-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormulaOne.java
147 lines (138 loc) · 7.01 KB
/
FormulaOne.java
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
140
141
142
143
144
145
146
147
package ExamPrep3;
import java.util.Scanner;
public class FormulaOne {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int currentPlayerRow = 0;
int currentPlayersCol = 0;
int n = Integer.parseInt(scan.nextLine());
String [][] matrix = new String[n][n];
int countOfCommands = Integer.parseInt(scan.nextLine());
int sizeOfInput = 0;
for (int row = 0; row < n; row++) {
String[] input = scan.nextLine().split("");
sizeOfInput = input.length;
for (int col = 0; col < sizeOfInput; col++) {
matrix[row][col] = input[col];
if (matrix[row][col].equals("P")) {
currentPlayerRow = row;
currentPlayersCol = col;
matrix[row][col] = ".";
}
}
}
boolean isFinish = false;
while (countOfCommands > 0) {
String command = scan.nextLine();
int oldPlayerRow = currentPlayerRow;
int OldPlayerCol = currentPlayersCol;
switch (command) {
case "up":
currentPlayerRow--;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayerRow = n - 1;
}
if (matrix[currentPlayerRow][currentPlayersCol].equals("F")) {
matrix[currentPlayerRow][currentPlayersCol] = "P";
isFinish = true;
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("B")) {
currentPlayerRow--;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayerRow = n - 1;
}
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("T")) {
currentPlayerRow++;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = 0;
}
}
break;
case "down":
currentPlayerRow++;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayerRow = 0;
}
if (matrix[currentPlayerRow][currentPlayersCol].equals("F")) {
matrix[currentPlayerRow][currentPlayersCol] = "P";
isFinish = true;
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("B")) {
currentPlayerRow++;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayerRow = 0;
}
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("T")) {
currentPlayerRow--;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = n - 1;
}
}
break;
case "left":
currentPlayersCol--;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = sizeOfInput - 1;
}
if (matrix[currentPlayerRow][currentPlayersCol].equals("F")) {
matrix[currentPlayerRow][currentPlayersCol] = "P";
isFinish = true;
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("B")) {
currentPlayersCol--;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = sizeOfInput - 1;
}
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("T")) {
currentPlayersCol++;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = 0;
}
}
break;
case "right":
currentPlayersCol++;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = 0;
}
if (matrix[currentPlayerRow][currentPlayersCol].equals("F")) {
matrix[currentPlayerRow][currentPlayersCol] = "P";
isFinish = true;
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("B")) {
currentPlayersCol++;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = 0;
}
} else if (matrix[currentPlayerRow][currentPlayersCol].equals("T")) {
currentPlayersCol--;
if (isThePlayerOut(n, sizeOfInput, currentPlayerRow, currentPlayersCol)) {
currentPlayersCol = sizeOfInput - 1;
}
}
break;
}
if (!isFinish) {
countOfCommands--;
} else {
break;
}
}
if(!isFinish){
matrix[currentPlayerRow][currentPlayersCol] = "P";
}
if(isFinish){
System.out.println("Well done, the player won first place!");
}else{
System.out.println("Oh no, the player got lost on the track!");
}
for (int r = 0; r < n; r++) {
for (int c = 0; c < sizeOfInput; c++) {
System.out.print(matrix[r][c]);
}
System.out.println();
}
}
private static boolean isThePlayerOut(int n, int sizeOfInput, int currentPlayerRow, int currentPlayersCol) {
if (currentPlayerRow > n - 1 || currentPlayerRow < 0 || currentPlayersCol > sizeOfInput - 1 || currentPlayersCol < 0) {
return true;
}
return false;
}
}