#include "stdafx.h" #include <iostream> #include <conio.h> #include <iostream> #include <fstream> #include <sstream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #define NOMINMAX #include <windows.h> #include <ctype.h> #include <cctype> using namespace std; class VocE { public: bool TF; char Beginner[100],Intermediate[100],Complex[100], store[100], word[20],fakeword[20], accept[20], hintWord; string Names,Reading; int YN, level, iBe,iIn,iCom, line[5], numRan,ranPoi,RanPoi[20], hintNum, scores, Scores[100],poiNames[100],nums,poi,step; double timePlayed; void settimePlayed_scores(double timePlayed,int scores) { this->timePlayed=timePlayed; this->scores=scores; } void InitializeFile_FetchData() { iBe=0; iIn=0; iCom=0; step = 0; nums = 0; poi = 0; ifstream checkBe("Beginner.txt"); if (!checkBe) { ofstream createBe("Beginner.txt"); createBe<<"Pencil\nRule\nBook\nBag\nUniform\n"; createBe.close(); } ifstream readBe("Beginner.txt"); while (readBe) { readBe.get(Beginner[iBe]); iBe++; } readBe.close(); checkBe.close(); ifstream checkIn("Intermediate.txt"); if (!checkIn) { ofstream createIn("Intermediate.txt"); createIn<<"PersonalComputer\nOpticalMouse\nChickletKeyboard\nDolbySpeaker\nMainBoard\n"; createIn.close(); } ifstream readIn("Intermediate.txt"); while (readIn) { readIn.get(Intermediate[iIn]); iIn++; } readIn.close(); checkIn.close(); ifstream checkCom("Complex.txt"); if (!checkCom) { ofstream createCom("Complex.txt"); createCom<<"RandomAccessMemory\nReadOnlyMemory\nCenterProcessorUnit\nLiquidCrystalDisplay\nUniversalSystemBus\n"; createCom.close(); } ifstream readCom("Complex.txt"); while (readCom) { readCom.get(Complex[iCom]); iCom++; } readCom.close(); checkCom.close(); ifstream checkHighScores("HighScores.txt");// HighScores if (!checkHighScores) { ofstream createHighScores("HighScores.txt"); createHighScores<<"SnoOpy 99 SunghaJung 88 PaddySun 77"; createHighScores.close(); } checkHighScores.close(); ifstream readHighScores("HighScores.txt"); string checkReading = "chk"; while (readHighScores) { getline(readHighScores,Reading,' '); if (checkReading == Reading) break; checkReading = Reading; /*cout<<Reading<<endl;*/ Sleep(100); if (step % 2 == 0) { Names += Reading + ' '; step++; } else { /*cout<<"Reading (num): "<<atoi(Reading.c_str())<<endl;*/ Scores[nums++] = atoi(Reading.c_str()); step++; } } readHighScores.close(); /*cout<<"Step: "<<--step<<endl; cout<<"Length Name: "<<Names.length();*/ update_poiNames(); /*cout<<endl<<"Final Names string: "<<Names;*/ } void Run(int level) { int i=0; if (level == 1)//////////////////////////////////// { for (i=0;i<iBe;i++) store[i]=Beginner[i]; }////////////////////////////////////////////////// if (level == 2) { for (i=0;i<iIn;i++) store[i]=Intermediate[i]; }////////////////////////////////////////////////// if (level == 3) { for (i=0;i<iCom;i++) store[i]=Complex[i]; }////////////////////////////////////////////////// int x=0; for (int I=0;I<i-1;I++) { if (store[I] == '\n') { line[x]=I; x++; } } int rd=(rand()%x)+1; if (rd == 1) { int j=0; for (j=0;j<line[0];j++) word[j]=store[j]; word[j]='\0'; } else { int y=0; int w=line[rd-2]; int q=line[rd-1]; w+=1; for (int s=w;s<q;s++) { word[y]=store[s]; y++; } word[y]='\0'; } } void Random(int level) { hintNum=0; numRan=0; int len=strlen(word); if (level == 1) numRan=len/3; else numRan=len/2; int I = 0; int flag = 0; int chk = 0; int rnd; while (chk < numRan) { rnd=rand()%len; if (I == 0) { RanPoi[I++] = rnd; chk++; } else { for (int j = 0; j < I; j++) { if (RanPoi[j] == rnd) { flag = 1; break; } } if (flag!=1) { RanPoi[I++] = rnd; chk++; } } flag = 0; } hintNum = numRan-1; for (int i=0;i<len;i++) { fakeword[i]=word[i]; } fakeword[len]='\0'; for (int x=0;x<numRan;x++) fakeword[RanPoi[x]]='*'; } void GotoXY(short x,short y) { HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X=x-1; pos.Y=y-1; SetConsoleCursorPosition(hCon, pos); } void SetColor(WORD color) { HANDLE hConsoleOutput; hConsoleOutput=GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info; GetConsoleScreenBufferInfo(hConsoleOutput,&screen_buffer_info); WORD wAttributes=screen_buffer_info.wAttributes; color&=0x000f; wAttributes&=0xfff0; wAttributes|=color; SetConsoleTextAttribute(hConsoleOutput,wAttributes); } void Hint(char hintWord) { int is=0; int len=strlen(word); for(int i=0;i<numRan;i++) { if (word[RanPoi[i]] == hintWord || tolower(word[RanPoi[i]]) == hintWord) { is=1; system("cls"); hintNum -= 1; if (hintNum<0) hintNum=0; Info(timePlayed,scores); cout<<"\"";SetColor(15);cout<<accept;SetColor(7);cout<<"\" is available !\n You lost 1 Hint !\n"; fakeword[RanPoi[i]]=word[RanPoi[i]]; } } if (is == 0) { system("cls"); hintNum -= 1; Info(timePlayed,scores); cout<<"Oh,\""<<hintWord<<"\" isn't available !\n You lost 1 Hint !\n"; } Sleep(500); cout<<"\nEnter a character for hint or whole word:\t";SetColor(12);cout<<fakeword<<endl;SetColor(7); cin>>accept; Sleep(500); if (strlen(accept) == 1 && hintNum>0) { hintWord = accept[0]; Hint(hintWord); } } void Info(double timePlayed,int scores) { GotoXY(60,1); cout<<"\tTime play: "<<timePlayed<<"s"; GotoXY(60,2); cout<<"\tScores: "<<scores; GotoXY(60,3); cout<<"\tHint left: ";SetColor(15);cout<<hintNum<<endl;SetColor(7); } void update_poiNames() { poi = 0; for (int I=0;I<Names.length();I++) //Luu vi tri name tuong ung voi score { if (Names[I] == ' ') poiNames[poi++] = I; } } void update_Names_to_out() { int sbuffer_length = 0; for (int i=0;i<=nums;i++) { std::stringstream ssbuffer; // Tao stringstream ssbuffer de truyen int Score[i] vao => string => gan cho string sbuffer string sbuffer; // ssbuffer<<Scores[i]<<' '; // sbuffer = ssbuffer.str(); // if (i == 0) sbuffer_length = 0; // Luc dau chen dunng vao vi tri do luon (chua bi doi len) Names.insert(poiNames[i]+1+sbuffer_length,sbuffer); sbuffer_length += sbuffer.length(); // Cong don length cua sbuffer_length ta duoc: poiNames[i] + sbuffer_length (nhay coc + cong don) /*cout<<endl<<"Updating Names string: "<<Names;*/ Sleep(500); } } void update_HighScores() { Names.erase(Names.length()-1,1); ofstream updateHighScores("HighScores.txt"); updateHighScores<<Names; updateHighScores.close(); } void newHighScore(int score,string name) { name += ' '; int poiLower; for (poiLower = 0;poiLower < nums;poiLower++) //Xac dinh vi tri be hon dau tien { if (score >= Scores[poiLower]) break; } /*cout<<"nums: "<<nums;*/ //Sleep(1000); for (int i = nums;i >= poiLower;i--) //Dich chuyen vi tri cac phan tu so len 1 ke tu vi tri be hon do Scores[i] = Scores[i-1]; Scores[poiLower] = score; //Lap score vao o trong do (vi tri be hon do) int moveNames = poiNames[poiLower-1]+1; if (poiLower == 0) moveNames = 0; if (poiLower == nums) moveNames = Names.length(); Names.insert(moveNames,name); update_poiNames(); update_Names_to_out(); update_HighScores(); cout<<"Your results was submitted !!"<<endl; Sleep(2000); /*cout<<endl<<"Updated Names string: "<<Names<<endl; cout<<"Updated Scores array: "; for (int i = 0;i<=nums;i++) { cout<<Scores[i]<<" "; Sleep(1000); }*/ } int showHighScores() { system("cls"); char SM; int YN; string name; step = 0; int rank = 1; ifstream readHighScores("HighScores.txt"); cout<<endl<<"Rank\tName\t\t\tScore"<<endl; string checkReading = "chk"; while (readHighScores)// && int(readHighScores.tellg()) < Names.length()-1) { getline(readHighScores,Reading,' '); if (checkReading == Reading) break; checkReading = Reading; if (step % 2 == 0) { cout<<rank++<<"\t"; if (Reading.length() <= 7) cout<<Reading<<"\t\t\t"; else cout<<Reading<<"\t\t"; } else cout<<Reading<<endl; step++; } readHighScores.close(); cout<<endl<<"Do you want to submit you result ? (Y/N)"; cin>>SM; if (tolower(SM) == 'y') cout<<endl<<"Enter your nickname: "; cin>>name; for (int i=0;i<name.length();i++) { if (name[i] == ' ') name[i] = '-'; } newHighScore(scores,name); system("cls"); cout<<endl<<endl<<"Enter 1 to replay or 2 to exit...\n"; cin>>YN; return YN; } void enterLevel() { int lv; cout<<"Enter 1/2/3 to choose Level Beginer/Intermediate/Complex !"<<endl; cin>>lv; if (!(lv>=1 && lv<=3) || cin.fail()) { cin.clear(); cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); system("cls"); cout<<"Please enter 1/2/3 only !"; Sleep(1000); system("cls"); enterLevel(); } else level = lv; } }; int _tmain(int argc, _TCHAR* argv[]) { time_t t; srand((unsigned) time(&t)); clock_t start,finish; int scores=0; //int level; VocE v; v.YN = 1; if (v.TF != TRUE) { v.InitializeFile_FetchData(); v.TF = TRUE; } while (v.YN == 1 || v.YN == 2) { double timePlayed=0; system("cls"); v.enterLevel(); system("cls"); /*while (!(level>=1 && level<=3)) { cout<<"Enter 1/2/3 to choose Level Beginer/Intermediate/Complex !"<<endl; cin>>level; system("cls"); cout<<"level: "<<level; Sleep(5000); if (!(level >=1 && level<=3)) { cout<<"Please enter 1/2/3 only !"; Sleep(1000); system("cls"); } else v.level=level; }*/ //system("cls"); //if (v.level >= 1 && v.level <= 3) //{ v.Run(v.level); v.Random(v.level); start=clock(); v.GotoXY(60,3); cout<<"\tHint left: ";v.SetColor(15);cout<<v.hintNum<<endl;v.SetColor(7); cout<<"\nEnter a character for hint or whole word:\t";v.SetColor(12); cout<<v.fakeword<<endl;v.SetColor(7); cin>>v.accept; Sleep(500); if (strlen(v.accept) == 1) { finish=clock(); int elapsed=int(finish-start)/1000; timePlayed+=elapsed; v.settimePlayed_scores(timePlayed,scores); if (v.hintNum > 0) { v.hintWord = v.accept[0]; v.Hint(v.hintWord); } } if (int TF=_strcmpi(v.accept,v.word) == 0 && strlen(v.accept) != 1) { cout<<"\nTrue !!!\n"; if (v.level == 1) { cout<<"\tYou earned 1 score !"<<endl; scores+=1; } if (v.level == 2) { cout<<"\tYou earned 2 scores !"<<endl; scores+=2; } if (v.level == 3) { cout<<"\tYou earned 3 scores !"<<endl; scores+=3; } Sleep(1000); } else { cout<<"Oh, False !!!\nPluzze word is: "<<v.word; scores=scores; Sleep(1000); } finish=clock(); int elapsed=int(finish-start)/1000; timePlayed+=elapsed; v.settimePlayed_scores(timePlayed,scores); system("cls"); v.Info(timePlayed,scores); cout<<"\n See you again !\n"; cout<<"Enter 1 to replay, 2 to view HighScores or 3 to exit...\n"; cin>>v.YN; if (v.YN == 2) v.YN = v.showHighScores(); if (v.YN !=1 && v.YN !=2) break; /*} else break;*/ } _getch(); return 0; }