커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
코드 오류 질문_도와주세요ㅠㅠㅠㅠㅠ
C++ 문법 뽀개기 v2
기타
북마크
주*비
댓글
5
추천
0
조회수
28
조회수
28
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


개인적으로 작성한 코드인데 혹시 질문을 드려도 될까요..?

플레이어의 차례를 정하는 부분에서는 주사위를 굴려서 숫자가 잘 나타나는데

게임이 진행되었을 때 플레이어가 주사위를 굴리면 계속 숫자가 0이 나옵니다.

제 코드에 어떤 오류가 있는 걸까요..??



스파르타 즉문즉답스파르타 즉문즉답



 계신 화면 전체를 캡처해 주시면, 튜터님들이 빠르게 상황을 이해할 수 있어요.

작성한 코드 및 에러 메세지

오류 발생 시, 작성한 코드 전체와 에러 메시지를 첨부해 주세요.

Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.

Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!


-main.cpp-

#include <iostream>

#include <cstdlib>

#include <string>

#include <fstream>

#include "main.h"

#include "Player.h"

#include "Dice.h"

#include "Point.h"

#include "Position.h"

using namespace std;







void Game::howToPlay() {

    std::ifstream filein("HowToPlay.txt");

    string line;

    if (filein.is_open()) {

        while (!filein.eof()) {

            getline(filein, line);

            cout << line << endl;

        }

    }

    filein.close();

}

void Game::gameBoard(){

    cout << " __________________________________________________________________________" << endl;

    cout << endl << " ----------------------------------------------------------------------" << endl;

    cout << "|  " << "S2   " << "|  " << "42  " << "|  " << "43  "  << "|  " << "S3  " << "|  " << "45  " << "|  " << "46  " << "|  " << "47  " << "|  " << "48  " << "|  " << "49  " << "|  " << "50  |" <<endl;

    cout << endl << " ----------------------------------------------------------------------" << endl;

    cout << "|  " << "40   " << "|  " << "39  " << "|  " << "38  "  << "|  " << "37  " << "|  " << "S1  " << "|  " << "35  " << "|  " << "34  " << "|  " << "33  " << "|  " << "32  " << "|  " << "31  |" <<endl;

    cout << endl << " ----------------------------------------------------------------------" << endl;

    cout << "|  " << "21   " << "|  " << "22  " << "|  " << "23  "  << "|  " << "24  " << "|  " << "S1  " << "|  " << "26  " << "|  " << "27  " << "|  " << "28  " << "|  " << "29  " << "|  " << "30  |" <<endl;

    cout << endl << " ----------------------------------------------------------------------" << endl;

    cout << "|  " << "20   " << "|  " << "19  " << "|  " << "18  "  << "|  " << "17  " << "|  " << "16  " << "|  " << "15  " << "|  " << "S3  " << "|  " << "13  " << "|  " << "12  " << "|  " << "11  |" <<endl;

    cout << endl << " ----------------------------------------------------------------------" << endl;

    cout << "|  " << "1    " << "|  " << "2   " << "|  " << "S2  "  << "|  " << "4   " << "|  " << "5   " << "|  " << "6   " << "|  " << "7   " << "|  " << "8   " << "|  " << "9   " << "|  " << "10  |" <<endl;

    cout << endl << " ----------------------------------------------------------------------" << endl;




}

void Game::welcomeBoard(){

    cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;

    cout << "                      Welcome to the game"                <<endl;

    cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;

    system("pause");

}

void Game::startGame(){

    string P1, P2;

    cout<<" Enter the name, Player 1 "<<endl;

    cin>>P1;

    Player1.setName(P1);

    Player1.setPosition(1);

    Player1.setPoint(0);

    Player1.roll();

    cout<<Player1.getName()<< " rolls the dice : " << Player1.rollDice();

    cout<<endl<<" Enter the name, Player 2 "<<endl;

    cin>>P2;

    Player2.setName(P2);

    Player2.setPosition(1);

    Player2.setPoint(0);

    Player2.roll();

    cout<<Player2.getName()<< " rolls the dice : " << Player2.rollDice();

    if (Player1.rollDice()>Player2.rollDice()){

        cout<<endl<<Player1.getName()<<" YOU WILL START FIRST ";

    }

    else {cout<<endl<<Player2.getName()<< " YOU WILL START FIRST ";}

    cout<<endl<<"Press any key to continue"<<endl;

    cin.get();

}

