瀏覽代碼

Fixing bytes/CP437 issues.

You can't treat bytes as strings and expect them to be ok.
Steve Thielemann 3 年之前
父節點
當前提交
fad5771996
共有 2 個文件被更改,包括 42 次插入35 次删除
  1. 34 9
      deck.go
  2. 8 26
      playcards.go

+ 34 - 9
deck.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"bytes"
 	"math/rand"
 	"red-green/door"
 	"strings"
@@ -71,10 +72,20 @@ func BackOf(level int) door.Panel {
 		Y:     0,
 		Width: 5,
 	}
-	back := string(BackSymbol(level))
+	var back string
+
+	if door.Unicode {
+		back = strings.Repeat(string(BackSymbol(level)), 5)
+	} else {
+		// bytes get destroyed as string.
+		br := make([]byte, 1)
+		br[0] = byte(BackSymbol(level))
+		back = string(bytes.Repeat(br, 5))
+	}
+
 	for x := 0; x < 3; x++ {
 		p.Lines = append(p.Lines,
-			door.Line{Text: strings.Repeat(back, 5)})
+			door.Line{Text: back})
 	}
 	return p
 }
@@ -82,19 +93,33 @@ func BackOf(level int) door.Panel {
 func MarkOf(c int) door.Panel {
 	p := door.Panel{Width: 1}
 	color := door.ColorText("BLUE ON WHITE")
-	var m rune
+	// var m rune
 	if c == 0 {
-		m = ' '
+		p.Lines = append(p.Lines,
+			door.Line{Text: " ",
+				DefaultColor: color,
+			})
+		// m = ' '
 	} else {
 		if door.Unicode {
-			m = '\u25a0'
+			p.Lines = append(p.Lines,
+				door.Line{Text: "\u25a0",
+					DefaultColor: color,
+				})
+			// m = '\u25a0'
 		} else {
-			m = '\xfe'
+			/*
+				var b [1]byte
+				b[0] = 0xfe
+				m = string(b[:]) // '\xfe'
+			*/
+			var b [1]byte
+			b[0] = 0xfe
+			p.Lines = append(p.Lines,
+				door.Line{Text: string(b[:]),
+					DefaultColor: color})
 		}
 	}
-	p.Lines = append(p.Lines,
-		door.Line{Text: string(m),
-			DefaultColor: color})
 	return p
 }
 

+ 8 - 26
playcards.go

@@ -4,7 +4,6 @@ import (
 	"crypto/sha1"
 	"encoding/binary"
 	"fmt"
-	"log"
 	"math/rand"
 	"red-green/door"
 	"strconv"
@@ -81,46 +80,29 @@ func NormalizeDate(date *time.Time) {
 }
 
 func cmdLineRender(bracket string, inner string, outer string) func(string) string {
-	cmdRender := func(input string) string {
-		var result string
+	return func(input string) string {
+		var r door.Render = door.Render{Line: input}
 		inOuter := true
-		var lastColor string
 
 		for _, c := range input {
 			if c == '[' {
 				inOuter = false
-				if lastColor != bracket {
-					result += bracket
-					lastColor = bracket
-				}
-				result += string(c)
+				r.Append(bracket, 1)
 				continue
 			}
 			if c == ']' {
 				inOuter = true
-				if lastColor != bracket {
-					result += bracket
-					lastColor = bracket
-				}
-				result += string(c)
+				r.Append(bracket, 1)
 				continue
 			}
 			if inOuter {
-				if lastColor != outer {
-					result += outer
-					lastColor = outer
-				}
+				r.Append(outer, 1)
 			} else {
-				if lastColor != inner {
-					result += inner
-					lastColor = inner
-				}
+				r.Append(inner, 1)
 			}
-			result += string(c)
 		}
-		return result
+		return r.Result
 	}
-	return cmdRender
 }
 
 func (pc *PlayCards) InitValues() {
@@ -538,7 +520,7 @@ Next_Hand:
 		var seed int64 = int64(binary.BigEndian.Uint64(result[0:8]))
 		pc.RNG.Seed(seed)
 		// I'm seeing changes in the seed_seq bytes, but the seed is the same number.
-		log.Printf("%#v\nSeed %d\nLen %d\nresult %#v\n", seed_seq, seed, len(seed_seq), result)
+		// log.Printf("%#v\nSeed %d\nLen %d\nresult %#v\n", seed_seq, seed, len(seed_seq), result)
 
 		pc.Deck = ShuffleCards(pc.RNG, 1)
 		pc.State = MakeCardStates(1)