Difference between revisions of "W3911 Sudoku Server"

From Coder Merlin
(Added difficulty)
Line 53: Line 53:
|initialCode=import Foundation                                                                                                                                                                                                                           
|initialCode=import Foundation                                                                                                                                                                                                                           
import FoundationNetworking                                                                                                                                                                                                                 
import FoundationNetworking                                                                                                                                                                                                                 
                                                                                                                                                                                                                                           
// Form the request                                                                                                                                                                                                                                         
var request = URLRequest(url: URL(string: "https://language-server.codermerlin.com/now")!)                                                                                                                                                   
var request = URLRequest(url: URL(string: "https://language-server.codermerlin.com/now")!)                                                                                                                                                   
request.httpMethod = "GET"                                                                                                                                                                                                                
request.httpMethod = "GET"  
let session = URLSession.shared                                                                                                                                                                                                            
 
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in                                                                                                                                           
// Execute synchronously (uses extension in code below)                                                                                                                                                                                                               
                                              if let data = data,                                                                                                                                                                           
let (data, response, error) = URLSession.shared.syncRequest(with: request)
                                                    let string = String(data: data, encoding: .utf8) {                                                                                                                                     
if let data = data,                                                                                                                                                                           
                                                  print(string)                                                                                                                                                                             
  let string = String(data: data, encoding: .utf8) {                                                                                                                                     
                                              }                                                                                                                                                                                          
  print(string)                                                                                                                                                                             
                                          })                                                                                                                                                                                              
} else if let error = error {
task.resume()                                                                                                                                                                                                                              
    print("Error: \(error)")
sleep(3)                                                                                                                                                                                                                                  
} else {
    print("An unexpected error occurred.")
}                                                                                                                                                                                                                           
 
 
 
 
// Reference: https://stackoverflow.com/questions/26784315/can-i-somehow-do-a-synchronous-http-request-via-nsurlsession-in-swift
extension URLSession {
  func syncRequest(with request: URLRequest) -> (Data?, URLResponse?, Error?) {
      var data: Data?
      var response: URLResponse?
      var error: Error?
     
      let dispatchGroup = DispatchGroup()
      let task = dataTask(with: request) {
        data = $0
        response = $1
        error = $2
        dispatchGroup.leave()
      }
      dispatchGroup.enter()
      task.resume()
      dispatchGroup.wait()
     
      return (data, response, error)
  } 
}
}}
}}

Revision as of 11:57, 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

CoderMerlin™ Code Explorer: W0000 (1) 🟢


CoderMerlin™ Code Explorer: W0000 (2) 🟢