Kodewick
学びはじめる →
ミニチャレンジ←→0 / 60

残りのライフ

#015ミニチャレンジlives-- / lives++約 6 分

小さなゲームはライフ 3 から始まります。出来事ごとに、"hit" なら 1 減り、"heart" なら 1 増え、それ以外は変わりません。ライフが 0 になったらゲームオーバーで、そのあとの出来事は数えないので 0 を返します。残りのライフを返す lives_left(events) を書きましょう。

例
lives_left({"hit", "heart", "hit"})→2
lives_left({"hit", "hit", "hit", "heart"})→0
関数int lives_left(const vector<string> &events)
main.cpp
#include <string>
#include <vector>
using namespace std;

int lives_left(const vector<string> &events) {
    int lives = 3;
    for (const string &e : events) {
        // ここにコードを書く:e に合わせてライフを増減し、0 になったら 0 を返す
    }
    return lives;
}
Ctrl + Enter でも実行できます
結果

実行すると例で試せます。よさそうなら提出しましょう。