deck.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "red-green/door"
  4. "strings"
  5. )
  6. type Deck struct {
  7. Cards []door.Panel
  8. Backs []door.Panel
  9. Mark []door.Panel
  10. }
  11. func (d *Deck) Init() {
  12. d.Cards = make([]door.Panel, 52)
  13. for x := 0; x < 52; x++ {
  14. d.Cards[x] = CardOf(x)
  15. }
  16. d.Backs = make([]door.Panel, 5)
  17. for x := 0; x < 5; x++ {
  18. d.Backs[0] = BackOf(x)
  19. }
  20. d.Mark = make([]door.Panel, 2)
  21. d.Mark[0] = MarkOf(0)
  22. d.Mark[1] = MarkOf(1)
  23. }
  24. func CardOf(c int) door.Panel {
  25. suit := GetSuit(c)
  26. rank := GetRank(c)
  27. var is_red bool = suit < 2
  28. var color string
  29. if is_red {
  30. color = door.ColorText("RED ON WHITE")
  31. } else {
  32. color = door.ColorText("BLACK ON WHITE")
  33. }
  34. p := door.Panel{
  35. X: 0,
  36. Y: 0,
  37. Width: 5}
  38. r := RankSymbol(rank)
  39. s := SuitSymbol(suit)
  40. p.Lines = append(p.Lines,
  41. door.Line{Text: string(r) + string(s) + " ",
  42. DefaultColor: color})
  43. p.Lines = append(p.Lines,
  44. door.Line{Text: " " + string(s) + " ",
  45. DefaultColor: color})
  46. p.Lines = append(p.Lines,
  47. door.Line{Text: " " + string(s) + string(r),
  48. DefaultColor: color})
  49. return p
  50. }
  51. func BackOf(level int) door.Panel {
  52. p := door.Panel{
  53. X: 0,
  54. Y: 0,
  55. Width: 5,
  56. }
  57. for x := 0; x < 3; x++ {
  58. p.Lines = append(p.Lines,
  59. door.Line{Text: strings.Repeat(string(BackSymbol(level)), 5)})
  60. }
  61. return p
  62. }
  63. func MarkOf(c int) door.Panel {
  64. p := door.Panel{Width: 1}
  65. color := door.ColorText("BLUE ON WHITE")
  66. var m rune
  67. if c == 0 {
  68. m = ' '
  69. } else {
  70. if door.Unicode {
  71. m = '\u25a0'
  72. } else {
  73. m = '\xfe'
  74. }
  75. }
  76. p.Lines = append(p.Lines,
  77. door.Line{Text: string(m),
  78. DefaultColor: color})
  79. return p
  80. }
  81. func RankSymbol(c int) rune {
  82. const symbols = "A23456789TJQK"
  83. return rune(symbols[c])
  84. }
  85. func SuitSymbol(c int) rune {
  86. if door.Unicode {
  87. switch c {
  88. case 0:
  89. return '\u2665'
  90. case 1:
  91. return '\u2666'
  92. case 2:
  93. return '\u2663'
  94. case 3:
  95. return '\u2660'
  96. }
  97. } else {
  98. if door.Full_CP437 {
  99. switch c {
  100. case 0:
  101. return '\x03'
  102. case 1:
  103. return '\x04'
  104. case 2:
  105. return '\x05'
  106. case 3:
  107. return '\x06'
  108. }
  109. } else {
  110. switch c {
  111. case 0:
  112. return '*'
  113. case 1:
  114. return '^'
  115. case 2:
  116. return '%'
  117. case 3:
  118. return '$'
  119. }
  120. }
  121. }
  122. return '?'
  123. }
  124. func BackSymbol(level int) rune {
  125. if level == 0 {
  126. return ' '
  127. }
  128. if door.Unicode {
  129. switch level {
  130. case 1:
  131. return '\u2591'
  132. case 2:
  133. return '\u2592'
  134. case 3:
  135. return '\u2593'
  136. case 4:
  137. return '\u2588'
  138. }
  139. } else {
  140. switch level {
  141. case 1:
  142. return '\xb0'
  143. case 2:
  144. return '\xb1'
  145. case 3:
  146. return '\xb2'
  147. case 4:
  148. return '\xdb'
  149. }
  150. }
  151. return '?'
  152. }
  153. func GetRank(c int) int {
  154. return (c % 52) % 13
  155. }
  156. func GetSuit(c int) int {
  157. return (c % 52) / 13
  158. }
  159. func GetDeck(c int) int {
  160. return c / 52
  161. }