File: PlayingDeck.cxx

package info (click to toggle)
oonsoo 1.2-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,604 kB
  • ctags: 422
  • sloc: cpp: 24,373; makefile: 82
file content (205 lines) | stat: -rw-r--r-- 6,191 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
///////////////////////////////////////////////////////////////////////////////
// $Id: PlayingDeck.cxx,v 1.3 1996/01/06 05:12:00 bwmott Exp $
///////////////////////////////////////////////////////////////////////////////
//
// PlayingDeck.cxx - Oonsoo playing deck
//
//
// Bradford W. Mott
// Copyright (C) 1994
// December 11,1994
//
///////////////////////////////////////////////////////////////////////////////
// $Log: PlayingDeck.cxx,v $
// Revision 1.3  1996/01/06 05:12:00  bwmott
// Changed all NULLs to 0
//
// Revision 1.2  1996/01/03 20:13:27  bwmott
// Fixed definitions inside case statement
//
// Revision 1.1  1995/01/08 06:45:30  bmott
// Initial revision
//
///////////////////////////////////////////////////////////////////////////////

#include "UIApplication.hxx"
#include "Sprite.hxx"
#include "SpriteCollection.hxx"
#include "Card.hxx"
#include "PlayingDeck.hxx"
#include "PlainDeck.hxx"
#include "DeckMoveCommand.hxx"

///////////////////////////////////////////////////////////////////////////////
// Construct an empty deck of playing cards
///////////////////////////////////////////////////////////////////////////////
PlayingDeck::PlayingDeck(ContainerWidget* parent, const char *const widgetName,
    int x, int y, int width, int maxHeight, SpriteCollection* sprites,
    Command* command)
    : Deck(parent, widgetName, x, y, width, maxHeight, sprites),
      myCommand(command)
{
  // Update my view
  updateView();
}

///////////////////////////////////////////////////////////////////////////////
// Construct a partial deck of playing cards
///////////////////////////////////////////////////////////////////////////////
PlayingDeck::PlayingDeck(ContainerWidget* parent, const char *const widgetName,
    int x, int y, int width, int maxHeight, SpriteCollection* sprites,
    LinkedList<Card>* cards, Command* command)
    : Deck(parent, widgetName, x, y, width, maxHeight, sprites),
      myCommand(command)
{
  // Move each of the cards from the LinkedList into myListOfCards
  for(Card* card = cards->remove(); card != (Card*)0; card = cards->remove())
  {
    myListOfCards.prepend(card);
  }

  // Update my view
  updateView();
}

///////////////////////////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////////////////////////
PlayingDeck::~PlayingDeck()
{ 
  // Free my command
  delete myCommand;
}

///////////////////////////////////////////////////////////////////////////////
// See if the deck consists of one complete suit in order
///////////////////////////////////////////////////////////////////////////////
int PlayingDeck::checkCompleteSuitInOrder()
{
  // There has to be 4 cards on the deck for it to be complete
  if(myListOfCards.size() != 4)
    return(0);

  Card* top = myListOfCards.first();
  for(Card* card = myListOfCards.next(); card != 0; card = myListOfCards.next())
  {
    // Suites have to be the same
    if(top->suite() != card->suite()) 
      return(0);

    // Top card has to be the same or next priority in the suite
    if((top->priority() != card->priority()) &&
       (top->priority() != (card->priority() + 1)))
        return(0); 

    top = card;
  }
  return(1);
}


///////////////////////////////////////////////////////////////////////////////
// See if these cards form a legal oonsoo move
///////////////////////////////////////////////////////////////////////////////
int PlayingDeck::checkValidAddition(LinkedList<Card>* cards)
{
  // Get the top card
  Card* top = myListOfCards.tail();

  for(Card* card = cards->first(); card != 0; card = cards->next())
  {
    if(top == 0)
    { 
      // Only highest level cards can be added to an empty deck
      if(card->priority() != 4)
        return(0);
    }
    else
    {
      // Suites have to be the same
      if(top->suite() != card->suite()) 
        return(0);

      // Top card has to be the same or next priority in the suite
      if((top->priority() != card->priority()) &&
         (top->priority() != (card->priority() + 1)))
        return(0); 
    }
    top = card;
  }
  return(1);
}


///////////////////////////////////////////////////////////////////////////////
// Called whenever an event arrives for me (I need to override the default)
///////////////////////////////////////////////////////////////////////////////
void PlayingDeck::handleEvent(XEvent* event)
{
  if ((event->type == ButtonPress) && (event->xbutton.button == Button1))
  {
    int done = 0;
    int t;

    // Return if there are no cards to move
    if (myListOfCards.size() == 0)
      return;

    // Calculate the root x and y coordinate of my parent widget
    int parentRootX = event->xbutton.x_root - event->xbutton.x - x();
    int parentRootY = event->xbutton.y_root - event->xbutton.y - y();

    // Calculate how many cards to remove from myself
    int num = myListOfCards.size() - cardIndexFromCoordinate(event->xbutton.y);

    // Remove cards from myself and place them in a temporary list
    LinkedList<Card> tmpListOfCards;
    for(t = 0; t < num; ++t)
    {
      tmpListOfCards.prepend(myListOfCards.remove());
    }
  
    // Create a plain deck with the temporary list of cards
    PlainDeck* plainDeck = new PlainDeck(myParent, "plainDeck",
        x() + event->xbutton.x - (width()/2), y() + event->xbutton.y - 24, 
        width(), myMaximumHeight, mySprites, &tmpListOfCards); 
    plainDeck->manage();

    // Update my view
    updateView();

    while(!done)
    {
      XEvent event;

      XNextEvent(application->display(), &event);

      switch(event.type)
      {
        case MotionNotify:
        {
          int newX = event.xmotion.x_root - parentRootX - width()/2;
          int newY = event.xmotion.y_root - parentRootY - 24;

          plainDeck->move(newX, newY);
          break; 
        }

        case ButtonRelease:
          done = 1;
          break;

        default:
          application->dispatchEvent(&event);
          break;
      }
    }

    // Build the command arguments
    DeckMoveCommandArguments args(this, plainDeck);
    
    // Execute my command passing the arguments
    myCommand->execute(&args);
  }
}