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

1. a 2. prednaska

parents
# Default ignored files
/workspace.xml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="gamestudio2020" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" project-jdk-name="13" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sk.tuke</groupId>
<artifactId>gamestudio</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>gamestudio</name>
<description>Gamestudio Project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>13</java.version>
<maven.compiler.source>1.13</maven.compiler.source>
<maven.compiler.target>1.13</maven.compiler.target>
</properties>
</project>
package sk.tuke.gamestudio.game.mines;
import sk.tuke.gamestudio.game.mines.core.Clue;
import sk.tuke.gamestudio.game.mines.core.Field;
import sk.tuke.gamestudio.game.mines.core.Tile;
public class Test {
public static void main(String[] args) {
Field field = new Field(9, 9, 3);
field.openTile(0, 0);
for (int row = 0; row < field.getRowCount(); row++) {
for (int column = 0; column < field.getColumnCount(); column++) {
Tile tile = field.getTile(row, column);
System.out.print(" ");
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("M");
break;
}
}
System.out.println();
}
}
}
package sk.tuke.gamestudio.game.mines.core;
public class Clue extends Tile {
private final int value;
public Clue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
package sk.tuke.gamestudio.game.mines.core;
import java.util.Random;
public class Field {
private final int rowCount;
private final int columnCount;
private final int mineCount;
private final Tile[][] tiles;
private GameState state = GameState.PLAYING;
public Field(int rowCount, int columnCount, int mineCount) {
this.rowCount = rowCount;
this.columnCount = columnCount;
this.mineCount = mineCount;
if (rowCount * columnCount <= mineCount)
throw new IllegalArgumentException("Too many mines for the field");
tiles = new Tile[rowCount][columnCount];
generate();
}
private void generate() {
generateMines();
fillWithClues();
}
private void generateMines() {
Random random = new Random();
int minesToStore = mineCount;
while (minesToStore > 0) {
int row = random.nextInt(rowCount);
int column = random.nextInt(columnCount);
if (tiles[row][column] == null) {
tiles[row][column] = new Mine();
minesToStore--;
}
}
}
private void fillWithClues() {
for (int row = 0; row < rowCount; row++) {
for (int column = 0; column < columnCount; column++) {
final Tile tile = tiles[row][column];
if (tile == null)
tiles[row][column] = new Clue(countNeighbourMines(row, column));
}
}
}
private int countNeighbourMines(int row, int column) {
int count = 0;
if (isMineOn(row - 1, column - 1)) count++;
if (isMineOn(row - 1, column)) count++;
if (isMineOn(row - 1, column + 1)) count++;
if (isMineOn(row, column - 1)) count++;
if (isMineOn(row, column + 1)) count++;
if (isMineOn(row + 1, column - 1)) count++;
if (isMineOn(row + 1, column)) count++;
if (isMineOn(row + 1, column + 1)) count++;
return count;
}
private boolean isMineOn(int row, int column) {
return positionExists(row, column) && tiles[row][column] instanceof Mine;
}
private boolean positionExists(int row, int column) {
return row >= 0 && row < rowCount && column >= 0 && column < columnCount;
}
public int getRowCount() {
return rowCount;
}
public int getColumnCount() {
return columnCount;
}
public int getMineCount() {
return mineCount;
}
public GameState getState() {
return state;
}
public Tile getTile(int row, int column) {
return tiles[row][column];
}
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);
}
}
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);
}
}
}
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);
if (positionExists(row - 1, column + 1)) openTile(row - 1, column + 1);
if (positionExists(row, column - 1)) openTile(row, column - 1);
if (positionExists(row, column + 1)) openTile(row, column + 1);
if (positionExists(row + 1, column - 1)) openTile(row + 1, column - 1);
if (positionExists(row + 1, column)) openTile(row + 1, column);
if (positionExists(row + 1, column + 1)) openTile(row + 1, column + 1);
//ine riesenie pomocou cyklov
// for (int drow = -1; drow <= 1; drow++) {
// for (int dcolumn = -1; dcolumn <= 1; dcolumn++) {
// int aRow = row + drow;
// int aColumn = column + dcolumn;
// if (aRow >= 0 && aRow < rowCount && aColumn >= 0 && aColumn < columnCount)
// openTile(aRow, aColumn);
// }
// }
}
}
package sk.tuke.gamestudio.game.mines.core;
public enum GameState {
PLAYING, SOLVED, FAILED
}
package sk.tuke.gamestudio.game.mines.core;
public class Mine extends Tile {
}
package sk.tuke.gamestudio.game.mines.core;
public abstract class Tile {
private TileState state = TileState.CLOSED;
public TileState getState() {
return state;
}
void setState(TileState state) {
this.state = state;
}
}
package sk.tuke.gamestudio.game.mines.core;
public enum TileState {
OPEN, CLOSED, MARKED
}
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