the tho adjacent Location to ap Apply C+language to program a puzzle game The pu
ID: 3757354 • Letter: T
Question
the tho adjacent Location to ap Apply C+language to program a puzzle game The puzzle involves arranging a set of thirty- six numbers in an ascending order. The numbers are displayed in a 6x6 grid. These numbers are randomly generated and therefore are not sorted initially. The player has to sort these numbers by swapping two adjacent numbers one at a time. A number can be swapped with another number on its right, left, top or bottom. A number cannot be swapped with a number that is diagonally adjacent to it. Figure Q1(a)(iii) shows the result if the player has successfully sorted the numbers. Figure Q1(i) shows a sample initial screen of the game with the 36 randomly generated numbers arranged in a 6x6 grid. 557 2673 0711 12138 ave sorted e You are required to implement the puzzle game with the following specifications: (a) The program should randomly generate thirty-six number. Different set of numbers should be displayed each time the program is started. Enter the twe et Loaton to sap Figure Q1(a (5 marks) For example, if the number 5461 is to be moved, then it can be swapped with either the number 2118 (top). 218 (bottom), 5397 (left), or 8145 (right). Any illegal swapping will result in an error message being displayed. Figure Q1(ii) shows the result of player swapping the numbers 5461 and 218 in a partially sorted manner. (b) The program displays the thirty-six numbers in a 6x6 grid. Each number should be enclosed in an ASCII box. (5 marks) (c) The program should prompt the player to swap two adjacent numbers. It should check for illegal moves i.e. if the two numbers are not adjacent to each other either horizontally or vertically. It should display an error message if the move is not allowed. (5 marks)Explanation / Answer
#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#define MAXR 6
#define MAXC 6
using namespace std;
// Function to sort the matrix
void sortMatrix(int matrix[][MAXC])
{
// Temporary array of size MAXR x MAXC
int tempArray[MAXR * MAXC];
int t = 0;
// Copy the elements of matrix one by one into temp[]
// Loops till number of rows
for(int r = 0; r < MAXR; r++)
// Loops till number of columns
for (int c = 0; c < MAXC; c++)
// Assigns each element of matrix to array t index position
// increase the t index by one
tempArray[t++] = matrix[r][c];
// Calls the method to sort the array
sort(tempArray, tempArray + t);
// Copy each element of array temp[] one by one in matrix[][]
t = 0;
// Loops till number of rows
for (int r = 0; r < MAXR; r++)
// Loops till number of columns
for (int c = 0; c < MAXC; c++)
// Assigns element at t index position of array to r and c index position of matrix
matrix[r][c] = tempArray[t++];
}// End of function
// Function to generate random number and stores it in matrix and copyMatrix
void generateMatrix(int matrix[][MAXC], int sortedMatrix[][MAXC])
{
// Loops till number of rows
for(int r = 0; r < MAXR; r++)
{
// Loops till number of rows
for(int c = 0; c < MAXC; c++)
{
// Generates random number between 100 and 5000
int no = rand() % 5000 + 100;
// Assigns the random number at r and c index position of matrix and sortedMatrix
matrix[r][c] = sortedMatrix[r][c] = no;
}// End of inner for loop
}// End of outer for loop
}// End of function
// Function to display matrix contents
void displayMatrix(int matrix[][MAXC])
{
// Loops till number of rows
for(int r = 0; r < MAXR; r++)
{
// Loops till number of rows
for(int c = 0; c < MAXC; c++)
cout<<setw(6)<<matrix[r][c]<<" ";
cout<<endl;
}// End of for loop
}// End of function
// Function to return if the matrix is sorted otherwise returns false
bool isSorted(int matrix[][MAXC], int sortedMatrix[][MAXC])
{
// Initially sorted status is true
bool sorted = true;
// Loops till number of rows
for(int r = 0; r < MAXR; r++)
{
// Loops till number of rows
for(int c = 0; c < MAXC; c++)
{
// Checks if r and c index position of matrix is not equals to r and c index position of sorted matrix
if(matrix[r][c] != sortedMatrix[r][c])
{
// Update the sorted status to false
sorted = false;
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Returns sorted status
return sorted;
}// End of function
// Function to return if both location 1 and location 2 position is valid otherwise returns false
bool checkPosition(int row1, int col1, int row2, int col2)
{
// Initially location status is false
bool status = false;
// Checks if location 1 row and column and location 2 row and column is within the boundary of matrix
if(row1 >= 0 && row1 < MAXR && col1 >= 0 && col1 < MAXC && row2 >= 0 && row2 < MAXR && col2 >= 0 && col2 < MAXC)
{
// Checks if both the rows numbers are same
if(row1 == row2)
{
// Checks if location 1 column number is equals to next column number of location 2
// location 1 column number is equals to previous column number of location 2
if(col1 == col2+1 || col1 == col2-1)
// Update the status to true
status = true;
}// End of if condition
// Otherwise checks if both the column number is same
else if(col1 == col2)
{
// Checks if location 1 row number is equals to next row number of location 2
// location 1 row number is equals to previous row number of location 2
if(row1 == row2+1 || row1 == row2-1)
// Update the status to true
status = true;
}// End of else if condition
}// End of outer if condition
// Returns the status
return status;
}// End of function
// main function definition
int main()
{
// Declares two matrix of size MAXR for row and MAXC for column
// originalMatrix is the original matrix needs to be sorted
// sortedMatrix to store the sorted matrix
int originalMatrix[MAXR][MAXC], sortedMatrix[MAXR][MAXC];
// To store row and column number entered by the user for both the locations
int row1, row2, col1, col2, temp;
// Calls the function to generate random number and stores it in the matrix
generateMatrix(originalMatrix, sortedMatrix);
// Calls the function to display matrix
displayMatrix(originalMatrix);
// Calls the function to sort the matrix sortedMatrix
sortMatrix(sortedMatrix);
// Loops till originalMatrix is not sorted
do
{
// Calls the method to check originalMatrix is sorted or not
if(isSorted(originalMatrix, sortedMatrix))
{
cout<<" Congratulation matrix sorted.";
break;
}// End of if condition
// Otherwise originalMatrix is not sorted
else
{
// Accepts data for both the location to swap
cout<<" Location 1 -> column: ";
cin>>col1;
cout<<" Location 1 -> row: ";
cin>>row1;
cout<<" Location 2 -> column: ";
cin>>col2;
cout<<" Location 2 -> row: ";
cin>>row2;
// Calls the function to validate both the locations row and column for swapping
if(checkPosition(row1, col1, row2, col2))
{
// Swapping process
int temp = originalMatrix[row1][col1];
originalMatrix[row1][col1] = originalMatrix[row2][col2];
originalMatrix[row2][col2] = temp;
// Calls the function to display originalMatrix after swapping
displayMatrix(originalMatrix);
}// End of else
else
cout<<" Invalid location.";
}// End of inner for loop
}while(1); // End of do - while loop
}// End of main function
Sample Output:
141 3567 1434 1600 4269 824
1578 4458 2062 4564 805 3245
3381 1927 5061 591 3095 2042
4927 536 2491 4704 4002 253
392 2482 2521 3816 4818 4995
547 1826 4871 1638 1969 5012
Location 1 -> column: 12
Location 1 -> row: 10
Location 2 -> column: 2
Location 2 -> row: 3
Invalid location.
Location 1 -> column: 0
Location 1 -> row: 5
Location 2 -> column: 5
Location 2 -> row: 4
Invalid location.
Location 1 -> column: 0
Location 1 -> row: 5
Location 2 -> column: 0
Location 2 -> row: 4
141 3567 1434 1600 4269 824
1578 4458 2062 4564 805 3245
3381 1927 5061 591 3095 2042
4927 536 2491 4704 4002 253
547 2482 2521 3816 4818 4995
392 1826 4871 1638 1969 5012
Location 1 -> column: 0
Location 1 -> row: 4
Location 2 -> column: 0
Location 2 -> row: 3
141 3567 1434 1600 4269 824
1578 4458 2062 4564 805 3245
3381 1927 5061 591 3095 2042
547 536 2491 4704 4002 253
4927 2482 2521 3816 4818 4995
392 1826 4871 1638 1969 5012
Location 1 -> column: 0
Location 1 -> row: 2
Location 2 -> column: 0
Location 2 -> row: 0
Invalid location.
Location 1 -> column: 0
Location 1 -> row: 2
Location 2 -> column: 0
Location 2 -> row: 2
Invalid location.
Location 1 -> column: 0
Location 1 -> row: 2
Location 2 -> column: 0
Location 2 -> row: 3
141 3567 1434 1600 4269 824
1578 4458 2062 4564 805 3245
547 1927 5061 591 3095 2042
3381 536 2491 4704 4002 253
4927 2482 2521 3816 4818 4995
392 1826 4871 1638 1969 5012
Location 1 -> column: 0
Location 1 -> row: 1
Location 2 -> column: 0
Location 2 -> row: 2
141 3567 1434 1600 4269 824
547 4458 2062 4564 805 3245
1578 1927 5061 591 3095 2042
3381 536 2491 4704 4002 253
4927 2482 2521 3816 4818 4995
392 1826 4871 1638 1969 5012
Location 1 -> column: 1
Location 1 -> row: 1
Location 2 -> column: 0
Location 2 -> row: 1
141 3567 1434 1600 4269 824
4458 547 2062 4564 805 3245
1578 1927 5061 591 3095 2042
3381 536 2491 4704 4002 253
4927 2482 2521 3816 4818 4995
392 1826 4871 1638 1969 5012
Location 1 -> column: 1
Location 1 -> row: 0
Location 2 -> column: 1
Location 2 -> row: 1
141 547 1434 1600 4269 824
4458 3567 2062 4564 805 3245
1578 1927 5061 591 3095 2042
3381 536 2491 4704 4002 253
4927 2482 2521 3816 4818 4995
392 1826 4871 1638 1969 5012
Location 1 -> column:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.