Untitled
unknown
plain_text
2 years ago
3.4 kB
14
Indexable
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Archery_Champion {
public static boolean indexInRange (ArrayList<Integer> numbers, int index){
return index >= 0 && index < numbers.size();
}
public static int shootLeft (ArrayList<Integer> numbers, int index, int length){
if (length > numbers.size()) length = length % numbers.size();
int indexShoot = numbers.size() - length + index;
if (indexShoot > numbers.size()-1) indexShoot = indexShoot - numbers.size();
return indexShoot;
}
public static int shootRight (ArrayList<Integer> numbers, int index, int length){
if (length > numbers.size()) length = length % numbers.size();
int indexShoot = index + length;
if (indexShoot > numbers.size()-1) indexShoot = indexShoot - numbers.size();
return indexShoot;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
String[] textParts = text.split("\\|");
ArrayList<Integer> targets = new ArrayList<>();
int points;
int totalPoints = 0;
for (String part : textParts){
targets.add(Integer.parseInt(part));
}
while (true){
String input = sc.nextLine();
String[] inputParts = input.split("@");
String command = inputParts[0];
if (command.equals("Game over")) break;
if (command.equals("Shoot Left")){
int index = Integer.parseInt(inputParts[1]);
int length = Integer.parseInt(inputParts[2]);
if(indexInRange(targets, index)){
int indexShoot = shootLeft(targets, index, length);
int newValue = targets.get(indexShoot) - 5;
points = 5;
if (newValue < 0) {
points = 5 - Math.abs(newValue);
newValue = 0;
}
targets.set(indexShoot, newValue);
totalPoints = totalPoints + points;
}
}
else if (command.equals("Shoot Right")) {
int index = Integer.parseInt(inputParts[1]);
int length = Integer.parseInt(inputParts[2]);
if (indexInRange(targets, index)) {
int indexShoot = shootRight(targets, index, length);
int newValue = targets.get(indexShoot) - 5;
points = 5;
if (newValue < 0) {
points = 5 - Math.abs(newValue);
newValue = 0;
}
targets.set(indexShoot, newValue);
totalPoints = totalPoints + points;
}
}
else if (command.equals("Reverse")) {
Collections.reverse(targets);
}
}
for (int i = 0; i < targets.size()-1; i++) {
System.out.printf("%d - ", targets.get(i));
}
System.out.printf("%d\n", targets.get(targets.size()-1));
System.out.printf("John finished the archery tournament with %d points!", totalPoints);
}
}
Editor is loading...