Forum Navigation
You need to log in to create posts and topics.

GameplayKit Card Shuffler

Posted by: kashmidheiser <kashmidheiser@...>

This demo uses CocoaUI GameplayKit's:


   fn GKRandomSourceArrayByShufflingObjectsInArray

function to simulate shuffling a deck of cards and drawing five random cards. The code can easily be adapted to dealing any kind of card hands.


When the app is launched, the code parses a  2560x1160 open source Wikipedia image of a card deck found at:


and stores an array of ImageRefs for each parsed card as an app property. (The Wikimedia Commons image has an ugly green background which I easily removed in Affinity Photo.) I've attached a copy of my modified image, but it may not come through at the correct resolution. If you need a copy of the modified image, contact me back channel. Note: The image is renamed "card_deck.png" in the demo.

An interesting thing about the GamplayKit function, is that its use is not necessarily limited to games. It's useful any time an array needs to be randomly assorted.

Hope you enjoy.

Ken

output file "GameplayKit Card Shuffler"

include "Tlbx GameplayKit.incl"
include resources "card_deck.png"

_window = 1
begin enum output 1
_card1
_card2
_card3
_card4
_card5
_shuffleBtn
end enum


void local fn BuildWindow
'~'1
CGRect     r
NSUInteger i

r = fn CGRectMake( 0, 0, 730, 240 )
window _window, @"GameplayKit Card Shuffler", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizable

r = fn CGRectMake( 20, 60, 130, 160 )
for i = _card1 to _card5
imageview i, YES, ,r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFrameGrayBezel, _window
r = fn CGRectOffset( r, 140, 0 )
next

r = fn CGRectMake( 585, 15, 130, 26 )
button _shuffleBtn,,, @"Shuffle cards", r
end fn


local fn CropImage( image as ImageRef, moveRight as CGFloat, moveUp as CGFloat, croppedRect as CGRect ) as ImageRef
'~'1
ImageRef croppedImage
CGRect   originalImageRect

originalImageRect = fn CGRectMake( moveRight, moveUp, 411, 295 )
croppedImage      = fn ImageWithSize( fn CGSizeMake(  212, 295 ) )

ImageLockFocus( croppedImage )
ImageDrawInRectFromRect( image, croppedRect, originalImageRect, NSCompositeCopy, 1.0 )
ImageUnlockFocus( croppedImage )
end fn = croppedImage


// Load array of card ImageRefs on launch
void local fn ProcessImagesFromMaster
'~'1
ImageRef   image, croppedImage
NSUInteger i, cardCounter
CGFloat    moveRight, moveUp
CFMutableArrayRef cardArr

_rightIncrement = 196
_upIncrement    = 287

cardArr = fn MutableArrayWithCapacity( 0 )
image = fn ImageNamed( @"card_deck.png" )

moveRight = 0 : moveUp = 0 : cardCounter = 1
for i = 1 to 52
if ( cardCounter == 14 )
cardCounter = 1
moveRight   = 0
moveUp      = moveUp + _upIncrement
end if
croppedImage = fn CropImage( image, moveRight, moveUp, fn CGRectMake( -1, -1, 427, 305 ) )
moveRight = moveRight + _rightIncrement
cardCounter++
MutableArrayAddObject( cardArr, (CFTypeRef)croppedImage )
next
AppSetProperty( @"Cards", cardArr )
end fn


void local fn ShuffleCards
'~'1
CFArrayRef shuffledCardsArr
NSUInteger i
ImageRef   image

shuffledCardsArr = fn GKRandomSourceArrayByShufflingObjectsInArray( fn GKRandomSourceInit, fn AppProperty( @"Cards" ) )
for i = 1 to 5
image = fn ArrayObjectAtIndex( shuffledCardsArr, i )
ImageViewSetImage( i, image )
next
end fn


void local fn DoDialog( ev as long, tag as long, wnd as long )
'~'1
select ( ev )
case _btnClick
select ( tag )
case _shuffleBtn : fn ShuffleCards
end select
end select
end fn

on dialog fn DoDialog
fn ProcessImagesFromMaster
fn BuildWindow
fn ShuffleCards

HandleEvents






--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@freegroups.net To access the list archives, go to: http://freegroups.net/groups/futurebasic/