18 Mart 2013 Pazartesi

Guessing game (Challange Program)

Click picture and see more large


---------------------------------------------------efe.h-----------------------------------------------------


#include "stdafx.h"
#include <iostream>
using namespace std;
void process(int min,int max,int tryy);
void lastscreen(int tryy,int tot,int min,int med,bool e);
void screen() {
int min,max,tryy;
cout << "Choose space by entering two numbers(firstly min and then max)" << endl;
cin >> min >> max;
cout << "Hmm your numbers are between " << min << " and " << max << "." << endl;
cout << "Okey,Lets make bet.What is your bet? How many try can I find your number?" << endl;
cin >> tryy;
process(min,max,tryy);
}
void process(int min,int max,int tryy) {
char ans;
int med=(min + max) / 2,tot=0,c;
cout << "Your number is between " << min << " and " << med << " ?\n";
cin >> ans;
bool result=false;


do {
     if(ans=='y') {
if((med-min) == 2) {
 lastscreen(tryy,tot,min,med,true);
 result=true;
 break;
}
tot++;
max=med;
med=(min + max) / 2;
c=med-1;
if(c==min) {   //c never equal min
c+=1;
}
 if((med-min)==1) {
 do {
 
cout << "Is correct answer " << c << " ?" << endl;
cin >> ans;
tot++;
if(ans=='y') {
lastscreen(tryy,tot,c,c,true);
 result=true;
 break;
}
c++;
 }while(result==false);
}
 if(result==false) {
cout << "Your number is between " << min << " and " << med << " ?\n";
cin >> ans;
 }
}else if(ans=='n') {
tot++;
min=med;
med=(min + max) / 2;

 c=min-1;
 if((med-min)==1) {
 do {
  c++;
cout << "Is correct answer " << c << " ?" << endl;
cin >> ans;
tot++;
if(ans=='y') {
lastscreen(tryy,tot,c,c,true);
 result=true;
 break;
}
 }while(result==false);
}
if(result==false) {
 cout << "Your number is between 1111111" << min << " and " << med << " ?\n";
cin >> ans;
  }
}

}while(result==false);

}
void lastscreen(int tryy,int tot,int min,int med,bool e) {
if(tryy<tot) {
cout << "Ahh,You are winner" << endl;
cout << "Answer is " << med << ".I solved it by trying " << tot << " times." << endl;    //answer is med or min it doesn't matter now.
cout << "By the way,You can visit efekanpulatli.blogspot.com" << endl;
}else if(tryy==tot) {
        cout << "What a coincidence,No one is winner!.We are equal.\n";
cout << "Answer is " << med << ".I solved it by trying " << tot << " times."  << endl;          //answer is med or min it doesn't matter now.
cout << "By the way,You can visit you efekanpulatli.blogspot.com" << endl;
}else if(tryy>tot) {
cout << "Ha ha ha get out now!I am the winner!\n";
cout << "Answer is " << med << ".I solved it by trying " << tot << " times."  << endl; //answer is med or min it doesn't matter now.
cout << "By the way,You can visit efekanpulatli.blogspot.com" << endl;
}
}








------------------------------------------------main---------------------------------------------------------





#include "stdafx.h"
#include <iostream>
#include "efe.h"


using namespace std;

int main() {
char anss;
do {
cout << "Hey,Do you wanna play guessing game? y/n" << endl;
cin >> anss;
if(anss == 'n') {
exit (1);
}
}while(anss != 'y') ;
char app='y';
do {
screen();
cout << "\n\nDo you wanna play again? y/n";
cin >> app;
if(app=='y') {
system("cls");
}else {
exit (1);
}
}while(app=='y');
    system("pause");
return 0;
}










Hiç yorum yok:

Yorum Gönder