void Game::playGame() {

    int P1position=0, P2position=0;

    int n;

    Player1.setPosition(P1position);

    Player2.setPosition(P2position);

    Player1.setPoint(P1position);

    Player2.setPoint(P2position);

    Player1.roll();

    Player2.roll();

    while (Player1.getPosition() < 50 && Player2.getPosition() < 50) {

        gameBoard();

        if (Player2.rollDice() > Player1.rollDice())goto TP2;

        TP1 :

        cout << "Now, your turn " << Player1.getName() << endl;

        cout <<endl<<" Press any key to roll" << std::endl;

        cin.get();

        cout << " Your value of the dice is : " << rollingDice.rollDice() << endl;

        if (P1position >= 44) {

            if (n > (50 - P1position)) {

                cout << "You bounced back!" << endl;

                int bounceBack = (50 - Player1.getPosition()) + (50 - rollingDice.rollDice());

                Player1.setPosition(bounceBack);

                Player1.displayPosition();

                goto TP2;

            } else if (n == (50 - Player1.getPosition())) {

                cout << endl << "Congratulation! " << Player1.getName() << endl;

                Player1.displayPoint();

                break;

            }

        }

        Player1.movePosition(rollingDice.rollDice());

        Player1.displayPosition();

        if (Player1.getPosition() == 44) {

            cout << " You got the Snake" << endl;

            Player1.setPosition(14);

            Player1.displayPosition();

        } else if (Player1.getPosition() == 41) {

            cout << "you got the Snake" << endl;

            Player1.setPosition(3);

            Player1.displayPosition();

        } else if (Player1.getPosition() == 36) {

            cout << "You got the snake" << endl;

            Player1.setPosition(17);

            Player1.displayPosition();

        } else if (Player1.getPosition() == 4) {

            cout << "Congratulations! You got the ladder !" << endl;

            Player1.setPosition(25);

            Player1.displayPosition();

        } else if (Player1.getPosition() == 8) {

            cout << "Congratulations! You got the ladder !" << endl;

            Player1.setPosition(34);

            Player1.displayPosition();

        } else if (Player1.getPosition() == 12) {

            cout << "Congratulations! You got the ladder !" << endl;

            Player1.setPosition(31);

            Player1.displayPosition();

        }

        P1position = Player1.getPosition();

        Player1.setPosition(P1position);

        Player1.plusPoint(rollingDice.rollDice());

        if (rollingDice.rollDice() == 6) {

            cout << "Congratulations! you got roll twice " << Player1.getName() << endl << endl;

            goto TP1;

        }

        else goto TP2;




        TP2 :

        cout << "Now, your turn " << Player2.getName() << endl;

        cout << " Press any key to roll" << std::endl;

        cin.get();

        cout << " Your value of the dice is : " << rollingDice.rollDice() << endl;

        if (P2position >= 44) {

            if (n > 50 - P2position) {

                cout << "You bounced back!" << endl;

                int bounceBack = (50 - Player2.getPosition()) + (50 - rollingDice.rollDice());

                Player2.setPosition(bounceBack);

                Player2.displayPosition();

                goto TP1;

            } else if (n == (50 - Player2.getPosition())) {

                cout << endl << "Congratulations! " << Player2.getName() << endl;

                Player2.displayPoint();

                break;

            }

        }




        Player2.movePosition(rollingDice.rollDice());

        Player2.displayPosition();

        if (Player2.getPosition() == 44) {

            cout << " You got the Snake" << endl;

            Player2.setPosition(14);

            Player2.displayPosition();

        } else if (Player2.getPosition() == 41) {

            cout << "you got the Snake" << endl;

            Player2.setPosition(3);

            Player2.displayPosition();

        } else if (Player2.getPosition() == 36) {

            cout << "You got the snake" << endl;

            Player2.setPosition(17);

            Player2.displayPosition();

        } else if (Player2.getPosition() == 4) {

            cout << "Congratulations! You got the ladder !" << endl;

            Player2.setPosition(25);

            Player2.displayPosition();

        } else if (Player2.getPosition() == 8) {

            cout << "Congratulations! You got the ladder !" << endl;

            Player2.setPosition(34);

            Player2.displayPosition();

        } else if (Player2.getPosition() == 12) {

            cout << "Congratulations! You got the ladder !" << endl;

            Player2.setPosition(31);

            Player2.displayPosition();

        }

        P2position = Player2.getPosition();

        Player2.setPosition(P2position);

        Player2.plusPoint(rollingDice.rollDice());

        if (rollingDice.rollDice() == 6) {

            cout << "Congratulations! You got roll twice! " << Player2.getName() << endl << endl;

            goto TP2;

        }

        goto TP1;

    } }







    void Game::checkBoard(){

        cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

        cout << "                        Point Check Board" << endl;

        cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

        if (Player1.getPoint() > Player2.getPoint()) {

            cout << " - " << Player1.getName() << "\tPoints:  " << Player1.getPoint() << endl;

            cout << " - " << Player2.getName() << "\tPoints:  " << Player2.getPoint() << endl;

        } else {

            cout << " - " << Player2.getName() << "\t\t\t\t\t\tPoints:  " << Player2.getPoint() << endl << endl;

            cout << " - " << Player1.getName() << "\t\t\t\t\t\tPoints:  " << Player1.getPoint() << endl << endl;

        }

        exit(0);

    }







