Main:
import java.util.*;
public class Main {
private static Map<Integer, Integer> shots = new HashMap<>();
private static GameMap<String> gameMap;
public static void main(String[] args) {
gameMap = new GameMap<>(String.class);
userInteraction();
fillXInDots();
gameMap.printMap();
}
private static void userInteraction(){
System.out.println("Podaj liczbę prób w formacie \"1 1\"");
Scanner scanner = new Scanner(System.in);
int numberOfSteps = scanner.nextInt();
ValuesParser parseAttempts = new ValuesParser(numberOfSteps);
if (!parseAttempts.isNumberOfAttemptsCorrect()) {
System.out.println("Liczba prób nie może byc większa niż 1000 i mniejsza niż 1 \nSpróbuj jeszcze raz!");
return;
}
for (int i = 0; i < numberOfSteps; i++) {
if (fillShotsMap(scanner, i)) break;
}
}
private static boolean fillShotsMap(Scanner scanner, int i) {
System.out.println("Podaj " + i + " współrzędne strzału");
Integer userShot1 = scanner.nextInt();
Integer userShot2 = scanner.nextInt();
ValuesParser parseCoordinates = new ValuesParser(userShot1, userShot2);
if (!parseCoordinates.areCoordinatesCorrect()) {
System.out.println("Nieprawidłowe współrzędne");
return true;
}
shots.put(userShot1, userShot2);
return false;
}
private static void fillXInDots() {
shots.forEach((key, value) -> gameMap.changeDotForX(key - 1, value - 1));
}
}
GameMap:
import java.lang.reflect.Array;
import java.util.Arrays;
public class GameMap<T> {
private T[][] defaultMap;
private static final int MAP_SIZE = 8;
public GameMap(Class<T> clazz) {
this.defaultMap = (T[][]) Array.newInstance(clazz, MAP_SIZE, MAP_SIZE);
fillMap();
}
private void fillMap() {
for (T[] row : defaultMap)
Arrays.fill(row, Option.DOT.toString());
}
public void printMap() {
System.out.println("Twoje próby:");
for (T[] aDefaultMap : defaultMap) {
for (T anADefaultMap : aDefaultMap) {
System.out.print(anADefaultMap + " ");
}
System.out.println();
}
}
public void changeDotForX(int row, int cell) {
defaultMap[row][cell] = (T) Option.X.toString();
}
}
Parser:
public class ValuesParser {
private int attempts = 0;
private int firstValue = 0;
private int secondValue = 0;
public ValuesParser(int attempts) {
this.attempts = attempts;
}
public ValuesParser(int firstValue, int secondValue) {
this.firstValue = firstValue;
this.secondValue = secondValue;
}
public boolean isNumberOfAttemptsCorrect() {
return !(attempts > 1000 && attempts < 1);
}
public boolean areCoordinatesCorrect() {
return (!(firstValue < 1 && firstValue > 8) || !(secondValue < 1 && secondValue > 8));
}
}
Opcje:
public enum Option {
DOT("."),
X("X");
private final String name;
Option(String s) {
name = s;
}
}