QuestionQuestion

Objective:

This lab is intended to extend your use of basic C++ language features and to give you experience using object-oriented programming techniques in C++, including:
• encapusulating game data within a class,
• prompting for and receiving user input for game moves,
• determining whether or not a move is valid, and if it is
• updating and displaying the new state of the game.
To do this, you will develop a C++ program with a game class that can (1) repeatedly display the current state of a single-player game, (2) prompt the user and receive input for the next move in the game, (3) check whether or not each input move is valid, and if it is update the game state; and (4) detect the following three conditions: successful completion of the game, if the game has reached a point where no further valid moves are possible, or if there are still remaining valid moves available.

Program Design and Implementation:
Note: the details of this assignment are again intentionally somewhat under-specified, leaving you some room to choose what you think is the best way to implement them, as long as what you do is reasonable and you explain your design decisions in comments in the code and in your Readme.txt file.

1. Open up Visual Studio, and create a new Win32 Console Application Visual C++ project for this lab.

2. Declare and define a class that implements a single-player game version of the the Nine Almonds puzzle (e.g., of type NineAlmondsGame) whose member variables are all private, as follows:
o The game board is five squares wide and five squares tall. The lower left square has coordinate 0,0 (as in the previous lab) and the upper right square has coordinate 4,4.
o Each piece in this game is a brown almond, and should be represented by an 'A' character on the terminal screen when the board is printed.
o When the game begins, the inner nine squares of the board (coordinates 1,1 through 3,3) should all have brown almonds on them, and the outer sixteen squares of the board (each of which has a 0 or 4 in the horizontal or vertical portion of their coordinate) should be empty.
o Outside your game class, declare and define an insertion operator (operator<<) that takes a reference to an ostream and a reference to a const game class object as parameters, and returns the ostream reference that was passed in (this allows the operator to be called repeatedly on the same ostream object as in cout << almond_game << endl;). Inside your game class, declare the insertion operator to be a friend so that it can access the private member variables of the game object. The operator should print out the current state of the game board with 'A' characters for squares that have almonds on them, and spaces for empty squares, with the horizontal and vertical coordinate axes labeled. For example, initially the board might be printed out as:

4

3   A A A

2   A A A

1   A A A

0

X 0 1 2 3 4

