paizaの配信メールでcode warriorsというブラウザゲームを知った。
コーディングしてロボット同士で戦うというもので面白そうなのでやってみた。
が、あんまりコーディングする意味を感じなくて面白くなかった。チュートリアルしかやってないので先に進めば面白いのかもしれないけど。ターン制なのでコマンド選択をコードでやってるだけという感じがしたんだけど。
それで消化不良感があったのでempire of codeというゲームを試してみた。リアルタイムストラテジか?こっちのほうが面白そうな感じはしたが、やはり何か求めてるものとは違う感があってチュートリアルだけでやめてしまった。
次にcodingameを試してみた。ミニゲームをクリアしていく感じで面白そう。
というわけでいっこやってみた。画面は下のような感じ。
このゲームの目的は主人公を雷マークへ導くこと。
最初は次のコードが雛形として与えられている。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
* ---
* Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
**/
int main()
{
int lightX; // the X position of the light of power
int lightY; // the Y position of the light of power
int initialTX; // Thor's starting X position
int initialTY; // Thor's starting Y position
cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore();
// game loop
while (1) {
int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
cin >> remainingTurns; cin.ignore();
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
// A single line providing the move to be made: N NE E SE S SW W or NW
cout << "SE" << endl;
}
}
coutで移動できる仕様となっている。これを改変して目的を達成するわけだ。
動かすと主人公が実際に移動する様子が見えてうれしい。
やってることは競技プログラミングみたいなもんなんだけど、絵があると断然うれしいな。
次のコードでクリアできた。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
* ---
* Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
**/
int main()
{
int lightX; // the X position of the light of power
int lightY; // the Y position of the light of power
int initialTX; // Thor's starting X position
int initialTY; // Thor's starting Y position
cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore();
int tx = initialTX;
int ty = initialTY;
// game loop
while (1) {
int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
cin >> remainingTurns; cin.ignore();
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
// A single line providing the move to be made: N NE E SE S SW W or NW
//cout << "SE" << endl;
if(lightY-ty > 0)
{
cout << "S";
ty++;
}
else if(lightY!=ty)
{
cout << "N";
ty--;
}
if(lightX-tx > 0)
{
cout << "E" << endl;
tx++;
}
else if(lightX!=tx)
{
cout << "W" << endl;
tx--;
}
else
{
cout << endl;
}
}
}