diff -r 89d6a7a84779 -r 25a17d01db0c Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/cardgamedealer_8cpp-source.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/cardgamedealer_8cpp-source.html Fri Jan 22 18:26:19 2010 +0000 @@ -0,0 +1,141 @@ + +
+00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). +00002 // All rights reserved. +00003 // This component and the accompanying materials are made available +00004 // under the terms of "Eclipse Public License v1.0" +00005 // which accompanies this distribution, and is available +00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". +00007 // +00008 // Initial Contributors: +00009 // Nokia Corporation - initial contribution. +00010 // +00011 // Contributors: +00012 // +00013 // Description: +00014 // +00015 +00016 #include <e32base.h> +00017 #include <e32math.h> +00018 +00019 #include "cardgamedealer.h" +00020 #include "cardgamebase.h" +00021 +00022 const TInt KCardDesLength = 2; // Descriptor length for a single card +00023 +00027 CCardGameDealer::~CCardGameDealer() +00028 { +00029 } +00030 +00034 void CCardGameDealer::ConstructL(RArray<TInetAddr>& aRemoteNames, RSocket& aSocket) +00035 { +00036 iRemoteNames = aRemoteNames; +00037 CCardGameBase::ConstructL(aSocket); +00038 } +00039 +00047 void CCardGameDealer::ShuffleCards(TDesC8& aCards) +00048 { +00049 RArray<TInt> usedNumbers;// Array of used positions +00050 TBool oldNum = ETrue; +00051 TPtrC8 ptr; +00052 TInt random = 0; +00053 const TInt deckLength = aCards.Length(); +00054 RBuf8 tempBuffer; +00055 tempBuffer.Create(deckLength); +00056 //Take first card (2chars) and put it into tempBuffer in random position +00057 // make a note of this pos as we can use it again. or just check if empty +00058 for (TInt i=0; i<=deckLength; i=i+KCardDesLength) +00059 { +00060 while (oldNum) +00061 { +00062 //generate random number between 0 and 96 +00063 random = Math::Random() % deckLength-KCardDesLength; +00064 if (random <= deckLength-KCardDesLength && random >= 0) +00065 { +00066 TReal remainder; +00067 Math::Mod(remainder,random,KCardDesLength); +00068 if (remainder)// if true then we have an odd , make it into an even. +00069 random++; +00070 // now we have an even number check that we haven't used it before +00071 oldNum = EFalse; +00072 for (TInt j=0; j<usedNumbers.Count();j++) +00073 { +00074 if (random == usedNumbers[j]) +00075 oldNum = ETrue; +00076 } +00077 +00078 if (!oldNum) +00079 { +00080 // We have a card that we haven't used before +00081 usedNumbers.Append(random); +00082 } +00083 } +00084 +00085 } +00086 +00087 +00088 ptr.Set(aCards.Mid(random,KCardDesLength)); +00089 tempBuffer.Append(ptr); +00090 oldNum = ETrue; +00091 } +00092 +00093 //finished shuffle, now copy into full deck. +00094 aCards = tempBuffer; +00095 tempBuffer.FillZ();// clear the temp. buffer +00096 usedNumbers.Close(); +00097 tempBuffer.Close(); +00098 } +00099 +00107 void CCardGameDealer::DealCardsL(TDesC8& aCards) +00108 { +00109 TInt deckLength = aCards.Length(); +00110 TInt playerCount = iRemoteNames.Count(); +00111 // playerCount is used to divide deck. +00112 +00113 +00114 ShuffleCards(aCards); +00115 TInt handLength = (deckLength) / playerCount; +00116 TReal remainder; +00117 Math::Mod(remainder, deckLength, playerCount); +00118 // we want to give each player handlength *2 cards +00119 // then n(remainder) players one card extra each +00120 +00121 // send out a message to each player stating the size of recieve buffer they will need +00122 TRequestStatus status; +00123 TInt size; +00124 TInt startPoint = 0; +00125 for (TInt i=0;i<iRemoteNames.Count();i++) +00126 { +00127 if (remainder > 0) +00128 { +00129 // we want to set recieve buffer to handLength*2 + 2 +00130 size = handLength*2 + 2; +00131 // Send pointer to portion of the deck +00132 iSocket.SendTo(aCards.Mid(startPoint,size), iRemoteNames[i], 0, status); +00133 User::WaitForRequest(status); +00134 User::LeaveIfError(status.Int()); +00135 startPoint = size;// change start point to end of last hand sent out +00136 remainder--; +00137 } +00138 else +00139 { +00140 //we want to set the players recieve buffer to handlength * 2 +00141 size = handLength*2; +00142 iSocket.SendTo(aCards.Mid(startPoint,size), iRemoteNames[i], 0, status); +00143 User::WaitForRequest(status); +00144 User::LeaveIfError(status.Int()); +00145 startPoint = size+startPoint; +00146 } +00147 } +00148 +00149 } +