-
Notifications
You must be signed in to change notification settings - Fork 0
/
hellSweeper.java
180 lines (167 loc) · 5.39 KB
/
hellSweeper.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.lang.Math.*;
import java.util.Scanner;
/**
* Title: Minesweeper
* Description: A game where you try to avoid mines.
* Authors: Tamir Enkjargle, Dane Miller, Otakar Andrysek
* Date: 2/13/17
*/
public class HellSweeper
{
// Initialize board (this is a 5x5 board, two extra rows and column are added for padding)
// NEW: The board can now be any size.
static int[][] board = new int[7][7];
// The board that the user sees
static String[][] visableBoard = new String[board.length - 2][board.length - 2];
// The number of mines left on the board
static int safeSpots = 0;
/**
* This method gets user input
*/
public static int getInput()
{
Scanner reader = new Scanner(System.in);
int input = reader.nextInt(); // Scans the input as an int
reader.close();
return input;
}
/**
* This method prints the hidden board as an (n*m) matrix
* NOTE: No current application except for testing
*/
public static void printRealBoard()
{
for (int i = 0; i < board.length - 1; i++)
{
for (int j = 0; j < board.length - 1; j++)
{
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
/**
* This method prints the game board as an (n*m) matrix
*/
public static void printBoard()
{
for (int i = 0; i < visableBoard.length - 1; i++)
{
for (int j = 0; j < visableBoard.length - 1; j++)
{
System.out.print(visableBoard[i][j] + " ");
}
System.out.println();
}
}
/**
* This method check nearby spots for mines and returns the number of mines in the nearby 3x3 area
*/
public static int showNearby(int row, int col)
{
int nearbyMines = 0;
// Check the top - left corner
if (board[row + 1][col - 1] == 1)
{
nearbyMines += 1;
}
// Check the top - right corner
if (board[row + 1][col + 1] == 1)
{
nearbyMines += 1;
}
// Check the bottom - left corner
if (board[row - 1][col - 1] == 1)
{
nearbyMines += 1;
}
// Check the bottom - right corner
if (board[row - 1][col + 1] == 1)
{
nearbyMines += 1;
}
// Check the top side
if (board[row + 1][col] == 1)
{
nearbyMines += 1;
}
// Check the bottom side
if (board[row - 1][col] == 1)
{
nearbyMines += 1;
}
// Check the left side
if (board[row][col - 1] == 1)
{
nearbyMines += 1;
}
// Check the right side
if (board[row][col + 1] == 1)
{
nearbyMines += 1;
}
return nearbyMines;
}
/**
* This method is the main game run code
*/
public static void main(String[] args)
{
for (int i = 1; i < board.length - 2; i++)
{
// Set the i'th row of the first and last column to zero
board[i][0] = 0;
board[i][board.length - 1] = 0;
for (int j = 1; j < board.length - 2; j++)
{
// Set the j'th column of the first and last row to zero
board[0][j] = 0;
board[board.length - 1][j] = 0;
// This generates a random number 0 | 1, 1 indicates there is a bomb, 0 is safe
int isBomb = (int)(Math.random() * 2);
if(isBomb == 0)
{
// Increment safeSpots, the number of squares without mines
safeSpots += 1;
}
// Set the board to contain (or not contain) the mines
board[i][j] = isBomb;
visableBoard[i-1][j-1] = "x";
}
}
// The main game loop
boolean playing = true;
while (playing == true)
{
// Print the baord to the user
printBoard();
// Scan for row
System.out.println("Enter row number: ");
int row = getInput();
// Scan for column
System.out.println("Enter column number: ");
int col = getInput();
// RIP, the user landed on a mine
if (board[row][col] == 1)
{
System.out.println("Game over. Welcome to Hellsweeper, the game with literally no strategy or skill.");
printRealBoard();
playing = false;
}
// Looks like they got lucky
else
{
showNearby(row, col);
safeSpots -= 1;
visableBoard[row-1][col-1] = Integer.toString(showNearby(row, col));
System.out.println("You're not dead yet.");
// Maybe there are no more mines left?
if(safeSpots <= 0)
{
System.out.println("WINNER WINNER MAD LUCK SUPER CHEATS ALERT");
playing = false;
}
}
}
}
}