-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWoo.java
More file actions
190 lines (160 loc) · 5.42 KB
/
Copy pathWoo.java
File metadata and controls
190 lines (160 loc) · 5.42 KB
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
181
182
183
184
185
186
187
188
189
190
import java.util.*;
public class Woo{
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_RESET = "\u001B[0m";
public static void main(String[] args) {
Scanner response = new Scanner(System.in);
int r, c, m; //row, column, mines
String input;
long startT, endT, resultT; //record time
//display start page/description
System.out.println("======================");
System.out.println("WELCOME TO MINESWEEPER");
System.out.println("======================");
String desc =
"The classic version of the game consists of a board of " +
"hidden squares and randomly placed mines underneath some squares.\n" +
"The player can choose to select a square to reveal," +
" upon which the board will either reveal that square \n" +
"by showing the total number of mines directly or diagonally " +
"adjacent to it, or the player loses because they selected a mine. \n" +
"The goal is to reveal the entire board without selecting a mine. " +
"A player can also select a square to flag it as a mine.";
String desc2 =
"WASD(Arrow Keys): One square of the board will be highlighted in red/blue,\n" +
"which the player can control with WASD and enter 'e' or 'f' to reveal or flag.\n" +
"Without WASD: Column/row indices (alphabet/number) will be printed on the side\n" +
"of the board, and the player can input a certain code for the move they want to make.";
System.out.print(ANSI_BLUE);
System.out.println(desc);
System.out.println();
System.out.println(desc2);
System.out.println();
System.out.print(ANSI_RESET);
//choose board
System.out.println("Choose your board size.");
System.out.println();
System.out.print("Do you want the default board? (y/n): ");
input = response.nextLine();
if (input.equals("y")) {
//set to default board
r = 10;
c = 10;
m = 16;
System.out.println();
}
//set your own board
else if (input.equals("n")) {
System.out.print("Choose length: ");
input = response.nextLine();
try {
r = Integer.parseInt(input);
} catch (Exception e) {
System.out.println("Invalid input. Length set to 10.");
r = 10;
}
System.out.print("Choose width: ");
input = response.nextLine();
try {
c = Integer.parseInt(input);
} catch (Exception e) {
System.out.println("Invalid input. Width set to 10.");
c = 10;
}
System.out.print("Number of mines: ");
input = response.nextLine();
try {
m = Integer.parseInt(input);
} catch (Exception e) {
System.out.println("Invalid input. Random number of mines.");
m = (int)(Math.random()*(r*c/6.0));
}
System.out.println();
} else {//neither y or n. Set to default board.
r = 10;
c = 10;
m = 16;
System.out.println("There was a problem with your input." +
" You'll get the default board.");
System.out.println();
}
//make an instance of Minesweeper
Minesweeper you;
if (r >= 1 && c >= 1 && r <= 99 && c <=49 && m >= 0 && m <= r*c) {
you = new Minesweeper(r, c, m);
} else {
System.out.println("There was a problem with your input." +
" You'll get the default board.");
you = new Minesweeper(10, 10, 16);
}
you.svC = 1;
you.svR = 1;
//set game board
System.out.print("Do you want to use your arrow keys? (y/n): ");
input = response.nextLine();
//set to WASD(arrowkey) mode
if (input.toLowerCase().equals("y")) {
you.playMode = 0;
System.out.println();
you.printInstr(0);
} else if (input.toLowerCase().equals("n")) { //set to typing mode
you.playMode = 1;
System.out.println();
you.printInstr(1);
} else { //set to typing mode
System.out.println("I'll take that as a no.");
you.playMode = 1;
you.printInstr(1);
}
startT = System.currentTimeMillis(); //start timer
//WASD(arrowkey) mode
if (you.playMode == 1) {
while(you.playState == 0) {
you.printBoardI();
System.out.print("your move: ");
input = response.nextLine();
try {
you.processInputI(input);
} catch (Exception e) {
System.out.println("INVALID MOVE. Type 'p' for play guide.");
System.out.println();
}
//print help guide
if (input.equals("p")) {
you.printInstr(1);
}
if (you.playState == 0) {
you.checkWin();
} else {}
}
} else { //Typing mode
while(you.playState == 0) {
you.printBoardC();
System.out.println("type 'p' for play guide.");
System.out.print("your move: ");
input = response.nextLine();
//print play guide
if (input.equals("p")){
you.printInstr(0);
}
try {
you.processInputC(input);
} catch (Exception e) {
}
if (you.playState == 0) {
you.checkWin();
} else {}
}
}
if (you.playState == 1) {
endT = System.currentTimeMillis(); //end timer
you.win(); //!!
//print time
resultT = (int)((endT - startT)/1000);
System.out.println("Your time: " + resultT + " seconds");
} else {
endT = System.currentTimeMillis(); //end timer
you.lose(); //smh
}
}//end of main
}//end of class