I have been making a Minesweeper game and it's working. However, once I take out the print statement it stops updating to the play again JPanel.
Why is it doing this?
Here is all my code for the game:
The print statements I am talking about is in the Game main function. As long as I have one uncommented out there it works.
I am using Eclipse Java EE IDE for Web Developers.
Version: Juno Service Release 2
Game
Code:
package mineSweeper;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class Game extends JFrame implements ActionListener {
int rows=10,cols=10, mines=10;
Board board;
boolean again=true;
boolean check = true;
windowPlayAgain againWindow;
Game(){
super("MineSweeper");
setSize(600,600);
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
board = new Board(mines,rows,cols);
add(board, BorderLayout.CENTER );
setVisible(true);
}
public void change(){
Board temp = new Board(mines,rows,cols);
remove(board);
board = temp;
add(board);
validate();
repaint();
board.validate();
board.repaint();
board.gameOver = false;
board.win = false;
}
public void showAgainWindow(){
remove(board);
add(againWindow);
validate();
repaint();
}
public void removeAgainWindow(){
remove(againWindow);
add(board);
validate();
repaint();
board.win = false;
board.gameOver = false;
}
public static void main(String[] args) throws InterruptedException {
Game game = new Game();
while (true){
while(game.check){
//System.out.print("");
//System.out.println(game.board.win + " " + game.board.gameOver);
if(game.board.win){
game.againWindow = new windowPlayAgain( true, game);
game.showAgainWindow();
game.check = false;
}else if(game.board.gameOver){
game.againWindow = new windowPlayAgain( false, game);
game.showAgainWindow();
game.check = false;
}
//System.out.println(game.check);
}
}
}
public void actionPerformed(ActionEvent e) {
removeAgainWindow();
check = true;
change();
}
}
Board
Code:
package mineSweeper;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Board extends JPanel implements ActionListener {
int numOfRows=10, numOfCol=10;
int numOfMines=5;
JTextField topField;
Space[][] spaces = new Space[numOfRows][numOfCol];
JPanel[][] list = new JPanel[numOfRows][numOfCol];
JPanel cent;
boolean win = false;
boolean gameOver = false;
Board(int mines, int rows, int cols){
numOfRows = rows;
numOfCol = cols;
numOfMines = mines;
setSize(600,600);
setLayout(new BorderLayout());
cent = new JPanel();
cent.setLayout(new GridLayout(numOfRows,numOfCol));
for(int i =0; i < numOfRows; i++){
for(int f =0; f < numOfCol; f++){
list[i][f] = new JPanel();
list[i][f].setLayout(new GridLayout());
spaces[i][f] = new Space();
spaces[i][f].mystery = new JButton();
spaces[i][f].mystery.addActionListener(this);
spaces[i][f].mystery.setActionCommand(i + " " + f);
list[i][f].add(spaces[i][f].mystery);
cent.add(list[i][f]);
}
}
add(cent, BorderLayout.CENTER);
JPanel top = new JPanel();
topField = new JTextField(30);
topField.setEditable(false);
top.add(topField);
add(top, BorderLayout.NORTH);
setMines();
setVisible(true);
}
public void setMines(){
for(int i = 0; i < numOfMines; i++){
int randomRow = (int)(Math.random() * numOfRows);
int randomCol = (int)(Math.random() * numOfCol);
if(!spaces[randomRow][randomCol].mine){
spaces[randomRow][randomCol].mine = true;
spaces[randomRow][randomCol].mystery.setBackground(Color.GREEN);
}
else
i--;
}
for(int i=0;i<spaces.length;i++){
for(int j=0;j<spaces[i].length;j++){
spaces[i][j].setNumOfMines(spaces, i, j);
String name = whichImage(spaces[i][j].numOfMinesAround);
spaces[i][j].setImage(name);
}
}
}
public String whichImage(int numberMines){
String img;
switch(numberMines){
case 1:
img = "one.png";
break;
case 2:
img = "two.png";
break;
case 3:
img = "three.png";
break;
case 4:
img = "four.png";
break;
case 5:
img = "five.png";
break;
case 6:
img = "six.png";
break;
case 7:
img = "seven.png";
break;
case 8:
img = "eight.png";
break;
case 0:
img = "blank.png";
break;
default:
img = "Untitled.png";
//System.out.println(numberMines);
break;
}
return img;
}
public void actionPerformed(ActionEvent e) {
String which = e.getActionCommand();
int row = Integer.parseInt(which.substring(0,1));
int col = Integer.parseInt(which.substring(2));
if(spaces[row][col].mine == true){
topField.setText("Game Over! Game Over! Game Over!");
gameOver = true;
} else{
topField.setText("safe");
show(row,col);
checkForZerosAround(row,col);
validate();
cent.repaint();
boolean isWin = true;
for(int i =0; i < numOfRows; i++){
for(int f =0; f < numOfCol; f++){
if(!spaces[i][f].mine){
if(!spaces[i][f].show){
isWin=false;
}
}
}
}
win = isWin;
//System.out.println(win);
}
}
public void show(int row, int col){
JLabel label = new JLabel(new ImageIcon(spaces[row][col].myPicture));
list[row][col].remove(spaces[row][col].mystery);
list[row][col].add(label);
spaces[row][col].show = true;
}
public void checkForZerosAround(int row, int col){
if(spaces[row][col].numOfMinesAround==0){
int curR, curC;
for(int r= -1; r<=1; r++){
curR = row+r;
for(int c=-1;c<=1;c++){
curC = col + c;
if(curR >=0 && curR < spaces.length && curC >=0 && curC < spaces[spaces.length-1].length && !(r == 0 && c == 0))
if(!spaces[curR][curC].show){
show(curR,curC);
checkForZerosAround(curR,curC);
}
}
}
} else{
if(!spaces[row][col].show){
show(row,col);
}
}
}
}
Spacing
Code:
package mineSweeper;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
public class Space {
Boolean mine = false;
int numOfMinesAround=0;
BufferedImage myPicture;
JButton mystery;
boolean show = false;
public void setImage(String which){
try {
myPicture = ImageIO.read(new File(which));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setNumOfMines(Space[][] space, int row, int col){
int curR;
int curC;
for(int r = -1; r<=1;r++){
curR = row + r;
for(int c = -1; c<=1;c++){
curC = col + c;
if(curR >=0 && curR < space.length && curC >=0 && curC < space[space.length-1].length && !(r == 0 && c == 0)){
if(space[curR][curC].mine){
space[row][col].numOfMinesAround += 1;
}
}
}
}
}
}
windowPlayAgain
Code:
package mineSweeper;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class windowPlayAgain extends JPanel implements ActionListener {
String answer;
Game game;
windowPlayAgain(boolean win, Game game){
this.game = game;
setSize(300,200);
setLayout(new BorderLayout());
JLabel again;
if(win){
again = new JLabel("Congrats, you won! Do you want to play again?");
}else{
again = new JLabel("Game Over. Do you want to play again?");
}
add(again, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton playAgain = new JButton("Play again?");
playAgain.addActionListener(this);
playAgain.setActionCommand("again");
panel.add(playAgain);
JButton quit = new JButton("Quit");
quit.addActionListener(this);
quit.setActionCommand("quit");
panel.add(quit);
add(panel, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="again"){
game.actionPerformed(e);
} else{
System.exit(0);
}
}
}