![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
MineSweeper.java
I saw everyone else posting games they've made, so I decided to post a CLI game of Minesweeper I made some time ago. It's in java, and may contain some errors, because like I said I did it a while ago.
/*
file: MineSweeper.java
created: 11/24/04 11:26:39 PM
by: Ian(2roll4life7)
*/
import java.util.Random;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class MineSweeper {
public static void main(String args[])throws IOException, NumberFormatException {
class Board {
Random prng = new Random();
public Board(final int height, final int width, final int mines) {
this.height = height;
this.width = width;
this.mines = mines;
board = new String[height][width];
guessed = new String[height][width];
this.setBoard();
}
public boolean isDone() {
int minesFound=0;
for(int i=0; i<height; i++) {
for(int j=0; j<width; j++) {
if(guessed[i][j].equals("*")) {
return false;
}
if((guessed[i][j].equals("#")) && (board[i][j].equals("x"))) {
minesFound++;
if(minesFound == mines) {
return true;
}
}
}
}
return true;
}
public void printBoard() {
String padding = "";
for(int i=0; i<height; i++) {
if(i == 0) {
System.out.print(" ");
for(int k=0; k<width; k++) {
if(k>9 && k<100) {
padding = "";
} else {
padding = " ";
}
System.out.print(" |"+k+padding);
}
System.out.println(" |");
}
if(i>9 && i<100) {
padding = "";
} else {
padding = " ";
}
System.out.print(i+padding+"| ");
for(int j=0; j<width; j++) {
System.out.print(guessed[i][j]+" | ");
}
System.out.println();
}
System.out.println();
}
private void setBoard() {
for(int i=0; i<height; i++) {
for(int j=0; j<width; j++) {
board[i][j] = "0";
guessed[i][j] = "*";
}
}
if(mines > (height*width)) {
System.out.println("too many mines\n");
return;
}
while(mines > 0) {
int rnd1 = prng.nextInt(height),
rnd2 = prng.nextInt(width);
if(!(board[rnd1][rnd2].equals("x"))) {
board[rnd1][rnd2] = "x";
mines--;
}
}
for(int i=0; i<height; i++) {
for(int j=0; j<width; j++) {
if(board[i][j].equals("x")) {
if(j-1>=0&&j-1<board.length) {
if(!(board[i][j-1].equals("x"))) {
n=Integer.parseInt(board[i][j-1])+1;
board[i][j-1]=""+n;
}
}
if((i-1>=0&&i-1<board.length)&&(j-1>=0&&j-1<board.length)) {
if(!(board[i-1][j-1].equals("x"))) {
n=Integer.parseInt(board[i-1][j-1])+1;
board[i-1][j-1]=""+n;
}
}
if(j+1>=0&&j+1<board.length) {
if(!(board[i][j+1].equals("x"))) {
n=Integer.parseInt(board[i][j+1])+1;
board[i][j+1]=""+n;
}
}
if((i+1>=0&&i+1<board.length)&&(j+1>=0&&j+1<board.length)) {
if(!(board[i+1][j+1].equals("x"))) {
n=Integer.parseInt(board[i+1][j+1])+1;
board[i+1][j+1]=""+n;
}
}
if(i-1>=0&&i-1<board.length) {
if(!(board[i-1][j].equals("x"))) {
n=Integer.parseInt(board[i-1][j])+1;
board[i-1][j]=""+n;
}
}
if((i+1>=0&&i+1<board.length)&&(j-1>=0&&j-1<board.length)) {
if(!(board[i+1][j-1].equals("x"))) {
n=Integer.parseInt(board[i+1][j-1])+1;
board[i+1][j-1]=""+n;
}
}
if(i+1>=0&&i+1<board.length) {
if(!(board[i+1][j].equals("x"))) {
n=Integer.parseInt(board[i+1][j])+1;
board[i+1][j]=""+n;
}
}
if((i-1>=0&&i-1<board.length)&&(j+1>=0&&j+1<board.length)) {
if(!(board[i-1][j+1].equals("x"))) {
n=Integer.parseInt(board[i-1][j+1])+1;
board[i-1][j+1]=""+n;
}
}
}
}
}
}
public String board[][];
public String guessed[][];
private int height, width, mines, n=0;
}
BufferedReader bufrdRdr = new BufferedReader(new InputStreamReader(System.in));
int mines=0, n=0, height=0, width=0;
System.out.print("(l)ame, (p)ro, (u)ser-defined: ");
switch(bufrdRdr.readLine().toLowerCase().charAt(0)) {
case 'l':
height = 9;
width = 9;
mines = 10;
break;
case 'p':
height = 16;
width = 16;
mines = 40;
break;
case 'u':
System.out.print("height: ");
height = Integer.parseInt(bufrdRdr.readLine());
System.out.print("width: ");
width = Integer.parseInt(bufrdRdr.readLine());
if((height<0 || height>30) || (width<0 || width>30)) {
System.out.println("invalid\n");
return;
}
System.out.print("mines: ");
mines = Integer.parseInt(bufrdRdr.readLine());
break;
default:
System.out.println("invalid\n");
return;
}
Board game = new Board(height, width, mines);
game.printBoard();
while(!game.isDone()) {
System.out.print("row: ");
int row = Integer.parseInt(bufrdRdr.readLine());
System.out.print("col: ");
int col = Integer.parseInt(bufrdRdr.readLine());
System.out.print("flag(y/n): ");
if(bufrdRdr.readLine().equalsIgnoreCase("n")) {
if(!(game.guessed[row][col].equals("#"))) {
game.guessed[row][col] = game.board[row][col];
}
game.printBoard();
if(game.board[row][col].equals("x")) {
System.out.println("boom\n");
return;
}
} else {
if(game.guessed[row][col].equals("#")) {
game.guessed[row][col] = "*";
} else {
game.guessed[row][col] = "#";
}
game.printBoard();
}
}
}
}
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Didn't play it too much but i do have 2 observations :
If i go with : user defined and enter a ridiculous value, it says invalid number and the program stops :-P. Use a loop to make the user enter a new value, maybe even specify the range. Also when i enter in a table 8x8 a row number of 9 .. it will cause : java.lang.ArrayIndexOutOfBoundsException ... that's not cool , you could check what the user inputs before using it. All the best to you!
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|