From 82f181f200d9bef948c58c52a470f8b5d82351e3 Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 29 Nov 2024 16:19:19 +0100 Subject: [PATCH] board serlization --- v1/sud.go | 183 ++++++++++++++++++++++++++++++++++++++++++++------ v1/sudoku.sud | Bin 60 -> 83 bytes 2 files changed, 162 insertions(+), 21 deletions(-) diff --git a/v1/sud.go b/v1/sud.go index 7135ff2..bc64703 100644 --- a/v1/sud.go +++ b/v1/sud.go @@ -7,7 +7,7 @@ import ( "strings" ) -const Version = "1.1.1" +const Version = "0.1.1" type Sudoku struct { Title string @@ -16,29 +16,90 @@ type Sudoku struct { Board [][]int } -func Serialize() { +func (sudoku *Sudoku) toBytes() []byte { + var bytes []byte + bytes = append(bytes, []byte(sudoku.Title)...) + bytes = append(bytes, 0) + bytes = append(bytes, []byte(sudoku.Author)...) + bytes = append(bytes, 0) + bytes = append(bytes, []byte(sudoku.Rules)...) + bytes = append(bytes, 0) + board := boardtoBytes(sudoku.Board) + bytes = append(bytes, board...) + return bytes +} + +func boardtoBytes(boardraw [][]int) []byte { + var board []byte + var buffer byte + for _, row := range boardraw { + for _, cell := range row { + if cell > 9 || cell < 0 { + print(cell, " is not a valid cell value") + panic("Invalid cell value") + } + + fmt.Println(cell, buffer) + + if buffer == 0 { + buffer = byte(cell) + } else { + buffer = buffer<<4 | byte(cell) + board = append(board, buffer) + buffer = 0 + } + } + } + buffer = buffer << 4 + board = append(board, buffer) + return board +} + +func bytesToBoard(input []byte) [][]int { + var output [][]int + row := []int{} + + for _, cell := range input { + lower := cell & 0x0F + upper := cell >> 4 + + row = append(row, int(upper)) + if len(row) == 9 { + rowCopy := make([]int, len(row)) + copy(rowCopy, row) + output = append(output, rowCopy) + row = []int{} + } + + if lower == 0xF { + break + } + + row = append(row, int(lower)) + if len(row) == 9 { + rowCopy := make([]int, len(row)) + copy(rowCopy, row) + output = append(output, rowCopy) + row = []int{} + } + } + + return output +} + +func Serialize(sudoku Sudoku) { var serialized []byte var header []byte var body []byte var footer []byte var length uint16 - var empty byte = 0 //HEADER version := getVersion() header = append(header, byte(version>>8), byte(version)) //BODY - title := stringToBytes("Sudoku") - author := stringToBytes("John Doe") - rules := stringToBytes("Fill the board with numbers from 1 to 9") - - body = append(body, title...) - body = append(body, empty) - body = append(body, author...) - body = append(body, empty) - body = append(body, rules...) - body = append(body, empty) + body = sudoku.toBytes() //FOOTER @@ -68,6 +129,10 @@ func Deserialize() { printBytes([]byte{byte(getVersion() >> 8), byte(getVersion())}) panic("Invalid version") } + + body := serialized[4 : len(serialized)-2] + board := bytesToBoard(body) + fmt.Println(board) } func getVersion() uint16 { @@ -115,14 +180,90 @@ func printBytes(bytes []byte) { } } -func stringToBytes(s string) []byte { - return []byte(s) -} - func main() { - Serialize() - Deserialize() + sudoku := Sudoku{ + Title: "Sudoku", + Author: "John Doe", + Rules: "Fill the board with numbers from 1 to 9", + Board: [][]int{ + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9}, + }, + } - print(usedBits(5)) - printBytes(stringToBytes("Hello, World!")) + Serialize(sudoku) + Deserialize() } + +/* +func clutter(input [][]int) []byte { + var output []byte + var buffer byte + var full bool + for _, row := range input { + for _, cell := range row { + if cell > 9 || cell < 0 { + print(cell, " is not a valid cell value") + panic("Invalid cell value") + } + + if !full { + buffer = byte(cell) + full = true + } else { + buffer = buffer<<4 | byte(cell) + output = append(output, buffer) + buffer = 0 + full = false + } + } + } + + if buffer != 0 { + buffer = buffer << 4 + buffer = buffer | 0x0F + output = append(output, buffer) + } + + return output +} + +func declutter(input []byte) { + var output [][]int + row := []int{} + + for _, cell := range input { + lower := cell & 0x0F + upper := cell >> 4 + + row = append(row, int(upper)) + if len(row) == 9 { + rowCopy := make([]int, len(row)) + copy(rowCopy, row) + output = append(output, rowCopy) + row = []int{} + } + + if lower == 0xF { + break + } + + row = append(row, int(lower)) + if len(row) == 9 { + rowCopy := make([]int, len(row)) + copy(rowCopy, row) + output = append(output, rowCopy) + row = []int{} + } + } + + fmt.Print("\n") + fmt.Println(output) +}*/ diff --git a/v1/sudoku.sud b/v1/sudoku.sud index 64a2e0f2455d74bb83195941525ee411069032f2..2758af640ff08e0289ba3805edc498e0351a8aca 100644 GIT binary patch delta 34 pcmcBsW?^Jx2%gAdAs$?iAQ>