Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

carddeck.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #include <stdlib.h>
00021 #include <time.h>
00022 #include "card.h"
00023 #include "carddeck.h"
00024 
00025 
00026 CardDeck::CardDeck(int jokers, int numOfDecks) : numberOfJokers(jokers), numberOfDecks(numOfDecks), deckCreated(FALSE)
00027 {
00028     cards = new Card *[getNumberOfCards()];
00029 }
00030 
00031 
00032 CardDeck::~CardDeck()
00033 {
00034     for (int i = 0; i < getNumberOfCards(); i++)
00035         delete cards[i];
00036     delete cards;
00037 }
00038 
00039 
00040 void CardDeck::createDeck()
00041 {
00042     if (!deckCreated) {
00043         for (int j = 0; j < getNumberOfDecks(); j++) {
00044             for (int i = 0; i < 52; i++) {
00045                 cards[i+j*52] = newCard( (eValue)((i % 13) + 1), (eSuit)((i / 13) + 1), FALSE);
00046                 cards[i+j*52]->setDeckNumber(j);
00047             }
00048         }
00049         for (int i = 0; i < getNumberOfJokers(); i++)
00050             cards[52*getNumberOfDecks() + i] = newCard( jokerVal, jokerSuit, FALSE);
00051         deckCreated = TRUE;
00052     }
00053 }
00054 
00055 
00056 void CardDeck::shuffle()
00057 {
00058     srand(time(NULL));
00059     for (int i = 0; i < getNumberOfCards(); i++) {
00060         int index = rand() % getNumberOfCards();
00061         Card *tmpCard = cards[i];
00062         cards[i] = cards[index];
00063         cards[index] = tmpCard;
00064     }
00065 }
00066 
00067 
00068 int CardDeck::getNumberOfCards()
00069 {
00070     return 52*getNumberOfDecks() + getNumberOfJokers();
00071 }
00072 
00073 
00074 int CardDeck::getNumberOfDecks()
00075 {
00076     return numberOfDecks;
00077 }
00078     
00079 
00080 int CardDeck::getNumberOfJokers()
00081 {
00082     return numberOfJokers;
00083 }
00084     
00085 
00086 Card *CardDeck::newCard( eValue v, eSuit s, bool f)
00087 {
00088     return new Card(v, s, f);
00089 }
00090 
00091 

Generated on Sat Nov 5 16:17:25 2005 for OPIE by  doxygen 1.4.2