瀏覽代碼

More code ported.

Steve Thielemann 3 年之前
父節點
當前提交
2d13ff7938
共有 2 個文件被更改,包括 204 次插入0 次删除
  1. 176 0
      deck.go
  2. 28 0
      playcards.go

+ 176 - 0
deck.go

@@ -0,0 +1,176 @@
+package main
+
+import (
+	"red-green/door"
+	"strings"
+)
+
+type Deck struct {
+	Cards []door.Panel
+	Backs []door.Panel
+	Mark  []door.Panel
+}
+
+func (d *Deck) Init() {
+	d.Cards = make([]door.Panel, 52)
+	for x := 0; x < 52; x++ {
+		d.Cards[x] = CardOf(x)
+	}
+	d.Backs = make([]door.Panel, 5)
+	for x := 0; x < 5; x++ {
+		d.Backs[0] = BackOf(x)
+	}
+	d.Mark = make([]door.Panel, 2)
+	d.Mark[0] = MarkOf(0)
+	d.Mark[1] = MarkOf(1)
+}
+
+func CardOf(c int) door.Panel {
+	suit := GetSuit(c)
+	rank := GetRank(c)
+	var is_red bool = suit < 2
+	var color string
+
+	if is_red {
+		color = door.ColorText("RED ON WHITE")
+	} else {
+		color = door.ColorText("BLACK ON WHITE")
+	}
+
+	p := door.Panel{
+		X:     0,
+		Y:     0,
+		Width: 5}
+	r := RankSymbol(rank)
+	s := SuitSymbol(suit)
+
+	p.Lines = append(p.Lines,
+		door.Line{Text: string(r) + string(s) + "   ",
+			DefaultColor: color})
+	p.Lines = append(p.Lines,
+		door.Line{Text: "  " + string(s) + "  ",
+			DefaultColor: color})
+	p.Lines = append(p.Lines,
+		door.Line{Text: "   " + string(s) + string(r),
+			DefaultColor: color})
+	return p
+}
+
+func BackOf(level int) door.Panel {
+	p := door.Panel{
+		X:     0,
+		Y:     0,
+		Width: 5,
+	}
+	for x := 0; x < 3; x++ {
+		p.Lines = append(p.Lines,
+			door.Line{Text: strings.Repeat(string(BackSymbol(level)), 5)})
+	}
+	return p
+}
+
+func MarkOf(c int) door.Panel {
+	p := door.Panel{Width: 1}
+	color := door.ColorText("BLUE ON WHITE")
+	var m rune
+	if c == 0 {
+		m = ' '
+	} else {
+		if door.Unicode {
+			m = '\u25a0'
+		} else {
+			m = '\xfe'
+		}
+	}
+	p.Lines = append(p.Lines,
+		door.Line{Text: string(m),
+			DefaultColor: color})
+	return p
+}
+
+func RankSymbol(c int) rune {
+	const symbols = "A23456789TJQK"
+	return rune(symbols[c])
+}
+
+func SuitSymbol(c int) rune {
+	if door.Unicode {
+		switch c {
+		case 0:
+			return '\u2665'
+		case 1:
+			return '\u2666'
+		case 2:
+			return '\u2663'
+		case 3:
+			return '\u2660'
+		}
+	} else {
+		if door.Full_CP437 {
+			switch c {
+			case 0:
+				return '\x03'
+			case 1:
+				return '\x04'
+			case 2:
+				return '\x05'
+			case 3:
+				return '\x06'
+			}
+		} else {
+			switch c {
+			case 0:
+				return '*'
+			case 1:
+				return '^'
+			case 2:
+				return '%'
+			case 3:
+				return '$'
+			}
+		}
+	}
+	return '?'
+}
+
+func BackSymbol(level int) rune {
+	if level == 0 {
+		return ' '
+	}
+	if door.Unicode {
+		switch level {
+		case 1:
+			return '\u2591'
+		case 2:
+			return '\u2592'
+		case 3:
+			return '\u2593'
+		case 4:
+			return '\u2588'
+		}
+	} else {
+		switch level {
+		case 1:
+			return '\xb0'
+		case 2:
+			return '\xb1'
+		case 3:
+			return '\xb2'
+		case 4:
+			return '\xdb'
+		}
+	}
+	return '?'
+}
+
+func GetRank(c int) int {
+	return (c % 52) % 13
+}
+
+func GetSuit(c int) int {
+	return (c % 52) / 13
+}
+
+func GetDeck(c int) int {
+	return c / 52
+}

+ 28 - 0
playcards.go

@@ -9,6 +9,33 @@ import (
 	"time"
 )
 
+func StringToANSIColor(colorCode string) string {
+	colors := []string{
+		"BLUE",
+		"BROWN",
+		"RED",
+		"CYAN",
+		"GREEN",
+		"MAGENTA",
+		"WHITE",
+	}
+
+	code := strings.ToUpper(colorCode)
+
+	for _, c := range colors {
+		if c == code {
+			return door.ColorText(code)
+		}
+	}
+
+	if code == "ALL" {
+		rand.Seed(time.Now().UnixNano())
+		pos := rand.Intn(len(colors))
+		return door.ColorText(colors[pos])
+	}
+	return door.ColorText("WHITE")
+}
+
 type Pos struct {
 	X     int
 	Y     int
@@ -533,6 +560,7 @@ func (pc *PlayCards) PlayCards() int {
 	off_x := (MX - game_width) / 2
 	off_y := (MY - game_height) / 2
 
+	// We can now position things properly centered
 	pc.SpaceAceTriPeaks.X = (MX - pc.SpaceAceTriPeaks.Width) / 2
 	pc.SpaceAceTriPeaks.Y = off_y
 	off_y += 3