-------------------------------------------------------------------------------------------------------------------

-game.cpp-

#include <iostream>

#include "main.h"




int main(){

    Game play;

    play.howToPlay();

    play.gameBoard();

    play.welcomeBoard();

    play.startGame();

    play.playGame();

    play.checkBoard();

}

-------------------------------------------------------------------------------------------------------------------

-main.h-

#ifndef A3_MAIN_H

#define A3_MAIN_H

#include "Player.h"

#include "Dice.h"




class Game{

private:

    Player Player1, Player2;

    Dice rollingDice;

public:

    void howToPlay();

    void gameBoard();

    void welcomeBoard();

    void startGame();

    void playGame();

    void checkBoard();

};

#endif 

-------------------------------------------------------------------------------------------------------------------

-dice.h-

#ifndef A3_DICE_H

#define A3_DICE_H




class Dice{

private : int dice;

public : Dice();

         void roll();

         int rollDice();

};




#endif 

-------------------------------------------------------------------------------------------------------------------

-dice.cpp-

#include "Dice.h"

#include <iostream>

#include <cstdlib>




Dice::Dice(){

    dice = 0;

}

void Dice::roll(){

    srand(time(NULL));

    dice = (rand()%6) +1;

}

int Dice::rollDice(){

    return dice;

}

-------------------------------------------------------------------------------------------------------------------

-position.h-




#ifndef A3_POSITION_H

#define A3_POSITION_H




class Position{

private : int gamePosition;

public : Position();

         void setPosition(int newPosition);

         void movePosition (int newPosition);

         int getPosition();

};

#endif

-------------------------------------------------------------------------------------------------------------------

-position.cpp-

#include "Position.h"




Position::Position(){

    gamePosition=0;

}

void Position::setPosition(int newPosition){

    gamePosition=newPosition;

}

void Position::movePosition(int newPosition){

    gamePosition+=newPosition;

}

int Position::getPosition(){

    return gamePosition;

}

-------------------------------------------------------------------------------------------------------------------

-point.h-

#ifndef A3_POINT_H

#define A3_POINT_H




class Point{

private : int gamePoint;

public: Point();

        void setPoint(int newPoint);

        void plusPoint(int newPoint);

        int getPoint();

};




#endif

-------------------------------------------------------------------------------------------------------------------

-point.cpp-

#include "Point.h"

Point::Point(){

    gamePoint=0;

}

void Point::setPoint(int newPoint){

    gamePoint=newPoint;

}

void Point::plusPoint(int newPoint){

    gamePoint+=newPoint;

}

int Point::getPoint(){

    return gamePoint;

}

-------------------------------------------------------------------------------------------------------------------

-player.h-

#ifndef A3_PLAYER_H

#define A3_PLAYER_H

#include "Point.h"

#include "Position.h"

#include <string>

using namespace std;




class Player{

private :

    string name;

    Position*position= new Position;

    Point*point = new Point;

int dice;

public : Player();

         void setName (string newName);

         string getName();

         void setPosition(int n);

         void movePosition(int n);

         int getPosition();

         void displayPosition();

         void setPoint(int n);

         int getPoint();

         void displayPoint();

         void roll();

         void plusPoint(int n);

         int rollDice();







};




#endif

-------------------------------------------------------------------------------------------------------------------

-player.cpp-

#include "Player.h"

#include <iostream>

#include <string>

#include <sstream>

using namespace std;




Player::Player(){

    name = "";

    dice = 0;

}

void Player::setName(string newName){

    name = newName;

}

string Player::getName(){

    return name;

}

void Player::setPosition(int n){

    position[0].setPosition(n);

}

void Player::movePosition(int n){

    position[0].movePosition(n);

}

int Player::getPosition(){

    return position[0].getPosition();

}

void Player::displayPosition(){

    cout<<endl<<getName()<<" Your position is " << getPosition() << endl << endl;

}

void Player::setPoint(int n){

    point[0].setPoint(n);

}

void Player::plusPoint(int n){

    point[0].plusPoint(n);

}

int Player::getPoint(){

    return point[0].getPoint();

}

void Player::displayPoint(){

    cout<<endl<<getName()<< "Your point is " << getPoint()<<endl;

}

void Player::roll(){

    srand(time(NULL));

    dice = rand()%6+1;

}

int Player::rollDice(){

    return dice;

}


취소
 공유
취소
댓글 0
댓글 알림
나의얼굴