函式與類別

Last updated: April 08, 2020

總覽

在左側的區域,可以看到我們總共有 6 個 Classes。

# 類別名稱 說明
1 Game 存取遊戲資訊
2 Player 存取玩家資訊
3 Iter 容器,支援任意存取
4 Zone 存取戰區資訊
5 Drone 存取無人機資訊
6 Coord 存座標點,Drone & Zone 繼承 Coord

Class Game

Constructor


game(np, me, nd, nz)
np int 遊戲總人數
me int 自己的 ID
nd int 玩家的無人機數量
nz int 此遊戲的戰區數量

Member Variables


players
Iter<Player> players

Iter of Player

範例

輸出玩家的 ID

for(const Player & player : game.players) {
    cerr << "Player #" << player.id << ':' << endl;
}

zones
Iter<Zone> zones

Iter of Zone

範例

輸入各個 zone 的位置

for(Zone & zone : game.zones) {
    cin >> zone; cin.ignore();
}

Member Functions


Me()
Player & Me() const

得到自己的 Player object

參數

none

回傳值型態

Player &


player()
Player & player(int) const

透過 ID 得到某個 Player object

參數
  1. player’s ID
回傳值型態

Player &


zone()
Zone & zone(int) const

透過 ID 得到某個 Zone object

參數
  1. the zone’s ID
回傳值型態

Zone &


drones()
Iter<Drone> drones(const Player &, const Zone &) const

查詢某位玩家在某個 zone 裡的所有無人機

參數
  1. the Player object
  2. the Zone object
回傳值型態

Iter<Drone>


UpdateZones()
void UpdateZones()

更新每個 zone 的佔領者,注意你需要在遊戲回合中呼叫它,請參考我們的 main.cpp

參數

none

回傳值型態

none


Output()
void Output() const

輸出自己每臺無人機的目標位置,注意你需要在遊戲回合中呼叫它,請參考我們的 main.cpp

參數

none

回傳值型態

none


Controller()
Player & Controller(const Zone &) const

查詢某個 zone 的佔領者是誰

參數
  1. the Zone object
回傳值型態

Player &


Control()
int Control(const Zone &) const

查詢某個 zone 當下最少需要派遣幾架無人機才能佔領

參數
  1. the Zone object
回傳值型態

int


Trace()
Zone & Trace(const Drone &) const

根據某臺無人機的動向預測他要去哪個 zone

參數
  1. the Drone object
回傳值型態

Zone &

說明

根據該無人機前進的方向,找尋夾角最小的 zone,如果沒有 zone 與前進方向夾角小於 90 度,則會回傳最近的 zone


SocialDistancing()
void SocialDistancing() const

期間限定功能,讓每臺無人機維持社交距離

參數

none

回傳值型態

none


StayHome()
void StayHome() const

期間限定功能,讓每臺無人機進行長時間的居家隔離,大約會進行到遊戲結束

參數

none

回傳值型態

none


Sort()
template<typename T, typename U> static void Sort(Iter<T> &, const U &)

根據與給定目標的距離,排序給定的 Iter,給定目標可以是一個 zone 或是一台 drone

參數
  1. the Iter object
  2. Zone object or Drone object
回傳值型態

none

template<typename T> static void Sort(Iter<T> &, bool (*)(const T &, const T &))

根據給定的 compare function,排序給定的 Iter

參數
  1. the Iter object
  2. a compare function with bool type returns
回傳值型態

none

Class Player

Member Variables

drones()
Iter<Drone> drones;

Iter of Drone

Example

輸入 0 號玩家各個 drone 的位置

Player p = game.player(0);
for(Drone & drone : p.drones) {
    cin >> drone; cin.ignore();
}

Member Functions


isControl()
bool isControl(const Zone &) const;

判斷 Player 是否控制指定的 zone

Example

判斷 0 號玩家是否控制 0 號 zone

Player p = game.player(0);
Player z = game.zone(0);
bool isControl = p.isContro(z);

drone()
Drone & drone(int) const;

取得某個 id 的 Drone

Example

取得 0 號玩家的 0 號無人機物件

Player p = game.player(0);
Drone d = p.drone(0);

Constant

id
const int id {-1};

取得 Player 的 id

Example

取得玩家的 id

Player p = game.player(0);
int id = p.id;

Class Iter

template <typename T> class Iter;

A Random-Access-Iterator template for double pointer

Iter 是我們類別庫中特色的容器,我們用 Iter 來儲存多個 Drones, Zones, Players, … Iter 除了可以像陣列一般使用 for loop 遍歷,我們也提供排序 Iter 的方法,在許多函式中我們會用 Iter 作為參數作為傳遞多個物件的方法,他有以下幾個 Member types,不感興趣的可以跳過。

Member types


difference_type size_t
value_type T
reference T &
pointer T *
pointer T *
iterator_category std::random_access_iteratoe_tag

Member Functions


size()
difference_type size() const

回傳 Iter 內的元素數量

參數

none

回傳值型態

difference_type,

範例

假設 drones 是一個 Iter of Drone

drones[0] = (Drone){123, 456};

operator[]
reference operator[](difference_type) const;

您可以使用中括號存取 Iter 中的資料


begin() end()
Iter begin() const
Iter end() const

您可以使用 begin() 和 end() 作為排序的參數

Class Zone

Zone 是繼承 Coord 的類別,其中有

Member Variables


x 是區域的 x 座標

y 是區域的 y 座標

Member Functions


In()
template<typename U> bool In(const U &) const

判斷是否距離目標小於等於 100 單位

參數
  1. a target point, can be a Drone or Zone
回傳值型態

bool

Example

判斷這個 zone 這個物件是否距離 (0, 1) 這個 Drone 小於等於 100 。

Zone z = {123, 345};
z.In((Drone){0, 1});

operator-()
template<typename U> int operator-(const U &) const

運算子,到其他物件 (Zone, Drone) 的距離平方。

Example

回傳與座標 (123, 345) 這個無人機的距離平方

Zone z = game.zone(0);
Drone d = {123, 345};
int distance = d - z;

Non-member Functions


istream & operator>>(istream &, const Game &);

運算子重載用於 cin ,輸入座標 x, y

ostream & operator<<(ostream &, const Game &);

運算子重載用於 cout, cerr ,輸出座標 x, y

Class Drone

Member Variables

x 是無人機的 x 座標

y 是無人機的 y 座標

Member Functions


In()
template<typename U> bool In(const U &) const

判斷是否距離目標小於等於 100 單位

參數
  1. a target point, can be a Drone or Zone
回傳值型態

bool

Example

判斷這個 drone 這個物件是否距離 (0, 1) 這個 Zone 小於等於 100 。

Drone d = {123, 345};
d.In((Zone){0, 1});

operator-()
template<typename U> int operator-(const U &) const;

運算子,到其他物件 (Zone, Drone) 的距離平方。

Example

回傳與 Drone d 的距離平方

Drone d = {123, 345};
int distance = d - (Zone)(11, 26);

Non-member Functions

istream & operator>>(istream &, const Game &);

運算子重載用於 cin ,輸入座標 x, y

ostream & operator<<(ostream &, const Game &);

運算子重載用於 cout, cerr ,輸出座標 x, y

Class Coord

紀錄一個座標點,被 Drone, Zone 繼承

還有一堆運算子重載,有興趣可以看 Source Code XD

Member Variables


x 是區域的 x 座標

y 是區域的 y 座標