memory game
Please support our C++ advertiser: Programming Forums
![]() |
•
•
Posts: 11
Reputation:
Solved Threads: 0
Hi all i am working on a memory game and i am having problems inputing the data so it checks if a 3rd number is pulled up to ignore it
thanks in advance
thanks in advance
int card[6][6],total=0,value,test;
srand((unsigned)time(NULL));//randomize timer
for (int i=0;i<6;i++)
{
for (int j=0;j<6;j++)
{
value=rand()%18;//random
test=0;
card[i][j]=value;//sets the value
for (int x=0;x<6;x++)
{
for (int y=0;y<6;y++)//starts the check for doubles
{
if(value==card[x][y])
{
test++;
cout<<i<<endl;
}
if (test==3)
{
//need a code to stop for the 3rd one to be put in.
}
}
}
}
} So you want to populate the board with random numbers in the range of [0, 18) and disallow more than two of a kind? I'd say use a frequency table for the range since it's small, and repeat getting the random number until you find one that doesn't go beyond the limit:
The frequency table holds a count of all of the values in your range.
cplusplus Syntax (Toggle Plain Text)
#include <cstdlib> #include <iomanip> #include <iostream> int main() { using namespace std; int board[6][6]; int freq[18] = {0}; for ( int i = 0; i < 6; i++ ) { for ( int j = 0; j < 6; j++ ) { int r; do r = rand() % 18; while ( freq[r] >= 2 ); ++freq[r]; board[i][j] = r; } } for ( int i = 0; i < 6; i++ ) { for ( int j = 0; j < 6; j++ ) cout<< left << setw ( 3 ) << board[i][j]; cout<<'\n'; } }
I'm here to prove you wrong.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Beautyfull Memory Game (Geeks' Lounge)
- Spark Game (Highly Addictive) (Geeks' Lounge)
- Memory game with c++ console applicaton (C++)
- why wont my flash game work? (Graphics and Multimedia)
- why i can not load game on one partition but can on aother (Troubleshooting Dead Machines)
- Freezes and Odd things after memory upgrade (Windows NT / 2000 / XP / 2003)
- Big Game need progrmmers (C++)
- SHUTDOWNS, new video card (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: Need homework help
- Next Thread: Free safe DLL to play with
•
•
•
•
Views: 315 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode