When we left off at the end of session 01 I said we would look at
- New Game Control
- Controlling the Number of Players
- Shuffling the Deck.
If the lesson does not get too long I will review the animation of dealing a card as well.
I created this short video to show where we are so far and it shows the animation of the card being dealt. Watch it if you want to see the game in action.
If you watched the video you saw that the New Game control actually runs all three of the subroutines we will be reviewing. First here is the onPush event code for New Game.
'Date Created: 23-Mar-2016 10:14:25 AM
'Last Updated: 23-Mar-2016 11:11:08 AM
'Created By : NLaws
'Updated By : NLaws
parentform.commit()
text35.text = "Building New Deck..."
t = table.open("tc_gamedeck")
t.zap(.t.)
t.close()
xretry:
dim i as N
dim ii as N
dim xSuit as C
dim xPosInDeck as N
dim prmpt_title as c
dim prmpt_prompt as c
dim prmpt_default as c
prmpt_title = "Number of Players"
prmpt_prompt = "Enter the Number of Players 1 to 4"
prmpt_default = "1"
Dim SHARED xNbrofPlayers as N
xNbrofPlayers = ui_get_number(prmpt_title,prmpt_prompt,prmpt_default)
xbasic_wait_for_idle()
text35.text = text35.text + crlf() + "Setting up " + Alltrim(Str(xNbrofPlayers)) + "Players ..."
DIM SHARED xDecks AS N
xDecks = xNbrofPlayers
gt = table.open("tc_gametable")
gt.fetch_first()
gt.change_begin()
gt.nbrofplayers = xNbrofPlayers
gt.player1bank = 100.00
gt.player1score = 0
gt.dealerbank = 1000.00
gt.dealerscore = 0
gt.nbrofdecks = xDecks
if xNbrofPlayers = 2 then
gt.player2bank = 100.00
gt.player2score = 0
gt.dealerbank = 2000.00
else if xNbrofPlayers = 3 then
gt.player3bank = 100.00
gt.player3score = 0
gt.dealerbank = 3000.00
else if xNbrofPlayers = 4 then
gt.player4bank = 100.00
gt.player4score = 0
gt.dealerbank = 4000.00
end if
gt.change_end(.t.)
gt.close()
xbasic_wait_for_idle()
text35.text = text35.text + crlf() + "Building Initial Deck ..."
t = table.open("tc_gamedeck")
for i = 1 to 4
if i = 1 then
xSuit = "Hearts"
xPosInDeck = 0
else if i = 2 then
xSuit = "Clubs"
xPosInDeck = 13
else if i = 3 then
xSuit = "Diamonds"
xPosInDeck = 26
else
xSuit = "Spades"
xPosInDeck = 39
end if
for ii = 1 to 13
if ii = 1 then
t.enter_begin()
t.suits = xSuit
t.cards = "Ace"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 2 then
t.enter_begin()
t.suits = xSuit
t.cards = "Two"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 3 then
t.enter_begin()
t.suits = xSuit
t.cards = "Three"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 4 then
t.enter_begin()
t.suits = xSuit
t.cards = "Four"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 5 then
t.enter_begin()
t.suits = xSuit
t.cards = "Five"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 6 then
t.enter_begin()
t.suits = xSuit
t.cards = "Six"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 7 then
t.enter_begin()
t.suits = xSuit
t.cards = "Seven"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 8 then
t.enter_begin()
t.suits = xSuit
t.cards = "Eight"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 9 then
t.enter_begin()
t.suits = xSuit
t.cards = "Nine"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 10 then
t.enter_begin()
t.suits = xSuit
t.cards = "Ten"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 11 then
t.enter_begin()
t.suits = xSuit
t.cards = "Jack"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 12 then
t.enter_begin()
t.suits = xSuit
t.cards = "Queen"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
else if ii = 13 then
t.enter_begin()
t.suits = xSuit
t.cards = "King"
if ii <= 10 then
t.cardvalue = ii
else
t.cardvalue = 10
end if
t.nbrofdecks = xDecks
t.cardstoplay = xDecks
t.positionindeck = ii + xPosInDeck
t.shuffled = .f.
t.enter_end(.t.)
end if
Next ii
Next i
t.close()
xbasic_wait_for_idle()
text35.text = text35.text + crlf() + "Preparing to Shuffel " + Alltrim(Str(xNbrofPlayers)) + "Decks ..."
script_play("ShuffleDeck")
xbasic_wait_for_idle()
script_play("AssingPlayers")
xbasic_wait_for_idle()
text35.text = text35.text + crlf() + "Done Let's Play ..."
Our code starts by zapping our game deck. Then we build our game table and set all values back to a starting point. Next we build a new deck of cards just as if we real new deck of playing cards and removed them form the box. We create our deck in the order of a real deck and number the position of each card in the deck. As stated in the video, we know card 4 would be the 3 of hearts since the order of suits is
- Hearts
- Clubs
- Diamonds
- Spades
and the order of cards in each suit is Ace to King. This would make the fourth card the three of hearts. We also know the face value of each card. One through ten is the value of the card (Ace being One) and Jack through King is also Ten.
Now I know the Ace can be eleven as well if paired with a ten or higher. We will use the in game code to address that variable when it arises.
Building the game deck was the focus of our last lesson so I am not going to repeat it here. You just need to know that the number of decks is proportionate to the number of players. Once the game deck is created we then shuffle the deck. Here is the code for the shuffleDeck script.
'Date Created: 14-Mar-2016 10:55:02 AM
'Last Updated: 23-Mar-2016 11:02:47 AM
'Created By : NLaws
'Updated By : NLaws
dim deckshuffel as N
dim cardshuffel as N
Dim newOrder as N
newOrder = 1
a_tbl = table.open("tc_gamedeck")
update.fields = 1
update.field1 = "SHUFFLED"
update.expr1 = ".f."
a_tbl.update()
a_tbl.close()
xbasic_wait_for_idle()
if val(time("m")) > 52 then
deckshuffel = val(time("m"))-6
else
deckshuffel = val(time("m"))
end if
if deckshuffel = r_nbr then
deckshuffel = r_nbr + 3
end if
r_nbr = deckshuffel
xbasic_wait_for_idle()
text35.text = text35.text + crlf() + "Shuffeling Deck " + Alltrim(Str(deckshuffel)) + " times ..."
cardshuffel = round(deckshuffel,0)
t = table.open("tc_gamedeck")
shuffelagain:
t.fetch_first()
t.batch_begin()
while .not. t.fetch_eof()
if t.shuffled = .f. then
if t.recno() = cardshuffel then
t.change_begin()
t.positionindeck = newOrder
t.shuffled = .t.
t.change_end(.t.)
newOrder = newOrder + 1
cardshuffel = cardshuffel + deckshuffel
end if
else
cardshuffel = cardshuffel + deckshuffel
end if
t.fetch_next()
end while
t.batch_end()
deckshuffel = deckshuffel - 1
cardshuffel = deckshuffel
xbasic_wait_for_idle()
if deckshuffel > 0 then
goto shuffelagain
end if
t.close()
xbasic_wait_for_idle()
parentform.Refresh_Layout()
xbasic_wait_for_idle()
text35.text = text35.text + crlf() + "Thanks for waiting ... Lets Play!"
End
We start by updating the field Shuffled to .f. . Next we use the time function with the m parameter to return the current minute in the hour. If the minute is greater than 52 we subtract 8 to insure the number of shuffle’s is within the value of number of cards in the deck. Now we are creating a loop and passing through our table assigning a new position in the deck. Once assigned we mark the record as shuffled then move on to the next record in the table. This process repeats until deckshuffel equals 0. (I know I transposed the e and l in the word shuffle. ) Let me give you and example.
- If the minute is 8 then we open the table using table.open() and fetch the first record in the table.
- If the record number equals our variable cardshuffel (8) then the position in deck becomes NewOrder (1) and shuffled becomes .t..
- We then increment NewOrder up by one so the next card found will become position 2.
- Then we add deckshuffel to cardshuffel to get the next card to look for. (8 + 8 = 16) and we continue to fetch through the table until we find record 16 then we repeat the process.
- Once we have reached the end of the table we reduce deckshuffel by one and if it is greater than 0 we go to our label shuffelagain and repeat steps one through 5 only we start with record 7 then 6 then 5 etc. Once deckshuffel = 0 the loop ends.
I know this is an unorthodox method for shuffling a deck but the rand function always creates random numbers in the same sequence. This means you would have to test each value generated against the previous values generated to insure no two cards would have the same place in the deck. This can be done but not as easily as I did above. With that said, you are free to create your own random shuffle as you see fit.
The final thing our new game code does is set up the number of player positions on the board. Here is the code for the AssignPlayers script.
if xNbrofPlayers = 2 then button3.Show() button4.show() button14.Show() else button3.Hide() button4.Hide() button14.Hide() end if
Currently it only controls player 2 but 3 and 4 would be the same expect the object names would differ.
Well we have covered a fair amount so we will save the animation code for the next session. I hope you all found this interesting and look forward to you stopping by again.
Leave a comment