Starting from:
$35

$29

Homework # 1 Your First C++ Program Solved


You will write a C++ program that will play the game of Peg Solitaire. Please see the Wikipedia page for the rules of the game at https://en.wikipedia.org/wiki/Peg_solitaire. The game of Peg Solitaire is played by one player on a two dimensional board (2D Vector) with rectangular cells. Each cell contains a peg (P) or is empty (.). The game starts with the following board for a 7x7 game

abcdefg
1    PPP
2    PPP
3 PPPPPPP
4 PPP.PPP
5 PPPPPPP
6    PPP
7    PPP


where ‘.’ represents the empty cells, ‘P’ represents the pegs. The player makes a legal move and eliminates one of the pegs. The move is done by the user who chooses the column (letter) and the row (number) of the cell first and then the direction of the movement. For the above board if we give command B4-R, the computer moves the peg at B4 towards right to eliminate the peg at C4, which results in the following board. Use L for left, R for right, U for up, and D for down.

abcdefg
1    PPP
2    PPP
3 PPPPPPP
4 P..PPPP
5 PPPPPPP
6    PPP
7    PPP

The game continues until there are no pegs to move legally. The number of pegs remaining is the final score (1 is the best score).

Your program will do the following

    1. When your program starts, you will ask the user the board type from 1 to 6. The types of the boards are taken from Wikipedia and are shown below. Our example board above is type 4;











    2. Ask the user if this a human player game or a computer game;
    3. You will display the initial board;

    4. If the game is a human game, you ask the user to make a move, get the user move from the keyboard and draw the new board. If the move is not legal, then ask for another move;

    5. If the game is a computer game, then the computer makes a random valid move and draws the new board. You should also print the move made;

    6. The game continues until the end the final score is printed.

Notes:

    • Do not use any functions from the standard C library (like printf), do not use C arrays. For random numbers and math functions you may use standard C functions.

    • Use C++11 strong enum types for representing cell state(empty, peg)
    • Use C++ standard classes such as string, vector, list, etc.
    • Your program should have only functions and no classes.
    • Do not forget to indent your code and provide meaningful comments.
    • Check the validity of the user input.

    • Test your programs very carefully at least with 10 different runs. For some runs, let the computer play and for others the user should play. There should be cases for illegal moves and your program should handle them appropriately.

    • You should submit your work to the Teams page.

More products