Tackle the problem JAVA FXML
Hints:- on what to do
Need to replace the Model in the starter code with a model that provides logic for the concentration game. It is recommend to keep the same basic methods as the original model, except that you should replace getSide() with getRows() and getColumns() methods.
*-------------------------------*
package model;
import java.util.ArrayList;import java.util.Random;
public class TilePuzzle{private String [ ][ ] tiles;private int side; // grid sizeprivate int emptyRow;private int emptyCol;private Random randGen;private int row;private int column;/** constructor* @param newSide grid size*/public TilePuzzle( int newSide ){randGen = new Random();setUpGame( newSide );}/** setUpGame* @param newSide grid size*/public void setUpGame( int newSide ){if ( newSide < 3="">side =3;elseside = newSide;tiles = new String[side][side];ArrayList textList = new ArrayList<>();for ( int i = 0; i < side*side;="" i+="1">textList.add(String.valueOf(i));textList.add( "" );// initialize tilesfor ( int i = 0; i < side;="" i++="">{for ( int j = 0; j < side;="" j++="">{tiles[i][j] = textList.remove(randGen.nextInt(textList.size()));if(tiles[i][j].equals("")){emptyRow = i;emptyCol = j;}}}}/** getSide* @return side*/public int getSide( ){return side;}/** getTiles* @return a copy of tiles*/public String[ ][ ] getTiles( ){String[ ][ ] copyOfTiles = new String[side][side];for ( int i = 0; i < side;="" i++="">{for ( int j = 0; j < side;="" j++="">{copyOfTiles[i][j] = tiles[i][j];}}return copyOfTiles;}/** tryToPlay* enable play if play is legal* @return true if the play is legal, false otherwise*/public boolean tryToPlay( int row, int col ){if ( possibleToPlay( row, col ) ){// play: switch empty String and tile label at row, coltiles[emptyRow][emptyCol] = tiles[row][col];tiles[row][col] = "";// update emptyRow and emptyColemptyRow = row;emptyCol = col;return true;}elsereturn false;}/** possibleToPlay* @return true if the play is legal, false otherwise*/public boolean possibleToPlay( int row, int col ){if ( ( col == emptyCol && Math.abs( row - emptyRow ) == 1 )|| ( row == emptyRow && Math.abs( col - emptyCol ) == 1 ) )return true;elsereturn false;}/** won* @return true if the tiles are all at the right place, false*/public boolean won( ){for ( int i = 0; i < side="" ;="" i++="">{for ( int j = 0; j < side;="" j++="">{if ( !( tiles[i][j].equals(String.valueOf( i * side + j + 1 ) ) )&& ( i != side - 1 || j != side - 1 ) )return false;}}return true;}}Just need to fix the class to display a empty gridPane
}
Just need to fix the class to display a empty gridPane
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here