海戦ゲームはサーバーと2人のプレイヤーによって構成される。サーバ-プレイヤー間の情報の受け渡しはJSONで行う。 フィールド上の位置は要素2の配列で管理し、[0,0] から[4,4]までとり得る。
艦の初期配置を以下のJSONで送る。なお、先攻後攻はサーバーで決める。
- w 戦艦の位置
- c 巡洋艦の位置
- s 潜水艦の位置
例:
{
"w": [0,1],
"c": [0,0],
"s": [1,1]
}手番のプレイヤーが行動を報告する際には、以下のJSONを送る。行動が可能かどうかの判定はサーバー側で行われる。
- attack 攻撃する場合に存在
- to 攻撃する座標
- move 移動する場合に存在
- ship 移動する艦。戦艦"w"、巡洋艦"c"、潜水艦"s"
- to 移動先
例:
{
"attack": {
"to": [0,0]
}
}[0,0]に攻撃する。
{
"move": {
"ship": "w",
"to": [0,4]
}
}戦艦を[0,4]に移動する。
手番プレイヤーの行動を処理した後、両プレイヤーに結果と状態を通知する。
resultのattackedは行動プレイヤーは過去形、相手プレイヤーは受動態の意味である。
プログラムの解説はserver_doc.mdにある.
プレイヤーの手番のJSONを受け取り、処理を行った後、行動したプレイヤーに以下のJSONを返す。 存在しないマスへ移動や攻撃したり、重複するマスに移動したりすると違反行為として敗北する。
- outcome 勝敗が決した場合に存在。勝利ならtrue、敗北ならfalse
- result 行動の結果。初回の通知の時のみ存在しない
- attacked 攻撃した場合に存在
- position 攻撃した座標
- hit 命中した場合に存在。攻撃された艦。戦艦"w"、巡洋艦"c"、潜水艦"s"
- near 隣接マスを攻撃した場合に存在。艦種の配列
- attacked 攻撃した場合に存在
- condition 行動終了後のフィールドや耐久値の状態。hpが0の艦についての情報は存在しない
- me 自分のデータ
- ship 各艦
- hp 残り耐久値
- position 座標
- ship 各艦
- enemy 相手のデータ
- ship 各艦
- hp 残り耐久値
- ship 各艦
- me 自分のデータ
例:
{
"result": {
"attacked": {
"position": [1,1],
"hit": "w",
"near": ["c"]
}
},
"condition": {
"me": {
"w": {
"hp": 2,
"position": [0,0]
},
"c": {
"hp": 2,
"position": [0,1]
},
"s": {
"hp": 1,
"position": [1,1]
}
},
"enemy": {
"w": {
"hp": 2
},
"c": {
"hp": 2
},
"s": {
"hp": 1
}
}
}
}相手の戦艦に攻撃がヒットした。隣接マスに巡洋艦がいた。
{
"outcome": true,
"result": {
"attacked": {
"position": [2,3],
"hit": "s"
}
},
"condition": {
"me": {
"w": {
"hp": 1,
"position": [0,0]
},
"c": {
"hp": 1,
"position": [0,1]
},
"s": {
"hp": 1,
"position": [1,1]
}
},
"enemy": {
}
}
}相手の艦をすべて撃沈したので勝利した。
手番プレイヤーの行動の結果を相手プレイヤーに通知する。相手プレイヤーが違反行為をすると勝利する。
- outcome 勝敗が決した場合に存在。勝利ならtrue、敗北ならfalse
- result 相手の行動の結果。初回の通知の時のみ存在しない
- attacked 攻撃された場合に存在
- position 攻撃された座標
- hit 命中した場合に存在。攻撃された艦。戦艦"w"、巡洋艦"c"、潜水艦"s"
- near 隣接マスが攻撃された場合に存在。艦種の配列
- moved 移動された場合に存在
- ship 移動した艦
- distance 座標の差分
- attacked 攻撃された場合に存在
- condition 行動終了後のフィールドや耐久値の状態。hpが0の艦についての情報は存在しない
- me 自分のデータ
- ship 各艦
- hp 残り耐久値
- position 座標
- ship 各艦
- enemy 相手のデータ
- ship 各艦
- hp 残り耐久値
- ship 各艦
- me 自分のデータ
例:
{
"result": {
"attacked": {
"position": [0,0],
"hit": "w",
"near": ["c"]
}
},
"condition": {
"me": {
"w": {
"hp": 2,
"position": [0,0]
},
"c": {
"hp": 2,
"position": [0,1]
},
"s": {
"hp": 1,
"position": [1,1]
}
},
"enemy": {
"w": {
"hp": 3
},
"c": {
"hp": 2
},
"s": {
"hp": 1
}
}
}
}自分の戦艦に攻撃がヒットした。隣接マスに巡洋艦がいた。
{
"result": {
"moved": {
"ship": "w",
"distance": [0,-2]
}
},
"condition": {
"me": {
"w": {
"hp": 2,
"position": [0,0]
},
"c": {
"hp": 2,
"position": [0,1]
},
"s": {
"hp": 1,
"position": [1,1]
}
},
"enemy": {
"w": {
"hp": 3
},
"c": {
"hp": 2
},
"s": {
"hp": 1
}
}
}
}相手の戦艦が上に2マス動いた。
{
"outcome": true,
"condition": {
"me": {
"w": {
"hp": 2,
"position": [0,0]
},
"c": {
"hp": 2,
"position": [0,1]
},
"s": {
"hp": 1,
"position": [1,1]
}
},
"enemy": {
"w": {
"hp": 3
},
"c": {
"hp": 2
},
"s": {
"hp": 1
}
}
}
}相手の違反により勝利した。
- サーバが起動する
- プレイヤーが二人接続する
- 接続確認メッセージ"you are connectd. please send me initial state.\n"が各クライアントに送られる
- 各プレイヤーは艦の初期配置を上述のJSON形式で送る
- 行動できるプレイヤーには"your turn\n"、相手待ちのプレイヤーには"waiting\n"というメッセージが送られる
- 行動プレイヤーは行動を上述のJSON形式で送る
- 行動の結果が上述のJSON形式で各プレイヤーに送られる
- もし勝敗が決すれば勝利プレイヤーに"you win\n"、敗北プレイヤーに"you lose\n"のメッセージが送られる。ターンが10000回を超えると引き分けで、"even\n"が送られる。
- 6~8を勝敗が決するまで繰り返す