Browse Source

Initial commit

Tankernn 8 years ago
commit
ffef8c6c8e

+ 26 - 0
.classpath

@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

+ 7 - 0
.gitignore

@@ -0,0 +1,7 @@
+/bin/
+/target/
+/res/
+res
+.project
+.settings
+.DS_Store

+ 52 - 0
pom.xml

@@ -0,0 +1,52 @@
+<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>eu.tankernn.minesweeper</groupId>
+	<artifactId>tankernn-minesweeper</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<name>Tankernn Minesweeper</name>
+	<description>A simple minesweeper interpretation.</description>
+
+	<repositories>
+		<repository>
+			<id>tankernn</id>
+			<name>Tankernn Maven Repository</name>
+			<url>http://repo.maven.tankernn.eu</url>
+		</repository>
+	</repositories>
+
+	<dependencies>
+		<dependency>
+			<groupId>eu.tankernn.gameEngine</groupId>
+			<artifactId>tankernn-game-engine</artifactId>
+			<version>1.2</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.5.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>com.googlecode.mavennatives</groupId>
+				<artifactId>maven-nativedependencies-plugin</artifactId>
+				<version>0.0.6</version>
+				<executions>
+					<execution>
+						<id>unpacknatives</id>
+						<phase>package</phase>
+						<goals>
+							<goal>copy</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>

+ 169 - 0
src/main/java/eu/tankernn/mines/Mines.java

@@ -0,0 +1,169 @@
+package eu.tankernn.mines;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.Scanner;
+
+import org.lwjgl.util.vector.Vector2f;
+
+import eu.tankernn.gameEngine.GameLauncher;
+import eu.tankernn.gameEngine.TankernnGame;
+import eu.tankernn.gameEngine.loader.textures.Texture;
+import eu.tankernn.gameEngine.renderEngine.gui.GuiRenderer;
+import eu.tankernn.gameEngine.renderEngine.gui.GuiTexture;
+import eu.tankernn.mines.Tile.TileState;
+
+public class Mines extends TankernnGame {
+	public static String GAME_NAME = "Minesweeper";
+
+	private Random rand = new Random();
+	private Scanner sc = new Scanner(System.in);
+	private GuiRenderer renderer;
+	
+	private Texture hidden, checked, exploded, flagged;
+
+	private int boardWidth, boardHeight;
+	private Tile[][] tiles;
+	private Pos[] pattern = {new Pos(0, 1), new Pos(0, -1), new Pos(1, 0), new Pos(1, 1), new Pos(1, -1), new Pos(-1, 0), new Pos(-1, -1), new Pos(-1, 1)};
+	private int totalMines;
+	private boolean running;
+
+	public Mines(String name) {
+		super(name);
+		renderer = new GuiRenderer(loader);
+
+		totalMines = 10;
+		boardWidth = boardHeight = 7;
+
+		startGame();
+	}
+
+	public void startGame() {
+		List<Pos> minePositions = new ArrayList<Pos>();
+		for (int i = 0; i < totalMines; i++) {
+			minePositions.add(new Pos(rand.nextInt(boardWidth), rand.nextInt(boardHeight)));
+		}
+
+		tiles = new Tile[boardWidth][boardHeight];
+
+		for (int y = 0; y < boardHeight; y++) {
+			for (int x = 0; x < boardWidth; x++) {
+				tiles[x][y] = new Tile(minePositions.contains(new Pos(x, y)), new Pos(x, y));
+			}
+		}
+		
+		running = true;
+	}
+
+	public void check(Tile tile) {
+		if (running)
+			if (tile.isMine)
+				lose();
+			else
+				calculateMinesAround(tile);
+	}
+
+	public void calculateMinesAround(Tile tile) {
+		int count = 0;
+		List<Tile> testTiles = new ArrayList<Tile>();
+
+		for (int i = 0; i < pattern.length; i++) {
+			try {
+				testTiles.add(tiles[tile.pos.x + pattern[i].x][tile.pos.y + pattern[i].y]);
+			} catch (ArrayIndexOutOfBoundsException e) {
+				continue;
+			}
+		}
+
+		for (Tile testTile : testTiles)
+			if (testTile.isMine)
+				count++;
+
+		tile.setMinesAround(count);
+
+		if (count == 0)
+			for (Tile testTile : testTiles)
+				if (testTile.getState().equals(Tile.TileState.HIDDEN))
+					calculateMinesAround(testTile);
+	}
+
+	private void lose() {
+		running = false;
+		for (Tile[] tArr : tiles)
+			for (Tile t : tArr)
+				if (t.isMine)
+					t.setState(TileState.EXPLODED);
+	}
+
+	@Override
+	public void update() {
+		
+		String[] command = sc.nextLine().split(" ");
+		
+		int x = Integer.parseInt(command[1]), y = Integer.parseInt(command[2]);
+		
+		switch (command[0]) {
+		case "flag":
+			tiles[x][y].toggleFlag();
+			break;
+		case "check":
+			check(tiles[x][y]);
+			break;
+		default:
+			System.out.println("Unknown command.");
+		}
+		
+		super.update();
+	}
+
+	@Override
+	public void render() {
+		List<GuiTexture> toRender = new ArrayList<GuiTexture>();
+		
+		for (int y = 0; y < boardHeight; y++) {
+			for (int x = 0; x < boardWidth; x++) {
+				Tile t = tiles[x][y];
+				System.out.print(t.getState().equals(TileState.CHECKED) ? Integer.toString(t.getMinesAround()) : t.getState().appearance);
+				Texture tex;
+				switch (t.getState()) {
+				case CHECKED:
+					tex = checked;
+					break;
+				case EXPLODED:
+					tex = exploded;
+					break;
+				case FLAGGED:
+					tex = flagged;
+					break;
+				case HIDDEN:
+					tex = hidden;
+					break;
+				default:
+					tex = hidden;
+					break;
+				}
+				toRender.add(new GuiTexture(tex, new Vector2f(), new Vector2f()));
+			}
+			System.out.println();
+		}
+		System.out.println("-------------------");
+		
+		
+		renderer.render(toRender);
+		
+
+		super.render();
+	}
+	
+	@Override
+	public void cleanUp() {
+		sc.close();
+		super.cleanUp();
+	}
+
+	public static void main(String[] args) {
+		GameLauncher.init(GAME_NAME, 800, 800);
+		GameLauncher.launch(new Mines(GAME_NAME));
+	}
+}

