-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deal.cs
479 lines (389 loc) · 9.87 KB
/
Deal.cs
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
using Clutter;
using GLib;
using System;
public class Deal
{
static int [] CardPositionsX = { 100, 250, 400, 550, 700 };
static int [] CoinPositionsX = { 225, 250, 275, 300, 325 };
static Stage Stage;
static Deck Deck;
static Hand PlayerHand;
static Hand OpponentHand;
static Stack Stack;
static int Bet;
static StepButton StepButton;
static BetButton BetButton;
static DealButton DealButton;
static Text ScoreText;
static Coin [] Coins;
static void Main ()
{
Clutter.Application.Init ();
Stage = new Stage ();
Stage.Title = "Deal!";
Stage.Add (new Texture ("Pixmaps/Table.png"));
Stage.SetSize (800, 480);
Stage.KeyPressEvent += HandleKeyPress;
Texture C = new Texture ("Pixmaps/Coin.png");
C.SetSize (50, 50);
C.SetPosition (35, 405);
Stage.Add (C);
Bet = 0;
BetButton = new BetButton ();
BetButton.ButtonPressEvent += IncreaseBet;
Stage.Add (BetButton);
DealButton = new DealButton ();
DealButton.ButtonPressEvent += NewGame;
Stage.Add (DealButton);
StepButton = new StepButton ();
StepButton.ButtonPressEvent += NextStep;
Stage.Add (StepButton);
Stack = new Stack ();
Stack.Decrease (20);
ScoreText = new Text ("Droid Sans Bold 21", "" + Stack.GetAmount());
ScoreText.SetColor (new Clutter.Color (0xff, 0xff, 0xff, 0xff));
ScoreText.SetPosition (100, 413);
Stage.Add (ScoreText);
Coins = new Coin [5];
Coins [0] = new Coin ();
Coins [1] = new Coin ();
Coins [2] = new Coin ();
Coins [3] = new Coin ();
Coins [4] = new Coin ();
for (int i = 0; i < 5; i++) {
Coins [i].SetPosition (35, 405);
Stage.Add (Coins [i]);
}
Deck = new Deck ();
PlayerHand = new Hand (Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw ());
OpponentHand = new Hand (Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw ());
SetupAnimation ();
Stage.ShowAll();
Clutter.Application.Run ();
}
static void NewGame (object o, ButtonPressEventArgs args)
{
Bet = 0;
DealButton.Opacity = 0;
BetButton.Opacity = 255;
BetButton.Reactive = true;
BetButton.Show ();
StepButton.HoldState ();
StepButton.Show ();
// Needs to be replaced with "return to deck" animation
for (int i = 0; i < 5; i++) {
PlayerHand.GetCard (i).Hide ();
OpponentHand.GetCard (i).Hide ();
}
Deck = new Deck ();
PlayerHand = new Hand (Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw ());
OpponentHand = new Hand (Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw (), Deck.Draw ());
SetupAnimation ();
}
static void SetupAnimation ()
{
for (int i = 0; i < 5; i++)
{
PlayerHand.GetCard (i).ButtonPressEvent += ToggleSelection;
Stage.Add (PlayerHand.GetCard (i));
Stage.Add (OpponentHand.GetCard (i));
OpponentHand.GetCard (i).TurnToBack ();
AnimateProperty (PlayerHand.GetCard (i), 1000, "y", 300, (uint) (i * 250));
AnimateProperty (PlayerHand.GetCard (i), 1000, "x", CardPositionsX [i], (uint) (i * 250));
AnimateProperty (OpponentHand.GetCard (i), 500, "y", 100, (uint) 5250);
AnimateProperty (OpponentHand.GetCard (i), 500, "x", CardPositionsX [i], (uint) 1750);
}
for (int i = 0; i < 2; i++)
AnimateProperty (Coins [i], 500, "x", CoinPositionsX[i], (uint) (i * 250));
}
static void NextStep (object o, ButtonPressEventArgs args)
{
int j = 0;
for (int i = 0; i < 5; i++)
{
// Replace the selected cards
if (PlayerHand.GetCard (i).Selected) {
j++;
PlayerHand.GetCard (i).TurnToBack ();
// Remove card from table
AnimateProperty (PlayerHand.GetCard (i), 1000, "y", -200, 250);
// Get a new card
PlayerHand.Replace (i, Deck.Draw ());
PlayerHand.GetCard (i).SetPosition (400, -100);
// Return new card to the right position
AnimateProperty (PlayerHand.GetCard (i), 1000, "y", 300, (uint) (750 + j * 250));
AnimateProperty (PlayerHand.GetCard (i), 1000, "x", CardPositionsX [i], (uint) (750 + j * 250));
Stage.Add (PlayerHand.GetCard (i));
}
}
for (int i = 0; i < 5; i++)
OpponentHand.GetCard (i).TurnToFront ((uint) (1000 + j * 500));
Timeline [] Timelines = new Timeline [5];
for (int i = 0; i < 4; i++)
Timelines [i] = AnimateProperty (Coins [i], 500, "x", 35, (uint) ((1500 + (j * 500)) + (i * 250)));
Timeline Timeline1 = AnimateProperty (Coins [4], 500, "x", 35, (uint) ((1500 + (j * 500)) + (4 * 250)));
Timeline1.Completed += delegate { DealButton.Opacity = 255; };
StepButton.SwapState ();
BetButton.Hide ();
StepButton.Hide ();
foreach (Card Card in PlayerHand.Cards)
Card.Reactive = false;
}
public static void ToggleSelection (object o, ButtonPressEventArgs args)
{
Card Card = (Card) o;
float x, y;
Card.GetPosition(out x, out y);
if (Card.Selected) {
AnimateProperty (Card, 200, "y", y + 35, 0); // Deselect the card
Card.Selected = false;
} else {
AnimateProperty (Card, 200, "y", y - 35, 0); // Select the card
Card.Selected = true;
}
if (PlayerHand.NumberOfCardsSelected () >= 1)
StepButton.SwapState ();
else
StepButton.HoldState ();
}
static void IncreaseBet (object Object, ButtonPressEventArgs ButtonPressEventArgs)
{
if (++Bet == 3) {
Timeline Timeline = new Timeline (255);
Timeline.NewFrame += delegate (object o, NewFrameArgs args) {
BetButton.Opacity = (byte) (255 - args.FrameNum); };
Timeline.Start ();
BetButton.Reactive = false;
}
AnimateProperty (Coins [Bet + 1], 500, "x", CoinPositionsX [Bet + 1], 0);
Stack.Decrease (10);
ScoreText.Value = "" + Stack.GetAmount ();
}
static void HandleKeyPress (object o, KeyPressEventArgs args)
{
Clutter.Application.Quit ();
}
public static Timeline AnimateProperty (Actor Actor, uint Time, string Property, float To, uint Delay)
{
Timeline Timeline = new Timeline (Time);
Timeline.Delay = Delay;
string [] Properties = new string[1];
Properties [0] = Property;
Actor.AnimateWithTimelinev (6, Timeline, Properties, new GLib.Value ((float) To));
return Timeline;
}
public static bool InArray (int [] a, int b)
{
for (int i = 0; i < a.Length; i++)
if (a [i] == b)
return true;
return false;
}
}
public class Card : Texture
{
public bool Selected;
public int Type;
private Timeline Timeline;
public Card (int type)
{
Type = type;
Selected = false;
SetPosition (400, -100);
SetSize (130, 152);
SetAnchorPoint (65, 76);
Reactive = true;
FrontSide ();
}
private void BackSide ()
{
SetFromFile("Pixmaps/Card-Back.png");
}
private void FrontSide ()
{
SetFromFile("Pixmaps/" + Type + ".png");
}
public void TurnWithoutTexture ()
{
SetRotation(RotateAxis.Y, 180, 0, 0, 0);
}
public void TurnToBack ()
{
Timeline = new Timeline (180);
Timeline.NewFrame += delegate (object o, NewFrameArgs args) {
SetRotation(RotateAxis.Y, args.FrameNum, 0, 0, 0);
if (args.FrameNum > 90)
BackSide (); };
Timeline.Start ();
}
public void TurnToFront ()
{
Timeline = new Timeline (180);
Timeline.NewFrame += delegate (object o, NewFrameArgs args) {
SetRotation(RotateAxis.Y, args.FrameNum, 0, 0, 0);
if (args.FrameNum > 90)
FrontSide ();
};
Timeline.Start ();
}
public void TurnToFront (uint Delay)
{
Timeline = new Timeline (180);
Timeline.Delay = Delay;
Timeline.NewFrame += delegate (object o, NewFrameArgs args) {
SetRotation(RotateAxis.Y, args.FrameNum, 0, 0, 0);
if (args.FrameNum > 90)
FrontSide ();
};
Timeline.Start ();
}
public void TurnToBack (uint Delay)
{
Timeline = new Timeline (180);
Timeline.Delay = Delay;
Timeline.NewFrame += delegate (object o, NewFrameArgs args) {
SetRotation(RotateAxis.Y, args.FrameNum, 0, 0, 0);
if (args.FrameNum > 90)
FrontSide ();
};
Timeline.Start ();
}
}
public class Deck
{
private Card [] Cards;
private int CardPointer;
public Deck ()
{
Reset ();
}
public void Reset ()
{
Cards = new Card [30];
CardPointer = -1;
int i = 0;
while (i < Cards.Length)
{
for (int k = 0; k < 6; k++)
{
Cards [i] = new Card (k);
i++;
}
}
Shuffle ();
}
public void Shuffle ()
{
Random r = new Random ();
int first, second;
Card tmp;
for (int i = 0; i < 1000; i++)
{
first = r.Next (Cards.Length);
second = r.Next (Cards.Length);
tmp = Cards [first];
Cards [first] = Cards [second];
Cards [second] = tmp;
}
}
public Card Draw ()
{
return Cards [++CardPointer];
}
}
public class Hand
{
public Card [] Cards;
public Hand (Card a, Card b, Card c, Card d, Card e)
{
Cards = new Card [5];
Cards [0] = a; Cards [1] = b; Cards [2] = c; Cards [3] = d; Cards [4] = e;
}
public void Replace (int Position, Card Card)
{
Cards [Position] = Card;
}
public Card GetCard (int Position)
{
return Cards [Position];
}
public int NumberOfCardsSelected ()
{
int j = 0;
foreach (Card Card in Cards)
if (Card.Selected)
j++;
return j;
}
}
public class Stack
{
private int Amount;
public Stack ()
{
Amount = 1000;
}
public void Increase (int i)
{
Amount += i;
}
public void Decrease (int i)
{
Amount -= i;
}
public int GetAmount ()
{
return Amount;
}
}
public class Coin : Texture
{
public Coin ()
{
SetSize (50, 50);
SetFromFile ("Pixmaps/Coin.png");
SetPosition (35, 405);
}
}
public class StepButton : Texture
{
public int State;
public StepButton ()
{
SetSize (132, 61);
SetPosition (635, 400);
HoldState ();
Reactive = true;
}
public void HoldState ()
{
State = 0;
SetFromFile ("Pixmaps/Button-Hold.png");
}
public void SwapState ()
{
State = 0;
SetFromFile ("Pixmaps/Button-Swap.png");
}
}
public class BetButton : Texture
{
public BetButton ()
{
SetSize (132, 61);
SetPosition (485, 400);
SetFromFile ("Pixmaps/Button-Raise.png");
Reactive = true;
}
}
public class DealButton : Texture
{
public DealButton ()
{
SetSize (132, 61);
SetPosition (635, 400);
SetFromFile ("Pixmaps/Button-Deal.png");
Reactive = true;
Opacity = 0;
}
}