23
PL in C++ 2014.7.30 박박박 BattleShip Game #4

[Pl in c++] 9. battleship game 4

Embed Size (px)

Citation preview

Page 1: [Pl in c++] 9. battleship game 4

PL in C++

2014.7.30박민근

BattleShip Game #4

Page 2: [Pl in c++] 9. battleship game 4

퀴즈 #2

Page 3: [Pl in c++] 9. battleship game 4

수업전 질문

http://agebreak.clbnow.com

Page 4: [Pl in c++] 9. battleship game 4

•Component 가 변수일 수도 있고 , 함수일 수도 있는지가 궁금하고 , Component 와 Class 에서 사용하는 멤버변수와 멤버함수를 구별해야 하는지 궁금합니다 ~!

Component 는 일반적으로 객체 !!

Page 5: [Pl in c++] 9. battleship game 4

혹시 ppt 자료도 올려주실 수 있나요 ???

Why Not?

Page 6: [Pl in c++] 9. battleship game 4

와우에서는 플레이어가 인벤토리 대신에 가방을 들고 있는데요 , 이 가방이 인벤토리역할 말고도 닫아서 아이템처럼 다른 가방에도 들어갈 수 있는데 , 이런 것은 어떤 방식으로 구현이 되는건가요 ?

class Item{    };

class Inventory : public Item{public:    Item m_Items[32];};

Item innerWear;Item pants;Inventory bag1;Inventory bag2;

bag1.m_Items[0] = innerWear;bag1.m_Items[1] = pants;

bag2.m_Items[0] = bag1;

Page 7: [Pl in c++] 9. battleship game 4

Player 클래스

Page 8: [Pl in c++] 9. battleship game 4

class Player

배 객체들을 가지고 있다

• Aircraft, Battleship, Cruiser, Destroyer(x2)

내 배들을 배치한다 .

상대방의 공격에 피격 체크 결과를 알려준다 .

맵 객체들을 가지고 있다 .

상대방을 공격 한다 .

Page 9: [Pl in c++] 9. battleship game 4

class Player

배 객체들을 가지고 있다

• Aircraft, Battleship, Cruiser, Destroyer(x2)

내 배들을 배치한다 .

상대방의 공격에 피격 체크 결과를 알려준다 .

맵 객체들을 가지고 있다 .

상대방을 공격 한다 .

Page 10: [Pl in c++] 9. battleship game 4

#pragma once

///  게임을 진행하는 플레이어 클래스class Player{public:    Player();    ~Player();}; 

#pragma once#include "Aircraft.h"#include "Battleship.h"#include "Cruiser.h"#include "Destroyer.h"

///  게임을 진행하는 플레이어 클래스class Player{public:    Player();    ~Player();    

protected:    //  배 객체들 Aircraft    m_Aircraft;    Battleship  m_Battleship;    Cruiser     m_Cruiser;    Destroyer   m_Destroyer[2];};

Page 11: [Pl in c++] 9. battleship game 4

class Player

배 객체들을 가지고 있다

• Aircraft, Battleship, Cruiser, Destroyer(x2)

내 배들을 배치한다 .

상대방의 공격에 피격 체크 결과를 알려준다 .

맵 객체들을 가지고 있다 .

상대방을 공격 한다 .

Page 12: [Pl in c++] 9. battleship game 4

class Player

내 배들을 배치한다 .

///  게임을 진행하는 플레이어 클래스class Player{public:    Player();    ~Player();

    void SetupShips();

