File: Engine.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 (315 lines) | stat: -rw-r--r-- 9,900 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
///////////////////////////////////////////////////////////////////////////////
// $Id: Engine.cxx,v 1.2 1996/01/06 05:11:34 bwmott Exp $
///////////////////////////////////////////////////////////////////////////////
//
// Engine.cxx - Oonsoo engine class
//
//
// Bradford W. Mott
// Copyright (C) 1994
// December 13,1994
//
///////////////////////////////////////////////////////////////////////////////
// $Log: Engine.cxx,v $
// Revision 1.2  1996/01/06 05:11:34  bwmott
// Changed all NULLs to 0
//
// Revision 1.1  1995/01/12 02:07:15  bmott
// Initial revision
//
///////////////////////////////////////////////////////////////////////////////

#include <stdio.h>

#include "UIApplication.hxx"
#include "TopLevel.hxx"
#include "PushButton.hxx"

#include "MainDeck.hxx"
#include "PlainDeck.hxx"
#include "PlayingDeck.hxx"

#include "NewGameCommand.hxx"
#include "AboutCommand.hxx"
#include "HelpCommand.hxx"
#include "QuitOrCloseCommand.hxx"
#include "DealCardsCommand.hxx"
#include "DeckMoveCommand.hxx"


#include "LinkedList.hxx"

#include "Engine.hxx"


///////////////////////////////////////////////////////////////////////////////
// Constructor 
///////////////////////////////////////////////////////////////////////////////
Engine::Engine(SpriteCollection* sprites)
    : mySpriteCollection(sprites)
{
  myTitle = 0;
  myCongratulation = 0;

  // Init deck references
  myMainDeck = 0;
  for(int t=0; t<12; ++t)
    myPlayingDeck[t] = 0;

  // Build the oonsoo main application window
  TopLevel* toplevel = new TopLevel("Oonsoo", 100, 100, 800, 600,
      new QuitOrCloseCommand());
  toplevel->background("Gray65");

    // Create a frame with a border
    Frame* cardTableFrame = new Frame(toplevel, "cardTableFrame", 10,10,
        780, 540, Sunken);
    cardTableFrame->background("Gray70");

    // Create a frame to act as the card table
    myCardTable = new Frame(cardTableFrame, "cardTable", 5, 5,
        770, 530, Flat);
    myCardTable->background("Gray70");

      // Create the oonsoo title 
      Sprite* title = mySpriteCollection->getByName("Title");
      myTitle = new DrawingArea(myCardTable, "title",
          (770 - title->width()) / 2, (530 - title->height()) / 3,
          title->width(), title->height());
      myTitle->background(title);
          
    // Create push buttons
    PushButton* pushButton = new PushButton(toplevel, "newGame",
         10, 600-40, 90, 30, "New Game", new NewGameCommand(this));
    pushButton->background("Gray70");

    pushButton = new PushButton(toplevel, "about",
        110, 600-40, 90, 30, "About...", new AboutCommand(mySpriteCollection));
    pushButton->background("Gray70");

    pushButton = new PushButton(toplevel, "rules",
        210, 600-40, 90, 30, "Help...", new HelpCommand(mySpriteCollection));
    pushButton->background("Gray70");

    pushButton = new PushButton(toplevel, "quit",
        720, 600-40, 70, 30, "Quit", new QuitOrCloseCommand());
    pushButton->background("Gray70");

  // Manage the main application window
  toplevel->manage();
}

///////////////////////////////////////////////////////////////////////////////
// Destructor 
///////////////////////////////////////////////////////////////////////////////
Engine::~Engine()
{
}

