Difference between revisions of "W3911 Sudoku Server"
From Coder Merlin
Line 46: | Line 46: | ||
|initialCode=import Foundation | |initialCode=import Foundation | ||
let board = Board() | |||
let encoder = JSONEncoder() | let encoder = JSONEncoder() | ||
guard let data = try? encoder.encode( | guard let data = try? encoder.encode(board), | ||
let json = String(data: data, encoding: .utf8) else { | let json = String(data: data, encoding: .utf8) else { | ||
fatalError("Failed to encode data into json.") | fatalError("Failed to encode data into json.") | ||
} | } | ||
print(json) | print(json) | ||
// Structure definitions | |||
struct Cell: Codable { | |||
let value: Int? | |||
} | |||
struct Box: Codable { | |||
let cells: [Cell] | |||
init() { | |||
var cells = [Cell]() | |||
for cellIndex in 0 ..< 9 { | |||
cells.append(Cell(value: cellIndex)) | |||
} | |||
self.cells = cells | |||
} | |||
} | |||
struct Board: Codable { | |||
let board: [Box] | |||
init() { | |||
var board = [Box]() | |||
for _ in 0 ..< 9 { | |||
board.append(Box()) | |||
} | |||
self.board = board | |||
} | |||
} | |||
}} | }} | ||
=== JSON Decoding === | === JSON Decoding === |
Revision as of 08:20, 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: none
none
Errors:
{{{errors}}}
JSON Encoding[edit]
JSON Decoding[edit]
Issue Request from Client to Server[edit]