mirror of
https://github.com/LeRoid-hub/SUD.git
synced 2025-01-31 03:44:56 +00:00
board serlization
This commit is contained in:
parent
e43f3c6404
commit
82f181f200
183
v1/sud.go
183
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)
|
||||
}*/
|
||||
|
BIN
v1/sudoku.sud
BIN
v1/sudoku.sud
Binary file not shown.
Loading…
Reference in New Issue
Block a user