///////////////////////////////////////////////////////////////////////////////
// Setup a new game
///////////////////////////////////////////////////////////////////////////////
void Engine::newGame()
{
  int t;

  // Make sure myTitle has been removed
  delete myTitle;
  myTitle = 0;

  // Make sure myCongratulation has been removed
  delete myCongratulation;
  myCongratulation = 0;

  // Remove old decks
  delete myMainDeck;
  for(t=0; t<12; ++t)
    delete myPlayingDeck[t];

  // Main deck used to deal from
  myMainDeck = new MainDeck(myCardTable, "mainDeck", 20, 10, 64, 520,
      mySpriteCollection, new DealCardsCommand(this));
  myMainDeck->shuffle();
  myMainDeck->manage();
  
  // Create all twelve playing decks
  LinkedList<Card> cards;
  myPlayingDeck[0] = new PlayingDeck(myCardTable, "playingDeck",
      140    , 10, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[1] = new PlayingDeck(myCardTable, "playingDeck",
      140+110, 10, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[2] = new PlayingDeck(myCardTable, "playingDeck",
      140+220, 10, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[3] = new PlayingDeck(myCardTable, "playingDeck",
      140+330, 10, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[4] = new PlayingDeck(myCardTable, "playingDeck",
      140+440, 10, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[5] = new PlayingDeck(myCardTable, "playingDeck",
      140+550, 10, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[6] = new PlayingDeck(myCardTable, "playingDeck",
      140    , 275, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[7] = new PlayingDeck(myCardTable, "playingDeck",
      140+110, 275, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[8] = new PlayingDeck(myCardTable, "playingDeck",
      140+220, 275, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[9] = new PlayingDeck(myCardTable, "playingDeck",
      140+330, 275, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[10] = new PlayingDeck(myCardTable, "playingDeck",
      140+440, 275, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));
  myPlayingDeck[11] = new PlayingDeck(myCardTable, "playingDeck",
      140+550, 275, 64, 255, mySpriteCollection, &cards, 
      new DeckMoveCommand(this));

  // Manage each of the playing decks
  for(t=0; t<12; ++t)
    myPlayingDeck[t]->manage();

  // Deal the first twelve cards face down
  dealCards(FaceDown);
  
  // Deal the second twelve cards face up
  dealCards(FaceUp);
}


///////////////////////////////////////////////////////////////////////////////
// Deal twelve cards from the main deck to the playing deck
///////////////////////////////////////////////////////////////////////////////
void Engine::dealCards(CardOrientation orientation)
{
  // Add a card to each playing deck
  for(int t=0; t<12; ++t)
  {
    // Get the top card
    Card* card = myMainDeck->remove();
 
    // return if there are no more cards
    if(card == 0)
      return;

    card->orientation(orientation);
    myPlayingDeck[t]->add(card);
  }
}

///////////////////////////////////////////////////////////////////////////////
// Handle moving cards from one deck to somewhere else
///////////////////////////////////////////////////////////////////////////////
void Engine::moveCards(Deck* from, Deck* moved)
{
  int t;
  Sound* soundSystem = application->soundSystem();

  // See if the moved deck intersects with any of the playing decks
  for(t = 0; t < 12; ++t)
  {
    int x1 = myPlayingDeck[t]->x();
    int y1 = myPlayingDeck[t]->y();
    int x2 = x1 + myPlayingDeck[t]->width();
    int y2 = y1 + myPlayingDeck[t]->height();

    int x3 = moved->x();
    int y3 = moved->y();
    int x4 = x3 + moved->width();
    int y4 = y3 + moved->height();

    // See if the moved deck intersects this playing deck
    if((x3 >= x1) && (x3 <= x2) && (y3 >= y1) && (y3 <= y2))
      break; 
    if((x4 >= x1) && (x4 <= x2) && (y4 >= y1) && (y4 <= y2))
      break; 
    if((x4 >= x1) && (x4 <= x2) && (y3 >= y1) && (y3 <= y2))
      break; 
    if((x3 >= x1) && (x3 <= x2) && (y4 >= y1) && (y4 <= y2))
      break; 
  }

  // Unmanage the moved deck
  moved->unmanage();

  // Take the cards out of the moved deck and place them in a linked list
  LinkedList<Card> movedCards;
  for(Card* card = moved->remove(); card != 0; card = moved->remove())
    movedCards.prepend(card);
 
  // Delete the moved deck
  delete moved;
     
  if(t != 12)
  {
    // If it's the deck they moved from then go ahead and add it back
    if(myPlayingDeck[t] == from)
    {
      from->add(&movedCards);
    }
    else if(myPlayingDeck[t]->checkValidAddition(&movedCards))
    {
      myPlayingDeck[t]->add(&movedCards);

      // Tell the "from" deck to turn its top card over
      from->turnTopCardFaceUp();

      // Check to see if the game is finished
      CheckAndHandleFinished();
    }
    else
    {
      // Bad user! Move them back to where they came from
      from->add(&movedCards);
      if(soundSystem->state() == Enabled)
        soundSystem->playSample("BeepCasio");
      else
        XBell(application->display(), 50);
    }
  }
  else
  {
    // Bad user! Move them back to where they came from
    from->add(&movedCards);
    if(soundSystem->state() == Enabled)
      soundSystem->playSample("BeepCasio");
    else
      XBell(application->display(), 50);
  }
}


///////////////////////////////////////////////////////////////////////////////
// Check to see if the game has been finished
///////////////////////////////////////////////////////////////////////////////
void Engine::CheckAndHandleFinished()
{
  Sound* soundSystem = application->soundSystem();

  // Make sure each playing deck is correct  
  for(int t = 0; t < 12; ++t)
  {
    if(myPlayingDeck[t]->checkCompleteSuitInOrder() == 0)
      return;
  }

  // Ok, the game has been finished
  Sprite* congratulation = mySpriteCollection->getByName("Congratulation");
  myCongratulation = new DrawingArea(myCardTable, "congratulation",
      10, 10, congratulation->width(), congratulation->height());
  myCongratulation->background(congratulation);
  myCongratulation->manage();

  if(soundSystem->state() == Enabled)
    soundSystem->playSample("Chord");
  else
    XBell(application->display(), 50);
}