Skip to content

Latest commit

 

History

History
289 lines (273 loc) · 7.57 KB

File metadata and controls

289 lines (273 loc) · 7.57 KB

実装について

海戦ゲームはサーバーと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 隣接マスを攻撃した場合に存在。艦種の配列
  • condition 行動終了後のフィールドや耐久値の状態。hpが0の艦についての情報は存在しない
    • me 自分のデータ
      • ship 各艦
        • hp 残り耐久値
        • position 座標
    • enemy 相手のデータ
      • ship 各艦
        • hp 残り耐久値

例:

{
    "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 座標の差分
  • condition 行動終了後のフィールドや耐久値の状態。hpが0の艦についての情報は存在しない
    • me 自分のデータ
      • ship 各艦
        • hp 残り耐久値
        • position 座標
    • enemy 相手のデータ
      • ship 各艦
        • hp 残り耐久値

例:

{
    "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
            }
        }
    }
}

相手の違反により勝利した。

通信の流れ

  1. サーバが起動する
  2. プレイヤーが二人接続する
  3. 接続確認メッセージ"you are connectd. please send me initial state.\n"が各クライアントに送られる
  4. 各プレイヤーは艦の初期配置を上述のJSON形式で送る
  5. 行動できるプレイヤーには"your turn\n"、相手待ちのプレイヤーには"waiting\n"というメッセージが送られる
  6. 行動プレイヤーは行動を上述のJSON形式で送る
  7. 行動の結果が上述のJSON形式で各プレイヤーに送られる
  8. もし勝敗が決すれば勝利プレイヤーに"you win\n"、敗北プレイヤーに"you lose\n"のメッセージが送られる。ターンが10000回を超えると引き分けで、"even\n"が送られる。
  9. 6~8を勝敗が決するまで繰り返す