【ネタ】話題のCodin Gameをやってみた
Sponsored Links
皆さんこんにちは
お元気ですか。私は元気です。
さて、今日は少し巷で話題になっている「Codin Game」に挑戦してみたよ
どんなゲームかって?そりゃプログラミングで敵を倒すというよりパズルをときます。
http://www.codingame.com/games:enbed
とりあえず、Playを押してみましょう。
チュートリアル(Training)
以下の画面のTrainingに挑戦します。
進む
以下の画面で言語を選べるのだそう。なんかコマンドをvimとかにもできそうですね。
僕はC++を選択、一番得意なもので・・・・
この間に画面の説明があります。以下の画面の説明ですが、
右上:ソースコード
右下:実行
左上:コードの実行のViewer
左下:コンソールの出力
一応チュートリアルのソースコードの解説を載せておきます。
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; /** * The code below will read all the game information for you. * On each game turn, information will be available on the standard input, you will be sent: * -> the total number of visible enemies * -> for each enemy, its name and distance from you * The system will wait for you to write an enemy name on the standard output. * Once you have designated a target: * -> the cannon will shoot * -> the enemies will move * -> new info will be available for you to read on the standard input. **/ int main() { // game loop while (1) { int count; // The number of current enemy ships within range cin >> count; cin.ignore(); for (int i = 0; i < count; i++) { string enemy; // The name of this enemy int dist; // The distance to your cannon of this enemy cin >> enemy >> dist; cin.ignore(); } // Write an action using cout. DON'T FORGET THE "<< endl" // To debug: cerr << "Debug messages..." << endl; cout << "HotDroid" << endl; // The name of the most threatening enemy (HotDroid is just one example) } }
main内ですが、countに敵艦隊の数が入り、for文を使って敵の情報を入力しています。
最後のHotDroidですが、敵です。ここに名前を入力すると撃ち落としてくれます。
多分この仕様は他の言語でも同じだと思います。
無事勝つとこんな感じになります。
終了するとFacebookのアカウントで登録を促されるので、一応登録しておきます。
他にもシングルに色々なゲームがあるので、ぜひ試してみてください
最後に、一応トレーニングで提出したソースコードを載せておきます。まずは見ずに頑張ってください。
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; /** * The code below will read all the game information for you. * On each game turn, information will be available on the standard input, you will be sent: * -> the total number of visible enemies * -> for each enemy, its name and distance from you * The system will wait for you to write an enemy name on the standard output. * Once you have designated a target: * -> the cannon will shoot * -> the enemies will move * -> new info will be available for you to read on the standard input. **/ class Enemy{ public: int dist; string name; }; class Enemys{ public: vector<Enemy> enemys_vector; void addEnemy(Enemy &enemy){ enemys_vector.push_back(enemy); } Enemy minEnemy(){ Enemy result_enemy; int minDist = 1000000; for(int i = 0; i < enemys_vector.size(); i++){ if(enemys_vector[i].dist < minDist){ result_enemy = enemys_vector[i]; minDist = enemys_vector[i].dist; } } return result_enemy; } }; int main() { // game loop while (1) { int count; // The number of current enemy ships within range cin >> count; cin.ignore(); Enemys enemys; for (int i = 0; i < count; i++) { string enemy_name; // The name of this enemy int dist; // The distance to your cannon of this enemy cin >> enemy_name >> dist; cin.ignore(); Enemy enemy; enemy.dist = dist; enemy.name = enemy_name; enemys.addEnemy(enemy); } Enemy destoryEnemy = enemys.minEnemy(); // Write an action using cout. DON'T FORGET THE "<< endl" // To debug: cerr << "Debug messages..." << endl; cout << destoryEnemy.name << endl; // The name of the most threatening enemy (HotDroid is just one example) } }