-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mrsombre/moving
Moving, math and game reading
- Loading branch information
Showing
27 changed files
with
1,123 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,4 @@ var ( | |
GlobalB bool | ||
GlobalI int | ||
GlobalF float64 | ||
|
||
GlobalPoint Point | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// Command is an interface for game commands. | ||
|
||
var commandOutput io.Writer = os.Stdout | ||
|
||
type Command interface { | ||
String() string | ||
} | ||
|
||
type Commands []Command | ||
|
||
type MockCommand struct { | ||
Param1 float64 | ||
Param2 float64 | ||
} | ||
|
||
func (c MockCommand) String() string { | ||
return fmt.Sprintf("%.f %.f", c.Param1, c.Param2) | ||
} | ||
|
||
func ExecuteCommands(commands Commands) { | ||
for _, command := range commands { | ||
fmt.Fprintln(commandOutput, command) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockWriter struct { | ||
data []byte | ||
} | ||
|
||
func (w *mockWriter) Write(p []byte) (n int, err error) { | ||
w.data = p | ||
return len(p), nil | ||
} | ||
|
||
func TestMockCommand_String(t *testing.T) { | ||
cmd := MockCommand{1, 2} | ||
assert.Equal(t, "1 2", cmd.String()) | ||
} | ||
|
||
func TestExecuteCommand(t *testing.T) { | ||
commandOutput = &mockWriter{} | ||
ExecuteCommands(Commands{MockCommand{1, 2}}) | ||
|
||
want := "1 2\n" | ||
got := commandOutput.(*mockWriter).data | ||
assert.Equal(t, want, string(got)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
// Export/Import of data in the form of string arrays. | ||
// This can be used to unload the conditions of a problem (input) | ||
// in a compressed form into the debug console and unpack it in the IDE. | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"encoding/base64" | ||
"encoding/json" | ||
) | ||
|
||
// DataExport serializes and compresses a slice of strings, | ||
// returning a base64 encoded string. | ||
func DataExport(data []string) string { | ||
var err error | ||
|
||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var gzBuf bytes.Buffer | ||
gz := gzip.NewWriter(&gzBuf) | ||
if _, err = gz.Write(jsonData); err != nil { | ||
panic(err) | ||
} | ||
if err = gz.Close(); err != nil { | ||
panic(err) | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(gzBuf.Bytes()) | ||
} | ||
|
||
// DataImport decodes a base64 string, decompresses it, | ||
// and deserializes the JSON data into a slice of strings. | ||
func DataImport(encodedData string) []string { | ||
var err error | ||
|
||
gzData, err := base64.StdEncoding.DecodeString(encodedData) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
gz, err := gzip.NewReader(bytes.NewBuffer(gzData)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func(gz *gzip.Reader) { | ||
err = gz.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
}(gz) | ||
|
||
var jsonData bytes.Buffer | ||
if _, err = jsonData.ReadFrom(gz); err != nil { | ||
panic(err) | ||
} | ||
|
||
var data []string | ||
if err = json.Unmarshal(jsonData.Bytes(), &data); err != nil { | ||
panic(err) | ||
} | ||
|
||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var dataExportTests = []string{ | ||
"123", | ||
"abc", | ||
} | ||
|
||
func TestDataExport(t *testing.T) { | ||
data := DataExport(dataExportTests) | ||
assert.Equal(t, dataImportTests, data) | ||
} | ||
|
||
var dataImportTests = `H4sIAAAAAAAA/4pWMjQyVtJRSkxKVooFBAAA//9iXM2zDQAAAA==` | ||
|
||
func TestDataImport(t *testing.T) { | ||
data := DataImport(dataImportTests) | ||
assert.Equal(t, dataExportTests, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
// A set of helper methods for outputting debug information | ||
// to the Stderr stream in text or JSON format. | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
var debug = true | ||
var debugOutput = os.Stderr | ||
|
||
func asText(a ...any) { | ||
if !debug { | ||
return | ||
} | ||
fmt.Fprintln(debugOutput, a...) | ||
} | ||
|
||
func asJson(a any) { | ||
if !debug { | ||
return | ||
} | ||
b, _ := json.Marshal(a) | ||
asText(string(b)) | ||
} | ||
|
||
func asJsonPretty(a any) { | ||
if !debug { | ||
return | ||
} | ||
b, _ := json.MarshalIndent(a, ``, ` `) | ||
asText(string(b)) | ||
} | ||
|
||
func u(a ...any) { | ||
} |
Oops, something went wrong.