NAVER

질문 c++고수님들 질문좀요 ㅠㅠ 되게 복잡한코드입니다..
궁금 조회수 435 작성일2014.07.24
명품c++ 6장 openchallenge인데요;;; 
up and down게임이라해서 0이랑 99사이의 임의의 난수발생해서 정답에 가까워지게하는 게임이예요
예를들어
답이 43이라 치면

답은 0과 99사이에 있습니다.
player1>>50;//50은 난수발생으로인한거
답은 0과 50사이에 있습니다.
player2>>33;
답은 33과 50사이에 있습니다.

이런식으로요 조건은 static맴버로 변수설정을하라는데 이상하게 안되는 부이 너무많아서 코드좀봐주세요.
도저히 모르겠네여ㅣ;;
#include<iostream>
#include<conio.h>
#include<ctime>
#include<cstdlib>
#include<string>
using namespace std;
class Person{
public:
string players[2];
Person(){players[0]="성민규"; players[1]="박선영";}
string getName(int i);
};
string Person::getName(int i){
return players[i];
}
class UpAndDownGame{
public:
static int max;
static int min;
static int makeAnswer;
UpAndDownGame();
int gamePlay();
};
UpAndDownGame::UpAndDownGame()
{ max=99; 
min=0;
srand((unsigned)time(NULL));
makeAnswer=rand()%(max-min+1)+min;
}
int UpAndDownGame::gamePlay(){
cout<<"답은"<<min<<"과 "<<max<<"사이에 있습니다."<<endl;
int n=rand()%(max-min+1)+min;
if(makeAnswer==n)
return 0;
else if(makeAnswer<n)
{max=n; return n;}
else
{min=n; return n;}
}
void paly(){
Person a;
UpAndDownGame b;
int n;
string x;
cout<<"Up & Down 게임을 시작합니다."<<endl;
while(true)
{ for(int i=0;i<2;i++)
{ getch();
n=b.gamePlay();
x=a.getName(i);
cout<<x<<">>"<<n<<endl;
if(n==0)
{
cout<<x<<"이(가) 이겼습니다."<<endl;
break;
}
if(n==0) break;
}
}
}

int main()
{
void play();
cout<<"프로그램종료"<<endl;
getch();

}
프로필 사진

답변자님,

정보를 공유해 주세요.

1 개 답변
1번째 답변
프로필 사진
바로이순간
은하신
C, C++ 3위, 프로그래밍 7위, 프로그래머 24위 분야에서 활동
본인 입력 포함 정보
#include<iostream>
#include<conio.h>
#include<ctime>
#include<cstdlib>
#include<string>
using namespace std;
class Person{
public:
  string players[2];
  Person(){
    players[0]="성민규"; 
    players[1]="박선영";
  }
  string getName(int i);
};
string Person::getName(int i){
  return players[i];
}
class UpAndDownGame{
public:
  int max;
  int min;
  int makeAnswer;
  UpAndDownGame();
  int gamePlay();
};
UpAndDownGame::UpAndDownGame() { 
  max=99; 
  min=0;
  srand((unsigned)time(NULL));
  makeAnswer=rand()%(max-min+1)+min;
}
int UpAndDownGame::gamePlay(){
  cout<<"답은"<<min<<"과 "<<max<<"사이에 있습니다."<<endl;
  int n=rand()%(max-min+1)+min;
  if(makeAnswer==n) {
    cout<<"답 "<<n<<endl;
    return -1;
  } else if(makeAnswer<n) {
    max=n; 
    return n;
  } else {
    min=n; 
    return n;
  }
}
void play(){
  Person a;
  UpAndDownGame b;
  int n;
  string x;
  cout<<"Up & Down 게임을 시작합니다."<<endl;
  while(true) { 
    for(int i=0;i<2;i++) { 
      getch();
      x=a.getName(i);
      n=b.gamePlay();
      cout<<x<<">>"<<n<<endl;
      if(n==-1) {
        cout<<x<<"이(가) 이겼습니다."<<endl;
        break;
      }
    }
    if(n==-1) break;
  }
}

int main() {
  play();
  cout<<"프로그램종료"<<endl;
  getch();
  return 0;
}

2014.07.24.

  • 채택

    질문자가 채택한 답변입니다.

도움이 되었다면 UP 눌러주세요!
UP이 많은 답변일수록 사용자들에게 더 많이 노출됩니다.