Commit 7278f315 authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

3. prednaska

parent b5d879cd
package sk.tuke.gamestudio.game.mines;
import sk.tuke.gamestudio.game.mines.consoleui.ConsoleUI;
import sk.tuke.gamestudio.game.mines.core.Field;
public class Main {
public static void main(String[] args) {
Field field = new Field(9, 9, 1);
ConsoleUI ui = new ConsoleUI(field);
ui.play();
}
}
......@@ -27,6 +27,8 @@ public class Test {
else
System.out.print("M");
break;
default:
throw new IllegalArgumentException("Unsupported tile state " + tile.getState());
}
}
System.out.println();
......
package sk.tuke.gamestudio.game.mines.consoleui;
import sk.tuke.gamestudio.game.mines.core.Clue;
import sk.tuke.gamestudio.game.mines.core.Field;
import sk.tuke.gamestudio.game.mines.core.GameState;
import sk.tuke.gamestudio.game.mines.core.Tile;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConsoleUI {
private static final Pattern INPUT_PATTERN = Pattern.compile("([OM])([A-I])([1-9])");
private final Field field;
public ConsoleUI(Field field) {
this.field = field;
}
public void play() {
do {
printField();
processInput();
} while (field.getState() == GameState.PLAYING);
printField();
if (field.getState() == GameState.SOLVED) {
System.out.println("You won!!!");
} else
System.out.println("You failed!!!");
}
private void printField() {
printFieldHeader();
printFieldBody();
}
private void printFieldHeader() {
System.out.print(" ");
for (int column = 0; column < field.getColumnCount(); column++) {
System.out.print(" ");
System.out.print(column + 1);
}
System.out.println();
}
private void printFieldBody() {
for (int row = 0; row < field.getRowCount(); row++) {
System.out.print((char) ('A' + row));
for (int column = 0; column < field.getColumnCount(); column++) {
System.out.print(" ");
printTile(row, column);
}
System.out.println();
}
}
private void printTile(int row, int column) {
final Tile tile = field.getTile(row, column);
switch (tile.getState()) {
case CLOSED:
System.out.print("-");
break;
case MARKED:
System.out.print("M");
break;
case OPEN:
if (tile instanceof Clue)
System.out.print(((Clue) tile).getValue());
else
System.out.print("X");
break;
default:
throw new IllegalArgumentException("Unsupported tile state " + tile.getState());
}
}
protected void processInput() {
while (true) {
System.out.print("Enter input (e.g. MA0, OB3, X): ");
String input = new Scanner(System.in).nextLine().trim().toUpperCase();
if ("X".equals(input))
System.exit(0);
Matcher matcher = INPUT_PATTERN.matcher(input);
if (matcher.matches()) {
try {
int row = matcher.group(2).charAt(0) - 'A';
int column = Integer.parseInt(matcher.group(3)) - 1;
if (row >= 0 && row < field.getRowCount() && column >= 0 && column < field.getColumnCount()) {
if ("O".equals(matcher.group(1))) {
field.openTile(row, column);
return;
} else {
field.markTile(row, column);
return;
}
}
} catch (NumberFormatException e) {
//Jaro Porubän: Ocakavana vynimka ak je zadany vstup zle
}
}
}
}
}
......@@ -13,6 +13,8 @@ public class Field {
private GameState state = GameState.PLAYING;
private int numberOfOpenTiles;
public Field(int rowCount, int columnCount, int mineCount) {
this.rowCount = rowCount;
this.columnCount = columnCount;
......@@ -22,6 +24,7 @@ public class Field {
throw new IllegalArgumentException("Too many mines for the field");
tiles = new Tile[rowCount][columnCount];
generate();
}
......@@ -95,29 +98,43 @@ public class Field {
}
public void markTile(int row, int column) {
final Tile tile = tiles[row][column];
if (tile.getState() == TileState.CLOSED) {
tile.setState(TileState.MARKED);
} else if (tile.getState() == TileState.MARKED) {
tile.setState(TileState.CLOSED);
if (state == GameState.PLAYING) {
final Tile tile = tiles[row][column];
if (tile.getState() == TileState.CLOSED) {
tile.setState(TileState.MARKED);
} else if (tile.getState() == TileState.MARKED) {
tile.setState(TileState.CLOSED);
}
}
}
public void openTile(int row, int column) {
final Tile tile = tiles[row][column];
if (tile.getState() == TileState.CLOSED) {
tile.setState(TileState.OPEN);
if (tile instanceof Mine) {
state = GameState.FAILED;
return;
}
if (tile instanceof Clue && ((Clue) tile).getValue() == 0) {
openNeighbourTiles(row, column);
if (state == GameState.PLAYING) {
final Tile tile = tiles[row][column];
if (tile.getState() == TileState.CLOSED) {
tile.setState(TileState.OPEN);
numberOfOpenTiles++;
if (tile instanceof Mine) {
state = GameState.FAILED;
return;
}
if (tile instanceof Clue && ((Clue) tile).getValue() == 0) {
openNeighbourTiles(row, column);
}
if (isSolved()) {
state = GameState.SOLVED;
}
}
}
}
private boolean isSolved() {
return rowCount * columnCount - mineCount == numberOfOpenTiles;
}
private void openNeighbourTiles(int row, int column) {
if (positionExists(row - 1, column - 1)) openTile(row - 1, column - 1);
if (positionExists(row - 1, column)) openTile(row - 1, column);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment