-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython.java
134 lines (124 loc) · 5.47 KB
/
Python.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
import java.util.Scanner;
public class Python {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int pythonCurrentRow = 0;
int pythonCurrentCol = 0;
int countF = 0;
int eatenFood = 0;
int pythonLength = 0;
boolean isKilled = false;
boolean isEaten = false;
int n = Integer.parseInt(scan.nextLine());
String[] commandToMoveThePython = scan.nextLine().split(", ");
String[][] matrix = new String[n][n];
for (int row = 0; row < n; row++) { // 3
String[] input = scan.nextLine().split("\\s+");
for (int col = 0; col < n; col++) {
matrix[row][col] = input[col];
if (input[col].equals("s")) {
pythonCurrentRow = row;
pythonCurrentCol = col;
}
if (input[col].equals("f")) {
countF++;
}
}
}
for (int i = 0; i < commandToMoveThePython.length; i++) {
String command = commandToMoveThePython[i];
if (command.equals("up")) {
pythonCurrentRow--;
if(isPythonOut(n,pythonCurrentRow,pythonCurrentCol)){
pythonCurrentRow = n-1;
}
if(matrix[pythonCurrentRow][pythonCurrentCol].equals("e")) {
System.out.println("You lose! Killed by an enemy!");
isKilled = true;
break;
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("f")) {
eatenFood++;
if(eatenFood == countF){
System.out.printf("You win! Final python length is %d", pythonLength);
isEaten = true;
break;
}
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("*")){
pythonLength++;
}
} else if (command.equals("down")) {
pythonCurrentRow++;
if(isPythonOut(n,pythonCurrentRow,pythonCurrentCol)){
pythonCurrentRow = 0;
}
if(matrix[pythonCurrentRow][pythonCurrentCol].equals("e")) {
System.out.println("You lose! Killed by an enemy!");
isKilled = true;
break;
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("f")) {
eatenFood++;
if(eatenFood == countF){
System.out.printf("You win! Final python length is %d", pythonLength);
isEaten = true;
break;
}
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("*")){
pythonLength++;
}
} else if (command.equals("left")) {
pythonCurrentCol--;
if(isPythonOut(n,pythonCurrentRow,pythonCurrentCol)){
pythonCurrentCol = n-1;
}
if(matrix[pythonCurrentRow][pythonCurrentCol].equals("e")) {
System.out.println("You lose! Killed by an enemy!");
isKilled = true;
break;
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("f")) {
eatenFood++;
if(eatenFood == countF){
System.out.printf("You win! Final python length is %d", pythonLength);
isEaten = true;
break;
}
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("*")){
pythonLength++;
}
} else if (command.equals("right")) {
pythonCurrentCol++;
if(isPythonOut(n,pythonCurrentRow,pythonCurrentCol)){
pythonCurrentCol = 0;
}
if(matrix[pythonCurrentRow][pythonCurrentCol].equals("e")) {
System.out.println("You lose! Killed by an enemy!");
isKilled = true;
break;
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("f")) {
eatenFood++;
if(eatenFood == countF){
System.out.printf("You win! Final python length is %d", pythonLength);
isEaten = true;
break;
}
}else if(matrix[pythonCurrentRow][pythonCurrentCol].equals("*")){
pythonLength++;
}
}
}
if(!isKilled && !isEaten) {
if (countF > eatenFood) {
System.out.printf("You lose! There is still %d food to be eaten.%n", countF - eatenFood);
}
}
}
private static boolean isPythonOut(int n, int currentPlayerRow, int currentPlayersCol) {
if (currentPlayerRow > n - 1 || currentPlayerRow < 0 || currentPlayersCol > n - 1 || currentPlayersCol < 0) {
return true;
}
return false;
}
}
//⦁ *– that is a regular asterisk; it does nothing
//⦁ e – represents an enemy.
//⦁ f – this is the food
//⦁ s - the place where the game starts