Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- /**
- * Created by rickardkruusberg on 10/02/16.
- */
- public class Main {
- public static void main(String[] args) {
- int[] params;
- while (true) {
- try {
- params = getGameParams();
- break;
- } catch (Exception e) {
- System.out.println("Sisestasite midagi valesti, proovige uuesti!");
- }
- }
- int[][] map = makeMap(params[0], params[1], params[2]);
- startGame(map, params[2]);
- }
- public static void startGame(int[][] map, int treasuresCount) {
- System.out.println("Head kaevamist!");
- Scanner sc = new Scanner(System.in);
- int digCount = 0;
- int treasuresLeft = treasuresCount;
- int[] coordinates;
- while (true) {
- System.out.println("Tehtud on " + digCount + " kaevamist ja aardeid on jäänud " + treasuresLeft + " tükki!");
- System.out.println("Mida kaevame? ");
- while (true) {
- try {
- coordinates = makeMove(sc, map.length, map[0].length);
- break;
- } catch (Exception e) {
- System.out.println("Sisestasite valed koordinaadid, proovige uuesti!");
- }
- }
- if (map[coordinates[0]][coordinates[1]] == 1) {
- System.out.println("AARE!");
- map[coordinates[0]][coordinates[1]] = 3;
- treasuresLeft--;
- } else if (map[coordinates[0]][coordinates[1]] == 2) {
- System.out.println("Siit oled juba kaevanud!");
- } else {
- map[coordinates[0]][coordinates[1]] = 2;
- }
- digCount++;
- System.out.println(getMapForPrint(map));
- if (treasuresLeft <= 0) {
- System.out.println("Mäng on läbi! Aardeid leitud - " + treasuresCount +
- ", ja kaevamisi tehtud- " + digCount);
- return;
- }
- }
- }
- public static String getMapForPrint(int[][] map) {
- String mapString = "";
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[0].length; j++) {
- mapString += map[i][j] + " ";
- }
- mapString += "\n";
- }
- return mapString;
- }
- public static int[][] makeMap(int m, int n, int t) {
- Random random = new Random();
- int[][] map = new int[m][n];
- for (int i = t; i > 0; i--) {
- int x = random.nextInt(m);
- int y = random.nextInt(n);
- if (map[x][y] == 1) {
- i++;
- continue;
- }
- map[x][y] = 1;
- }
- return map;
- }
- public static int[] getGameParams() {
- Scanner sc = new Scanner(System.in);
- int m;
- int n;
- int t;
- while (true) {
- System.out.println("Sisesta mängulaua ridade, veergude ja aarete arv: ");
- m = sc.nextInt();
- n = sc.nextInt();
- t = sc.nextInt();
- if (m*n > 0 && m != 0 && (t > 0 && t <= m*n)) {
- break;
- }
- }
- return new int[] {m, n, t};
- }
- public static int[] makeMove(Scanner sc, int boardSizeY, int boardSizeX) {
- int x;
- int y;
- while (true) {
- x = sc.nextInt();
- y = sc.nextInt();
- if ((y >= 0 && y <= boardSizeY) && (x >= 0 && x <= boardSizeX)) {
- break;
- }
- }
- return new int[] {x, y};
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement