I wrote a class a while ago to solve a sudoku game using recursive backtracking - that's working as expected.
Now I want to visualize this algorithm step by step. I basically want to see where the algorithm put which number and when it is backtracking.
I wrote all the necessary function and implemented a basic gui that already calls and displays my sudokuSolver inside my gui (after you start it and press a random key once you want to begin the solving).
Now my problem is that I want to see each step of the algorithm - see more in the SudokuSolver.solve() function.
At the moment I somehow only update my gui once the whole backtracking is finished, although my SudokuSlver.setGui() function is called during the backtracking.
Which is also the reason why my approach to "slow down" the algorithm with "Thread.sleep()" and "TimeUnit" failed.
Github
Code Gui.java
import java.util.ArrayList;
import java.util.concurrent.*;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.shape.*;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.paint.*;
public class Gui extends Application{
static ArrayList<Rectangle> rects = new ArrayList<>();
static ArrayList<Text> texts = new ArrayList<>();
#Override
public void start(Stage stage) {
Pane pane = new Pane();
Scene scene = new Scene(pane, 297, 297);
createBoard();
for (int box = 0; box < rects.size(); box++) {
pane.getChildren().addAll(rects.get(box), texts.get(box));
}
scene.setOnKeyPressed(new EventHandler<KeyEvent>(){
public void handle(final KeyEvent keyEvent){
System.out.println("event triggered");
handleEvent(keyEvent);
}
});
stage.setTitle("SudokuSolver");
stage.setScene(scene);
stage.show();
}
public void handleEvent(KeyEvent key){
System.out.println("event triggered");
callSudokuSolver();
}
public void createBoard(){
for (int x = 0; x < 288; x+=32) {
for (int y = 0; y < 288; y+=32) {
Rectangle r = new Rectangle(x, y, 32, 32);
Text text = createText("0");
text.setX(x);
text.setY(y+32);
r.setFill(Color.WHITE);
r.setStroke(Color.BLACK);
r.setOpacity(0.5);
rects.add(r);
texts.add(text);
}
}
}
public static void setTextOfRect(String s, int pos){
texts.get(pos).setText(s);
}
public static void setColorOfRect(Color col, int pos){
rects.get(pos).setFill(col);
}
private Text createText(String string) {
Text text = new Text(string);
text.setBoundsType(TextBoundsType.VISUAL);
text.setStyle(
"-fx-font-family: \"Times New Roman\";" +
"-fx-font-size: 16px;"
);
return text;
}
private void callSudokuSolver(){
//test
int[][] board =
{{2,0,5,0,0,0,0,0,0},
{3,0,8,6,0,0,9,0,0},
{0,0,0,1,0,0,4,0,0},
{0,0,0,0,5,0,0,1,0},
{0,0,0,0,9,0,0,2,0},
{8,7,0,0,2,0,0,0,0},
{0,0,0,0,8,9,0,0,3},
{0,0,6,0,0,3,0,0,5},
{5,0,4,0,0,0,0,0,1}};
SudokuSolver sudokuSolver = new SudokuSolver();
if(!sudokuSolver.startSolving(board)){
System.out.println("No solution");
}else{
System.out.println("Solved");
}
}
public static void main(String[] args) throws Exception {
launch();
}
}
Code SudokuSolver.java
import javafx.scene.paint.*;
public class SudokuSolver {
private boolean solve(int[][] board, int counter){
int col = counter / board.length;
int row = counter % board.length;
if (col >= board.length){
return true;
}
if (board[row][col] == 0) {
for (int n = 1; n <= board.length; n++) {
if (isValid(n,row,col, board)){
board[row][col] = n;
setGui(false, counter, n);
if (solve(board,counter+1)){
return true;
}
}
board[row][col] = 0;
setGui(true, counter, n);
}
}else{
setGui(false, counter, board[row][col]);
return solve(board, counter + 1);
}
return false;
}
public boolean startSolving(int[][] board){
if(!solve(board, 0)){
return false;
}else{
return true;
}
}
private boolean isValid(int n, int row, int col, int[][] board){
int i;
for (i = 0; i < board.length; i++) {
if(board[row][i] == n){
return false;
}
}
for (i = 0; i < board.length; i++) {
if(board[i][col] == n){
return false;
}
}
//check if block is valid
final int blockRow = 3 * (row / 3);
final int blockCol = 3 * (col / 3);
return isBlockValid(n, board, blockRow, blockRow + 2, blockCol, blockCol + 2);
}
private boolean isBlockValid(int n, int[][] board, int starti, int stopi, int startj, int stopj){
for (int i = starti; i <= stopi; i++) {
for (int j = startj; j <= stopj; j++) {
if (board[i][j] == n) {
return false;
}
}
}
return true;
}
private void printBoard(int[][] board){
System.out.println();
for (int[] row : board){
System.out.print("|");
for (int col : row){
System.out.print(col);
System.out.print("|");
}
System.out.println();
}
System.out.println();
}
private void setGui(boolean wrong, int pos, int number){
String s = Integer.toString(number);
Color color = Color.GREEN;
if(wrong){
color = Color.RED;
}
Gui.setColorOfRect(color, pos);
Gui.setTextOfRect(s, pos);
}
}
https://stackoverflow.com/a/9167420/11162097 solved my problem.
It was necessary to open my sodokusolver in another thread and then run the gui commands with Platform.runLater() (#tevemadar).
new Thread() {
public void run() {
SudokuSolver sudokuSolver = new SudokuSolver();
sudokuSolver.startSolving(board);
}
}.start();
After that I used this to "slow down" my algorithm.
try {
Thread.sleep(10);
} catch (Exception e) {
//TODO: handle exception
}
Related
I am trying to make a snake JavaFX game. The scene is a board that consists of 25x25 images. For now, I just want to make the snake go to the right before I add userInput. The first scene is loading from Controller.java and then in Main.java, I am calling run_game() method (inside controller.java) which changes the scene every second. The issue is that the first scene shows up but the scene does not get updated. There is no error, the program just keeps running till I stop it.
Here are the classes:-
Main.java
import javafx.application.Application;
import javafx.stage.Stage;
import java.util.Timer;
import java.util.TimerTask;
public class Main extends Application {
public Controller game;
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Snake Game");
game = new Controller();
primaryStage.setScene(game.scene);
primaryStage.show();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
#Override
public void run() {
try {
game.run_game();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},0,1000);
}
public static void main(String[] args) { launch(args); }
}
Controller.java
import javafx.animation.AnimationTimer;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import java.util.TimerTask;
public class Controller {
public Snake snake;
public Board board;
public Food food;
public Movement move;
public static GridPane grid_pane;
public Scene scene;
// Creating all the necessary instances of snake game
public Controller() throws InterruptedException {
food = new Food();
grid_pane = new GridPane();
//assigning a random food position to the food apple
assign_random_position_to_food();
snake = new Snake(food);
board = new Board();
move = new Movement(snake);
board.generateBoard(food.row, food.col, snake);
Parent root = grid_pane;
scene = new Scene(root, Board.BOARDHEIGHT,Board.BOARDWIDTH);
}
public void run_game() throws InterruptedException {
snake = move.move_RIGHT();
scene.setRoot( grid_pane);
}
public void assign_board_to_grid(){
for ( int row = 0; row < board.board.length; ++row)
for( int col = 0; col < board.board[row].length; ++col )
grid_pane.add(board.board[row][col], row, col, 1, 1);
}
public void assign_random_position_to_food(){
food.RandomPosition();
}
}
Board.java
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
public class Board {
public static int BOARDHEIGHT = 500;
public static int BOARDWIDTH = 500;
public static int BLOCKHEIGHT = 20;
public static int BLOCKWIDTH = 20;
public static int TOTAL_BLOCKS_ROW = BOARDHEIGHT/ BLOCKHEIGHT;
public static int TOTAL_BLOCKS_COL = BOARDWIDTH/ BLOCKWIDTH;
public ImageView[][] board;
public Image black_square;
public Image apple;
public Image snake_dot;
public GridPane gridPane = new GridPane();
public void assign_images(){
try{
apple = new Image("images/apple.png");
snake_dot = new Image("images/dot.png");
black_square = new Image("images/black_square.png");
}catch(Exception e){
System.out.println("Error: related to the address of images");
}
}
public Board(){
assign_images();
//initializing the board
board = new ImageView[TOTAL_BLOCKS_ROW][TOTAL_BLOCKS_COL];
}
public void generateBoard(int food_x, int food_y, Snake snake_body) {
for(int row=0; row< TOTAL_BLOCKS_ROW; row++){
for(int col=0; col< TOTAL_BLOCKS_COL; col++){
if(row == food_x && col == food_y)// && check if it is not on snake body)
board[row][col] = new ImageView(apple);
else if( snake_body.is_snake_present(row,col) && col != 0)
board[row][col] = new ImageView(snake_dot);
else
board[row][col] = new ImageView(black_square);
// Setting the size of each block on the scene
board[row][col].setFitHeight(BLOCKHEIGHT);
board[row][col].setFitWidth(BLOCKWIDTH);
Controller.grid_pane.add(board[row][col], row, col, 1 ,1);
}
}
}
}
Movement.java
public class Movement {
public Snake snake;
public static int new_y_position;
public static int new_x_position;
public Movement(Snake snake){
this.snake = snake;
}
public Snake move_RIGHT(){
new_x_position = snake.getHead_x() + 1;
new_y_position = snake.getHead_y();
if(!check_if_snake_is_in_bounds(new_x_position, new_y_position))
return null;
//System.out.println(new_x_position);
snake.setHead_x(new_x_position);
Block b;
for (int i = snake.snake.size() - 1; i >= 0; --i){
b = snake.snake.get(i);
b.setX(b.getX() + 1);
snake.snake.set(i,b);
}
// System.out.println(snake);
return snake;
}
public Snake move_LEFT(){
new_x_position = snake.getHead_x() - 1;
new_y_position = snake.getHead_y();
if(!check_if_snake_is_in_bounds(new_x_position, new_y_position))
return null;
//snake is inbounds update position
snake.setHead_x(new_x_position);
Block b;
for (int i = snake.snake.size() - 1; i >= 0; --i){
b = snake.snake.get(i);
b.setX(b.getX() - 1);
snake.snake.set(i,b);
}
System.out.println(snake);
return snake;
}
public Snake move_UP(){
new_x_position = snake.getHead_x();
new_y_position = snake.getHead_y() + 1;
if(!check_if_snake_is_in_bounds(new_x_position, new_y_position))
return null;
//snake is inbounds update position
snake.setHead_y(new_y_position);
Block b;
for (int i = snake.snake.size() - 1; i >= 0; --i){
b = snake.snake.get(i);
b.setY(b.getY() + 1);
snake.snake.set(i,b);
}
System.out.println(snake);
return snake;
}
public Snake move_DOWN(){
new_x_position = snake.getHead_x();
new_y_position = snake.getHead_y() - 1;
if(!check_if_snake_is_in_bounds(new_x_position, new_y_position))
return null;
//snake is inbounds update position
snake.setHead_y(new_y_position);
Block b;
for (int i = snake.snake.size() - 1; i >= 0; --i){
b = snake.snake.get(i);
b.setY(b.getY() - 1);
snake.snake.set(i,b);
}
System.out.println(snake);
return snake;
}
public boolean check_if_snake_is_in_bounds(int x, int y){
return x >= 0 && x <= Board.TOTAL_BLOCKS_COL && y >= 0 && y <= Board.TOTAL_BLOCKS_ROW;
}
}
Snake.java
import java.util.ArrayList;
public class Snake {
public ArrayList<Block> snake = new ArrayList<>();
public int snake_size = snake.size();
int head_x, head_y;
public Snake(Food food){
// initializes a snake head location on board and stores it in a list.
do{
head_x = (int) (Math.random()*Board.TOTAL_BLOCKS_COL);
head_y = (int) (Math.random()*Board.TOTAL_BLOCKS_ROW);
}while(food.getRow() == head_x || food.getCol() == head_y);
//inserting head and a block in snake arraylist
snake.add(new Block(head_x, head_y));
snake.add(new Block(head_x - 1, head_y));
}
// Adds new body part when snake eats an apple.
public void add_snake_body() {
snake.add( retrieve_new_block_position() );
}
// gets new snake body part position.
public Block retrieve_new_block_position(){
int last_block_x = snake.get(snake_size - 1).getX();
int last_block_y = snake.get(snake_size - 1).getY();
int second_last_block_x = snake.get(snake_size - 2).getX();
int second_last_block_y = snake.get(snake_size - 2).getY();
int new_block_x;
int new_block_y;
if(second_last_block_x == last_block_x){
new_block_x = last_block_x;
new_block_y = last_block_y - 1;
}
else{
new_block_x = last_block_x - 1;
new_block_y = last_block_y;
}
return new Block(new_block_x, new_block_y);
}
//checks if a position is occupied by a snake body part.
public boolean is_snake_present(int row, int col){
int index = 0;
int snake_curr_block_x;
int snake_curr_block_y;
while(index < snake.size()){
snake_curr_block_x = snake.get(index).getX();
snake_curr_block_y = snake.get(index).getY();
if( snake_curr_block_x == row && snake_curr_block_y == col)
return true;
index++;
}
return false;
}
//GETTER & SETTER METHODS
public ArrayList<Block> getSnake() {
return snake;
}
public int getHead_x() {
return head_x;
}
public void setHead_x(int head_x) {
this.head_x = head_x;
}
public int getHead_y() {
return head_y;
}
public void setHead_y(int head_y) {
this.head_y = head_y;
}
}
Food.java
import java.util.Random;
public class Food {
int row;
int col;
public Food(){
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public void RandomPosition(){
this.row = (int) (Math.random() * Board.TOTAL_BLOCKS_ROW);
this.col = (int) (Math.random() * Board.TOTAL_BLOCKS_COL);
System.out.println(this.row+" -food- "+ this.col);
//need to check if it clashes with the snake body................
}
}
Block.java
public class Block {
private int x;
private int y;
public Block(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am having difficulty in enablng turn taking within my program.
A tile on the board gets clicked and those coordinates are passed to playMakeMove() which makes the move in the board matrix and sets text to represent a move visually.
Although when 2 players are involved (player, RandomAI), after using a loop to alternate turns, the method doesnt work. The RandomAI just makes all its moves in succession with player only making one move in the exact same spot (not waiting for mouse to be clicked).
Im thinking the problem can be sloved via waiting for a tile to be clicked(to run method playerMakeMove) and then letting RandomAI take a turn. Not sure how to implement this.
Below are two classes BoardGUI and Game there is also another class (not included) called Board.
public class BoardGUI extends Application {
private final int BOARD_SIZE = 15;
private Tile[][] tileBoard = new Tile[BOARD_SIZE][BOARD_SIZE];
private Pane root = new Pane();
private Parent createContent(Game game) {
root.setPrefSize(755, 755);
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Tile tile = new Tile(i, j);
tile.setTranslateX(j * 50);
tile.setTranslateY(i * 50);
tile.setOnMousePressed(e -> {
System.out.println("tile clicked");
// sets coordinates of tileClicked in game - to be used in making a player move
game.getTile().setRow(tile.getTileRow());
game.getTile().setColumn(tile.getTileCol());
});
root.getChildren().add(tile);
tileBoard[i][j] = tile;
}
}
return root;
}
#Override
public void start(Stage primaryStage) {
Game game = new Game();
Scene scene = new Scene (game.GUI.createContent(game));
primaryStage.setScene(scene);
primaryStage.setTitle("Gomoku");
primaryStage.show();
for (int turns = 0; turns < (game.getBoardSize()*game.getBoardSize()); turns++) {
if(game.getGameOver()) break;
if(!game.isPlayerTurn()) {
//AI make move
game.makeMoveAIRandom(game.getGameBoard());
game.setPlayerTurnTrue();
}
else {
// player make move
game.playerMakeMove(game);
}
}
System.out.println("game over");
}
class Tile extends StackPane {
Text text = new Text();
int row, column;
Tile(int x, int y) {
this.row = x;
this.column = y;
Rectangle border = new Rectangle(50, 50);
border.setFill(Color.BURLYWOOD);
border.setStroke(Color.BLACK);
text.setFont(Font.font(40));
setAlignment(Pos.CENTER);
getChildren().addAll(border, text);
}
void makeMove(Board board, int player, int row, int col) {
System.out.println(row + " " + col);
if (board.isMoveAvailable(row, col)) {
drawTile(player);
makeMoveMatrix(board, player, row, col);
}
System.out.println("makeMove executed");
}
void drawTile(int player) {
System.out.println("setTextTile executed");
text.setText("O");
if (player == 1) text.setFill(Color.BLACK);
else text.setFill(Color.WHITE);
}
void makeMoveMatrix(Board board, int player, int row, int col) {
board.make_move(player, row, col);
}
int getTileRow() { return row; }
void setRow(int row) { this.row = row; }
void setColumn(int column) { this.column = column; }
int getTileCol() { return column; }
}
Tile[][] getTileBoard() { return tileBoard; }
}
public class Game extends BoardGUI {
private final int PLAYER_ONE = 1;
private final int PLAYER_TWO = 2;
BoardGUI GUI;
private Board gameBoard;
private int boardSize = 15;
private int winLength = 5;
private boolean turn = true;
private boolean isGameOver = false;
private Tile tileClicked = new Tile(0, 0);
Game() {
this.gameBoard = new Board(boardSize, winLength);
this.GUI = new BoardGUI();
}
void makeMoveAIRandom(Board gameBoard) {
Random random = new Random();
int index = random.nextInt(gameBoard.availableMoves.size());
int[] move = gameBoard.availableMoves.get(index);
int row = move[0];
int col = move[1];
GUI.getTileBoard()[row][col].makeMove(gameBoard, PLAYER_TWO, move[0], move[1]);
setGameOver(getGameBoard().check_win_all(PLAYER_TWO, row, col));
gameBoard.availableMoves.remove(index);
setPlayerTurnTrue();
}
void playerMakeMove(Game game) {
int row = game.getTile().getTileRow();
int col = game.getTile().getTileCol();
game.GUI.getTileBoard()[row][col].makeMove(game.getGameBoard(), PLAYER_ONE, row, col);
setGameOver(getGameBoard().check_win_all(PLAYER_TWO, row, col));
System.out.println("player taken turn");
setPlayerTurnFalse();
}
Board getGameBoard() { return gameBoard; }
int getBoardSize () { return boardSize; }
boolean isPlayerTurn() { return turn; }
private void setPlayerTurnFalse() { turn = false; }
void setPlayerTurnTrue() { turn = true; }
boolean getGameOver () { return isGameOver; }
private void setGameOver (boolean result) { isGameOver = result; }
Tile getTile () { return tileClicked; }
}
You have 2 main options for implementing a game loop. Rather than writing their equivalent in Java I'll just provide pseudocode and you can ask if you have trouble converting to Java.
The first option is to trigger all activity from the user taking an action. So, for example, the 'event' may be a drag-and-drop, option click etc. depending on the game:
on user event:
change state according to user's choice
go to next player
while next player is AI:
perform automated move
go to next player
The second, more generic option, is to have a timer that triggers periodically to check if an AI action can occur.
on user event:
change state according to user's choice
go to next player
on timer tick:
if current player is AI
perform automated move
go to next player
Each of these has their place depending on game mechanics
The following is a complete runable code that implements change of turns.
It also suggests some improvements as well as design changes. Please note the comments.
End of game logic was not implemented.
For convenience the entire code can be copy pasted into one file (BoardGUI.java) and run:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class BoardGUI extends Application {
private final static int BOARD_SIZE = 15;
private final Tile[][] tileBoard = new Tile[BOARD_SIZE][BOARD_SIZE];
private final Pane root = new Pane();
private Parent createContent(Game game) {
root.setPrefSize(755, 755);
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Tile tile = new Tile(i, j);
tile.setTranslateX(j * 50);
tile.setTranslateY(i * 50);
final int row = i, col = j;
tile.setOnMousePressed(e -> {
//the click causes a move
//game.playerMakeMove(game, row, col); //no need to pass a reference of Game to Game
game.playerMakeMove(row, col); //no need to pass a reference of Game to Game
});
root.getChildren().add(tile);
tileBoard[i][j] = tile;
}
}
return root;
}
#Override
public void start(Stage primaryStage) {
//there is no need to get a new instance of BoardGUI from Game.
//Game game = new Game();
//Scene scene = new Scene (game.gui.createContent(game));
//instead:
Game game = new Game(this);
Scene scene = new Scene (createContent(game));
primaryStage.setScene(scene);
primaryStage.setTitle("Gomoku");
primaryStage.show();
//the player should make a move (click) and it should trigger the next move
}
//method moved from Tile
void makeMove(Board board, int player, int row, int col) {
if (board.isMoveAvailable(row, col)) {
tileBoard[row][col].drawTile(player);
makeMoveMatrix(board, player, row, col);
}
}
//method moved from Tile
void makeMoveMatrix(Board board, int player, int row, int col) {
board.make_move(player, row, col);
}
Tile[][] getTileBoard() { return tileBoard; }
class Tile extends StackPane {
Text text = new Text();
int row, column;
Tile(int row, int col) {
this.row = row; column = col;
Rectangle border = new Rectangle(50, 50);
border.setFill(Color.BURLYWOOD);
border.setStroke(Color.BLACK);
text.setFont(Font.font(40));
setAlignment(Pos.CENTER);
getChildren().addAll(border, text);
}
void drawTile(int player) {
text.setText("O");
if (player == Game.PLAYER_ONE) {
text.setFill(Color.BLACK);
} else {
text.setFill(Color.WHITE);
}
}
int getTileRow() { return row; }
void setRow(int row) { this.row = row; }
void setColumn(int column) { this.column = column; }
int getTileCol() { return column; }
}
public static void main(String[] args) {
launch(null);
}
}
class Game {// there is no need for Game to extend BoardGUI
public static final int FREE = 0, PLAYER_ONE = 1, PLAYER_TWO = 2;
private final BoardGUI gui;
private final Board gameBoard;
private final int boardSize = 15, winLength = 5;
private final Random random = new Random(); //no need to create new Randon with every move
//replace this attribute with property so you could listen to it
//private boolean turn = true;
private final BooleanProperty playerTurn = new SimpleBooleanProperty(true);
private boolean isGameOver = false;
Game(BoardGUI gui) { //get a reference to BoardGUI rather than constructing it
gameBoard = new Board(boardSize, winLength);
this.gui = gui;
playerTurn.addListener( (obs,oldValue,newValue) ->{//listen to turn changes
if(!newValue) {
makeMoveAIRandom();
}
});
}
void makeMoveAIRandom() {
if( playerTurn.get() ) return; //run only if not player turn
List<int[]> availableMoves = gameBoard.getAvailableMoves();
int index = random.nextInt(availableMoves.size());
int[] move = availableMoves.get(index);
gui.makeMove(gameBoard, PLAYER_TWO, move[0], move[1]);
setPlayerTurnTrue();
System.out.println("AI taken turn " + move[0] +"-"+ move[1]);
}
//void playerMakeMove(Game game, int row, int col) { //no need to path reference to this
void playerMakeMove(int row, int col) {
if(! playerTurn.get() ) return; //run only if player turn
gui.makeMove(gameBoard, PLAYER_ONE, row, col);
setPlayerTurnFalse();
System.out.println("player taken turn " + row +"-"+col );
}
Board getGameBoard() { return gameBoard; }
int getBoardSize () { return boardSize; }
boolean isPlayerTurn() { return playerTurn.get(); }
private void setPlayerTurnFalse() { playerTurn.set(false); }
void setPlayerTurnTrue() { playerTurn.set(true); }
boolean getGameOver () { return isGameOver; }
private void setGameOver (boolean result) { isGameOver = result; }
}
class Board {
private final int[][] board_matrix;
private final int board_size, win_length;
Board(int board_size, int win_length) {
board_matrix = new int[board_size][board_size];
this.board_size = board_size;
this.win_length = win_length;
for (int i = 0; i < board_size; i++) {
for (int j = 0; j < board_size; j++) {
board_matrix[i][j] = Game.FREE;
}
}
}
boolean isMoveAvailable(int row, int col) {
return board_matrix[row][col] != Game.PLAYER_ONE && board_matrix[row][col] != Game.PLAYER_TWO ;
}
void make_move(int player, int x_pos, int y_pos) {
if (player == Game.PLAYER_ONE) {
board_matrix[x_pos][y_pos] = Game.PLAYER_ONE;
} else {
board_matrix[x_pos][y_pos] = Game.PLAYER_TWO;
}
}
List<int[]> getAvailableMoves(){
List<int[]> availableMoves = new ArrayList<>();
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
if (board_matrix[row][col] == Game.FREE){
availableMoves.add(new int[]{ row, col});
}
}
}
return availableMoves;
}
}
Hello guys I am working on a project where I am trying to create a maze generator.
So far I have a gird that is a 2D array of a Cell class and a JPanel that paints the grid to a JFrame and a function which uses Depth first Search to visit each Cell in the grid.
When the Cell has been visited the Cell color changes to black on the grid. My problem is the repaint on the grid is too fast is there anyway I can slow the time or set a timer to repaint after a number of seconds. Here is the code below
import java.util.Arrays;
import java.util.Stack;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.SecureRandom;
public class Maze extends JPanel implements ActionListener {
private Cell [][] maze;
private int dims;
private Stack<Cell> S = new Stack<Cell>();
private SecureRandom num = new SecureRandom();
Timer t;
public Maze(int din)
{
dims = din;
maze = new Cell[dims][dims];
}
public void generator()
{
for(int i = 0; i < maze.length; i++)
{
for (int j = 0;j < maze[0].length; j++)
{
maze[i][j] = new Cell(i,j);
}
}
}
public boolean checkAll()
{
for(int i = 0; i < maze.length; i++)
{
for (int j = 0;j < maze[0].length; j++)
{
if(!maze[i][j].visited)
return false;
}
}
return true;
}
public void adjlist()
{
for(int i = 0; i < maze.length; i++)
{
for (int j = 0;j < maze[0].length; j++)
{
if(i+1 >= 0 && i+1 < dims)
{
maze[i][j].neighbor.add(maze[i+1][j]);
}
if(i-1 >= 0 && i-1 < dims)
{
maze[i][j].neighbor.add(maze[i-1][j]);
}
if(j-1 >= 0 && j-1 < dims)
{
maze[i][j].neighbor.add(maze[i][j-1]);
}
if(j+1 >= 0 && j+1 < dims)
{
maze[i][j].neighbor.add(maze[i][j+1]);
}
}
}
}
public void DFS(Cell x)
{
if (!checkAll() && !maze[x.row][x.column].visited)
{
S.push(x);
System.out.println(Arrays.toString(S.toArray()));
maze[x.row][x.column].visited = true;
int randnum = num.nextInt(maze[x.row][x.column].neighbor.size());
Cell temp1 = maze[x.row][x.column].neighbor.get(randnum);
if (!maze[temp1.row][temp1.column].visited)
{
DFS(maze[temp1.row][temp1.column]);
}
else if (maze[x.row][x.column].neighbor.isEmpty())
{
S.pop();
maze[S.peek().row][S.peek().column].visited = false;
DFS(S.pop());
}
else
{
if(S.size()-1 == 0)
return;
Cell temp = null;
for (Cell c : maze[x.row][x.column].neighbor)
{
if (!maze[c.row][c.column].visited)
{
temp = c;
DFS(temp);
}
}
if (temp == null) {
S.pop();
maze[S.peek().row][S.peek().column].visited = false;
DFS(S.pop());
}
}
}
}
public void paint(Graphics g)
{
super.paint(g);
for (int row = 0; row < maze.length; row++)
{
for (int col = 0; col < maze[0].length; col++)
{
g.drawRect(35*row, 35 * col , 35, 35);
if(maze[row][col].visited)
{
//t.start();
g.fillRect(35*row, 35 * col , 35, 35);
//t.setDelay(5000);
}
}
}
repaint();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args)
{
Maze p = new Maze(10);
p.generator();
p.adjlist();
System.out.println("------------------------");
JFrame f = new JFrame();
f.setTitle("Maze");
f.add(p);
f.setVisible(true);
f.setSize(700, 700);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.DFS(new Cell(1,5));
}
#Override
public void actionPerformed(ActionEvent e) {}
}
The cell class
import java.util.ArrayList;
public class Cell{
public int row, column;
boolean visited;
ArrayList<Cell> neighbor;
public Cell(int i, int j)
{
row = i;
column = j;
visited = false;
neighbor = new ArrayList<Cell>();
}
public int getRow()
{
return this.row;
}
public int getCol()
{
return this.column;
}
public String toString()
{
return this.row + " " + this.column;
}
}
The following code demonstrates the use of SwingWorker to demonstrate performing long tasks (in your case dfs search) which update the gui.
Specific dfs information was removed because they are not relevant:
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
public class Maze extends JPanel {
private final Cell [][] maze;
private final int dims;
private static final int SIZE = 35;
public Maze(int dim) {
setPreferredSize(new Dimension( dim*SIZE,dim*SIZE));
dims = dim;
maze = new Cell[dims][dims];
}
public void generator()
{
for(int i = 0; i < maze.length; i++)
{
for (int j = 0;j < maze[0].length; j++)
{
maze[i][j] = new Cell(i,j);
//set some arbitrary initial date
if(i%2 ==0 && j%2 ==0) {
maze[i][j].setVisited(true);
}
}
}
}
public void DFS()
{
new DFSTask().execute();
}
#Override
public void paintComponent(Graphics g) //override paintComponent not paint
{
super.paintComponent(g);
for (int row = 0; row < maze.length; row++)
{
for (int col = 0; col < maze[0].length; col++)
{
g.drawRect(SIZE*row, SIZE * col , SIZE, SIZE);
if(maze[row][col].visited)
{
g.fillRect(SIZE*row, SIZE * col , SIZE, SIZE);
}
}
}
}
public static void main(String[] args)
{
Maze p = new Maze(10);
p.generator();
JFrame f = new JFrame();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setVisible(true);
p.DFS();
}
//use swing worker perform long task
class DFSTask extends SwingWorker<Void,Void> {
private static final long DELAY = 1000;
private final Random rand = new Random();
#Override
public Void doInBackground() {
dfs();
return null;
}
#Override
public void done() { }
void dfs() { //simulates long process that repeatedly updates gui
while (true){ //endless loop, just for demonstration
//update info
int row = rand.nextInt(dims);
int col = rand.nextInt(dims);
maze[row][col].setVisited(! maze[row][col].isVisited());
repaint(); //update jpanel
try {
Thread.sleep(DELAY); //simulate long process
} catch (InterruptedException ex) { ex.printStackTrace();}
}
}
}
}
class Cell{
private final int row, column;
boolean visited;
public Cell(int i, int j)
{
row = i;
column = j;
visited = false;
}
int getRow() { return row; }
int getColumn() {return column; }
boolean isVisited() { return visited; }
void setVisited(boolean visited) { this.visited = visited;}
#Override
public String toString()
{
return row + " " + column;
}
}
For more information about using SwingWorker see doc
I am creating a GUI application using JavaFX in Eclipse. In this application I am creating a map of squares where you can toggle the square between filled space and empty space. I want the program to allow the user to toggle all the squares except for those on the edges but for some reason my handle method is allowing me to toggle all the squares. I just want to know if my handler method is fine or not.Thanks in advance for help.
this is how the GUI should look like
This is my GUI class called mazeGuiPane
package lab8;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class MazeGuiPane extends Application {
private GridPane grid = new GridPane();
private BorderPane border = new BorderPane();
private Scene sc = new Scene(border);
public Label[][] labelGridArray = new Label[20][20];
private StreetMap map = new StreetMap();
int col;
int row;
public void start(Stage primary) {
sc.getStylesheets().add("/styles/style.css");
grid.getStyleClass().add("grid-style");
border.getStyleClass().add("border-style");
Label mainTitle = new Label("Map Of Pamplona");
mainTitle.getStyleClass().add("white-text");
Button butt = new Button("RUN!");
VBox vBox = new VBox();
HBox buttHBox = new HBox();
HBox titleBox = new HBox();
VBox titleVBox = new VBox();
VBox buttVBox = new VBox();
map.makeGrid();
for (col = 0; col < 20; col++) {
for (row = 0; row < 20; row++) {
Label square = new Label();
if (map.gridArray[col][row].getValue() == ' ') {
square.getStyleClass().add("default-box");
} else if (map.gridArray[col][row].getValue() == 'S') {
square.setText("Start");
square.getStyleClass().add("start-end");
} else if (map.gridArray[col][row].getValue() == 'E') {
square.setText("End");
square.getStyleClass().add("start-end");
} else {
square.getStyleClass().add("wall");
}
square.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
row = GridPane.getRowIndex(square);
col = GridPane.getColumnIndex(square);
for (int col = 1; col < 19; col++) {
for (int row = 1; row < 19; row++) {
if (map.gridArray[col][row].getTier() != 0
|| map.gridArray[col][row].getTier() != 19
|| map.gridArray[col][row].getColumn() != 0
|| map.gridArray[col][row].getColumn() != 19) {
if(map.gridArray[col][row].getValue() == 'W'){
square.getStyleClass().removeAll("wall");
square.getStyleClass().add("default-box");
map.gridArray[col][row].setTier(row);
map.gridArray[col][row].setColumn(col); map.gridArray[col][row].setValue(' ');
}
else {
square.getStyleClass().removeAll("default-box");
square.getStyleClass().add("wall");
map.gridArray[col][row].setTier(row);
map.gridArray[col][row].setColumn(col);
map.gridArray[col][row].setValue('W');
}
}
}
}
}
});
labelGridArray[col][row] = square;
grid.add(square, col, row);
}
}
titleBox.getStyleClass().add("title");
titleBox.getChildren().add(mainTitle);
titleVBox.getChildren().add(titleBox);
buttHBox.getStyleClass().add("button-style");
buttHBox.getChildren().add(butt);
buttVBox.getChildren().add(buttHBox);
vBox.getChildren().add(grid);
border.setTop((titleBox));
border.setCenter(vBox);
border.setBottom(buttVBox);
primary.setScene(sc);
primary.show();
}
public static void main(String[] args) {
launch(args);
}
}
When a label is clicked you update this lable for every label except for border labels. This is probably not what you want.
Furthermore the value you write to the column and row fields doesn't seem to be used anywhere. You should remove them and stick to local variables.
If you don't want to trigger updated when a label on the border is clicked you could simply not register listeners for those nodes. Move the label creation and the handler registration to different loops:
for (int col = 0; col < 20; col++) {
for (int row = 0; row < 20; row++) {
Label square = new Label();
if (map.gridArray[col][row].getValue() == ' ') {
square.getStyleClass().add("default-box");
} else if (map.gridArray[col][row].getValue() == 'S') {
square.setText("Start");
square.getStyleClass().add("start-end");
} else if (map.gridArray[col][row].getValue() == 'E') {
square.setText("End");
square.getStyleClass().add("start-end");
} else {
square.getStyleClass().add("wall");
}
labelGridArray[col][row] = square;
grid.add(square, col, row);
}
}
for (int col = 1; col < 19; col++) {
final int finalCol = col; // copy accessible from handler
for (int row = 1; row < 19; row++) {
final int finalRow = row; // copy accessible from handler
final Label square = labelGridArray[col][row];
square.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if (map.gridArray[finalCol][finalRow].getValue() == 'W'){
square.getStyleClass().remove("wall");
square.getStyleClass().add("default-box");
map.gridArray[finalCol][finalRow].setTier(finalRow);
map.gridArray[finalCol][finalRow].setColumn(finalCol);
map.gridArray[finalCol][finalRow].setValue(' ');
} else {
square.getStyleClass().remove("default-box");
square.getStyleClass().add("wall");
map.gridArray[finalCol][finalRow].setTier(finalRow);
map.gridArray[finalCol][finalRow].setColumn(finalCol);
map.gridArray[finalCol][finalRow].setValue('W');
}
}
});
}
}
Additional notes:
Unless getColumn and getTier returns different values the following disjunction always yields true, since a primitive value cannot be equal to both 0 and 19.
if (map.gridArray[col][row].getTier() != 0
|| map.gridArray[col][row].getTier() != 19
|| map.gridArray[col][row].getColumn() != 0
|| map.gridArray[col][row].getColumn() != 19) {
Better use an enum instead of char for the value. This allows you to add data/methods to the object and also prevents you from accidentally using wrong values or the wrong case.
public enum CellType {
EMPTY("", "default-box"),
START("Start", "start-end"),
END("End", "start-end"),
WALL("", "wall");
private final String styleClass;
private final String text;
CellType(String text, String styleClass) {
this.text = text;
this.styleClass = styleClass;
}
public String getStyleClass() {
return styleClass;
}
public String getText() {
return text;
}
public Values inverted() {
// this requires the values to be kept in this order
CellType[] vals = values();
return vals[vals.length - 1 - ordinal()];
}
}
This allows you to simplify the code in the handler:
CellType type = map.gridArray[finalCol][finalRow].getValue();
square.getStyleClass().remove(type.getStyleClass());
type = type.inverted();
map.gridArray[finalCol][finalRow].setValue(type);
square.getStyleClass().add(type.getStyleClass());
// are the following lines really necessary?
map.gridArray[finalCol][finalRow].setTier(finalRow);
map.gridArray[finalCol][finalRow].setColumn(finalCol);
Here is how an MCVE demonstrating the question could look like. The following code also demonstrates a simplified implementation responding to a mouse click as you need :
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class MazeGuiPane extends Application {
private int gridSize = 20;
int col, row;
#Override
public void start(Stage primary) {
StreetMap map = new StreetMap();
map.makeGrid(gridSize);
GridPane grid = new GridPane();
for (col = 0; col < 20; col++) {
for (row = 0; row < 20; row++) {
grid.add(map.getGridArray()[row][col], col, row);
}
}
primary.setScene(new Scene(grid));
primary.show();
}
public static void main(String[] args) {
launch(args);
}
}
//represents a single square
class Square extends Label{
private int squareSize = 20;
private static final String empty = " ", wall = "W"; //better use enum
Square(){ this(empty); }
Square(String txt){
setText(txt);
setStyle("-fx-border-color: black");
setPrefSize(squareSize,squareSize);
}
void toggle() {
setText( getText().equals(empty) ? wall : empty);
}
}
//represents grid of squares
class StreetMap {
private Label[][] gridArray;
public void makeGrid(int size) {
gridArray = new Square[size][size];
for (int col = 0; col < size; col++) {
for (int row = 0; row < size; row++) {
Label square = new Square();
gridArray[row][col] = square;
//add handler to those who need to respond to mouse clicks
if((row > 0) && (row < (size -1)) ) {
if((col > 0) && (col < (size -1)) ) {
square.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
((Square)square).toggle();
}
});
}
}
}
}
}
Label[][] getGridArray() { return gridArray; }
}
EDIT
An improved version, following kleopatra's comments :
public class MazeGuiPane extends Application {
private int gridSize = 20;
int col, row;
#Override
public void start(Stage primary) {
StreetMap map = new StreetMap();
map.makeGrid(gridSize);
GridPane grid = new GridPane();
for (col = 0; col < 20; col++) {
for (row = 0; row < 20; row++) {
grid.add(map.getGridArray()[row][col].getNode(), col, row);
}
}
primary.setScene(new Scene(grid));
primary.show();
}
public static void main(String[] args) {
launch(args);
}
}
//represents a single square
class Square{
private Label label;
private int squareSize = 20;
private static final String empty = " ", wall = "W"; //better use enum
private SimpleStringProperty squareText = new SimpleStringProperty();
Square(){ this(empty); }
Square(String txt){
squareText.set(txt);
label = new Label();
label.textProperty().bind(squareText);
label.setStyle("-fx-border-color: black");
label.setPrefSize(squareSize,squareSize);
}
void toggle() {
squareText.set(squareText.get().equals(empty) ? wall : empty);
}
void addMouseHandler() {
label.setOnMouseClicked(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent t) { toggle();}
});
}
Node getNode() {return label; }
}
//represents grid of squares
class StreetMap {
private Square[][] gridArray;
public void makeGrid(int size) {
gridArray = new Square[size][size];
for (int col = 0; col < size; col++) {
for (int row = 0; row < size; row++) {
Square square = new Square();
gridArray[row][col] = square;
//add handler to those who need to respond to mouse clicks
if((row > 0) && (row < (size -1)) ) {
if((col > 0) && (col < (size -1)) ) {
square.addMouseHandler();
}
}
}
}
}
Square[][] getGridArray() { return gridArray; }
}
I am creating a simple 9x9 grid for Minesweeper. One of the primary functions of this game is to have a recursion to check all the sides when the tile clicked has no bombs surrounding it. In the code attached below, I have been able to create a function that checks the upper and left side of the tile. If I add more directions, such as lower and right side, the program will crash and will not properly display the tiles. (Check the method countBorders under the line //MY MAIN PROBLEM)
//displays the main GUI
package Minesweeper4;
public class mainFrame {
public static void main(String[] args) {
new Grid().setVisible(true);
}
}
// the main code
package Minesweeper4;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class Grid extends JFrame implements ActionListener {
private JPanel mainGrid;
private JButton button1, button2;
private JButton[][] buttons = new JButton[9][9];
private String[][] mines = new String[9][9];
private ArrayList<ParentSquare> parentSquare = new ArrayList<ParentSquare>();
Random rand = new Random();
NumberSquare numberSquare = new NumberSquare();
MineSquare mineSquare = new MineSquare();
public void addMines() {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
mines[j][k] = ".";
}
}
for (int i = 0; i < 3; i++) {
int temp_x = rand.nextInt(9);
int temp_y = rand.nextInt(9);
mines[temp_x][temp_y] = "x";
}
}
public void showMines() {
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
String temp = mines[x][y];
if (temp.equals("x")) {
System.out.println("X: " + (x + 1) + " Y: " + (y + 1) + " Value: " + temp);
}
}
}
}
public Grid() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setTitle("Minesweeper 1.0");
mainGrid = new JPanel();
mainGrid.setLayout(new GridLayout(9, 9));
this.add(mainGrid);
button1 = new JButton("Boop");
button2 = new JButton("Poop");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
buttons[i][j] = new JButton("");
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.GRAY);
}
}
for (int k = 0; k < 9; k++) {
for (int l = 0; l < 9; l++) {
mainGrid.add(buttons[k][l]);
}
}
addMines();
showMines();
}
public void countBorders(int x, int y) {
int UL = 0, UU = 0, UR = 0, LL = 0, RR = 0, DL = 0, DD = 0, DR = 0, SUM = 0;
if (x > 0) {
UU = checkTile(x - 1, y);
}
if (y > 0) {
LL = checkTile(x, y - 1);
}
if (y < 8) {
RR = checkTile(x, y + 1);
}
if (x < 8) {
DD = checkTile(x + 1, y);
}
if ((x > 0) && (y > 0)) {
UL = checkTile(x - 1, y - 1);
}
if ((x > 0) && (y < 8)) {
UR = checkTile(x - 1, y + 1);
}
if ((x < 8) && (y > 0)) {
DL = checkTile(x + 1, y - 1);
}
if ((x < 8) && (y < 8)) {
DR = checkTile(x + 1, y + 1);
}
SUM = UL + UU + UR + LL + RR + DL + DD + DR;
printTile(x, y, SUM);
if (SUM == 0) { //MY MAIN PROBLEM
// if ((x > 0) && (y > 0)) {countBorders(x-1, y-1);} //Upper left
if (x > 0) {
countBorders(x - 1, y);
} //Upper
// if ((x > 0) && (y < 8)) {countBorders(x-1, y+1);} //Upper right
if (y > 0) {
countBorders(x, y - 1);
} //Left
// if (y < 8) {countBorders(x, y+1);} //Right
// if ((x < 8) && (y > 0)) {countBorders(x+1, y-1);} //Down Left
// if (x < 8) {countBorders(x+1, y);} //Down
// if ((x < 8) && (y < 8)) {countBorders(x+1, y+1);} //Down Right
}
}
public void printTile(int x, int y, int SUM) {
String text = Integer.toString(SUM);
buttons[x][y].setText(text);
buttons[x][y].setBackground(Color.CYAN);
}
public int checkTile(int x, int y) {
String c = mines[x][y];
if (c.equals("x")) {
return 1;
} else {
return 0;
}
}
public void click(int x, int y) {
String mine = mines[x][y];
if (mine.equals("x")) {
System.out.println("Bomb!!!");
buttons[x][y].setText("!");
buttons[x][y].setBackground(Color.RED);
} else {
countBorders(x, y);
System.out.println("Safe!!!");
// buttons[x][y].setText("√");
// buttons[x][y].setBackground(Color.WHITE);
}
}
#Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (e.getSource() == buttons[i][j]) {
System.out.println("Clicked Tile X: " + (i + 1) + " Y: " + (j + 1));
//buttons[i][j].setText("!");
click(i, j);
}
}
}
}
}
Is there a way on how to fix this recursion problem?
Thank you in advance and I'm really trying to learn Java. Have a nice day!
Your error-causing recursion has no stopping logic that I can find, and what you need to do is to somehow check to make sure that a cell hasn't already been counted or pressed before re-counting it. Otherwise the code risks throwing a stackoverflow error. This will require giving the cells being counted some state that would tell you this information, that would tell you if the cell has already been counted.
For an example of a successful program that does this logic, feel free to look at my Swing GUI example, one I created 5 years ago. In this code, I've got a class, MineCellModel, that provides the logic (not the GUI) for a single mine sweeper cell, and the class contains a boolean field, pressed, that is false until the cell is "pressed", either by the user pressing the equivalent button, or recursively in the model's logic. If the cell is pressed, if the boolean is true, the recursion stops with this cell.
You can find the code here: Minesweeper Action Events. It's an old program, and so I apologize for any concepts or code that may be off.
Running the code results in this:
Here's the code present in a single file:
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.event.SwingPropertyChangeSupport;
#SuppressWarnings("serial")
public class MineSweeper {
private JPanel mainPanel = new JPanel();
private MineCellGrid mineCellGrid;
private JButton resetButton = new JButton("Reset");
public MineSweeper(int rows, int cols, int mineTotal) {
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mineCellGrid = new MineCellGrid(rows, cols, mineTotal);
resetButton.setMnemonic(KeyEvent.VK_R);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mineCellGrid.reset();
}
});
mainPanel.add(mineCellGrid);
mainPanel.add(new JSeparator());
mainPanel.add(new JPanel() {
{
add(resetButton);
}
});
}
private JPanel getMainPanel() {
return mainPanel;
}
private static void createAndShowUI() {
JFrame frame = new JFrame("MineSweeper");
// frame.getContentPane().add(new MineSweeper(20, 20,
// 44).getMainPanel());
frame.getContentPane().add(new MineSweeper(12, 12, 13).getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
#SuppressWarnings("serial")
class MineCellGrid extends JPanel {
private MineCellGridModel model;
private List<MineCell> mineCells = new ArrayList<MineCell>();
public MineCellGrid(final int maxRows, final int maxCols, int mineNumber) {
model = new MineCellGridModel(maxRows, maxCols, mineNumber);
setLayout(new GridLayout(maxRows, maxCols));
for (int row = 0; row < maxRows; row++) {
for (int col = 0; col < maxCols; col++) {
MineCell mineCell = new MineCell(row, col);
add(mineCell);
mineCells.add(mineCell);
model.add(mineCell.getModel(), row, col);
}
}
reset();
}
public void reset() {
model.reset();
for (MineCell mineCell : mineCells) {
mineCell.reset();
}
}
}
class MineCellGridModel {
private MineCellModel[][] cellModelGrid;
private List<Boolean> mineList = new ArrayList<Boolean>();
private CellModelPropertyChangeListener cellModelPropChangeListener = new CellModelPropertyChangeListener();
private int maxRows;
private int maxCols;
private int mineNumber;
private int buttonsRemaining;
public MineCellGridModel(final int maxRows, final int maxCols, int mineNumber) {
this.maxRows = maxRows;
this.maxCols = maxCols;
this.mineNumber = mineNumber;
for (int i = 0; i < maxRows * maxCols; i++) {
mineList.add((i < mineNumber) ? true : false);
}
cellModelGrid = new MineCellModel[maxRows][maxCols];
buttonsRemaining = (maxRows * maxCols) - mineNumber;
}
public void add(MineCellModel model, int row, int col) {
cellModelGrid[row][col] = model;
model.addPropertyChangeListener(cellModelPropChangeListener);
}
public void reset() {
buttonsRemaining = (maxRows * maxCols) - mineNumber;
// randomize the mine location
Collections.shuffle(mineList);
// reset the model grid and set mines
for (int r = 0; r < cellModelGrid.length; r++) {
for (int c = 0; c < cellModelGrid[r].length; c++) {
cellModelGrid[r][c].reset();
cellModelGrid[r][c].setMined(mineList.get(r * cellModelGrid[r].length + c));
}
}
// advance value property of all neighbors of a mined cell
for (int r = 0; r < cellModelGrid.length; r++) {
for (int c = 0; c < cellModelGrid[r].length; c++) {
if (cellModelGrid[r][c].isMined()) {
int rMin = Math.max(r - 1, 0);
int cMin = Math.max(c - 1, 0);
int rMax = Math.min(r + 1, cellModelGrid.length - 1);
int cMax = Math.min(c + 1, cellModelGrid[r].length - 1);
for (int row2 = rMin; row2 <= rMax; row2++) {
for (int col2 = cMin; col2 <= cMax; col2++) {
cellModelGrid[row2][col2].incrementValue();
}
}
}
}
}
}
private class CellModelPropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
MineCellModel model = (MineCellModel) evt.getSource();
int row = model.getRow();
int col = model.getCol();
if (evt.getPropertyName().equals(MineCellModel.BUTTON_PRESSED)) {
if (cellModelGrid[row][col].isMineBlown()) {
mineBlown();
} else {
buttonsRemaining--;
if (buttonsRemaining <= 0) {
JOptionPane.showMessageDialog(null, "You've Won!!!", "Congratulations",
JOptionPane.PLAIN_MESSAGE);
}
if (cellModelGrid[row][col].getValue() == 0) {
zeroValuePress(row, col);
}
}
}
}
private void mineBlown() {
for (int r = 0; r < cellModelGrid.length; r++) {
for (int c = 0; c < cellModelGrid[r].length; c++) {
MineCellModel model = cellModelGrid[r][c];
if (model.isMined()) {
model.setMineBlown(true);
}
}
}
}
private void zeroValuePress(int row, int col) {
int rMin = Math.max(row - 1, 0);
int cMin = Math.max(col - 1, 0);
int rMax = Math.min(row + 1, cellModelGrid.length - 1);
int cMax = Math.min(col + 1, cellModelGrid[row].length - 1);
for (int row2 = rMin; row2 <= rMax; row2++) {
for (int col2 = cMin; col2 <= cMax; col2++) {
cellModelGrid[row2][col2].pressedAction();
}
}
}
}
}
#SuppressWarnings("serial")
class MineCell extends JPanel {
private static final String LABEL = "label";
private static final String BUTTON = "button";
private static final int PS_WIDTH = 24;
private static final int PS_HEIGHT = PS_WIDTH;
private static final float LABEL_FONT_SIZE = (float) (24 * PS_WIDTH) / 30f;
private static final float BUTTON_FONT_SIZE = (float) (14 * PS_WIDTH) / 30f;
private JButton button = new JButton();
private JLabel label = new JLabel(" ", SwingConstants.CENTER);
private CardLayout cardLayout = new CardLayout();
private MineCellModel model;
public MineCell(final boolean mined, int row, int col) {
model = new MineCellModel(mined, row, col);
model.addPropertyChangeListener(new MyPCListener());
label.setFont(label.getFont().deriveFont(Font.BOLD, LABEL_FONT_SIZE));
button.setFont(button.getFont().deriveFont(Font.PLAIN, BUTTON_FONT_SIZE));
button.setMargin(new Insets(1, 1, 1, 1));
setLayout(cardLayout);
add(button, BUTTON);
add(label, LABEL);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pressedAction();
}
});
button.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
model.upDateButtonFlag();
}
}
});
}
public MineCell(int row, int col) {
this(false, row, col);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PS_WIDTH, PS_HEIGHT);
}
public void pressedAction() {
if (model.isFlagged()) {
return;
}
model.pressedAction();
}
public void showCard(String cardConstant) {
cardLayout.show(this, cardConstant);
}
// TODO: have this change the button's icon
public void setFlag(boolean flag) {
if (flag) {
button.setBackground(Color.yellow);
button.setForeground(Color.red);
button.setText("f");
} else {
button.setBackground(null);
button.setForeground(null);
button.setText("");
}
}
private void setMineBlown(boolean mineBlown) {
if (mineBlown) {
label.setBackground(Color.red);
label.setOpaque(true);
showCard(LABEL);
} else {
label.setBackground(null);
}
}
public MineCellModel getModel() {
return model;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
model.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
model.removePropertyChangeListener(listener);
}
private class MyPCListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
if (propName.equals(MineCellModel.MINE_BLOWN)) {
setMineBlown(true);
} else if (propName.equals(MineCellModel.FLAG_CHANGE)) {
setFlag(model.isFlagged());
} else if (propName.equals(MineCellModel.BUTTON_PRESSED)) {
if (model.isMineBlown()) {
setMineBlown(true);
} else {
String labelText = (model.getValue() == 0) ? "" : String.valueOf(model
.getValue());
label.setText(labelText);
}
showCard(LABEL);
}
}
}
public void reset() {
setFlag(false);
setMineBlown(false);
showCard(BUTTON);
label.setText("");
}
}
class MineCellModel {
public static final String FLAG_CHANGE = "Flag Change";
public static final String BUTTON_PRESSED = "Button Pressed";
public static final String MINE_BLOWN = "Mine Blown";
private int row;
private int col;
private int value = 0;
private boolean mined = false;;
private boolean flagged = false;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(this);
private boolean pressed = false;
private boolean mineBlown = false;
public MineCellModel(boolean mined, int row, int col) {
this.mined = mined;
this.row = row;
this.col = col;
}
public void incrementValue() {
int temp = value + 1;
setValue(temp);
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setMineBlown(boolean mineBlown) {
this.mineBlown = mineBlown;
PropertyChangeEvent evt = new PropertyChangeEvent(this, MINE_BLOWN, false, true);
pcSupport.firePropertyChange(evt);
}
public boolean isMineBlown() {
return mineBlown;
}
public void setMined(boolean mined) {
this.mined = mined;
}
public void setFlagged(boolean flagged) {
this.flagged = flagged;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public boolean isMined() {
return mined;
}
public boolean isFlagged() {
return flagged;
}
public void pressedAction() {
if (pressed) {
return;
}
pressed = true;
if (mined) {
setMineBlown(true);
}
PropertyChangeEvent evt = new PropertyChangeEvent(this, BUTTON_PRESSED, -1, value);
pcSupport.firePropertyChange(evt);
}
public void upDateButtonFlag() {
boolean oldValue = flagged;
setFlagged(!flagged);
PropertyChangeEvent evt = new PropertyChangeEvent(this, FLAG_CHANGE, oldValue, flagged);
pcSupport.firePropertyChange(evt);
}
public void reset() {
mined = false;
flagged = false;
pressed = false;
mineBlown = false;
value = 0;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
}
Edit Regarding Recursion
My code uses recursion, but with a level of indirection, since it is based on a Model-View-Controller type of design pattern, and the recursion is within the notification of listeners. Note that each GUI MineCell object holds its own MineCellModel object, the latter holds the MineCell's state. When a GUI JButton held within the MineCell object is pressed, its ActionListener calls the same class's pressed() method:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pressedAction();
}
});
This method first checks the corresponding MineCellModel to see if it has been "flagged", if a boolean called flagged is true. If so, this means that the user has right-clicked on the button, and it is not active, and so the method returns. Otherwise the MineCellModel's pressedAction() method is called,
public void pressedAction() {
if (model.isFlagged()) {
return;
}
model.pressedAction();
}
and here is where the recursion starts, and it does so through an Observer Design Pattern:
// within MineCellModel
public void pressedAction() {
if (pressed) {
// if the button's already been pressed -- return, do nothing
return;
}
// otherwise make pressed true
pressed = true;
// if we've hit a mine -- blow it!
if (mined) {
setMineBlown(true);
}
// *** Here's the key *** notify all listeners that this button has been pressed
PropertyChangeEvent evt = new PropertyChangeEvent(this, BUTTON_PRESSED, -1, value);
pcSupport.firePropertyChange(evt);
}
The two lines of code on the bottom notify any listeners to this model that its BUTTON_PRESSED state has been changed, and it sends the MineCellModel's value to all listeners. The value int is key as its the number of neighbors that have mines. So what listens to the MineCellModel? Well, one key object is the MineCellGridModel, the model that represents the state of the entire grid. It has a CellModelPropertyChangeListener class that does the actual listening, and within this class is the following code:
private class CellModelPropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
// first get the MineCellModel for the cell that triggered this notification
MineCellModel model = (MineCellModel) evt.getSource();
int row = model.getRow();
int col = model.getCol();
// if the event is a button pressed event
if (evt.getPropertyName().equals(MineCellModel.BUTTON_PRESSED)) {
// first check if a mine was hit, and if so, call mineBlown()
if (cellModelGrid[row][col].isMineBlown()) {
mineBlown(); // this method iterates through all cells and blows all mines
} else {
// here we check for a winner
buttonsRemaining--;
if (buttonsRemaining <= 0) {
JOptionPane.showMessageDialog(null, "You've Won!!!", "Congratulations",
JOptionPane.PLAIN_MESSAGE);
}
// here is the key spot -- if cell's value is 0, call the zeroValuePress method
if (cellModelGrid[row][col].getValue() == 0) {
zeroValuePress(row, col);
}
}
}
}
private void mineBlown() {
// ... code to blow all the un-blown mines
}
// this code is called if a button pressed has 0 value -- no mine neighbors
private void zeroValuePress(int row, int col) {
// find the boundaries of the neighbors
int rMin = Math.max(row - 1, 0); // check for the top edge
int cMin = Math.max(col - 1, 0); // check for the left edge
int rMax = Math.min(row + 1, cellModelGrid.length - 1); // check for the bottom edge
int cMax = Math.min(col + 1, cellModelGrid[row].length - 1); // check for right edge
// iterate through the neighbors
for (int row2 = rMin; row2 <= rMax; row2++) {
for (int col2 = cMin; col2 <= cMax; col2++) {
// *** Here's the recursion ***
// call pressedAction on all the neighbors
cellModelGrid[row2][col2].pressedAction();
}
}
}
}
So the key method in the listener above is the zeroValuePress(...) method. It first finds the boundaries of the neighbors around the current mine cell, using Math.min(...) and Math.max(...) to be careful not to go beyond the right, left, or top or bottom boundaries of the grid. It then iterates through the cell neighbors calling pressedAction() on each one of the neighbors MineCellModels held by this grid. As you know from above, the pressedAction() method will check if the cell has already been pressed, and if not, changes its state, which then notifies this same listener, resulting in recursion.
One of the primary functions of this game is to have a recursion to check all the sides when the tile clicked has no bombs surrounding it.
Looks like you are stucked on the part where you need to update the cell with number according to the number of bombs surrounding it.
These are the things for you to take note:
To update the numbers on the cells, there is no need to use recursion. The only part I used recursion is when user clicks on a cell with value == 0(stepped on an empty grid).
Checking all 8 directions can be done easily without writing large number of if-conditions. All you need is a pair of nested for-loop. Just traverse the 3x3 grid like a 2D array (see diagram below for illustration).
In the loop, set conditions to ensure you are within bounds (of the 3x3 matrix) before reading current grid's value (see code below).
To traverse the 3x3 matrix as shown in the diagram, we can use a pair of nested loops:
for(int x=(coordX-1); x<=(coordX+1); x++)
for(int y=(coordY-1); y<=(coordY+1); y++)
if(x!=-1 && y!= -1 && x! = ROWS && y! = COLS && map[x][y] != 'B')
if(map[x][y] == '.')
map[x][y] = '1';
else
map[x][y] += 1;
The if-condition prevents working on array element which is out of bounds.