i trying setup minesweeper game having problems carrying on new 'v' int board file. below list of things have done.i hope have said below makes sense, original minesweeeper base game code can found @ bottom of this.
- made easy.java file contains:
import java.util.scanner;
public class easy{ public static int b; public static void setvariable(int s) { b = s; } public static int getvariable() { return b; } }
added public static int top of mines.java (under
private final int frame_height = 290;
)
added main method withing public void run of mines.java file:
system.out.println("please enter number difficulty"); system.out.println("1: easy 2: medium 3: hard"); scanner kb = new scanner(system.in); int difficulty = kb.nextint(); system.out.println("difficulty set to: " + difficulty); if(difficulty==1){ easy.setvariable(40); v = easy.getvariable(); system.out.println(v); system.out.println("easy mode chosen"); }
i unsure on how link board.java file take value of 'v' have tried
int n_mines = mines.v;
however doesn't see value set in above piece of code if set v equal number see's this. in previous code "system.out.println(v);" prints out correct variable board.java not see number. how supposed go this?
board.java:
import java.awt.graphics; import java.awt.image; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.util.random; import java.util.*; import javax.swing.imageicon; import javax.swing.jlabel; import javax.swing.jpanel; public class board extends jpanel { private final int num_images = 13; private final int cell_size = 15; private final int cover_for_cell = 10; private final int mark_for_cell = 10; private final int empty_cell = 0; private final int mine_cell = 9; private final int covered_mine_cell = mine_cell + cover_for_cell; private final int marked_mine_cell = covered_mine_cell + mark_for_cell; private final int draw_mine = 9; private final int draw_cover = 10; private final int draw_mark = 11; private final int draw_wrong_mark = 12; int n_mines = mines.v; private int n_rows = 16; private int n_cols = 16; private int[] field; private boolean ingame; private int mines_left; private image[] img; private int all_cells; private jlabel statusbar; public board(jlabel statusbar) { this.statusbar = statusbar; img = new image[num_images]; (int = 0; < num_images; i++) { img[i] = (new imageicon(i + ".png")).getimage(); } setdoublebuffered(true); addmouselistener(new minesadapter()); newgame(); } private void newgame() { random random; int current_col; int = 0; int position = 0; int cell = 0; /* system.out.println("please enter number difficulty"); system.out.println("1: easy 2: medium 3: hard"); scanner kb = new scanner(system.in); int difficulty = kb.nextint(); system.out.println("difficulty set to: " + difficulty); if(difficulty==1){ n_mines=n_mines*1; system.out.println("easy mode chosen"); } if(difficulty==2){ n_mines=n_mines*2; system.out.println("medium mode chosen"); } if(difficulty==3){ n_mines=n_mines*3; system.out.println("hard mode chosen"); } */ random = new random(); ingame = true; mines_left = n_mines; all_cells = n_rows * n_cols; field = new int[all_cells]; (i = 0; < all_cells; i++) field[i] = cover_for_cell; statusbar.settext(integer.tostring(mines_left)); = 0; while (i < n_mines) { position = (int) (all_cells * random.nextdouble()); if ((position < all_cells) && (field[position] != covered_mine_cell)) { current_col = position % n_cols; field[position] = covered_mine_cell; i++; if (current_col > 0) { cell = position - 1 - n_cols; if (cell >= 0) if (field[cell] != covered_mine_cell) field[cell] += 1; cell = position - 1; if (cell >= 0) if (field[cell] != covered_mine_cell) field[cell] += 1; cell = position + n_cols - 1; if (cell < all_cells) if (field[cell] != covered_mine_cell) field[cell] += 1; } cell = position - n_cols; if (cell >= 0) if (field[cell] != covered_mine_cell) field[cell] += 1; cell = position + n_cols; if (cell < all_cells) if (field[cell] != covered_mine_cell) field[cell] += 1; if (current_col < (n_cols - 1)) { cell = position - n_cols + 1; if (cell >= 0) if (field[cell] != covered_mine_cell) field[cell] += 1; cell = position + n_cols + 1; if (cell < all_cells) if (field[cell] != covered_mine_cell) field[cell] += 1; cell = position + 1; if (cell < all_cells) if (field[cell] != covered_mine_cell) field[cell] += 1; } } } } public void find_empty_cells(int j) { int current_col = j % n_cols; int cell; if (current_col > 0) { cell = j - n_cols - 1; if (cell >= 0) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } cell = j - 1; if (cell >= 0) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } cell = j + n_cols - 1; if (cell < all_cells) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } } cell = j - n_cols; if (cell >= 0) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } cell = j + n_cols; if (cell < all_cells) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } if (current_col < (n_cols - 1)) { cell = j - n_cols + 1; if (cell >= 0) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } cell = j + n_cols + 1; if (cell < all_cells) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } cell = j + 1; if (cell < all_cells) if (field[cell] > mine_cell) { field[cell] -= cover_for_cell; if (field[cell] == empty_cell) find_empty_cells(cell); } } } @override public void paintcomponent(graphics g) { int cell = 0; int uncover = 0; (int = 0; < n_rows; i++) { (int j = 0; j < n_cols; j++) { cell = field[(i * n_cols) + j]; if (ingame && cell == mine_cell) ingame = false; if (!ingame) { if (cell == covered_mine_cell) { cell = draw_mine; } else if (cell == marked_mine_cell) { cell = draw_mark; } else if (cell > covered_mine_cell) { cell = draw_wrong_mark; } else if (cell > mine_cell) { cell = draw_cover; } } else { if (cell > covered_mine_cell) cell = draw_mark; else if (cell > mine_cell) { cell = draw_cover; uncover++; } } g.drawimage(img[cell], (j * cell_size), (i * cell_size), this); } } if (uncover == 0 && ingame) { ingame = false; statusbar.settext("game won"); } else if (!ingame) statusbar.settext("game lost"); } class minesadapter extends mouseadapter { @override public void mousepressed(mouseevent e) { int x = e.getx(); int y = e.gety(); int ccol = x / cell_size; int crow = y / cell_size; boolean rep = false; if (!ingame) { newgame(); repaint(); } if ((x < n_cols * cell_size) && (y < n_rows * cell_size)) { if (e.getbutton() == mouseevent.button3) { if (field[(crow * n_cols) + ccol] > mine_cell) { rep = true; if (field[(crow * n_cols) + ccol] <= covered_mine_cell) { if (mines_left > 0) { field[(crow * n_cols) + ccol] += mark_for_cell; mines_left--; statusbar.settext(integer.tostring(mines_left)); } else statusbar.settext("no marks left"); } else { field[(crow * n_cols) + ccol] -= mark_for_cell; mines_left++; statusbar.settext(integer.tostring(mines_left)); } } } else { if (field[(crow * n_cols) + ccol] > covered_mine_cell) { return; } if ((field[(crow * n_cols) + ccol] > mine_cell) && (field[(crow * n_cols) + ccol] < marked_mine_cell)) { field[(crow * n_cols) + ccol] -= cover_for_cell; rep = true; if (field[(crow * n_cols) + ccol] == mine_cell) ingame = false; if (field[(crow * n_cols) + ccol] == empty_cell) find_empty_cells((crow * n_cols) + ccol); } } if (rep) repaint(); } } } }
mines.java:
import java.awt.borderlayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingutilities; import java.util.*; public class mines extends jframe { private final int frame_width = 250; private final int frame_height = 290; public static int v; private final jlabel statusbar; public mines() { setdefaultcloseoperation(jframe.exit_on_close); setsize(frame_width, frame_height); setlocationrelativeto(null); settitle("minesweeper"); statusbar = new jlabel(""); add(statusbar, borderlayout.south); add(new board(statusbar)); setresizable(false); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { jframe ex = new mines(); ex.setvisible(true); system.out.println("please enter number difficulty"); system.out.println("1: easy 2: medium 3: hard"); scanner kb = new scanner(system.in); int difficulty = kb.nextint(); system.out.println("difficulty set to: " + difficulty); if(difficulty==1){ easy.setvariable(40); v = easy.getvariable(); system.out.println(v); system.out.println("easy mode chosen"); } if(difficulty==2){ //medium.setvariable(80); //v = medium.getvariable(); //system.out.println(v); system.out.println("medium mode chosen"); //medium.main(args); } if(difficulty==3){ hard.main(args); } } }); } }
edit constructor of board class accept number of mines.
private int n_mines = 40; // default public board(int mines, jlabel statusbar) { this.n_mines = mines; this.statusbar = statusbar;
make new board(20, new jlabel());
easier game
Comments
Post a Comment