ftp.nice.ch/Attic/openStep/games/Solitaire.3.1.s.tgz#/Solitaire.3.1/Games/FortyThieves/FortyThieves.m

This is FortyThieves.m in view mode; [Download] [Up]

/* indent:4  tabsize:8  font:fixed-width */

#import "FortyThieves.h"
#import "localstrings.h"
#import "FTDrawDelegate.h"
#import "FTSuitDelegate.h"
#import "FTGameDelegate.h"
#import "FTAlertPanel.h"

static id _fortyThievesSharedInstance = nil;


@implementation FortyThieves

+ (FortyThieves*) sharedInstance
/*"
 Instead of the old method of calling SolGameController when
 you wanted to message the FortyThieves game module, you now
 call [FortyThieves sharedInstance] and direct messages to
 the returned instance. You can use this to check if the game
 has been won, among other things.
"*/
{
    return _fortyThievesSharedInstance;
}

- initFromBundle:(NSBundle*)aBundle withName:(NSString*)name
/*"
 Extends our superclass method in order to set our shared
 instance variable.
"*/
{
    [super initFromBundle:aBundle withName:name];

    // This is used instead of having a global SolGameController()
    // function. It is assumed that this method will be called
    // only once and that our +sharedInstance method will return
    // the single instance.
    _fortyThievesSharedInstance = self;

    return self;
}

/*---------------------------------------------------------------------------
|
|    - startGame:
|
|----------------------------------------------------------------------------
|
|    Start a new game.  Get confirmation from the user before aborting a 
|    game in progress.
|			
\----------------------------------------------------------------------------*/

- (void) startGame:sender
{
    [super startGame:sender];
    [self setupGame:YES];
}


/*---------------------------------------------------------------------------
|
|    - restartGame:
|
|----------------------------------------------------------------------------
|
|    Restart the game in progress.
|			
\----------------------------------------------------------------------------*/

- (void) restartGame:sender
{
    [super restartGame:sender];
    [self setupGame:NO];
}


/*---------------------------------------------------------------------------
|
|    - setupGame:(BOOL)redeal
|
|----------------------------------------------------------------------------
|
|    Setup a new game.  If "redeal" is true, deal a new deck, otherwise
|    use the same cards as the previous game.
|			
\----------------------------------------------------------------------------*/

