Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

In this assignment, you will be writing a C++ command-line program that simulates a 3x3 sliding puzzle game. In a sliding puzzle game, 8 tiles with the numerals 1-8 are placed onto a 3x3 square grid. One of the positions is left open for tiles coming from the top, bottom, left, or right (depending on the puzzle configuration). The goal of the game is to slide the tiles around so they appear in order on the puzzle board. When writing this program, you should adhere to the object-oriented programming paradigm.

Assignment

When writing this program, you must design an object that represents the sliding puzzle board. To do this, you must consider the attributes and methods needed by the board to respond to a driver that contains the core logic of the game.

We should begin with a preliminary run-down of the object's features.

Attributes

int SlidingPuzzle::theBoard[3][3];
Description: The primary attribute of the object represents the data on the board. The board array contains the digits 1-8 and the asterisk (*) to represent the open position on the puzzle. When making moves, the data in this array will change to reflect the new state of the board. When the object displays itself on the screen, we will use this array to determine the board's current state.

Methods

SlidingPuzzle::SlidingPuzzle();
Description: This is the constructor method for the object. It will automatically execute once the board has been instantiated in the driver. Upon creation, it would be wise to immediately populate the board with valid values for all of the tiles. You should be able to trigger this with a single call to InitializeBoard().

void SlidingPuzzle::InitializeBoard();
Description: This method will manually populate the board with the game's starting configuration. Initially, the board should look like this:

1 2 3
4 5 6
7 8 *   <=== the * represents the blank space on the board

bool SlidingPuzzle::isBoardSolved();
Description: This method examines the board to determine if it is in the solved state. It returns a boolean value reflecting this determination.

bool SlidingPuzzle::slideTile(int);
Description: This method takes a single argument indicating the direction to slide a tile. A good approach would be to set up numerical codes representing the four possible movement directions (UP, DOWN, LEFT, and RIGHT). When the method is called, movement in the indicated direction should take place by exchanging the values in the appropriate tiles. If the move is not possible, the state of the board should remain unchanged. A boolean value should be returned reflecting whether or not the board has been mutated.

void SlidingPuzzle::scrambleBoard();
Description: This method will use the pseudo-random number generator to make an arbitrary number of random moves on the board (I suggest 10,000+ moves to thoroughly confuscate the board). Keep in mind that the seeding of the random number generator should take place in the driver code (not the object's constructor). Also keep in mind that randomly placing the pieces on the board will result in a situation where half of the time the puzzle can be solved and the other half of the time a solution is impossible.

void SlidingPuzzle::PrintBoard();
Description: This method will display the board on the console as a 3x3 grid. I suggest writing a "quick and dirty" version of this method initially so you can look at the maneuvers as they take place on the board during development. When the core logic of the program is done, you should revisit this method and upgrade it to support console color.

Hints

The following function demonstrates color printing. You can compile and play with the code to learn the basics of color. Keep in mind that you must include the Windows API in order for this to work.

#include
void printTheRainbow();

void printTheRainbow() {
int currentColor = 7;

// get the handle for the console
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// exhaustively print the colors
cout << left;
for(currentColor = 0; currentColor <= 255; currentColor++) {
SetConsoleTextAttribute(hConsole, currentColor);
cout << setw(5) << currentColor
<< "Look at the pretty COLORS!" << endl;
}
}

Sample Run

When the program begins, the board should be loaded with a solved configuration.

After the user presses any key, the board will be scrambled by making a large number of random moves (for a 3x3 puzzle, 100,000 attempted random moves in random directions will result in a sufficiently scrambled board). Keep in mind that scrambling the board in a totally random fashion will result in 50% of board states being solvable and 50% of board states being unsolvable.

Note: This sample output is provided to give you an idea of what a proper implementation of the program may look like. The output you provide with your program should be captured from your implementation of the problem solution (not a cut & paste job of this output). The output from your program should resemble this layout as closely as possible to ensure you have fulfilled all of the assignment criteria, but individual style and creativity in your personal implementation are expected and encouraged.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9453368

Have any Question?


Related Questions in C/C++

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As