mirror of
https://github.com/LeRoid-hub/SUD.git
synced 2025-01-31 03:44:56 +00:00
some inital dirty go code
This commit is contained in:
parent
2676e7d640
commit
e43f3c6404
128
v1/sud.go
Normal file
128
v1/sud.go
Normal file
@ -0,0 +1,128 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const Version = "1.1.1"
|
||||
|
||||
type Sudoku struct {
|
||||
Title string
|
||||
Author string
|
||||
Rules string
|
||||
Board [][]int
|
||||
}
|
||||
|
||||
func Serialize() {
|
||||
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)
|
||||
|
||||
//FOOTER
|
||||
|
||||
//LENGTH
|
||||
length = uint16(len(header) + 2 + len(body) + len(footer))
|
||||
header = append(header, byte(length>>8), byte(length))
|
||||
|
||||
serialized = append(serialized, header...)
|
||||
serialized = append(serialized, body...)
|
||||
serialized = append(serialized, footer...)
|
||||
os.WriteFile("sudoku.sud", serialized, 0644)
|
||||
}
|
||||
|
||||
func Deserialize() {
|
||||
serialized, err := os.ReadFile("sudoku.sud")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
printBytes(serialized)
|
||||
|
||||
version := uint16(serialized[0])<<8 | uint16(serialized[1])
|
||||
|
||||
if version != getVersion() {
|
||||
printBytes([]byte{byte(version >> 8), byte(version)})
|
||||
println("=/")
|
||||
printBytes([]byte{byte(getVersion() >> 8), byte(getVersion())})
|
||||
panic("Invalid version")
|
||||
}
|
||||
}
|
||||
|
||||
func getVersion() uint16 {
|
||||
version := strings.Split(Version, ".")
|
||||
|
||||
majorInt, err := strconv.Atoi(version[0])
|
||||
if err != nil || majorInt > 15 {
|
||||
fmt.Println("Invalid major version, the major version must be an integer between 0 and 15")
|
||||
panic(err)
|
||||
}
|
||||
major := uint16(majorInt)
|
||||
|
||||
minorInt, err := strconv.Atoi(version[1])
|
||||
if err != nil || minorInt > 15 {
|
||||
fmt.Println("Invalid minor version, the minor version must be an integer between 0 and 15")
|
||||
panic(err)
|
||||
}
|
||||
minor := uint16(minorInt)
|
||||
|
||||
patchInt, err := strconv.Atoi(version[2])
|
||||
if err != nil || patchInt > 255 {
|
||||
fmt.Println("Invalid patch version, the patch version must be an integer between 0 and 255")
|
||||
panic(err)
|
||||
}
|
||||
patch := uint16(patchInt)
|
||||
|
||||
versionbyte := major<<12 | minor<<8 | patch
|
||||
|
||||
return versionbyte
|
||||
}
|
||||
|
||||
func usedBits(n uint16) int {
|
||||
bits := 0
|
||||
for n > 0 {
|
||||
bits++
|
||||
n >>= 1
|
||||
}
|
||||
return bits
|
||||
}
|
||||
|
||||
func printBytes(bytes []byte) {
|
||||
for _, n := range bytes {
|
||||
fmt.Printf("%08b ", n)
|
||||
fmt.Print("\n")
|
||||
}
|
||||
}
|
||||
|
||||
func stringToBytes(s string) []byte {
|
||||
return []byte(s)
|
||||
}
|
||||
|
||||
func main() {
|
||||
Serialize()
|
||||
Deserialize()
|
||||
|
||||
print(usedBits(5))
|
||||
printBytes(stringToBytes("Hello, World!"))
|
||||
}
|
BIN
v1/sudoku.sud
Normal file
BIN
v1/sudoku.sud
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user