123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- package main
- import (
- "red-green/door"
- "strings"
- )
- type Deck struct {
- Cards []door.Panel
- Backs []door.Panel
- Mark []door.Panel
- }
- func (d *Deck) SetBackColor(color string) {
- for x := 0; x < 5; x++ {
- for idx, _ := range d.Backs[x].Lines {
- d.Backs[x].Lines[idx].DefaultColor = color
- }
- }
- }
- 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
- }
- type CardPair struct {
- card1 int
- card2 int
- }
- var blocks []CardPair
- func init() {
- blocks = []CardPair{
- CardPair{3, 4},
- CardPair{5, 6},
- CardPair{7, 8},
- CardPair{9, 10},
- CardPair{10, 11},
- CardPair{12, 13},
- CardPair{13, 14},
- CardPair{15, 16},
- CardPair{16, 17},
- CardPair{18, 19},
- CardPair{19, 20},
- CardPair{20, 21},
- CardPair{21, 22},
- CardPair{22, 23},
- CardPair{23, 24},
- CardPair{24, 25},
- CardPair{25, 26},
- CardPair{26, 27},
- }
- }
- // Which card(s) are unblocked by this card?
- func Unblocks(card int) []int {
- var result []int
- for idx := range blocks {
- if blocks[idx].card1 == card || blocks[idx].card2 == card {
- result = append(result, idx)
- }
- }
- return result
- }
- func CanPlay(card1 int, card2 int) bool {
- rank1 := GetRank(card1)
- rank2 := GetRank(card2)
- if (rank1+1)%13 == rank2 {
- return true
- }
- if rank1 == 0 {
- rank1 += 13
- }
- if rank1-1 == rank2 {
- return true
- }
- return false
- }
|