+ 19 - 0
src/main/java/eu/tankernn/mines/Pos.java

@@ -0,0 +1,19 @@
+package eu.tankernn.mines;
+
+public class Pos {
+	public final int x, y;
+
+	public Pos(int x, int y) {
+		this.x = x;
+		this.y = y;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if (obj instanceof Pos) {
+			Pos other = (Pos) obj;
+			return other.x == this.x && other.y == this.y;
+		} else
+			return false;
+	}
+}

+ 47 - 0
src/main/java/eu/tankernn/mines/Tile.java

@@ -0,0 +1,47 @@
+package eu.tankernn.mines;
+
+public class Tile {
+	public final boolean isMine;
+	private TileState state = TileState.HIDDEN;
+	private int minesAround;
+	public final Pos pos;
+	
+	public Tile(boolean mine, Pos pos) {
+		this.isMine = mine;
+		this.pos = pos;
+	}
+	
+	public void setMinesAround(int minesAround) {
+		this.minesAround = minesAround;
+		this.state = TileState.CHECKED;
+	}
+	
+	public int getMinesAround() {
+		return minesAround;
+	}
+	
+	public TileState getState() {
+		return state;
+	}
+	
+	public void setState(TileState state) {
+		this.state = state;
+	}
+	
+	public void toggleFlag() {
+		if (this.state.equals(TileState.HIDDEN))
+			this.setState(TileState.FLAGGED);
+		else
+			this.setState(TileState.HIDDEN);
+	}
+
+	public enum TileState {
+		HIDDEN('*'), CHECKED(' '), EXPLODED('X'), FLAGGED('P');
+		
+		public final char appearance;
+		
+		private TileState(char value) {
+			appearance = value;
+		}
+	}
+}