- (void) setupGame:(BOOL)redeal
{
    int pileIndex, i;
    int cycle;
    CardPileView*  suitCardPiles[8];
    CardPileView*  gameCardPiles[10];
    CardPile*  drawCardPile;

    [gameWindow disableFlushWindow];

    //----------------------------------------------------------------
    //    Sync pile id's with current game window and set 
    //    preferences and delegates
    //----------------------------------------------------------------
    
    suitCardPiles[0] = suitPileView1;
    suitCardPiles[1] = suitPileView2;
    suitCardPiles[2] = suitPileView3;
    suitCardPiles[3] = suitPileView4;
    suitCardPiles[4] = suitPileView5;
    suitCardPiles[5] = suitPileView6;
    suitCardPiles[6] = suitPileView7;
    suitCardPiles[7] = suitPileView8;

    for (i = 0; i < 8; i++)
    {
        [suitCardPiles[i] setBackgroundColor:desktopColor];
        [suitCardPiles[i] setCardSize:cardSize];
        [suitCardPiles[i] setDelegate:suitPileDelegate];
    }
    [gamePileDelegate setSuitCardPileViews:suitCardPiles];
    [discardPileDelegate setSuitCardPileViews:suitCardPiles];
    [discardPileView setBackgroundColor:desktopColor];
    [discardPileView setCardSize:cardSize];
    [discardPileView setDelegate:discardPileDelegate];
    
    gameCardPiles[0] = gamePileView1;
    gameCardPiles[1] = gamePileView2;
    gameCardPiles[2] = gamePileView3;
    gameCardPiles[3] = gamePileView4;
    gameCardPiles[4] = gamePileView5;
    gameCardPiles[5] = gamePileView6;
    gameCardPiles[6] = gamePileView7;
    gameCardPiles[7] = gamePileView8;
    gameCardPiles[8] = gamePileView9;
    gameCardPiles[9] = gamePileView10;
    for (i = 0; i < 10; i++)
    {
        [gameCardPiles[i] setBackgroundColor:desktopColor];
        [gameCardPiles[i] setCardSize:cardSize];
        [gameCardPiles[i] setDelegate:gamePileDelegate];
    }
    
    [drawPileView setBackgroundColor:desktopColor];
    [drawPileView setCardSize:cardSize];
    drawCardPile = [drawPileView cardPile];
    [drawPileDelegate setDiscardCardPileView:discardPileView];
    [drawPileView setDelegate:drawPileDelegate];
    
    [gameWindow display];
    [gameWindow enableFlushWindow];
    [gameWindow flushWindow];

    //-----------------------------------------------------------------------
    //    Initialize the drawPileView to have a 2 shuffled decks.
    //-----------------------------------------------------------------------
    
    [drawCardPile empty];
    if (redeal)
    {
        [drawCardPile addDeck];
        [drawCardPile addDeck];
	[drawCardPile shuffle];
	
	// make a copy of the CardPile for restart option
	if (!prevDeck)
	{
	    prevDeck = [[CardPile allocWithZone:[self zone]]
	                           initForCardSize:cardSize];
	}
	else
	{
	    [prevDeck empty];
	    [prevDeck setCardSize:cardSize];
	}
	[prevDeck addCopyOfPile:drawCardPile];
    }
    else
    {
        if (prevDeck)
	{
	    // copy the saved deck back to the game deck
	    [prevDeck setCardSize:cardSize];
	    [drawCardPile addCopyOfPile:prevDeck];
	}
	else
	{
	    // this shouldn't happen, but just in case...
        [drawCardPile empty];
	    [drawCardPile addDeck];
	    [drawCardPile addDeck];
	    [drawCardPile shuffle];
	}
    }
    
    //-----------------------------------------------------------------------
    //    Initialize the "suit piles" and discard pile as empty
    //-----------------------------------------------------------------------

    [[discardPileView cardPile] empty];
    [discardPileView display];
    
    for (pileIndex = 0; pileIndex < 8; pileIndex++)
    {
        [[suitCardPiles[pileIndex] cardPile] empty];
        [suitCardPiles[pileIndex] display];
    }

    //-----------------------------------------------------------------------
    //    Initialize and deal cards to the 10 "user piles"
    //-----------------------------------------------------------------------

    for (pileIndex = 0; pileIndex < 10; pileIndex++)
    {
        [[gameCardPiles[pileIndex] cardPile] empty];
    }
    
    for (cycle = 0; cycle < 4; cycle++)
    {
	for (pileIndex = 0; pileIndex < 10; pileIndex++)
	{
	    CardPile* 	userPile = [gameCardPiles[pileIndex] cardPile];
	    Card* 		tempCard = [drawCardPile dealTopCard];
	    [userPile insertCard:tempCard at:CS_TOP];
	    [[userPile topCard] flip];
	    [gameCardPiles[pileIndex] display];
	}
    }

    [gameWindow display];
    [gameWindow makeKeyAndOrderFront:self]; 
}


/*---------------------------------------------------------------------------
|
|    - endGame:sender
|
|----------------------------------------------------------------------------
|
|    End the game in progress.  Discard the game window.
|			
\----------------------------------------------------------------------------*/

- (void) endGame:sender
{
    // close the game window
    [super endGame:sender];

    // ****custom code here****
}

/*---------------------------------------------------------------------------
|
|    - win
|
|----------------------------------------------------------------------------
|
|    Called when the game has been won.  This is where you can insert fancy
|    winning routines, or just call the default (boring) routine.
|
\----------------------------------------------------------------------------*/

- (void) win
{
    [super win];  // replace this with something wonderful
}


/*---------------------------------------------------------------------------
|
|    - checkForWin
|
|----------------------------------------------------------------------------
|
|    Called to check the state of the game.  Always override (unless your
|    game is impossible to win).
|
\----------------------------------------------------------------------------*/

- (void) checkForWin
{
    if ([[suitPileView1 cardPile] cardCount] +
            [[suitPileView2 cardPile] cardCount] +
            [[suitPileView3 cardPile] cardCount] +
            [[suitPileView4 cardPile] cardCount] +
	    [[suitPileView5 cardPile] cardCount] +
	    [[suitPileView6 cardPile] cardCount] +
	    [[suitPileView7 cardPile] cardCount] +
	    [[suitPileView8 cardPile] cardCount] == 104)
    {
        [self win];
    }
}


- (void) peekAtNextCard:sender
{
    CardPile* peekCardPile = [peekCardView cardPile];
    Card* nextCard = [[drawPileView cardPile] topCard];
    Card* peekCard;
    
    if(nextCard != nil){
        peekCard = [[SmallCard allocWithZone:[self zone]] initSuit:[nextCard suit] value:[nextCard value] faceUp:YES];
        [peekCardView setDrawOutline:NO];
        [peekCardView setBackgroundColor:[NSColor lightGrayColor]];
        [peekCardPile empty];
        [peekCardPile addCard:peekCard];
        [peekCardView display];
        [peekPanel run:self];
    }
    else{
        NSRunAlertPanel(LOCALIZED_GAME_NAME, LOCALIZED_NO_STOCK_CARD, @"OK", nil, nil);
    } 
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.