Question

C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...

C++ Project - Create a memory game in c++ using structs and pointers.

For this exercise, you will create a simple version of the Memory Game.

You will again be working with multiple functions and arrays. You will be using pointers for your arrays and you must use a struct to store the move and pass it to functions as needed. Program Design You may want to create two different 4x4 arrays. One to store the symbols the player is trying to match and the second one to be used to show the current state of the game. You could do it with a single array for the symbols, as an alternative design. For the symbols, you can use any eight different characters. Possible options are the digits 1-8, the letters A-H, or a collection of special characters such as: !,@,#,$,%,^,&,*. The only requirement is that there are eight of them. To initialize the symbol array, you will randomly place two copies of each symbol in your 4x4 array. For each turn, you display the current state of the board, showing an X or a SPACE at each location. Each location is an X at the start of the game, SPACES are used to indicate where a symbol pair was found. After showing the current state, you ask for a move. Then you display the state, but at the chosen location, you show the real symbol instead of an X. Then you ask for a second move and display the board with those two locations showing the real symbol instead of X’s. If the two symbols showing are a match, you replace the X’s with SPACES. If they are not a match, you do nothing. Once all X’s have been replaced by SPACES, the game is done.


PROGRAM REQUIREMENTS


You need to have a function to initialize your memory game and return it to main. This can be done with dynamic allocation of the array (as a 1x16 element array instead of a 4x4 one). By using a 1x16 array, it makes it simpler to determine the location to place items.

You need a function to get a move from the player and return it to main. This should ask for a row and column (each between 0 and 3). Validate that each one is in range. Return a struct containing the move as two integers. You can return this as a pointer or by copy, your choice.

You will need a function to display the board. You will pass one or two moves in to this function depending on if it is the first time or the second time. If you want, you can have two separate functions so that you have one with one move and one with two moves. (Remember, you can have two functions with the same name as long as they have different parameter lists.)

Your program should allow the player to repeat playing if they want. You can keep track of how many moves it took if you would like.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to implement a memory game

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

// struct definition

struct Location

{

               int row,col;

};

// function declaration

int* generateBoard();

void printBoard(int *board,bool *userBoard, Location *l1);

void printBoard(int *board, bool *userBoard,Location *l1,Location *l2);

Location* input();

void updateMove(int *board, bool *userBoard, Location *l1, Location *l2,int &space);

int main() {

               srand(time(NULL));

               bool userBoard[16];

               for(int i=0;i<16;i++)

                              userBoard[i] =false;

               int *board = generateBoard();

               int countMoves = 0;

               int countSpace = 0;

               while(countSpace < 16)

               {

                              cout<<" Enter the first location "<<endl;

                              Location *l1 = input();

                              printBoard(board,userBoard,l1);

                              cout<<" Enter the second location "<<endl;

                              Location *l2 = input();

                              printBoard(board,userBoard,l1,l2);

                              updateMove(board,userBoard,l1,l2,countSpace);

                              countMoves++;

               }

               cout<<" Total number of moves required : "<<countMoves<<endl;

               return 0;

}

// function to generate the board

int* generateBoard()

{

               int *arr = new int[16];

               //printBoard(arr,4,4);

               for(int i=0;i<8;i++)

               {

                              for(int j=0;j<2;j++)

                              {

                                             int ind = rand()%16;

                                             while(arr[ind] != 0)

                                             {

                                                            ind = rand()%16;

                                             }

                                             arr[ind] = (i+1);

                              }

               }

               return arr;

}

// function to print the board

void printBoard(int *board,bool *userBoard, Location *l1)

{

               cout<<" Board : "<<endl;

               for(int i=0;i<4;i++)

               {

                              for(int j=0;j<4;j++)

                              {

                                             if(userBoard[4*i+j])

                                                            cout<<" ";

                                             else if(i == l1->row && j == l1->col)

                                                            cout<<board[4*i+j]<<" ";

                                             else

                                                            cout<<"X ";

                              }

                              cout<<endl;

               }

}

// function to print the board

