Difference between revisions of "W3911 Sudoku Server"
From Coder Merlin
Line 114: | Line 114: | ||
let json = | let json = | ||
#""" | #""" | ||
{"board": | |||
[{"cells":[{"position":{"boxIndex":0,"cellIndex":0},"value":0},{"position":{"boxIndex":0,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":0,"cellIndex":2},"value":2},{"position":{"boxIndex":0,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":0,"cellIndex":4},"value":4},{"position":{"boxIndex":0,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":0,"cellIndex":6},"value":6},{"position":{"boxIndex":0,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":0,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]}, | |||
{"cells":[{"position":{"boxIndex":1,"cellIndex":0},"value":0},{"position":{"boxIndex":1,"cellIndex":1},"value":1}, | |||
{"position":{"boxIndex":1,"cellIndex":2},"value":2},{"position":{"boxIndex":1,"cellIndex":3},"value":3}, | |||
{"position":{"boxIndex":1,"cellIndex":4},"value":4},{"position":{"boxIndex":1,"cellIndex":5},"value":5}, | |||
{"position":{"boxIndex":1,"cellIndex":6},"value":6},{"position":{"boxIndex":1,"cellIndex":7},"value":7}, | |||
{"position":{"boxIndex":1,"cellIndex":8},"value":8}]} | |||
]} | |||
"""# | |||
let decoder = JSONDecoder() | let decoder = JSONDecoder() | ||
Line 136: | Line 172: | ||
// Structure definitions | // Structure definitions | ||
struct Position: Codable { | |||
let boxIndex: Int | |||
let cellIndex: Int | |||
} | |||
struct Cell: Codable { | struct Cell: Codable { | ||
let position: Position | |||
let value: Int? | let value: Int? | ||
} | } | ||
struct Box: Codable { | struct Box: Codable { | ||
let cells: [Cell] | let cells: [Cell] | ||
} | |||
} | |||
struct Board: Codable { | struct Board: Codable { | ||
let board: [Box] | let board: [Box] | ||
} | } | ||
}} | }} |
Revision as of 09:50, 7 October 2021
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Background A Sudoku board is made up of nine boxes. Boxes (container for nine cells) are listed from top-to-bottom, left-to-right indexed from zero. Cells are listed from top-to-bottom, left-to-right, indexed from zero. All valid payloads and responses must use well-formed JSON. “cells” is returned as follows: “cells”: [[<nine values from top-left>], [<nine values from top-middle>], …]
End Points
POST /games?difficulty=<difficulty>
Creates a new game and associated board
Parameters:
difficulty: easy | medium | hard | hell
Body: none
Status code: 201 Created
Response: ID (integer) of newly created game {"id":<id>}
{"id": 728134}
Errors:
{{{errors}}}
GET /games/<id>/cells
Returns the current cells for the specified game
Parameters: none
Body: none
Status code: 200 OK
Response: Cells {"id":<id>}
{"id": 728134}
Errors:
{{{errors}}}
PUT /games/<id>/cells/<boxIndex>/<cellIndex>
Place specified value at in game at boxIndex, cellIndex
Parameters: none
Body: value (null for removing value)
Status code: 200 OK
Response: {"board":[ {"cells":[{"value":<value>},{"value":<value>},...,{"value":<value>}]}, ... {"cells":[{"value":<value>},{"value":<value>},...,{"value":<value>}]} ]}
{"board":[ {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]}, {"cells":[{"value":0},{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]} ]}
Errors:
{{{errors}}}
JSON Encoding[edit]
JSON Decoding[edit]
Issue Request from Client to Server[edit]