This repository was archived by the owner on Jun 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinesweeperTIO.java
More file actions
179 lines (159 loc) · 4.78 KB
/
MinesweeperTIO.java
File metadata and controls
179 lines (159 loc) · 4.78 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
package minesweeperCollab;
import java.util.Scanner;
//import TerminalIO.KeyboardReader;
/**
* TerminalIO to verify game functionality.
*
* @author Claire Shea
* @author 20cshea@westfordk12.us
* @author Jerry Xu
* @author 20jxu@westfordk12.us
* @author Ethan Mendes
* @author 20emendes@westfordk12.us
* @version 1.4
* @since 1.1
*/
public class MinesweeperTIO {
public static String printBoard(game Game) {
int xSize = Game.getDisplay().length;
int ySize = Game.getDisplay()[0].length;
String retVal = "\\x|";
for (int tens = 1; tens <= xSize; tens++) {
retVal += " " + Integer.toString(tens / 10);
}
retVal += "\ny\\|";
for (int ones = 1; ones <= xSize; ones++) {
retVal += " " + Integer.toString(ones % 10);
}
retVal += "\n--+-";
for (int xs = 1; xs <= xSize; xs++) {
retVal += "--";
}
for (int ys = 1; ys <= ySize; ys++) {
retVal += "\n" + Integer.toString(ys / 10) + Integer.toString(ys % 10) + "|";
for (int xs = 1; xs <= xSize; xs++) {
retVal += " ";
int status = Game.getDisplay()[xs - 1][ys - 1];
if (status == 0) retVal += " ";
else if (1 <= status && status <= 8) retVal += Integer.toString(status);
else if (status == -1) retVal += "#";
else if (status == 100) retVal += "^";
else if (status == 1000) retVal += "*";
}
}
/*
\x| 1 2 3 4 5 6 7 8 9 0 1
y\| 0 0 0 0 0 0 0 0 0 1 1
--+----------------------
01|
02|
03|
04|
05|
06|
07|
08|
09|
10|
*/
return retVal;
}
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
int statusTrigger = 0;
do {
try {
System.out.print("Enter -1 to quit, any other integer to continue: ");
statusTrigger = Integer.parseInt(sc.next());
if (statusTrigger == -1) {
System.out.println("Program ending...");
System.exit(0);
}
} catch (Exception e) {
System.out.println("Invalid number entered.");
sc = new Scanner(System.in);
continue;
}
int xdim = 0;
int ydim = 0;
int numMines = 0;
boolean firstTime = true;
do {
try {
if (!firstTime) System.out.println("One of your parameters was incorrect. Please try again");
firstTime = false;
System.out.print("X-Dimension of Game Board: ");
xdim = sc.nextInt();
System.out.print("Y-Dimension of Game Board: ");
ydim = sc.nextInt();
System.out.print("Number of mines on Game Board: ");
numMines = sc.nextInt();
} catch (Exception e) {
System.out.println("Invalid number entered.");
continue;
}
} while (xdim <= 4 || xdim >= 40 || ydim <= 4 || ydim >= 30 || numMines <= 0 || numMines >= xdim * ydim);
int xStart = -1;
int yStart = -1;
firstTime = true;
do {
try {
if (!firstTime) System.out.println("One of your parameters was incorrect. Please try again");
firstTime = false;
sc = new Scanner(System.in);
System.out.print("X-Dimension of Start Position: ");
xStart = sc.nextInt();
System.out.print("Y-Dimension of Start Position: ");
yStart = sc.nextInt();
} catch (Exception e) {
System.out.println("Invalid number entered.");
continue;
}
} while (xStart <= 0 || xStart > xdim || yStart <= 0 || yStart > ydim);
game Game = new game(xdim, ydim, numMines, xStart - 1, yStart - 1);
System.out.println(printBoard(Game));
int status = 0;
do {
firstTime = true;
int xCoord = -1;
int yCoord = -1;
boolean toMark = false;
do {
try {
if (!firstTime) System.out.println("One of your parameters was incorrect. Please try again");
firstTime = false;
sc = new Scanner(System.in);
System.out.print("X-Location of operation: ");
xCoord = sc.nextInt();
System.out.print("Y-Location of operation: ");
yCoord = sc.nextInt();
System.out.print("Operation (0 to dig, 1 to mark, 2 to quit): ");
int tempMark = sc.nextInt();
if (tempMark != 0 && tempMark != 1 && tempMark != 2) {
throw new Exception();
} else if (tempMark == 0) toMark = false;
else if (tempMark == 1) toMark = true;
else if(tempMark == 2) {
System.out.println("Program ending...");
System.exit(0);
}
} catch (Exception e) {
System.out.println("Invalid number entered.");
continue;
}
} while (xCoord <= 0 || xCoord > xdim || yCoord <= 0 || yCoord > ydim);
status = Game.gameState(xCoord - 1, yCoord - 1, toMark);
System.out.println(printBoard(Game));
} while (status == 0);
if (status == -1) {
System.out.println("You lose :(");
} else if (status == 1) {
System.out.println("You win! :)");
}
} while (statusTrigger != -1);
} catch(Exception e) {
System.out.println("Error");
}
}
}