-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.java
157 lines (147 loc) · 3.99 KB
/
Minesweeper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Minesweeper the Game
*
* @author Gabriel Murillo
* @version 11.14.2016
*/
import java.lang.Math;
public class Minesweeper{
int[] board;
int rows;
int columns;
public Minesweeper(int rows, int columns)
{
this.rows = rows;
this.columns = columns;
board = new int[rows*columns];
}
public Minesweeper()
{
this(10, 10);
board[95] = 9;
board[5] = 9;
board[49] = 9;
board[30] = 9;
board[0] = 9;
}
public void printSquareArray()
{
System.out.println();
System.out.println("M I N E S W E E P E R");
for (int i = 0; i < board.length ; i++)
{
if (i%columns == 0) {
System.out.println();
}
if (board[i] >= 9)
{
System.out.print("X ") ;
}
else {
System.out.print(board[i] + " ");
}
}
}
public void addBombs(int bombs)
{
while (bombs > 0)
{
int pos = (int)(Math.random()*(board.length + 1));
try
{
if (board[pos] != 9) {
board[pos] = 9;
bombs -= 1;
}
} catch (java.lang.ArrayIndexOutOfBoundsException e){
System.out.println();
}
}
}
public void addNums() {
for (int i = 0; i < board.length; i++)
{
try
{
if (board[i] == 9)
{
//Left
if ((board[i-1] < 9 && i%columns != 0))
{
board[i-1] ++;
}
//Right
if (board[i+1] < 9 && i%columns != (columns -1) )
{
board[i+1] ++;
}
//Bottom
if (board[i+columns] < 9)
{
board[i+columns] ++;
}
//Top
if (board[i-columns] < 9)
{
board[i-columns] ++;
}
//Top Right (Corner)
if (board[i-columns + 1] < 9 && i%columns != (columns -1))
{
board[i - columns +1 ] ++;
}
//Top Left (Corner)
if (board[i - columns - 1] < 9 && i%columns != 0)
{
board[i - columns - 1 ] ++;
}
//Bottom Right (Corner)
if (board[i+columns + 1] < 9 && i%columns != (columns -1))
{
board[i+columns + 1 ] ++;
}
//Bottom Left(Corner)
if (board[i+columns - 1] < 9 && i%columns != 0)
{
board[i + columns - 1 ] ++;
}
}
} catch(java.lang.ArrayIndexOutOfBoundsException e) {
System.out.println();
}
}
}
}
/*
* if ((board[i + 1] < 9) && i%columns != 0) { //right
board[i + 1] ++;
}
if ((board[i - 1] < 9) && (i%columns != 1))
{ //left
board[i - 1] ++;
}
if ((board[i + columns] < 9))
{ //down
board[i + columns] ++;
}
if ((board[i - columns] < 9))
{ //up
board[i - columns] ++;
}
if ((board[i + columns + 1 ]) < 9 && (i%columns != 0))
{ //down right
board[i + columns + 1] ++;
}
if ((board[i + columns - 1 ]) < 9 && (i%columns != 1))
{ //down left
board[i + columns - 1] ++;
}
if ((board[i - columns + 1 ]) < 9 && (i%columns != 0))
{ //up right
board[i - columns + 1] ++;
}
if ((board[i - columns - 1 ] < 9) && (i%columns != 1))
{ //up left
board[i - columns - 1] ++;
}
*/