# How to tell the NPC what hand it has?

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<52
card = randNum()
if isUsed(card) <> "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
strDeck = strDeck & translateCard(trueDeck(51)) 'to get rid of last comma

wscript.echo ""
wscript.echo "Shuffled Deck"
wscript.echo "-------------"
wscript.echo strDeck
end Function

function showdefDeck()
for i=0 to 51
wscript.echo i & ". " & defaultdeck(i)

'   strDeck = strDeck & truedeck(i) & ", "
next
'strDeck = strDeck & trueDeck(51) 'to get rid of last comma

'   wscript.echo strDeck
end Function

playa = numPlayers-1
carda = numCards-1

Dim hands()
ReDim Hands(playa,carda)
dim myHand()
reDim myHand(carda)

function Dealcards()
totalNumCards = (numPlayers * numCards)
tnc =0
i = 0
do while tnc < totalnumCards
for c=0 to (numCards - 1)
for p=0 to (numPlayers -1)

Hands(p,c) = trueDeck(i)

'debug line
'wscript.echo truedeck(i) & " dealt to player " & p+1 & "."

i=i+1
tnc = tnc+1
next
next

loop

'   objStdOut.Write "Dealing."
'   jsleep(1)
'   objStdOut.Write "."
'   jsleep(1)
'   objStdOut.Write "."
'   jsleep(1)
'   objStdOut.Write "Dealt."
objStdOut.WriteBlankLines(2)
jsleep(1)

End function

function showHand(h)
if (h = intMyHand) then
specialString = "*(You)* "
else
specialString = ""
end if

wscript.echo specialString & "Player " & h+1 & "'s hand:"

for i=0 to (numCards - 1)
theCard = translatecard(Hands(h, i))
wscript.echo theCard
next
wscript.echo ""
end Function

function showSortedHand(t)

if (t = intMyHand) then
specialString = "*(You)* "
else
specialString = ""
end if

wscript.echo specialString & "Player " & t+1 & "'s hand:"

theSortedHand = getHand(t)

for each card in theSortedHand
transCard = translateCard(card)
wscript.echo transCard
next

end Function

function showMyHand()
showHand(intMyHand)
end Function

function showAllHands()
for q=0 to playa
showHand(q)
next
end Function

function jsleep (seconds)

wscript.sleep seconds*1000
end function

'sort method

function SortArray(arrShort)
dim i, j, temp
for i = UBound(arrShort) - 1 To 0 Step -1
for j= 0 to i
if arrShort(j)>arrShort(j+1) then
temp=arrShort(j+1)
arrShort(j+1)=arrShort(j)
arrShort(j)=temp
end if
next
next
SortArray = arrShort
end function

function getHand(x)
purple = numCards-1

dim a()
redim a(purple)

for i=0 to (numCards-1)
a(i) = Hands(x, i)
next

sortArray a

getHand = a

end function

objstdOUt.Write "Welcome to PokerGame1.0"

objStdOut.WriteBlankLines(1)
objStdOut.Write "-----------------------"
objStdOut.WriteBlankLines(2)

'showdefdeck

shuffleDeck
showdeck

dealCards

'showMyHand
showAllHands

showSortedHand(0)

• As fun as this was, I'm switching to Python. – Jeff Feb 7 '13 at 14:29
• please post the Python code here when you have it working. I will take a look at this and maybe add a tag or two to get some more attention – Malachi Nov 18 '13 at 22:20

You're creating your cards with a nested 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 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).

• True, the OP seems to have no particular naming convention for variables. I particularly like purple which temporarily stores a number. – Daniel Dec 12 '13 at 16:53
• If you could think of a better color to store numbers, I'm all ears. – Jeff Feb 5 '15 at 13:51

In addition to @retailcoder's good feedback, here are a few points:

1. Option Explicit as your first line is considered a best practice for VBScript. It will require you to declare all of your variables, which will assure you don't accidentally use purpel when you meant purple

2. In general your names are inconsistent or simply odd. playa = numPlayers - 1 - weird, jsleep What's the j?, and purple - fun but meaningless.

3. Your shuffledeck method is a potential issue. In the extremely unlikely case your do loop would seemlingly hang due to repeatedly returning the same random number as isUsed(card) <> "Y" would keep being false. Something like populating trueDeck the same as defaultDeck then looping through swaping indexes might suit. Here's an example of what I mean:

'set up trueDeck prior to this
for i= 0 to 51
card = randNum()
temp = trueDeck(card)
trueDeck(card) = i
trueDeck(i) = temp
loop