snippetMinor
How to tell the NPC what hand it has?
Viewed 0 times
handthewhatnpctellhashow
Problem
Posted my question here.
Here is the full code:
```
'buy a blank deck of cards
'declare variables
dim defaultDeck(51)
dim trueDeck(51)
dim isUsed(51)
numPlayers = 1
numCards = 12
intMyHand = 0 '0 through numPlayers-1... highest number is dealer
Set objStdOut = WScript.StdOut
'put values on the blank cards:
i=0
for suit=0 to 3
for faceValue=0 to 12
select case suit
Case 0
strSuit = chr(3)
case 1
strSuit = chr(4)
case 2
strSuit = chr(5)
case 3
strSuit = chr(6)
case else
strSuit = "SuitNotFound"
end Select
face = faceValue+1
select case face
case 11
face = "J"
case 12
face = "Q"
case 13
face = "K"
case 1
face = "A"
case default
face=face
end select
defaultDeck(i) = face & strSuit
i = i+1
next
Next
function translateCard(number)
translateCard = defaultDeck(number)
end function
'shuffle the deck
function randNum()
max = 51
min = 0
Randomize
randNum = Int((max-min+1)*Rnd+min)
end Function
function shuffleDeck()
'reset from last shuffle
y=0
for i=0 to 51
isUsed(i) ="n"
next
'draw a card and put it in the deck
do while y "Y" then
isUsed(card) = "Y"
trueDeck(y) = card
y = y+1
end if
loop
' objStdOut.Write "Shuffling."
' jsleep(1)
' objStdOut.Write "."
' jsleep(1)
' objStdOut.Write "."
' jsleep(1)
' objStdOut.Write "."
' jsleep(1)
' objStdOut.Write "Shuffled."
objStdOut.WriteBlankLines(1)
jsleep(1)
end Function
function showDeck()
for i=0 to 50
'wscript.echo i & ". " & truedeck(i)
strDeck = strDeck & translateCard(truedeck(i)) & ","
next
strDe
Here is the full code:
```
'buy a blank deck of cards
'declare variables
dim defaultDeck(51)
dim trueDeck(51)
dim isUsed(51)
numPlayers = 1
numCards = 12
intMyHand = 0 '0 through numPlayers-1... highest number is dealer
Set objStdOut = WScript.StdOut
'put values on the blank cards:
i=0
for suit=0 to 3
for faceValue=0 to 12
select case suit
Case 0
strSuit = chr(3)
case 1
strSuit = chr(4)
case 2
strSuit = chr(5)
case 3
strSuit = chr(6)
case else
strSuit = "SuitNotFound"
end Select
face = faceValue+1
select case face
case 11
face = "J"
case 12
face = "Q"
case 13
face = "K"
case 1
face = "A"
case default
face=face
end select
defaultDeck(i) = face & strSuit
i = i+1
next
Next
function translateCard(number)
translateCard = defaultDeck(number)
end function
'shuffle the deck
function randNum()
max = 51
min = 0
Randomize
randNum = Int((max-min+1)*Rnd+min)
end Function
function shuffleDeck()
'reset from last shuffle
y=0
for i=0 to 51
isUsed(i) ="n"
next
'draw a card and put it in the deck
do while y "Y" then
isUsed(card) = "Y"
trueDeck(y) = card
y = y+1
end if
loop
' objStdOut.Write "Shuffling."
' jsleep(1)
' objStdOut.Write "."
' jsleep(1)
' objStdOut.Write "."
' jsleep(1)
' objStdOut.Write "."
' jsleep(1)
' objStdOut.Write "Shuffled."
objStdOut.WriteBlankLines(1)
jsleep(1)
end Function
function showDeck()
for i=0 to 50
'wscript.echo i & ". " & truedeck(i)
strDeck = strDeck & translateCard(truedeck(i)) & ","
next
strDe
Solution
You're creating your cards with a nested
This whole
Naming-wise, I don't understand how you can name a variable
Your Ace is worth 1; if you're playing Poker the Ace can be either 1 or 14, depending on the hand.
Lastly, in VBScript/VBA/VB6, you should always use the zero-footprint
For loop; one over suit, another over faceValue - I'd do the same (or quite similar). However I'm not buying the Select Case part:for suit=0 to 3
for faceValue=0 to 12
select case suit
Case 0
strSuit = chr(3)
case 1
strSuit = chr(4)
case 2
strSuit = chr(5)
case 3
strSuit = chr(6)
case else
strSuit = "SuitNotFound"
end Select
...This whole
select case suit could be replaced with strSuit = chr(suit + 3) with a comment that says something like 'chr(3)-chr(6): ♥ ♦ ♣ ♠. The "SuitNotFound" case is a WTF for me.Naming-wise, I don't understand how you can name a variable
suit (good), another faceValue (good) and then have horrible Hungarian notation with strSuit - call it currentSuit or cardSuit or whatever, but please don't prefix with an abbreviation of type's name! [and that's valid for python as well I guess!]Your Ace is worth 1; if you're playing Poker the Ace can be either 1 or 14, depending on the hand.
Lastly, in VBScript/VBA/VB6, you should always use the zero-footprint
vbNullString language constant instead of "" which takes up unnecessary memory (not that memory would be an issue though).Code Snippets
for suit=0 to 3
for faceValue=0 to 12
select case suit
Case 0
strSuit = chr(3)
case 1
strSuit = chr(4)
case 2
strSuit = chr(5)
case 3
strSuit = chr(6)
case else
strSuit = "SuitNotFound"
end Select
...Context
StackExchange Code Review Q#21338, answer score: 3
Revisions (0)
No revisions yet.