cleaned all trailing white space from source files.
[sdk] / samples / games / cards / tongIts / tongits.ec
1 import "ecere"
2
3 #define MCARD(k,n) ((n) * 4 + (k))
4 #define NUMBER(id) ((id) / 4)
5 #define KIND(id)   ((id) % 4)
6 #define OFFSETTER1   15
7 #define OFFSETTER2   120
8 #define HOUSETYPE_STRAIGHT 0
9 #define HOUSETYPE_KIND     1
10
11 typedef struct
12 {
13    int typeOfHouse;
14
15    union
16    {
17       struct straight
18       {
19          int first, last;
20          int suit;
21       } straight;
22       struct kind
23       {
24          int number;
25          bool suits[4];
26       } kind;
27    };
28 } House;
29
30 typedef struct
31 {
32    int numCards;
33    int cardValues[13];
34    House houseDown[4];
35    int numHouseDown;
36    bool cardLifted[13];
37    bool callTag;
38 } Player;
39
40 int cards[52];
41 int discard[28];
42 int discardCounter = 0;
43 int deckCounter = 0;
44 int numOfPlayers = 4;
45 int currentPlayer = 0;
46 int cardToReposition = 0;
47 int xMouseMove=0,yMouseMove=0,xLeftButtonUp=0,yLeftButtonUp=0;
48 int xCursorPositionAtCard=0,yCursorPositionAtCard=0,width=0,height=0;
49 int dragCard = -1;
50 Player player[4];
51 Point xyPositions[4] =
52 {
53    {30,150},
54    {30,500},
55    {600,500},
56    {600,150}
57 };
58
59 bool flagButtonDown, leftDoubleClick, gameOver;
60
61 //player[0].houseDown[0].straight.suit
62
63 void NewDeckOfCards()
64 {
65    int i;
66    for(i=0;i<52;i++)
67    {
68       cards[i]=i;
69    }
70 }
71
72 void Shuffle()
73 {
74    int i,j,randNum;
75    for(i=0;i<52;i++)
76    {
77       randNum = GetRandom(0,51);
78       j=cards[i];
79       cards[i]=cards[randNum];
80       cards[randNum]=j;
81    }
82 }
83
84 void DealCards(int numPlayers)
85 {
86    int c, p;
87
88    player[0].cardValues[player[0].numCards++] = cards[deckCounter++];
89    for(c = 0; c<((numPlayers > 3) ? 9 : 12); c++)
90       for(p = 0; p<numPlayers; p++)
91          player[(p+1)%numPlayers].cardValues[player[(p+1)%numPlayers].numCards++] = cards[deckCounter++];
92    //player[0].houseDown[0].straight.suit
93 }
94
95 int CheckHouse(int cardsToCheck,int * checkIfHouse)
96 {
97    int i,straight=0,house=0;
98    for(i=cardsToCheck-1;i>=0;i--)
99    {
100       if(checkIfHouse[i] == checkIfHouse[i-1] + 4)
101       {
102          straight++;  //possible straight house
103       }
104       else if(NUMBER(checkIfHouse[i]) == NUMBER(checkIfHouse[i-1]))
105       {
106          house++;    //possible 3or4 of a kind house
107       }
108    }
109    if(straight == cardsToCheck-1)
110       return 0;
111    else if(house == cardsToCheck-1)
112       return 1;
113    else
114       return -1;
115 }
116
117 int CompareInteger(int * a, int * b)
118 {
119    if(*a > *b)
120       return 1;
121    else if(*b > *a)
122       return -1;
123    else return 0;
124 }
125
126 void SortPlayerCards()
127 {
128    int c;
129    for(c=0;c<numOfPlayers;c++)
130    {
131       qsort(player[c].cardValues,player[c].numCards,sizeof(int),CompareInteger);
132       //qsort(tempHouse,tempCtr,sizeof(int),CompareInteger);
133    }
134 }
135
136 class TongIts : Window
137 {
138    Bitmap bitmapCards[52];
139    Bitmap cardBack {};
140    Button drawButton[4], chowButton[4], dumpButton[4], callButton[4], showButton[4], doneButton[4];
141
142    hasClose = true;
143    hasMinimize = true;
144    hasMaximize = true;
145    background = Color { 113,156,169 };
146    borderStyle = sizable;
147    text = "Tong-Its";
148    size = Size { 900,700 };
149
150    bool OnCreate()
151    {
152       int c;
153       RandomSeed((int)(GetTime() * 1000));
154       NewDeckOfCards();
155       Shuffle();
156       DealCards(numOfPlayers);
157       SortPlayerCards();
158       for(c = 0; c<numOfPlayers; c++)
159       {
160          drawButton[c] = Button { this, text = "Draw Card", position = xyPositions[c], size = Size { 80,20 }, id = c*6, disabled = true, NotifyClicked = DrawClicked };
161          chowButton[c] = Button { this, text = "Chow", position = Point { xyPositions[c].x + 80,xyPositions[c].y }, size = Size { 80,20 }, id = c*6, disabled = true, NotifyClicked = ChowClicked };
162          dumpButton[c] = Button { this, text = "Dump", position = Point { xyPositions[c].x + 160, xyPositions[c].y }, size = Size { 80,20 }, id = c*6, disabled = true, NotifyClicked = DumpClicked };
163          callButton[c] = Button { this, text = "Call", position = Point { xyPositions[c].x, xyPositions[c].y + 20 }, size = Size { 80,20 }, id = c*6, disabled = true, NotifyClicked = CallClicked };
164          showButton[c] = Button { this, text = "Show", position = Point { xyPositions[c].x + 80, xyPositions[c].y + 20 }, size = Size { 80,20 }, id = c*6, disabled = true, NotifyClicked = ShowClicked };
165          doneButton[c] = Button { this, text = "Done", position = Point { xyPositions[c].x + 160, xyPositions[c].y + 20 },size = Size { 80,20 }, id = c*6, disabled = true, NotifyClicked = DoneClicked };
166       }
167       showButton[currentPlayer].disabled = false;
168       dumpButton[currentPlayer].disabled = false;
169       return true;
170    }
171
172    bool OnLoadGraphics()
173    {
174       int i;
175       Bitmap ptrCardLoad {};
176       ptrCardLoad.Load(":cards.pcx",null,null);
177       cardBack.LoadT(":ecereCard.png", null, displaySystem);
178
179       for(i=0;i<52;i++)
180       {
181          Bitmap bitmap { transparent = true };
182          bitmap.Allocate(null,ptrCardLoad.width, ptrCardLoad.height/52,0,pixelFormat8,true);
183          CopyBytesBy4(bitmap.palette, ptrCardLoad.palette, 256);
184          bitmap.Grab(ptrCardLoad,0,(ptrCardLoad.height/52)*i);
185          bitmap.MakeDD(displaySystem);
186          bitmapCards[i] = bitmap;
187       }
188       delete ptrCardLoad;
189       return true;
190    }
191
192    void OnUnloadGraphics()
193    {
194       int i;
195       for(i=0;i<52;i++)
196          bitmapCards[i].Free();
197    }
198
199    void GameOver()
200    {
201       /*
202       Window gameOverBox
203       {
204          this, hasClose = true, background = LightBlue, text = "Game Over", [position.y] = A_CENTER|20, size = Size { 300, 200 }
205       };
206       Button { gameOverBox, text = "Close", [position.y] = A_CENTER|80, size = Size { 80,20 } };
207       */
208       gameOver = true;
209    }
210
211    void OnRedraw(Surface surface)
212    {
213       int h=0,i=0,j=0,k=0,x=0;
214
215       surface.Rectangle(375, 315, 495, 475);
216
217       if(gameOver)
218       {
219          surface.SetForeground(blue);
220          surface.SetBackground(red);
221    //      surface.Area(375,100,495,150);
222          surface.Area(375,370,495,420);
223          //surface.TextOpacity(true);
224    //      surface.WriteTextf(398, 115,"GAME OVER");
225          surface.WriteTextf(398, 385,"GAME OVER");
226       }
227
228       for(i=deckCounter,x=0;i<52;i+=5,x+=2)
229       {
230          Bitmap bitmap = cardBack;
231          surface.Blit(bitmap,400 + x, 195 + x,0,0, bitmap.width,bitmap.height);
232       }
233
234       if(discardCounter > 0)
235       {
236          Bitmap bitmap = bitmapCards[discard[discardCounter-1]];
237          surface.Blit(bitmap, xLeftButtonUp, yLeftButtonUp,0,0, bitmap.width,bitmap.height);
238       }
239
240       for(j=0;j<numOfPlayers;j++)
241       {
242          for(i=0;i<player[j].numCards;i++)
243          {
244             if(player[j].cardValues[i] != dragCard || (!flagButtonDown))
245             {
246                Bitmap bitmap = bitmapCards[player[j].cardValues[i]];
247                if(player[j].cardLifted[i])
248                {
249                   surface.Blit(bitmap,xyPositions[j].x + OFFSETTER1 * i,xyPositions[j].y - OFFSETTER2 - 20,0,0, bitmap.width,bitmap.height);
250                }
251                else
252                {
253                   surface.Blit(bitmap,xyPositions[j].x + OFFSETTER1 * i,xyPositions[j].y - OFFSETTER2,0,0, bitmap.width,bitmap.height);
254                }
255             }
256          }
257          for(i=0,h=0;i<player[j].numHouseDown;i++,h++)
258          {
259             if(player[j].houseDown[i].typeOfHouse == 0)
260             {
261                for(k=player[j].houseDown[i].straight.first;k<=player[j].houseDown[i].straight.last;k++)
262                {
263                   Bitmap bitmap = bitmapCards[MCARD((player[j].houseDown[i].straight.suit),(k))];
264                   surface.Blit(bitmap,xyPositions[j].x + OFFSETTER1 * h,xyPositions[j].y + 75,0,0, bitmap.width,bitmap.height);
265                   h++;
266                }
267             }
268
269             if(player[j].houseDown[i].typeOfHouse == 1)
270             {
271                for(k=0;k<4;k++) //player[j].houseDown[i].kind.suits[k]
272                {
273                   if(player[j].houseDown[i].kind.suits[k] == true)
274                   {
275                      Bitmap bitmap = bitmapCards[MCARD((k),(player[j].houseDown[i].kind.number))];
276                      surface.Blit(bitmap,xyPositions[j].x + OFFSETTER1 * h,xyPositions[j].y + 75,0,0, bitmap.width,bitmap.height);
277                      h++;
278                   }  //_2D xyPositions[4] = {{30,150},{30,500},{540,500},{540,150}};
279                }
280             }
281          }
282       }
283       if(flagButtonDown)
284       {
285          for(i=0;i<player[currentPlayer].numCards;i++)
286          {
287             Bitmap bitmap = bitmapCards[player[currentPlayer].cardValues[i]];
288             if(player[currentPlayer].cardValues[i] == dragCard)
289             {
290                surface.Blit(bitmap,xMouseMove + xCursorPositionAtCard,yMouseMove + yCursorPositionAtCard,0,0, bitmap.width,bitmap.height);
291             }
292          }
293       }
294       if(gameOver)
295       {
296          surface.SetForeground(blue);
297          surface.SetBackground(red);
298    //      surface.Area(375,100,495,150);
299          surface.Area(375,370,495,420);
300          //surface.TextOpacity(true);
301    //      surface.WriteTextf(398, 115,"GAME OVER");
302          surface.WriteTextf(398, 385,"GAME OVER");
303          for(i=0;i<numOfPlayers;i++)
304          {
305             drawButton[i].disabled = true;
306             chowButton[i].disabled = true;
307             dumpButton[i].disabled = true;
308             callButton[i].disabled = true;
309             showButton[i].disabled = true;
310             doneButton[i].disabled = true;
311          }
312    //      eWindow_Create("MessageBox",MSGBOX_OK,ES_CAPTION,window,null,A_LEFT,A_RIGHT,200,100,0,null,0,null,0);
313       }
314    /*      if(j == 1)
315          {
316             for(i=0;i<player[j].numCards;i++)
317             {
318                Bitmap bitmap = bitmapCards[MCARD(KIND(player[2].cardValues[i]),NUMBER(player[2].cardValues[i]))];
319                surface.Blit(bitmap,30+15*i,500,0,0, bitmap.width,bitmap.height);
320             }
321          }
322          else
323          {
324             for(i=0;i<player[j].numCards;i++)
325             {
326                //Bitmap bitmap1 = bitmapCards[MCARD(KIND(player[1].cardValues[i]),NUMBER(player[1].cardValues[i]))];
327                Bitmap bitmap = cardBack;
328                surface.Blit(bitmap,30+15*i,30+nextLane,0,0, bitmap.width,bitmap.height);
329             }
330          }
331       }*/
332    }
333
334    bool DrawClicked(Button button, int x, int y, Modifiers mods)
335    {
336       player[currentPlayer].cardValues[player[currentPlayer].numCards++] = cards[deckCounter++];
337       drawButton[currentPlayer].disabled = true;
338       chowButton[currentPlayer].disabled = true;
339       callButton[currentPlayer].disabled = true;
340       showButton[currentPlayer].disabled = false;
341       dumpButton[currentPlayer].disabled = false;
342       Update(null);
343       return true;
344    }
345
346    bool ChowClicked(Button button, int x, int y, Modifiers mods)
347    {
348       int i, j, suit, number;
349       int tempCtr = 0;
350       int tempHouse[13];
351       for(i=0;i<13;i++)
352       {
353          tempHouse[i]=0;
354       }
355       suit = KIND(discard[discardCounter-1]);
356       number = NUMBER(discard[discardCounter-1]);
357       tempHouse[tempCtr++] = discard[discardCounter-1];
358       for(i=0;i<player[currentPlayer].numCards;i++)
359       {
360          if(player[currentPlayer].cardLifted[i])
361          {
362             tempHouse[tempCtr++] = player[currentPlayer].cardValues[i];
363          }
364       }
365       if(tempCtr >= 3)
366       {
367          int checkResult;
368
369          qsort(tempHouse,tempCtr,sizeof(int),CompareInteger);
370          checkResult = CheckHouse(tempCtr,tempHouse);
371          switch(checkResult)
372          {
373             // 3/4 of a kind
374             case 1:
375             {
376                House * house = &player[currentPlayer].houseDown[player[currentPlayer].numHouseDown];
377                house->typeOfHouse = checkResult;
378                for(i=0;i<tempCtr;i++)
379                {
380                   house->kind.number = NUMBER(tempHouse[i]);
381                   house->kind.suits[KIND(tempHouse[i])] = true;
382                }
383                player[currentPlayer].numHouseDown++;
384                break;
385             }
386             case 0:
387             {
388                House * house = &player[currentPlayer].houseDown[player[currentPlayer].numHouseDown];
389                house->typeOfHouse = checkResult;
390                house->straight.first = NUMBER(tempHouse[0]);
391                house->straight.last = NUMBER(tempHouse[tempCtr-1]);
392                house->straight.suit = KIND(tempHouse[0]);
393                player[currentPlayer].numHouseDown++;
394                break;
395             }
396             case -1:
397             {
398                return true;
399                break;
400             }
401          }
402          for(i=0,j=0;i<player[currentPlayer].numCards;i++)
403          {
404             if(!player[currentPlayer].cardLifted[i])
405             {
406                player[currentPlayer].cardValues[j] = player[currentPlayer].cardValues[i];
407                player[currentPlayer].cardLifted[j] = false;
408                j++;
409             }
410          }
411          player[currentPlayer].numCards = j;
412          discard[discardCounter--];
413          drawButton[currentPlayer].disabled = true;
414          chowButton[currentPlayer].disabled = true;
415          callButton[currentPlayer].disabled = true;
416          showButton[currentPlayer].disabled = false;
417          dumpButton[currentPlayer].disabled = false;
418          Update(null);
419       }
420      if(player[currentPlayer].numCards == 0)
421      {
422          GameOver();
423      }
424      return true;
425   }
426
427    bool DumpClicked(Button button, int x, int y, Modifiers mods)
428    {
429       MessageBox { text = button.text }.Create();
430       return true;
431    }
432
433    bool CallClicked(Button button, int x, int y, Modifiers mods)
434    {
435       MessageBox { text = button.text }.Create();
436       return true;
437    }
438
439    bool ShowClicked(Button button, int x, int y, Modifiers mods)
440    {
441       int i, j;
442       int tempHouse[13];
443       int tempCtr = 0;
444       for(i=0;i<13;i++)
445       {
446          tempHouse[i]=0;
447       }
448       for(i=0;i<player[currentPlayer].numCards;i++)
449       {
450          if(player[currentPlayer].cardLifted[i])
451          {
452             tempHouse[tempCtr++] = player[currentPlayer].cardValues[i];
453          }
454       }
455       if(tempCtr >= 3)
456       {
457          int checkResult;
458          qsort(tempHouse,tempCtr,sizeof(int),CompareInteger);
459          checkResult = CheckHouse(tempCtr,tempHouse);
460          switch(checkResult)
461          {
462             // 3/4 of a kind
463             case 1:
464             {
465                House * house = &player[currentPlayer].houseDown[player[currentPlayer].numHouseDown];
466                house->typeOfHouse = checkResult;
467                for(i=0;i<tempCtr;i++)
468                {
469                   house->kind.number = NUMBER(tempHouse[i]);
470                   house->kind.suits[KIND(tempHouse[i])] = true;
471                }
472                player[currentPlayer].numHouseDown++;
473                break;
474             }
475             case 0:
476             {
477                House * house = &player[currentPlayer].houseDown[player[currentPlayer].numHouseDown];
478                house->typeOfHouse = checkResult;
479                house->straight.first = NUMBER(tempHouse[0]);
480                house->straight.last = NUMBER(tempHouse[tempCtr-1]);
481                house->straight.suit = KIND(tempHouse[0]);
482                player[currentPlayer].numHouseDown++;
483                break;
484             }
485             case -1:
486             {
487                return true; //false;
488                break;
489             }
490          }
491          for(i=0,j=0;i<player[currentPlayer].numCards;i++)
492          {
493             if(!player[currentPlayer].cardLifted[i])
494             {
495                player[currentPlayer].cardValues[j] = player[currentPlayer].cardValues[i];
496                player[currentPlayer].cardLifted[j] = false;
497                j++;
498             }
499          }
500          player[currentPlayer].numCards = j;
501          if(player[currentPlayer].numCards == 0)
502          {
503             GameOver();
504          }
505          Update(null);
506       }
507       return true;
508    }
509
510    bool DoneClicked(Button button, int x, int y, Modifiers mods)
511    {
512       MessageBox { text = button.text }.Create();
513       return true;
514    }
515
516    bool OnMouseMove(int x, int y, Modifiers mods)
517    {
518       if(flagButtonDown)
519       {
520          xMouseMove = x;
521          yMouseMove = y;
522          Capture();
523          Update(null);
524       }
525       return true;
526    }
527
528    bool OnLeftButtonDown(int x, int y, Modifiers mods)
529    {
530       int i;
531       if(!gameOver)
532       {
533          for(i=player[currentPlayer].numCards-1;i>=0;i--)
534          {
535             Bitmap bitmap = bitmapCards[player[currentPlayer].cardValues[i]];
536             if(Box {
537                xyPositions[currentPlayer].x + (OFFSETTER1 * i),
538                (player[currentPlayer].cardLifted[i] ? (xyPositions[currentPlayer].y - OFFSETTER2 - 20) :( xyPositions[currentPlayer].y - OFFSETTER2)),
539                bitmap.width + (xyPositions[currentPlayer].x + OFFSETTER1 * i),
540                bitmap.height + (player[currentPlayer].cardLifted[i] ? (xyPositions[currentPlayer].y - OFFSETTER2 - 20) : (xyPositions[currentPlayer].y - OFFSETTER2))
541                }.IsPointInside(Point { x, y }) && drawButton[currentPlayer].disabled)
542             {
543                dragCard = player[currentPlayer].cardValues[i];
544                cardToReposition = i;
545                width = bitmap.width;
546                height = bitmap.height;
547                xCursorPositionAtCard = (xyPositions[currentPlayer].x + (OFFSETTER1 * i)) - x;
548                yCursorPositionAtCard = (player[currentPlayer].cardLifted[i] ?
549                   (xyPositions[currentPlayer].y - OFFSETTER2 - y - 20) :
550                   (xyPositions[currentPlayer].y - OFFSETTER2 - y));
551                flagButtonDown = true;
552                OnMouseMove(x,y,mods);
553                break;
554             }
555          }
556       }
557       return true;
558    }
559
560    bool OnLeftButtonUp(int x, int y, Modifiers mods)
561    {
562       int h,i,j;
563       Box boxDiscard={375, 315, 495, 475};
564       Box boxCard = {x + xCursorPositionAtCard,y + yCursorPositionAtCard, x + xCursorPositionAtCard + width - 1, y + yCursorPositionAtCard + height - 1};
565       if(flagButtonDown)
566       {
567          if(boxCard.Overlap(boxDiscard))
568          {
569             xLeftButtonUp = 400;
570             yLeftButtonUp = 350;
571             CopyBytesBy4(&player[currentPlayer].cardValues[cardToReposition],
572                &player[currentPlayer].cardValues[cardToReposition+1],
573                player[currentPlayer].numCards-1-cardToReposition);
574             player[currentPlayer].numCards--;
575             discard[discardCounter++] = dragCard;
576             if(player[currentPlayer].numCards == 0 || deckCounter == 52)
577             {
578                GameOver();
579             }
580             else
581             {
582                for(i=0;i<player[currentPlayer].numCards;i++)
583                {
584                   player[currentPlayer].cardLifted[i] = false;
585                }
586                drawButton[currentPlayer].disabled = true;
587                chowButton[currentPlayer].disabled = true;
588                dumpButton[currentPlayer].disabled = true;
589                callButton[currentPlayer].disabled = true;
590                showButton[currentPlayer].disabled = true;
591                doneButton[currentPlayer].disabled = true;
592                player[currentPlayer].callTag = false;
593                currentPlayer++;
594                currentPlayer %= numOfPlayers;
595                drawButton[currentPlayer].disabled = false;
596                chowButton[currentPlayer].disabled = false;
597                if(player[currentPlayer].numHouseDown >= 2 && player[currentPlayer].callTag == false)
598                   callButton[currentPlayer].disabled = false;
599             }
600          }
601          else
602          {
603             for(j=0;j<numOfPlayers;j++)
604             {
605                Bitmap bitmap = cardBack;
606                Box boxHouseDown =
607                {
608                   xyPositions[j].x,xyPositions[j].y + 75,
609                   0, xyPositions[j].y + 75 + bitmap.height
610                };
611                for(i=0;i<player[j].numHouseDown;i++)
612                {
613                   if(player[j].houseDown[i].typeOfHouse == 0)
614                   {
615                      boxHouseDown.right = boxHouseDown.left + bitmap.width +
616                         (player[j].houseDown[i].straight.last - player[j].houseDown[i].straight.first) * OFFSETTER1 - 1;
617                      if(boxCard.Overlap(boxHouseDown))
618                      {
619                         if(dragCard == MCARD((player[j].houseDown[i].straight.suit),(player[j].houseDown[i].straight.first)) - 4)
620                         {
621                            player[j].houseDown[i].straight.first --;
622                            CopyBytesBy4(&player[currentPlayer].cardValues[cardToReposition],
623                               &player[currentPlayer].cardValues[cardToReposition+1],
624                               player[currentPlayer].numCards-1-cardToReposition);
625                            player[currentPlayer].numCards--;
626                            player[j].callTag = true;
627                            j = numOfPlayers;
628                            break;
629
630                         }
631                         else if(dragCard == MCARD((player[j].houseDown[i].straight.suit),(player[j].houseDown[i].straight.last )) + 4)
632                         {
633                            player[j].houseDown[i].straight.last ++;
634                            CopyBytesBy4(&player[currentPlayer].cardValues[cardToReposition],
635                               &player[currentPlayer].cardValues[cardToReposition+1],
636                               player[currentPlayer].numCards-1-cardToReposition);
637                            player[currentPlayer].numCards--;
638                            player[j].callTag = true;
639                            j = numOfPlayers;
640                            break;
641                         }
642                      }
643                      boxHouseDown.left +=
644                         (player[j].houseDown[i].straight.last - player[j].houseDown[i].straight.first + 2) * OFFSETTER1;
645                   }
646                   else if(player[j].houseDown[i].typeOfHouse == 1)
647                   {
648                      if(player[j].houseDown[i].kind.suits[0] == false || player[j].houseDown[i].kind.suits[1] == false ||
649                         player[j].houseDown[i].kind.suits[2] == false || player[j].houseDown[i].kind.suits[3] == false)
650                      {
651                         h=4;
652                         boxHouseDown.right = boxHouseDown.left + (2 * OFFSETTER1) + bitmap.width - 1;
653                      }
654                      else
655                      {
656                         h=5;
657                      }
658                      if(boxCard.Overlap(boxHouseDown))
659                      {
660                         if(NUMBER(dragCard) == player[j].houseDown[i].kind.number)
661                         {
662                            player[j].houseDown[i].kind.suits[KIND(dragCard)] = true;
663                            CopyBytesBy4(&player[currentPlayer].cardValues[cardToReposition],
664                                  &player[currentPlayer].cardValues[cardToReposition+1],
665                                  player[currentPlayer].numCards-1-cardToReposition);
666                            player[currentPlayer].numCards--;
667                            player[j].callTag = true;
668                            j = numOfPlayers;
669                            break;
670                         }
671                      }
672                      boxHouseDown.left += (h * OFFSETTER1);
673                   }
674                }
675             }
676          }
677          flagButtonDown = false;
678          ReleaseCapture();
679          Update(null);
680       }
681       return true;
682    }
683
684    bool OnLeftDoubleClick(int x, int y, Modifiers mods)
685    {
686       int i;
687       for(i=player[currentPlayer].numCards-1;i>=0;i--)
688       {
689          Bitmap bitmap = bitmapCards[player[currentPlayer].cardValues[i]];
690
691          if(Box {
692             xyPositions[currentPlayer].x + (OFFSETTER1 * i),
693             (player[currentPlayer].cardLifted[i] ? (xyPositions[currentPlayer].y - OFFSETTER2 - 20) :(xyPositions[currentPlayer].y - OFFSETTER2)),
694             bitmap.width + (xyPositions[currentPlayer].x + OFFSETTER1 * i),
695             bitmap.height + (player[currentPlayer].cardLifted[i] ? (xyPositions[currentPlayer].y - OFFSETTER2 - 20) : (xyPositions[currentPlayer].y - OFFSETTER2))
696             }.IsPointInside(Point{x,y}))
697          {
698             player[currentPlayer].cardLifted[i] ^= true;
699             Update(null);
700             return false;
701          }
702
703       }
704       return true;
705    }
706
707    bool OnRightButtonDown(int x, int y, Modifiers mods)
708    {
709       int i;
710
711       if(!gameOver)
712       {
713          for(i=player[currentPlayer].numCards-1;i>=0;i--)
714          {
715             Bitmap bitmap = bitmapCards[player[currentPlayer].cardValues[i]];
716             if(Box {
717                xyPositions[currentPlayer].x + (OFFSETTER1 * i),
718                (player[currentPlayer].cardLifted[i] ? (xyPositions[currentPlayer].y - OFFSETTER2 - 20) :(xyPositions[currentPlayer].y - OFFSETTER2)),
719                bitmap.width + (xyPositions[currentPlayer].x + OFFSETTER1 * i),
720                bitmap.height + (player[currentPlayer].cardLifted[i] ? (xyPositions[currentPlayer].y - OFFSETTER2 - 20) : (xyPositions[currentPlayer].y - OFFSETTER2))
721                }.IsPointInside(Point{x,y}) && drawButton[currentPlayer].disabled)
722
723             {
724                dragCard = player[currentPlayer].cardValues[i];
725                cardToReposition = i;
726                width = bitmap.width;
727                height = bitmap.height;
728                xCursorPositionAtCard = (xyPositions[currentPlayer].x + (OFFSETTER1 * i)) - x;
729                yCursorPositionAtCard = (player[currentPlayer].cardLifted[i] ?
730                   (xyPositions[currentPlayer].y - OFFSETTER2 - y - 20) :
731                   (xyPositions[currentPlayer].y - OFFSETTER2 - y));
732                flagButtonDown = true;
733                OnMouseMove(x,y,mods);
734                break;
735             }
736          }
737       }
738
739       return true;
740    }
741
742    bool OnRightButtonUp(int x, int y, Modifiers mods)
743    {
744       Bitmap bitmap = cardBack;
745       int swapCard,dest,source,i;
746       Box boxPlayerCards={xyPositions[currentPlayer].x,xyPositions[currentPlayer].y - OFFSETTER2,
747          xyPositions[currentPlayer].x + player[currentPlayer].numCards * OFFSETTER1 + bitmap.width,
748          xyPositions[currentPlayer].y - OFFSETTER2 + bitmap.height};
749       Box boxCard = {x + xCursorPositionAtCard,y + yCursorPositionAtCard, x + xCursorPositionAtCard,y + yCursorPositionAtCard + height - 1};
750
751       if(flagButtonDown)
752       {
753          if(boxCard.Overlap(boxPlayerCards))
754          {
755             swapCard = (x  + xCursorPositionAtCard >= player[currentPlayer].numCards * 15 + xyPositions[currentPlayer].x) ?
756                (player[currentPlayer].numCards-1) : ((x  + xCursorPositionAtCard - xyPositions[currentPlayer].x) / 15);
757             if(cardToReposition > swapCard)
758             {
759                dest = swapCard + 1;
760                MoveBytes(&player[currentPlayer].cardValues[dest],
761                   &player[currentPlayer].cardValues[swapCard],(cardToReposition - swapCard) * sizeof(int));
762                player[currentPlayer].cardValues[swapCard] = dragCard;
763                //player[currentPlayer].cardLifted[swapCard] = false;
764             }
765             else
766             {
767                dest = cardToReposition;
768                source = cardToReposition + 1;
769                MoveBytes(&player[currentPlayer].cardValues[dest],
770                   &player[currentPlayer].cardValues[source],(swapCard - cardToReposition) * sizeof(int));
771                   player[currentPlayer].cardValues[swapCard] = dragCard;
772                //player[currentPlayer].cardLifted[swapCard] = false;
773             }
774
775          }
776
777          for(i=0;i<player[currentPlayer].numCards;i++)
778          {
779             player[currentPlayer].cardLifted[i] = false;
780          }
781
782          flagButtonDown = false;
783          ReleaseCapture();
784          Update(null);
785       }
786       return true;
787    }
788 }
789
790 TongIts tongIts {};