void printBoard(int *board, bool *userBoard,Location *l1,Location *l2)

{

               cout<<" Board : "<<endl;

               for(int i=0;i<4;i++)

               {

                              for(int j=0;j<4;j++)

                              {

                                             if((i == l1->row && j == l1->col) || (i == l2->row && j == l2->col))

                                                            cout<<board[4*i+j]<<" ";

                                             else if(userBoard[4*i+j])

                                                            cout<<" ";

                                             else

                                                            cout<<"X ";

                              }

                              cout<<endl;

               }

}

// function to take input of location from the user

Location* input()

{

               Location *l = new Location;

               cout<<" Enter the row(0-3) : ";

               cin>>l->row;

               while(l->row < 0 || l->row > 3)

               {

                              cout<<" Invalid row value. Enter the row(0-3) : ";

                              cin>>l->row;

               }

               cout<<" Enter the column(0-3) : ";

               cin>>l->col;

               while(l->col < 0 || l->col > 3)

               {

                              cout<<" Invalid column value. Enter the column(0-3) : ";

                              cin>>l->col;

               }

               return l;

}

// function to update userBoard according to the inputs of user

void updateMove(int *board, bool *userBoard, Location *l1, Location *l2,int &space)

{

               if(board[4*l1->row+l1->col] == board[4*l2->row+l2->col])

               {

                              userBoard[4*l1->row+l1->col] = true;

                              userBoard[4*l2->row+l2->col] = true;

                              space = space + 2;

               }

}

//end of program

Output:

Enter the first location Enter the row(0-3) 1 Enter the column (0-3) 2 Enter the second location Enter the row(0-3) 0 Enter t

Enter the first location Enter the row(0-3) 1 Enter the column(0-3)3 Enter the second location Enter the row(0-3) 3 Enter the

Add a comment
Know the answer?
Add Answer to:
C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

  • C++ for this exercise, you will make a simple Dungeon Crawl game. For an enhanced version,...

    C++ for this exercise, you will make a simple Dungeon Crawl game. For an enhanced version, you can add monsters that randomly move around. Program Requirements While the program is an introduction to two-dimensional arrays, it is also a review of functions and input validation. check: Does the program compile and properly run? does all functions have prototypes? Are all functions commented? Is the program itself commented? Are constants used where appropriate? Are the required functions implemented? Is the dungeon...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • Hi, I need help with my assignment. •Create a memory game in C •Function oriented with...

    Hi, I need help with my assignment. •Create a memory game in C •Function oriented with several functions •Structured programming •All cards are randomly placed on the board with face down •The player pick one card from the board and the symbol is shown •The player picks another card, with the aim to get pair (same symbol twice), and the symbol is shown •If two equal card is found (pair) the player gets one point and the pair is removed...

  • Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...

    Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first....

    INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first. But rolling certain numbers will change the pace of the game. INCLUDE IN YOUR ASSIGNMENT: Annotation is a major part of any program. At the top of each of your C++ programs, you should have at least four lines of documentation: // Program name: tictactoe.cpp // Author: Twilight Sparkle // Date last updated: 5/26/2016 // Purpose: Play the game of Tic-Tac-Toe ASSIGNMENT: Sorry Game...

  • can someone solve this program using c++ (visual studio)? You have a grid of 4x4 cells...

    can someone solve this program using c++ (visual studio)? You have a grid of 4x4 cells which is filled by numbers from 1 to 8. Each number appears twice in the grid. The purpose of the Memory Game is to find the matching numbers. To start the game, Player 1 chooses two cells to uncover the numbers behind them. If the numbers match, player 1 gets to go on and uncover two more numbers. If the numbers don't match, player...

  • i need to to create a battleship game in c++ using 2d array and functions the...

    i need to to create a battleship game in c++ using 2d array and functions the pograms has to be 10*10 and have 2 enemy ship and 2 friendly ships each taking 3 spaces randomly placed first the program would display the user the full ocean and the issuer would need to input cordinates and much check that the corinates are correct if not ask again . when a the two enemies ships are drestroyed the game ends or if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT