View difference between Paste ID: 69qjB6Dp and xq0GntjV
SHOW: | | - or go back to the newest paste.
1
import java.util.Random;
2
import java.util.Scanner;
3
4-
public class Helper {
4+
public class asd {
5
	static int row = 0;
6
	static int col = 0;
7
	static int numberOfMines = 0;
8
9
	public static void main(String[] args) {
10
11
		Scanner sc = new Scanner(System.in);
12
		row = sc.nextInt();
13
		col = sc.nextInt();
14
		numberOfMines = sc.nextInt();
15
		sc.close();
16
17
		int[][] mineField = new int[row][col];
18
19
		placeMines(mineField);
20
		
21-
		calcNumbers(mineFiled);
21+
		calcNumbers(mineField);
22
		
23
		
24
		countMines(mineField);
25
26
		printBoard(mineField);
27
28
	}
29-
	public static void calcNumbers(int[][] board) {
29+
30-
		for (int i = 0; i < board.length; i++) {
30+
	public static void calcNumbers(int[][] mineFiled) {
31-
			for (int j = 0; j < board[i].length; j++) {
31+
		for (int i = 0; i < mineFiled.length; i++) {
32-
				if (board[i][j] != 9) {
32+
			for (int j = 0; j < mineFiled[i].length; j++) {
33-
					board[i][j] = minesNear(board, i, j);
33+
				if (mineFiled[i][j] != 9) {
34
					mineFiled[i][j] = minesNear(mineFiled, i, j);
35
				}
36
			}
37
		}
38
	}
39-
	public static int minesNear(int[][] board, int y, int x) {
39+
40
	public static int minesNear(int[][] mineFiled, int y, int x) {
41
		int mines = 0;
42-
		mines += mineChecker(board, x - 1, y - 1); // NW
42+
43-
		mines += mineChecker(board, x - 1, y); // N
43+
		mines += mineChecker(mineFiled, x - 1, y - 1); // NW
44-
		mines += mineChecker(board, x - 1, y + 1); // NE
44+
		mines += mineChecker(mineFiled, x - 1, y); // N
45-
		mines += mineChecker(board, x, y - 1); // W
45+
		mines += mineChecker(mineFiled, x - 1, y + 1); // NE
46-
		mines += mineChecker(board, x, y + 1); // E
46+
		mines += mineChecker(mineFiled, x, y - 1); // W
47-
		mines += mineChecker(board, x + 1, y - 1); // SW
47+
		mines += mineChecker(mineFiled, x, y + 1); // E
48-
		mines += mineChecker(board, x + 1, y); // S
48+
		mines += mineChecker(mineFiled, x + 1, y - 1); // SW
49-
		mines += mineChecker(board, x + 1, y + 1); // SE
49+
		mines += mineChecker(mineFiled, x + 1, y); // S
50
		mines += mineChecker(mineFiled, x + 1, y + 1); // SE
51
		if (mines > 0) {
52
			// we're changing an int to a char
53
			// why?!
54
			// http://www.asciitable.com/
55
			// 48 is ASCII code for '0'
56
			return mines;
57
		} else {
58
			return 0;
59
		}
60
	}
61
62
	public static int mineChecker(int[][] mineField, int x, int y) {
63
		if ((x >= 0 && x < row) && y >= 0 && y < col && mineField[x][y] == 9) {
64
			return 1;
65
		} else {
66
			return 0;
67
		}
68
	}
69
70
	public static void placeMines(int[][] mineField) {
71
		Random random = new Random();
72
		int minesPlaced = 0;
73
74
		while (minesPlaced < numberOfMines) {
75
			int x = random.nextInt(row);
76
			int y = random.nextInt(col);
77
			// make sure we don't place a mine on top of another
78
			if (mineField[y][x] != 9) {
79
				mineField[y][x] = 9;
80
				minesPlaced++;
81
			}
82
		}
83
	}
84-
	public static void countMines(int[][] board) {
84+
85
	public static void countMines(int[][] mineFiled) {
86-
		for (int i = 0; i < board.length; i++) {
86+
87-
			for (int j = 0; j < board[i].length; j++) {
87+
		for (int i = 0; i < mineFiled.length; i++) {
88-
				if (board[i][j] == 9)
88+
			for (int j = 0; j < mineFiled[i].length; j++) {
89
				if (mineFiled[i][j] == 9)
90
					mines++;
91
			}
92
		}
93
94
		System.out.println(mines);
95
	}
96-
	public static void printBoard(int[][] board) {
96+
97-
		for (int i = 0; i < board.length; i++) {
97+
	public static void printBoard(int[][] mineFiled) {
98-
			for (int j = 0; j < board[i].length; j++) {
98+
		for (int i = 0; i < mineFiled.length; i++) {
99-
				System.out.print(board[i][j] + " ");
99+
			for (int j = 0; j < mineFiled[i].length; j++) {
100
				System.out.print(mineFiled[i][j] + " ");
101
			}
102
			System.out.println();
103
		}
104
	}
105
106
}