2014-02-17 5 views
1

저는 초보자입니다. 두 개의 주사위를 굴려서 시뮬레이션 한 주사위 게임을 만들어야합니다. 나는 헤더 파일을 사용하는 것과 매우 혼란 스럽다. 하지만 무엇보다도 주사위의 얼굴을 왜 돌려야합니까? 둘째로 int 롤 기능은 무엇을 하는가? 값과 얼굴을 재설정 하시겠습니까? 그렇다면 기본값은 무엇입니까? 그리고 마지막 함수 Dice (int n)는이 함수를 사용하여 주사위 값의 최대 합계를 제어합니까? 함수는 다음 함수를 사용하는 클래스 헤더 파일을 가져야합니다.C++ 주사위 게임은 두 개의 주사위 굴림을 시뮬레이션합니다

class Dice{ 

private: 
    int face; // no. of faces of the dice 
    int value; // the face-value that shows up 

public: 
    int getFace()// returns the no. of face of the dice 
    { 
    }; 

    int getVal() 
    { 
     int dice1; 
     int dice2; 
     dice1 = rand() % 6 + 1; 
     dice2 = rand() % 6 + 1; 
    }; // returns the face value that shows up 

    int roll();  // simulate the roll pf the dice, the value field is set and returned. 
    Dice(); // default constructor , a standard six-face dice is created with value = 1 
    Dice(int size); // create a dice of given size 

}; 
+0

두 개의 실제 주사위를 굴리고 굴려서 어떤 일이 일어나는지 확인해야합니다. 앱에서'roll' 함수를 호출 할 때 그렇게해야합니다. 'Dice'와'Dice (int size)'는'생성자 '입니다. google 용어를 이해하는 데 도움이됩니다! –

+1

C++에서 책을 읽으려고 했습니까? – Gasim

+0

C++ 초보자가 아닌지 문제는 아닙니다. 모든 기능이 정의 된 방법을 보여주지 않으면 아무도 질문에 대답 할 수 없습니다. 수업 과제를 준비 중입니까? 강사가이 헤더를 보냈습니까? – nodakai

답변

2

이 질문에 대한 답변을 순서대로 입력하십시오.

각 주사위의 얼굴 수를 반환하는 유일한 이유는 현재 압연중인 주사위를 사용자에게 알리는 것입니다. 아래 예제에서 dOne.getFaces() 및 dTwo.getFaces()가있는 예제를 보았습니다.

int roll() 함수와 getVal()은 내가 가정하고있는 것과 같은 것으로 가정되었습니다. 나는 앞서 가서 getVal()을 제거하고 대신 roll()을 사용했다.

주사위()와 주사위 (int 크기)는 각 주사위의 얼굴 수를 초기화하는 중입니다. 기본 주사위는 6 개의 얼굴을 갖지만 사용자는 6 개 이상의 주사위를 굴릴 수 있으므로 int 크기가됩니다.

#include <iostream> 
#include <cstdlib> 
#include <time.h> 

class Dice 
{ 
    private: 
     int faces; // no. of faces of the dice 
     int value; // the face-value that shows up 
    public: 
     int getFaces() {return faces;} // returns the no. of faces of the dice 
     int roll() // returns the face value that shows up 
     { 
      value = rand() % faces + 1; 
      return value; 
     } 
     Dice() : faces(6) {}; // default constructor, create a dice of standard size 
     Dice(int size) : faces(size) {}; // create a dice of given size 
}; 

int main() 
{ 
    srand(time(NULL)); // Initialize random seed 
    char yesNo; 

    std::cout << "\nWould you like to roll two normal dice? (y/n)\n"; 
    std::cin >> yesNo; 

    if (yesNo == 'y') 
    { 
     Dice dOne, dTwo; 

     std::cout << "\nA dice with " << dOne.getFaces() << " faces rolled: " << dOne.roll() << '\n'; 
     std::cout << "A dice with " << dTwo.getFaces() << " faces rolled: " << dTwo.roll() << '\n'; 
    } 
    else 
    { 
     int dFaces; 
     std::cout << "\nHow many faces would you like each dice to have?\n\n"; 

     std::cout << "Dice 1: "; 
     std::cin >> dFaces; 
     Dice dOne(dFaces); 

     std::cout << "Dice 2: "; 
     std::cin >> dFaces; 
     Dice dTwo(dFaces); 

     std::cout << "\nA dice with " << dOne.getFaces() << " faces rolled: " << dOne.roll() << '\n'; 
     std::cout << "A dice with " << dTwo.getFaces() << " faces rolled: " << dTwo.roll() << '\n'; 
    } 

    return 0; 
} 
0

생성자를 정의해야합니다. 생성자가하는 일은 클래스의 상태를 설정하므로이 경우 주사위에 대한 정보를 설정합니다. 당신은 cpp에있는이 일을하는 경우

//file: dice.h 
//this is default constructor, note the lack of parameters being passed in 
Dice(): face(?), value(?) // here we are initializing "face" and "value", replace the question marks with the correct values as per your specification 
{ 
} 

조금 다르다 파일하지만 논리가 유지되어야합니다 : 당신이 헤더에 직접 넣을 경우

는 생성자의 형식은 다음과 같습니다 같은.

//file: dice.h 
Dice(); //This is only a declaration 

//file: dice.cpp 
#include "dice.h" 
Dice::Dice(): face(?), value(?) //This is where we define the constructor. note the Dice:: part here, we need to tell the compiler where to look 
{ 
} 

나머지는 상당히 비슷합니다. 당신이 어려움을 겪고 있다면, 나는 C++ 리소스를 더 연구 할 것을 제안합니다. 생성자/소멸자 및 초기화 목록을 조회합니다.