o Declare and define a public non-static done() method in the game class that takes no parameters (other than the this pointer that's passed implicitly to all non-static methods and operators) and has a bool return type. This method should return true if there is exactly one almond positioned at the center of the board (the square with coordinate 2,2), and all other squares are empty; otherwise the method should return false.
o Declare and define a public non-static stalemate() method in the game class that takes no parameters (other than the this pointer that's passed implicitly to all non-static methods and operators) and has a bool return type. This method should return false if there are any valid moves remaining in the game (as defined below) or if the done() method returns true; otherwise (if there are no valid moves remaining and the game has not been completed successfully) the stalemate() method should return true.
o Declare and define a public non-static prompt() method in the game class that takes references to two unsigned integers. The method should (repeatedly if necessary) use cout to prompt the user to type a line that either is "quit" to end the game or a string representing a valid coordinate of a square on the board as a comma-separated pair of unsigned decimal integers (for example "0,0" indicates the coordinate of the square at the lower left corner of the board).
Exactly how you handle the cases where the user inputs an invalid coordinate, a badly formed input string, etc., is up to you, but essentially the user should be re-prompted to input either "quit" or a valid coordinate until she or he does so.
If the user inputs "quit" the method should return a value indicating the user asked to quit (or you could design it to throw an exception that the program's main function or the play function should catch).
If the user inputs a string with the coordinate of a valid square on the board, the method should extract the value to the left of the comma into the first (horizontal) unsigned integer parameter that was passed to the method, and extract the value to the right of the comma into the second (vertical) unsigned integer parameter that was passed to the method.
Hint: one way to implement this method is to store the user's input in a C++-style string, find the comma character in that string and replace it with with a space character, wrap the string in an istringstream, and then extract out the values from the istringstream into the two unsigned integer variables (while of course testing whether each extraction succeeded or not).
o Declare and define a public non-static turn() method in the game class that takes no parameters. The method should (repeatedly if necessary) call the prompt() method twice to obtain coordinates for the beginning and ending squares (respectively) for a valid move in the game, or to determine that the user has quit the game (if the user quits, the turn() method should immediately return an appropriate failure value, instead of doing any further processing).
A move is valid if and only if it moves an almond to an empty square by jumping over another almond. Specifically, a valid move: (1) starts with a square on the board that contains an almond; (2) ends with an empty square that is two positions away from the original square either diagonally, vertically, or horizontally (for example 0,0 or 0,2 or 0,4 or 2,0 or 2,4 or 4,0 or 4,2 or or 4,4 would be two positions away from the center square 2,2); and (3) there is an almond on the square that is in between the two squares.
If a valid move has been made, the method should move the almond from the starting square (which becomes empty) to the ending square (which ends up with the almond in it) and also removes the almond from the square that was jumped over.
Unless the user quits, the method should require at least one valid move to be made before returning, but should allow the user to continue to make additional valid jumps with the same almond during that turn, updating the board as above for each valid move until the user decides not to make any more jumps that turn.
After each valid move, the method should print out the updated game board (using the ostream operator described above) and then after an intervening blank line, print out a single line showing the valid moves that have been made so far that turn, and then ask the user whether or not to continue that turn, as in:

4

3   A A A

2   A   A A

1   A

0

X 0 1 2 3 4

2,2 to 2,0 to 4,2

Continue this turn (YyNn)?
When (after at least one valid move) the user says the turn is over, the method should return success.
If the user says to continue the turn, the method should prompt for a single additional square to which to continue moving the almond, and should use the almond's current square as the starting square for that move.
o Declare and define a public non-static play() method that takes no parameters, prints the game board (using the ostream operator described above) and then repeatedly calls the turn() method and then the done() and stalemate() methods until either: (1) the done() method returns true in which case the program should print out a message indicating how many turns it took to complete the game successfully and then return a success code (with value 0); or (2) the stalemate() method returns true in which case the program should print out a message indicating how many turns were played and saying that no valid moves remain and then return a unique non-zero failure code; or (3) the user quits, in which case the program should print out a message indicating how many turns were played and saying that the user quit and then return a different unique non-zero failure code.

3. In your program's main function:
o Check that exactly one argument (or two if you did the extra credit portion) has been passed to the program (in addition to the program's name), and if so that the passed argument is "NineAlmonds". If not, your program should pass the program name and a second string showing the format of the command line arguments that need to be given, into a call to the usage function you implemented in the previous lab, and then return the (unique non-zero) value that was returned by that function.
o Declare an object of the Nine Almonds game class (e.g., of type NineAlmondsGame).
o Call the object's play() method, and use the result returned by that method as the program's return value.
o Check for non-zero values returned by (and catch any exceptions thrown by) the object's methods, and return an appropriate unique non-zero value from the program for each diferent failure case.
Note that in this lab (and in the remaining labs this semester) your main program should only return 0 if the game being played was successfully completed, and otherwise (if the user quit or the game reached a point where successful completion was no longer possible) should return a unique non-zero value for each different failure case.

4. Build your project, and fix any errors or warnings that occur. Please be sure to note in your ReadMe.txt file all of the different kinds of errors or warnings you ran into as you developed your lab solution (though you don't have to list every instance of each kind) in your ReadMe.txt file. If you were fortunate enough not to run into any errors or warnings, please note that instead, where you would have listed errors or warnings in your ReadMe.txt file.

5. Open up a Windows console (terminal) window and change to the directory where your program's executable file was created by Visual Studio.

6. Run the executable program through a series of trials that test it with good coverage of cases involving both well formed and badly formed inputs on the command line and from the user.
In your ReadMe.txt file please document which cases you ran (i.e., what the command lines were and what additional cases you added to the files) and summarize what your program did and whether that is correct behavior, in each case.
You should first make sure the program behaves correctly when run with a well-formed command line, as in:

lab2.exe NineAlmonds

Make sure your program (1) can handle valid and invalid move inputs from the user and keep on playing, (2) allows the user to quit when they choose, and identifies (3) successful completion of the game, or (4) when no further valid moves remain.
Then test your program's handling of badly formed command lines (e.g., without a game name, or with a game name other than "NineAlmonds").

7. In your ReadMe.txt file, make sure that your name and the lab number (lab 2) are at the top of the file, and that you've documented whether you ran into any errors or warnings while developing your solution (and if so what they were) and what the executable program did for each of the trials you ran. Be sure to save your changes to your ReadMe.txt file, as well as those to your source (and header) file(s) before preparing your solution zip file.

8. Prepare a zip file that contains only your source, header, and ReadMe.txt files (but not stdafx.h, stdafx.cpp or other Windows-generated or project files), by:
o Creating a new lab2 folder on your Windows desktop;
o Copying the appropriate files (here, ReadMe.txt, and the source and header files where you wrote your code) from Visual Studio into that folder; and then
o Right-clicking on the folder and selecting the Send To->Compressed (zipped) Folder item in the menu that appears.
Please make sure that you produce and submit a .zip file, and not a file in .rar, .7z, or other format.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#ifndef SQUARE_H
#define SQUARE_H

#include "Square.h"

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

class Square {
public:
    Square(int x, int y){
       this->x = x;
       this->y = y;
    };
    Square(const Square& o){
       this->x = o.x;
       this->y = o.y;
    };
    void setX(int x){
       this->x = x;
    }
    void setY(int y){
       this->y = y;
    }
    virtual ~Square(){
      
    };
    friend ostream &operator<<(ostream &stream, Square o){
       stream<<"("<<o.x<<", "<<o.y<<")";
       return stream;
    };
private:
    int x;
    int y;         
};

#endif /* SQUARE_H */...
$70.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats