Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,310 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 865 people online right now. Registration is fast and FREE... Join Now!




Help with C++ Memory Game

 
Reply to this topicStart new topic

Help with C++ Memory Game, Basic structure done, need help

jdc08002
1 Dec, 2008 - 05:45 PM
Post #1

New D.I.C Head
*

Joined: 1 Dec, 2008
Posts: 2

I'm new to c++ and we have to make a memory matching game. We are supposed to use a one dimensional array but have it print out so it looks like a 4x4 game board. So far i have initialized the array and shuffled it. I also have the user input two choices which can correspond to a point in the array. I can display a board but its ugly. I'm stuck right now i think i need to have a boolean function to use with another array or something, any help as to where to go from here would be greatly appreciated.
Thanks.


CODE

/ ********************************************************************************
*****
Homework Assignment 9: Memory Matching Game
CSE 1100C: Introduction to Computing
Fall 2008
Objective: Develop modular programming skills using functions and develop competence
in using arrays, random numbers.
********************************************************************************
*****/

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;


void shuffle(int [], int);
void displayBoard(const int [], int [], int, int, int);
void clearScreen();
bool checkGameStatus(const int [], int);

int main()
{
        int size = 16;
        int board[16]={1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
        int showBoard[16];
        int guesses=0;
        shuffle(board,size); //Shuffle board[]

        int n =0;
        for(int i=0;i<16;i++){
                cout<<board[i]<<" ";
                n++;
                if(n==4||n==8||n==12){
                        cout<<endl;
                }
        };
        
        int continueGame, i1, i2, ii1, ii2, choice1, choice2;
        do{
                
              

                cout<<endl<<"Please input your coordinates x and y: "<<endl;
                cout<<"x coordinate: ";
                cin>>i1;
                cout<<"y coordinate: ";
                cin>>i2;

                if(i1==1&&i2==1){choice1 = 0;}
                if(i1==1&&i2==2){choice1 = 1;}
                if(i1==1&&i2==3){choice1 = 2;}
                if(i1==1&&i2==4){choice1 = 3;}
                if(i1==2&&i2==1){choice1 = 4;}
                if(i1==2&&i2==2){choice1 = 5;}
                if(i1==2&&i2==3){choice1 = 6;}
                if(i1==2&&i2==4){choice1 = 7;}
                if(i1==3&&i2==1){choice1 = 8;}
                if(i1==3&&i2==2){choice1 = 9;}
                if(i1==3&&i2==3){choice1 = 10;}
                if(i1==3&&i2==4){choice1 = 11;}
                if(i1==4&&i2==1){choice1 = 12;}
                if(i1==4&&i2==2){choice1 = 13;}
                if(i1==4&&i2==3){choice1 = 14;}
                if(i1==4&&i2==4){choice1 = 15;}

                cout<<"Please input your second choice of coordinates x and y: "<<endl;
                cout<<"x coordinate: ";
                cin>>ii1;
                cout<<"y coordinate: ";
                cin>>ii2;

                if(ii1==1&&ii2==1){choice2 = 0;}
                if(ii1==1&&ii2==2){choice2 = 1;}
                if(ii1==1&&ii2==3){choice2 = 2;}
                if(ii1==1&&ii2==4){choice2 = 3;}
                if(ii1==2&&ii2==1){choice2 = 4;}
                if(ii1==2&&ii2==2){choice2 = 5;}
                if(ii1==2&&ii2==3){choice2 = 6;}
                if(ii1==2&&ii2==4){choice2 = 7;}
                if(ii1==3&&ii2==1){choice2 = 8;}
                if(ii1==3&&ii2==2){choice2 = 9;}
                if(ii1==3&&ii2==3){choice2 = 10;}
                if(ii1==3&&ii2==4){choice2 = 11;}
                if(ii1==4&&ii2==1){choice2 = 12;}
                if(ii1==4&&ii2==2){choice2 = 13;}
                if(ii1==4&&ii2==3){choice2 = 14;}
                if(ii1==4&&ii2==4){choice2 = 15;}

                guesses++;

                displayBoard(board, showBoard, choice1, choice2, size);
                
                 cout<<endl<<"would you like to continue '1' for yes '0' for no"<<endl;
                cin>> continueGame;
        
                if(continueGame!=0&&continueGame!=1){
                        cout<<"please input '1' or '0'"<<endl;
                        cin>> continueGame;}


        }while(continueGame!=0);

        cout<<"Number of guesses taken: "<<guesses<<endl;
}



void shuffle(int board[16], int size)
{
   int temp,x;
   srand(time(0));
   for (int i=0;i<100;i++)
   {
      
          x = rand()%16;
          temp = board[x];
          board[x]=board[0];
          board[0]=temp;
   }
        
        
}
void displayBoard(const int board[], int showBoard[], int choice1, int choice2, int size)
{
   int m =0;
        cout<<endl<<endl;
        cout << "     1 2 3 4 \n";
        cout << "     _______\n";
        cout<<endl<<"1  | ";
        for(int i=0;i<16;i++){
            showBoard[i]=0;

                showBoard[choice1]=board[choice1];
                showBoard[choice2]=board[choice2];
            
                cout<<showBoard[i]<<" ";
                m++;
                if(m==4){
                    cout<<endl<<"2  | ";}
                if(m==8){
                    cout<<endl<<"3  | ";}
                if(m==12){
                    cout<<endl<<"4  | ";}
                        
                }
        cout<<endl<<endl;
}  


void clearScreen()
{
}

bool checkGameStatus(const int arr[], int n)
{
        return 0;
}


User is offlineProfile CardPM
+Quote Post


KYA
RE: Help With C++ Memory Game
1 Dec, 2008 - 06:43 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
What status is that function checking? Putting clarification comments or design direction comments would be helpful. I can think of 3 or 4 things a checkGameStatus() function might be good for.

For a win/loss?
To verify the board contents? To decide if a card should be face up or face down?
User is offlineProfile CardPM
+Quote Post

jdc08002
RE: Help With C++ Memory Game
1 Dec, 2008 - 07:37 PM
Post #3

New D.I.C Head
*

Joined: 1 Dec, 2008
Posts: 2

I think the check game status function is to tell you which cards are face up and if they are all face up you win the game.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Help With C++ Memory Game
1 Dec, 2008 - 07:53 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
Best way: Make a card class, add a bool attribute to determine whether it is face up or not, and completely retool the program.

Faster way: There really isn't one because there is no quick function to see if a card is up or not. You pass the choice from board to shownBoard. I suppose you could modify shownBoard slightly.
User is offlineProfile CardPM
+Quote Post

youarecaught
RE: Help With C++ Memory Game
3 Dec, 2008 - 01:12 PM
Post #5

New D.I.C Head
*

Joined: 3 Dec, 2008
Posts: 1

I didn't know that you can rely on online forums to help you with our cse homework.. My students usually come to me for help after we finish lab. I wonder how Ruby is going to feel about this.
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Help With C++ Memory Game
3 Dec, 2008 - 01:27 PM
Post #6

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
Fortunately, this person does not appear to have benefited much from the interaction here wink2.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:23PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month