twentyone: Playing the game "twentyone"

Usage Arguments Examples

View source: R/pokergames.R

Usage

1
twentyone(n_people = 2, counter = 100, block = 10)

Arguments

n_people

Number of players.

counter

How many chips.

block

Prevent other to look.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (n_people = 2, counter = 100, block = 10) 
{
    n = n_people
    if (!(n <= 4 && n >= 2)) {
        cat("game limit 2~4 people", "\n")
    }
    if (n <= 4 && n >= 2) {
        counter = rep(counter, n)
        poker = paste0(c(rep(c("spade", "heart", "diamond", "club"), 
            13)), c(as.character(rep(1:10, each = 4)), rep(c("J", 
            "Q", "K"), each = 4)))
        point = c(rep(1:10, each = 4), rep(10, each = 12))
        while (sum(counter <= 0) < 1) {
            co = c()
            card = sample(1:52, size = 52, replace = F)
            player = c(paste0("player", as.character(c(1:n_people))), 
                "Bank")
            card_1 = card[1:length(player)]
            card = card[-c(1:length(player))]
            card_2 = card[1:length(player)]
            card = card[-c(1:length(player))]
            df = data.frame(player = player, counter = c(counter, 
                Inf), Hidden = poker[card_1], card1 = poker[card_2], 
                card2 = "NA", card3 = "NA", card4 = "NA", card5 = "NA", 
                card6 = "NA", card7 = "NA", card8 = "NA", card9 = "NA", 
                card10 = "NA")
            cat("Current situation on the table", "\n")
            w = 9
            sign_pad = stringr::str_pad(colnames(df), width = w, 
                side = c("both"), pad = c("_"))
            cat(sign_pad, "\n")
            w = 9
            for (i in c(1:(n + 1))) {
                d = c(c(df[i, ])[1:2], "*", c(df[i, ])[4:13])
                sign_pad = stringr::str_pad(d, width = w, side = c("both"), 
                  pad = c("_"))
                cat(sign_pad, "\n")
            }
            cat("Now every player will check their hidden cards", 
                "\n")
            for (i in c(1:length(player))) {
                ch = readline(player[i])
                cat(player[i], "Hidden is", df[i, 3], "\n")
                cat(player[i], "card1 is", df[i, 4], "\n")
                if (i == length(player)) {
                  cat("After watching, start the game")
                }
                else {
                  co = c(co, readline("How much to bet"))
                  ch = readline("After watching, change to the next one")
                }
                for (e in c(1:block)) {
                  cat("=====================================================", 
                    "\n")
                }
            }
            cat("Now the game starts", "\n")
            for (p in c(1:length(player))) {
                if (p == length(player)) {
                  cat(player[p], "Start to get cards", "\n")
                  cat("Your have", c("*", df[p, 4]), "\n")
                  ta = 0
                  tac = c()
                  po = which(poker %in% c(df[p, 3], df[p, 4]))
                  s = sum(point[po])
                  while (s < 17) {
                    y = "Y"
                    if (y == "Y") {
                      cat(player[p], "You got", poker[card[1]], 
                        "\n")
                      s = s + point[card[1]]
                      tac = c(tac, poker[card[1]])
                      card = card[-c(1)]
                      ta = ta + 1
                    }
                    else {
                      break
                    }
                  }
                  if (ta != 0) {
                    df[p, 5:(5 - 1 + ta)] = tac
                  }
                  sign_pad = stringr::str_pad(colnames(df), width = w, 
                    side = c("both"), pad = c("_"))
                  cat(sign_pad, "\n")
                  for (i in c(1:(n + 1))) {
                    d = c(c(df[i, ])[1:2], "*", c(df[i, ])[4:13])
                    sign_pad = stringr::str_pad(d, width = w, 
                      side = c("both"), pad = c("_"))
                    cat(sign_pad, "\n")
                  }
                }
                else {
                  cat(player[p], "Start to get cards", "\n")
                  cat("Your have", c("*", df[p, 4]), "\n")
                  ta = 0
                  tac = c()
                  while (ta < 10) {
                    y = readline("Do you want to get another card?? Y:Yes N:No")
                    if (y == "Y") {
                      cat(player[p], "You got", poker[card[1]], 
                        "\n")
                      tac = c(tac, poker[card[1]])
                      card = card[-c(1)]
                      ta = ta + 1
                    }
                    else {
                      break
                    }
                  }
                  if (ta != 0) {
                    df[p, 5:(5 - 1 + ta)] = tac
                  }
                  sign_pad = stringr::str_pad(colnames(df), width = w, 
                    side = c("both"), pad = c("_"))
                  cat(sign_pad, "\n")
                  for (i in c(1:(n + 1))) {
                    d = c(c(df[i, ])[1:2], "*", c(df[i, ])[4:13])
                    sign_pad = stringr::str_pad(d, width = w, 
                      side = c("both"), pad = c("_"))
                    cat(sign_pad, "\n")
                  }
                }
            }
            cat("Show hidden cards and count points", "\n")
            cat("Current situation on the table", "\n")
            sign_pad = stringr::str_pad(colnames(df), width = w, 
                side = c("both"), pad = c("_"))
            cat(sign_pad, "\n")
            for (i in c(1:(n + 1))) {
                d = c(c(df[i, ])[1:13])
                sign_pad = stringr::str_pad(d, width = w, side = c("both"), 
                  pad = c("_"))
                cat(sign_pad, "\n")
            }
            cat("The sum of the Bank's points is", s, "\n")
            sc = c()
            for (p in c(1:(length(player) - 1))) {
                cat(player[p], "has:", c(df[p, 3], df[p, 4], 
                  df[p, 5], df[p, 6], df[p, 7], df[p, 8], df[p, 
                    9], df[p, 10], df[p, 11], df[p, 12], df[p, 
                    13]), "\n")
                ss = readline("The sum of points is")
                sc = c(sc, ss)
            }
            if (sum(sc %in% c("21")) > 0) {
                for (p in c(1:length(sc))) {
                  if (sc[p] == "21" && df[p, 5] == "NA") {
                    cat(player[p], "achieve BlackJack", "\n")
                    cat("gave", player[p], "double the bet", 
                      "\n")
                    df[p, 2] = df[p, 2] + 2 * as.numeric(co[p])
                    sc[p] = "50"
                  }
                }
            }
            if (s <= 21) {
                cat("The Bank has not Bust", "\n")
                cat("Confiscate the bets of the Bust players and point smaller than Bank", 
                  "\n")
                cat("Pay one bet to players who have not Bust and points bigger than Bank", 
                  "\n")
                cat("If the points are the same as Bank, Tie, not confiscate the bets", 
                  "\n")
                for (p in c(1:length(sc))) {
                  ss = as.numeric(sc[p])
                  if (ss == 50) {
                    next
                  }
                  if (ss <= 21 && ss > s) {
                    df[p, 2] = df[p, 2] + as.numeric(co[p])
                  }
                  else if (ss > 21 | ss < s) {
                    df[p, 2] = df[p, 2] - as.numeric(co[p])
                  }
                  else {
                    df[p, 2] = df[p, 2]
                  }
                }
            }
            else if (s > 21) {
                cat("The Bank Bust", "\n")
                cat("Confiscate the bets of the Bust players", 
                  "\n")
                cat("Pay one bet to players who have not Bust", 
                  "\n")
                for (p in c(1:length(sc))) {
                  ss = as.numeric(sc[p])
                  if (ss <= 21) {
                    df[p, 2] = df[p, 2] + as.numeric(co[p])
                  }
                  else {
                    df[p, 2] = df[p, 2] - as.numeric(co[p])
                  }
                }
            }
            cat("Current situation on the table", "\n")
            sign_pad = stringr::str_pad(colnames(df), width = w, 
                side = c("both"), pad = c("_"))
            cat(sign_pad, "\n")
            for (i in c(1:(n + 1))) {
                d = c(c(df[i, ])[1:13])
                sign_pad = stringr::str_pad(d, width = w, side = c("both"), 
                  pad = c("_"))
                cat(sign_pad, "\n")
            }
            cat("This round of the game is over", "\n")
            y = readline("Whether to start the next round? Y:Yes N:No")
            if (y == "Y") {
                for (p in c(1:length(sc))) {
                  counter[p] = df[p, 2]
                  if (counter[p] <= 0) {
                    cat(player[p], "bankrupted", "\n")
                  }
                }
                if (sum(counter <= 0) >= 1) {
                  cat("End Game", "\n")
                  break
                }
            }
            else {
                cat("End Game", "\n")
                break
            }
        }
    }
  }

somnustp6/mypokergames documentation built on Dec. 23, 2021, 3:32 a.m.