Difference between revisions of "W3911 Sudoku Server"

From Coder Merlin
Line 90: Line 90:
|initialCode=import Foundation
|initialCode=import Foundation


let json =
#"""
    {"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}]}
    ]}
"""#
let decoder = JSONDecoder()
guard let data = json.data(using: .utf8),
      let board: Board = try? decoder.decode(Board.self, from: data) else {
    fatalError("Failed to decode json into board.")
}
dump(board)
// Structure definitions
struct Cell: Codable {
struct Cell: Codable {
     let value: Int?
     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
    }
}
}


let json = #"{"value":17}"#
struct Board: Codable {
let decoder = JSONDecoder()
    let board: [Box]
 
 
guard let data = json.data(using: .utf8),
    init() {
      let cell: Cell = try? decoder.decode(Cell.self, from: data) else {
        var board = [Box]()
    fatalError("Failed to decode json into cell.")
        for _ in 0 ..< 9 {
            board.append(Box())
        }
        self.board = board
    }
}
}
dump(cell)
}}
}}
=== Issue Request from Client to Server ===
=== Issue Request from Client to Server ===

Revision as of 08:36, 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]

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) 🟢