aboutsummaryrefslogtreecommitdiff
path: root/pkg/field/blank.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-09-26 19:29:00 +0200
committerJulien Dessaux2021-09-27 07:51:07 +0200
commit0038f88327dacd8d1d973de0f09992852402881c (patch)
tree03c934314411190b35f54547f7ad0b6c7df10aba /pkg/field/blank.go
parentRandomization and y fixes (diff)
downloadgofunge98-0038f88327dacd8d1d973de0f09992852402881c.tar.gz
gofunge98-0038f88327dacd8d1d973de0f09992852402881c.tar.bz2
gofunge98-0038f88327dacd8d1d973de0f09992852402881c.zip
Fixes some mycology test suite errors
Diffstat (limited to '')
-rw-r--r--pkg/field/blank.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/pkg/field/blank.go b/pkg/field/blank.go
new file mode 100644
index 0000000..2c30b6e
--- /dev/null
+++ b/pkg/field/blank.go
@@ -0,0 +1,97 @@
+package field
+
+func (f *Field) Blank(x, y int) {
+ if !f.isIn(x, y) {
+ // outside the field, nothing to do
+ return
+ }
+ l := &f.lines[y-f.y]
+ if x < l.x || x >= l.x+l.l {
+ // outside the current line's columns
+ return
+ }
+ if x > l.x && x < l.x+l.l-1 {
+ // just set the value
+ l.columns[x-l.x] = ' '
+ return
+ }
+ // do we need to trim this line?
+ if l.l == 1 {
+ // this was the last character on the line
+ if y == f.y {
+ // We need to trim the leading lines
+ leadingLines := 1
+ for i := 1; i < f.ly; i++ {
+ if f.lines[i].l == 0 {
+ leadingLines++
+ } else {
+ break
+ }
+ }
+ f.y += leadingLines
+ f.ly -= leadingLines
+ lines := make([]Line, f.ly)
+ for i := 0; i < f.ly; i++ {
+ lines[i] = f.lines[leadingLines+i]
+ }
+ f.lines = lines
+ } else if y == f.y+f.ly-1 {
+ // We need to trim the trailing lines
+ trailingLines := 1
+ for i := f.ly + f.y - 2; i >= 0; i-- {
+ if f.lines[i].l == 0 {
+ trailingLines++
+ } else {
+ break
+ }
+ }
+ f.ly -= trailingLines
+ f.lines = f.lines[:f.ly]
+ } else {
+ // it was a line in the middle
+ l.l = 0
+ l.columns = make([]int, 0)
+ }
+ } else if x == l.x {
+ // We need to remove leading spaces
+ leadingSpaces := 1
+ for i := 1; i < l.l; i++ {
+ if l.columns[i] == ' ' {
+ leadingSpaces++
+ } else {
+ break
+ }
+ }
+ l.x += leadingSpaces
+ l.l -= leadingSpaces
+ columns := make([]int, l.l)
+ for i := 0; i < l.l; i++ {
+ columns[i] = l.columns[leadingSpaces+i]
+ }
+ l.columns = columns
+ } else if x == l.l+l.x-1 {
+ // we need to remove trailing spaces
+ trailingSpaces := 1
+ for i := l.l - 2; i >= 0; i-- {
+ if l.columns[i] == ' ' {
+ trailingSpaces++
+ } else {
+ break
+ }
+ }
+ l.l -= trailingSpaces
+ l.columns = l.columns[:l.l]
+ }
+ // we now need to find the new field limits
+ f.x = f.lines[0].x
+ x2 := f.lines[0].l + f.lines[0].x
+ for i := 1; i < f.ly; i++ {
+ if f.x > f.lines[i].x {
+ f.x = f.lines[i].x
+ }
+ if x2 < f.lines[i].x+f.lines[i].l {
+ x2 = f.lines[i].x + f.lines[i].l
+ }
+ }
+ f.lx = x2 - f.x
+}