board serlization

This commit is contained in:
Jan 2024-11-29 16:19:19 +01:00
parent e43f3c6404
commit 82f181f200
2 changed files with 162 additions and 21 deletions

185
v1/sud.go
View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
) )
const Version = "1.1.1" const Version = "0.1.1"
type Sudoku struct { type Sudoku struct {
Title string Title string
@ -16,29 +16,90 @@ type Sudoku struct {
Board [][]int 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 serialized []byte
var header []byte var header []byte
var body []byte var body []byte
var footer []byte var footer []byte
var length uint16 var length uint16
var empty byte = 0
//HEADER //HEADER
version := getVersion() version := getVersion()
header = append(header, byte(version>>8), byte(version)) header = append(header, byte(version>>8), byte(version))
//BODY //BODY
title := stringToBytes("Sudoku") body = sudoku.toBytes()
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)
//FOOTER //FOOTER
@ -68,6 +129,10 @@ func Deserialize() {
printBytes([]byte{byte(getVersion() >> 8), byte(getVersion())}) printBytes([]byte{byte(getVersion() >> 8), byte(getVersion())})
panic("Invalid version") panic("Invalid version")
} }
body := serialized[4 : len(serialized)-2]
board := bytesToBoard(body)
fmt.Println(board)
} }
func getVersion() uint16 { func getVersion() uint16 {
@ -115,14 +180,90 @@ func printBytes(bytes []byte) {
} }
} }
func stringToBytes(s string) []byte {
return []byte(s)
}
func main() { func main() {
Serialize() sudoku := Sudoku{
Deserialize() Title: "Sudoku",
Author: "John Doe",
print(usedBits(5)) Rules: "Fill the board with numbers from 1 to 9",
printBytes(stringToBytes("Hello, World!")) 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},
},
} }
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)
}*/

Binary file not shown.