Difference between revisions of "W3911 Sudoku Server"

From Coder Merlin
(Added JSON decoding)
Line 27: Line 27:
     * Status: 204 No Content
     * Status: 204 No Content


=== JSON Encoding ===
{{CodeExplorer
{{CodeExplorer
|exerciseID=1
|exerciseID=1
Line 41: Line 42:


guard let data = try? encoder.encode(cell),  
guard let data = try? encoder.encode(cell),  
       let string = 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(string)
print(json)
}}
}}
=== JSON Decoding ===
{{CodeExplorer
|exerciseID=2
|mode=swift
|height=400
|initialCode=import Foundation


struct Cell: Codable {
    let value: Int?
}
let json = #"{"value":17}"#
let decoder = JSONDecoder()
guard let data = json.data(using: .utf8),
      let cell: Cell = try? decoder.decode(Cell.self, from: data) else {
    fatalError("Failed to decode json into cell.")
}
dump(cell)
}}
=== Issue Request from Client to Server ===
{{CodeExplorer
{{CodeExplorer
|exerciseID=2
|exerciseID=3
|mode=swift
|mode=swift
|height=400
|height=400

Revision as of 17:00, 6 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>
   * Action: Creates a new game and associated board
   * Parameters:
     * difficulty: easy|medium|hard|hell
   * Payload: None
   * Response: Id uniquely identifying a game
   * Status code: 201 Created
  • GET /games/<id>/cells
   * Action: None
   * Payload: None
   * Response: cells
   * Status code: 200 OK
  • PUT /games/<id>/cells/<boxIndex>/<cellIndex>
   * Action: Place specified value at in game at boxIndex, cellIndex
   * Payload: value (null for removing value)
   * Response: Nothing
   * Status: 204 No Content

JSON Encoding[edit]

CoderMerlin™ Code Explorer: W0000 (1) 🟢

JSON Decoding[edit]

CoderMerlin™ Code Explorer: W0000 (2) 🟢

Issue Request from Client to Server[edit]

CoderMerlin™ Code Explorer: W0000 (3) 🟢