void Player::SetupShips(){    //  가지고 있는 배들을 배치한다}

Page 13: [Pl in c++] 9. battleship game 4

class Player

내 배들을 배치한다 .

• 랜덤한 위치와 , 랜덤한 방향으로 배치한다 .

• 배의 위치는 맵의 제한 (8x8) 을 넘어서면 안된다 .

• 배의 위치는 서로 겹치면 안된다 .

Page 14: [Pl in c++] 9. battleship game 4

class Player

내 배들을 배치한다 .

1. 우선은 수동 배치 부터 해보자 .

2. 일단은 Aircraft 만 배치 해보자 .class Ship{    public:    void AddPosition(Position pos);

Page 15: [Pl in c++] 9. battleship game 4

class Player

내 배들을 배치한다 .

1. 우선은 수동 배치 부터 해보자 .

2. 일단은 Aircraft 만 배치 해보자 .

void Player::SetupShips(){    //  가지고 있는 배들을 배치한다

//  우선은 수동 배치 Position pos;    pos.x = 'c';    pos.y = '3';    m_Aircraft.AddPosition(pos);

    pos.x = 'c';    pos.y = '4';    m_Aircraft.AddPosition(pos);

    pos.x = 'c';    pos.y = '5';    m_Aircraft.AddPosition(pos);        pos.x = 'c';    pos.y = '6';    m_Aircraft.AddPosition(pos);        pos.x = 'c';    pos.y = '7';    m_Aircraft.AddPosition(pos);}

Page 16: [Pl in c++] 9. battleship game 4

함수 오버로딩 !

Page 17: [Pl in c++] 9. battleship game 4

함수 오버로딩

• 함수 이름은 같으나 , 인자가 다른 별개의 함수

• 들어오는 인자에 따라서 , 어떤 함수가 호출될지 결정된다 . ( 함수의

다형성 !)class Ship{    public:    void AddPosition(Position pos);    void AddPosition(char x, char y);

void Ship::AddPosition(Position pos) {    for (int i = 0; i < m_Hp; ++i)    {        if (m_Pos[i].x == 0)        {            m_Pos[i] = pos;            break;        }    }}

void Ship::AddPosition(char x, char y){    //  별개의 함수로 구현}

Page 18: [Pl in c++] 9. battleship game 4

함수 오버로딩void Player::SetupShips(){    //  가지고 있는 배들을 배치한다 //  우선은 수동 배치 Position pos;    pos.x = 'c';    pos.y = '3';    m_Aircraft.AddPosition(pos);

    pos.x = 'c';    pos.y = '4';    m_Aircraft.AddPosition(pos);

    pos.x = 'c';    pos.y = '5';    m_Aircraft.AddPosition(pos);        pos.x = 'c';    pos.y = '6';    m_Aircraft.AddPosition(pos);        pos.x = 'c';    pos.y = '7';    m_Aircraft.AddPosition(pos);}

void Player::SetupShips(){    //  가지고 있는 배들을 배치한다 //  우선은 수동 배치 m_Aircraft.AddPosition('c', '3');    m_Aircraft.AddPosition('c', '4');    m_Aircraft.AddPosition('c', '5');    m_Aircraft.AddPosition('c', '6');    m_Aircraft.AddPosition('c', '7');    }

Page 19: [Pl in c++] 9. battleship game 4

함수 오버로딩

void Ship::AddPosition(Position pos){    for (int i = 0; i < m_Hp; ++i)    {        if (m_Pos[i].x == 0)        {            m_Pos[i] = pos;            break;        }    }}

void Ship::AddPosition(char x, char y){    Position pos;    pos.x = x;    pos.y = y;    AddPosition(pos);}

Page 20: [Pl in c++] 9. battleship game 4

class Player

내 배들의 위치를 출력하자 .

int _tmain(int argc, _TCHAR* argv[]){     Player player;    player.SetupShips();    player.PrintShips();

    getchar();

    return 0;}

Page 21: [Pl in c++] 9. battleship game 4

class Player

내 배들의 위치를 출력하자 .

void Player::PrintShips(){    m_Aircraft.Print();    m_Battleship.Print();    m_Cruiser.Print();    m_Destroyer[0].Print();    m_Destroyer[1].Print();}

void Ship::Print(){    std::string strPrint;     strPrint = m_Name;    strPrint += " : ";

    for (int i = 0; i < m_Hp; ++i)    {        strPrint += m_Pos[i].x;        strPrint += m_Pos[i].y;        strPrint += " ";    }

    std::cout << strPrint << std::endl;}

Page 22: [Pl in c++] 9. battleship game 4

과제

STL 의 Vector, List, Map 의

사용법에 대해서 학습하고 , 샘플을

제작하여 발표하여라 .

(~8.4)

Page 23: [Pl in c++] 9. battleship game 4

수업후 질문

http://agebreak.clbnow.com