00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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;
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;
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
00057
00058 for (TInt i=0; i<=deckLength; i=i+KCardDesLength)
00059 {
00060 while (oldNum)
00061 {
00062
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)
00069 random++;
00070
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
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
00094 aCards = tempBuffer;
00095 tempBuffer.FillZ();
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
00112
00113
00114 ShuffleCards(aCards);
00115 TInt handLength = (deckLength) / playerCount;
00116 TReal remainder;
00117 Math::Mod(remainder, deckLength, playerCount);
00118
00119
00120
00121
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
00130 size = handLength*2 + 2;
00131
00132 iSocket.SendTo(aCards.Mid(startPoint,size), iRemoteNames[i], 0, status);
00133 User::WaitForRequest(status);
00134 User::LeaveIfError(status.Int());
00135 startPoint = size;
00136 remainder--;
00137 }
00138 else
00139 {
00140
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 }