Professional Documents
Culture Documents
Learning Objectives
The designing of the Memory Game project enable the students to: Create a simple C++ game application Generate random values and explore the use of random functions Handle arrays with pointers efficiently in the scope-based scenario Use the gotoxy function to locate the cursor point effectively Identify the levels of the project that map to the requirement standards
To implement the above tasks, we need to create the Game class with appropriate member variables and member functions as specified below. Member Variables Variable boardDefault[5][4] Description Specifies the matrix, whose elements are filled
with default @ symbol Specifies the matrix, whose elements are filled with respective values in each cells Specifies the matrix, whose elements are filled with values as per the user entry. Counts the number of attempts taken by the user to complete the game. Used for comparing the values of two consecutive entries in the matrix. Used to retain the cell number of two consecutive cells entered by the user.
Member Functions Functions CreateBoard CurrentMatrix Description Used to display the game board with the default @ symbol Used to store and display the values in the game board when the user had started playing FillBoard ChangeCell Identify Location the game. Used to fill a matrix with the values, that should be made visible when a cell is turned out. Used to fill the turned out cell with the value generated by FillBoard function. Used to find the location on the screen to place the turned out cell value. It takes the cell ReplaceWithDefault number as its parameter. Used to replace the turned out cell value with the default @ symbol if two consecutive turned out cells are not identical. It takes the cell number as its parameter.
Sets the seed to generate the psuedo -random numbers. Properties of the private members of the class to read their values. Their return type is int.
The cell number is of type char[2] as it is a combination of alphabets and numbers ( for example A0, E3, etc). But the matrix indices are of type integers. Hence, we need to convert each character in the cell number to its corresponding integer type. The code listing for identifying the first row elements is specified below: (cellno variable contains the cell number entered by the user. Ex: If the user enters, say A0) switch(cellno[0]) { case 'A': case 'a': cell_1=0; break; case 'B': case 'b': cell_1=1; break; case 'C': case 'c': cell_1=2; break; case 'D': case 'd': cell_1=3; break; case 'E': case 'e': cell_1=4; break; default : { gotoxy(1,22+cnt); cout<<"\nEnter valid cell no.."; } } The above code checks whether cellno[0] falls in the specified range. In the same manner, the cellno[1] is also converted to integer. Then use these integers as the matrix indices to identify the cell to be opened. We have the boardSet[5][4] matrix containing the original values that needs to the displayed in the cell. Use the gototxy function to display the cell value in the respective position.
To enforce this, a variable named counter is maintained. It is incremented whenever it encounters an output statement (including error messages). If this variable reaches a particular value the screen is cleared and the matrix is redrawn using the CurrentMatrix function. This function uses boardCurrent[5][4] matrix to store the current status of the game board.
{ int k=0; cout<<"\n\n\n\n"; cout<<"\t\t"; // Printing the column headings for(int j=0;j<4;j++) { cout<<"\t"<<j; } cout<<endl<<endl; cout<<"\t\t\t"; // Underlining the column headings for( j=0;j<25;j++) { cout<<"*"; } cout<<endl<<endl; cout<<"\t"; // Filling the board with the default characters char row='A'; for(int i=0;i<5;i++) { cout<<"\t"<<row; for(j=0;j<4;j++) { boardDefault[i][j] = '@'; boardCurrent[i][j] =boardDefault[i][j] ; cout<<"\t"<< boardDefault[i][j]; } cout<<endl<<endl; cout<<"\t"; row++; } } void FillBoard() { char a[10]; int randno[10]; int rno; RandomGeneration *rg; rg=new RandomGeneration();
rno=rg->DrawRandomNumber(); //cout<<rno; for(int k=0;k<10;k++) { randno[k]=rno * k; a[k]=randno[k]; if(a[k]=='\0') a[k]='%'; //cout<<a[k]<<endl; } // Filling the values for the board. // This symbol in the value is opened when the user // enters the respective cell number. k=0; for(int i=0;i<5;i++) { for(int j=0;j<4;j++) { boardSet[i][j] = a[k]; //cout<<boardSet[i][j]<<"\t"; if(k==9) k=0; else k++; } //cout<<endl; } } void ChangeCell() { int bb,cnt=0; int counter =0; int cell_1=0,cell_2=0; char cellno[2]; char choice; //cout<<endl;
i=0; while(true) { i++; //gotoxy(1,22); int flag=0; for(int m=0;m<5;m++) { for(int n=0;n<4;n++) { if( boardCurrent[m][n] == '@') { flag=1; continue; } } } if (flag==0) { system("cls"); gotoxy(24,9); cout<<" CONGRATULATIONS!!!!!" <<endl<<endl; gotoxy(26,10); cout<<"You have completed in "<<tries <<" Tries."; gotoxy(28,13); cout<<endl<<" PLAY AGAIN (Y/N) "; goto bb; } else { cc: if(counter >2) { system("cls"); CurrentMatrix(); counter =0; cnt=0; goto dd; } cout<<"Enter the cell number to be opened: "; cin>>cellno; counter++; if(strlen(cellno)>2) { cout<<"\n Enter valid cell no.."; counter++; goto cc; } else { tries++; if (i==3) { if(cmp1 != cmp2) {
dd:
}//if else i=1; }//if if(cellno[0] == '-' && cellno[1] == '1') { system("cls"); gotoxy(28,13); cout<<endl<<" PLAY AGAIN (Y/N) "; goto bb; break; } else {//cout<<cellno<<endl; // CONVERTING 'A','B','C','D','E' TO 0,1,2,3,4 switch(cellno[0]) { case 'A': case 'a': cell_1=0; break; case 'B': case 'b': cell_1=1; break; case 'C': case 'c': cell_1=2; break; case 'D': case 'd': cell_1=3; break; case 'E': case 'e': cell_1=4; break; default : { gotoxy(1,22+cnt); cout<<"\nEnter valid cell no.."; //gotoxy(1,22+cnt); counter++; goto cc;// break; } }
//cout<<cell_1<<endl; //CONVERTING '0','1','2','3' TO 0,1,2,3 switch(cellno[1]) { case '0': cell_2=0; break; case '1': cell_2=1; break; case '2': cell_2=2; break; case '3': cell_2=3; break; default : { gotoxy(1,22+cnt); cout<<"\nEnter valid cell no.."; counter++; goto cc; } } if(boardCurrent[cell_1][cell_2] != '@') { gotoxy(1,22+cnt); cout<<" Cell is already open... \n"; goto dd; } //cout<<endl; else { IdentifyLocation(cell_1,cell_2,cnt); if (i==1) { cmp1 = boardSet[cell_1][cell_2]; t1=cell_1; t2=cell_2; } if (i==2) { cmp2 = boardSet[cell_1][cell_2]; t3=cell_1; t4=cell_2; } cout<<boardSet[cell_1][cell_2]; // storing the current instance of the matrix boardCurrent[cell_1][cell_2] = boardSet[cell_1][cell_2]; gotoxy(1,22+cnt); // to go to the next line cnt++; }//else for re-entry of opened cell }//else }//else for strlen(cellno) }//else }//while
bb: cin>> choice; system("cls"); } while(choice =='Y' || choice=='y'); } void CurrentMatrix() { int i,j; cout<<"\n\n\n\n"; cout<<"\t\t"; for(j=0;j<4;j++) { cout<<"\t"<<j; } cout<<endl<<endl; cout<<"\t\t\t"; for( j=0;j<25;j++) { cout<<"*"; } cout<<endl<<endl; cout<<"\t"; char row='A'; for(i=0;i<5;i++) { cout<<"\t"<<row; for(j=0;j<4;j++) { if(boardCurrent[i][j] !='\0') cout<<"\t"<<boardCurrent[i][j]; else { boardCurrent[i][j] = '@'; cout<<"\t"<<boardCurrent[i][j]; } }// for j cout<<"\n"; row++; cout<<"\n\t"; }// for i } void IdentifyLocation(int cell_1,int cell_2,int cnt) { if(cell_1 == 0) // 'A' { switch(cell_2) { case 0: gotoxy(25,9); break;
case 1: gotoxy(33,9);break; case 2: gotoxy(41,9); break; case 3: gotoxy(49,9);break; default : { gotoxy(1,22+cnt); cout<<"\nEnter valid cell no"; break; } } }//if k=0 ,'A' if(cell_1 == 1) // 'B' { switch(cell_2) { case 0: gotoxy(25,11); break; case 1: gotoxy(33,11);break; case 2: gotoxy(41,11); break; case 3: gotoxy(49,11);break; } }////if k=1 ,'B' if(cell_1 == 2) // 'C' { switch(cell_2) { case 0: gotoxy(25,13); break; case 1: gotoxy(33,13);break; case 2: gotoxy(41,13); break; case 3: gotoxy(49,13);break; } }////if k=2 ,'C' if(cell_1 == 3) // 'D' { switch(cell_2) { case 0: gotoxy(25,15); break; case 1: gotoxy(33,15);break; case 2: gotoxy(41,15); break; case 3: gotoxy(49,15);break; } }//if k=3 ,'D' if(cell_1 == 4) // 'E' { switch(cell_2) { case 0: gotoxy(25,17); break; case 1: gotoxy(33,17);break; case 2: gotoxy(41,17); break; case 3: gotoxy(49,17);break; } // goto l1; }//if k=4 ,'E' }
void ReplaceWithDefault(int val1,int val2) { switch(val1) { case 0:{// 'A' switch(val2) { case 0: gotoxy(25,9); cout<<'@';break; case 1: gotoxy(33,9); cout<<'@';break; case 2: gotoxy(41,9); cout<<'@';break; case 3: gotoxy(49,9); cout<<'@';break; } break; } case 1:{// 'B' switch(val2) { case 0: gotoxy(25,11);cout<<'@'; break; case 1: gotoxy(33,11);cout<<'@';break; case 2: gotoxy(41,11);cout<<'@'; break; case 3: gotoxy(49,11);cout<<'@';break; } break; } case 2:{// 'C' switch(val2) { case 0: gotoxy(25,13);cout<<'@'; break; case 1: gotoxy(33,13);cout<<'@';break; case 2: gotoxy(41,13);cout<<'@'; break; case 3: gotoxy(49,13);cout<<'@';break; } break; } case 3:{// 'D' switch(val2) { case 0: gotoxy(25,15); cout<<'@'; break; case 1: gotoxy(33,15); cout<<'@'; break; case 2: gotoxy(41,15); cout<<'@'; break; case 3: gotoxy(49,15); cout<<'@'; break; } break; } case 4: {// 'E' switch(val2) { case 0: gotoxy(25,17); cout<<'@';break; case 1: gotoxy(33,17); cout<<'@';break; case 2: gotoxy(41,17); cout<<'@';break; case 3: gotoxy(49,17); cout<<'@';break; } break; } }//outer switch
}// ReplaceWithDefault(); }; void main() { Game g; g.FillBoard(); // board with values g.ChangeCell(); } /*RandomGeneration.h*/ // Generating random number #include "stdafx.h" #include <time.h> #include <iostream> #include<conio.h> /*#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif*/ using namespace std; class RandomGeneration { int Low,High; public: RandomGeneration(); void SetTimerSeed(); int DrawRandomNumber(); int GetHigh(); int GetLow(); }; RandomGeneration :: RandomGeneration() { Low = 1; High = 100; }
// Set the seed to generate pseudo-random numbers. Use the current time & date as seed. void RandomGeneration::SetTimerSeed() {
time_t SeedTime; struct tm SeedDate; SeedTime = time(0); SeedDate = *localtime(&SeedTime); int FinalSeed = SeedTime + SeedDate.tm_mday + (SeedDate.tm_mon+1) + (SeedDate.tm_year+1900); srand((unsigned int) FinalSeed); } // Return the Random number // Interval: Self explanatory - Get the difference between the high and low values // RandomOffset = using the rand() function to generate random numbers from 0 to interval -1 // RandomNumber = Add the RandomOffset to the lowest number. int RandomGeneration::DrawRandomNumber() { int Interval = GetHigh() - GetLow() + 1; int RandomOffset = rand() % Interval; int RandomNumber = GetLow() + RandomOffset; //cout<<RandomNumber; return RandomNumber; } // Standard Inpectors - Return High & Low values int RandomGeneration::GetHigh() { return High; } int RandomGeneration::GetLow() { return Low; }
Exercise
1. Create the memory game with 55 matrix.