In this part, you will need to create a Slide Puzzle Game of variable size. If y
ID: 3749022 • Letter: I
Question
In this part, you will need to create a Slide Puzzle Game of variable size. If you are not familiar with the Slide Puzzle Game, see this example:https:/len.wikipedia.org/wiki/Sliding puzzle, of which a special case is: https://en.wikipedia.org/wiki/15 puzzle You will firstly need to ask the user what size (s) board they desire, then create a board of size (s x s). The first s2 - 1 entries should be filled with a random permutation of the numbers 1: s2 - 1. The last element in the matrix is a zero, representing the free space. The free space is used to shift tiles horizontally and vertically (but not diagonally) around the board. The objective of the game is to re-arrange the tiles so that they are ordered correctly, with the free space in the bottom right corner To generate a move, the user simply needs to enter the row/column coordinate for the tile they wish to move into the free space. For an example of the input and output required, see the file on Moodle named slidepuzzle-output.txt This will also describe the behaviour of your game when an invalid move is made, and the text that should be output to the screen upon a win. You do not need to check for input correctness -just assume that the user is capable of entering two values with a space between them. The maximum value of s required for your implementation is 9 (ie single digit only, double digits not required) Hint: create a solution for the 3x3 problem first and then attempt to expand to larger casesExplanation / Answer
/*
algorithm (s,array[1..s-1],array[s][s]=0)
The following algorithm gets s, the size of the board and a list of elements.assumes that the last empty lement is set as 2
{
do
{
accept the coordinated of element to move
check whether the coordinates are nearer to the empty cell
if not accept other coordinate values.
else swap the element to the empty board
also update the coordinates of empty cell
check the elements are in sorted order without a gap in the board other than the last cell.
}until all the elements are in sorted order
}
*/
//The following is a sample code that implements the sliding puzzle in java
//logic is almost implemented.
import java.io.*;
public class Puzzle
{
public static void main(String args[])throws IOException
{
int ch,s,i,j,p,q,temp,r,t;
int[][] array;
InputStreamReader n = new InputStreamReader(System.in);
BufferedReader ob = new BufferedReader(n);
ch=1;
System.out.println("Enter the s value:");
s= Integer.parseInt(ob.readLine()); //entering the board size
array = new int[s][s];
System.out.println(" Enter the elements.Should be between 1 and "+ ((s*s)-1)); //recieving the initial board values
for(i=0;i<s;i++)
for(j=0;j<s;j++)
array[i][j]=Integer.parseInt(ob.readLine());
array[s-1][s-1]=0; //setting the last element as 0
r=s-1; //stores the coordinates of empty board
t=s-1;
do
{
display();
System.out.println("Enter the location of element to be changed:"); //recieving coordinates of element to move
p=Integer.parseInt(ob.readLine());
q=Integer.parseInt(ob.readLine());
int m = p-r;
int l = q-t;
if((m <= 1 || m>= -1) && (l <=1 || l >= -1)) //checking whether the entered coordinates are nearer or far
{
temp=array[p][q];
array[p][q]=array[r][t];
array[r][t]=temp;
r=p;
t=q;
}
else
System.out.println("Can't move the number");
display();
ch=checkch();
}while(ch!=0);
}
public void display()
{
for(i=0;i<s;i++)
{
for(j=0;j<s;j++)
{
if(array[i][j] == 0)
System.out.println(" ");
else
System.out.println(array[i][j]+" ");
}
System.out.println(" ");
}
}
public int checkch() //checks whether solution found or not
{
for(i=0;i<s;i++)
{
for(j=0;j<s;j++)
{
if(array[i][j]-array[i][j+1]!=1 || (array[s-1][s-1] != 0))
{
return 1;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.