-
Notifications
You must be signed in to change notification settings - Fork 3
/
ludo king.cpp
590 lines (470 loc) · 18.2 KB
/
ludo king.cpp
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "raylib.h"
#include "dice.h"
#include "board.h"
#include <chrono>// for time delay
#include <thread>// for time delay
//enum Color { RED, GREEN, BLUE, YELLOW };
enum GameState { MENU, PLAYING, GAMEOVER };
GameState currentGameState = MENU;
const int screenWidth = 1000;
const int screenHeight = 1000;
const int boardSize = 810;
const int cellSize = boardSize / 15;
const Color playersColors[4] = { RED, GREEN, YELLOW ,BLUE, };
const int diceSize = 50; // Size of the dice
const float rollDuration = 0.5f; // Duration of dice roll animation
float dicePosX = screenWidth / 2 - diceSize / 2;
float dicePosY = screenHeight / 2 - diceSize / 2;
Sound DiceSound;
Sound PieceSound;
Sound KillSound;
Sound FinalHomeSound;
Sound OnStarSound;
void PlayDice() {
PlaySound(DiceSound);
}
void PlayPiece() {
PlaySound(PieceSound);
}
void PlayFinalHome() {
PlaySound(FinalHomeSound);
}
void PlayKill() {
PlaySound(KillSound);
}
class Position {
public:
int x, y;
Position() {
x = 0;
y = 0;
}
Position(int a, int b) {
x = a;
y = b;
}
bool operator==(const Position& other) const {
return x == other.x && y == other.y;
}
};
void DrawPiece(Position position1, Color color) {
Vector2 position = { static_cast<float>(position1.x), static_cast<float>(position1.y) };
// Draw the base of the piece (a slightly larger circle)
DrawCircleV(position, 23, DARKGRAY);
// Draw the main body of the piece (a circle)
DrawCircleV(position, 20, color);
// Add a gradient effect by overlaying a semi-transparent circle on top
DrawCircleGradient(position.x, position.y, 20, Fade(WHITE, 0.3f), Fade(color, 0.3f));
// Add a small circle on top to give a rounded top
DrawCircleV(position, 10, WHITE);
}
std::vector<Position> StarPath = {
{176,446},{446,824} ,{ 824,554 },{554,176},{230,554},{554,770},{770,446},{446,230}
};
bool isInStar(Position pos) {
for (int i = 0; i < 8; i++) {
if (StarPath[i] == pos) {
return true;
}
}
return false;
}
std::vector<Position> ludoPath = {
{176,446},{230,446},{284,446},{338,446},{392,446},{446,392},{446,338},{446,284},{446,230},{446,176},{446,122},{500,122},{554,122},{554,176},
{554,230},{554,284},{554,338},{554,392},{608,446},{662,446},{716,446},{770,446},{824,446},{878,446},{878,500},{878,554},{824,554},{770,554},{716,554},
{662,554},{608,554},{554,608},{554,662},{554,716},{554,770},{554,824},{554,878},{500,878},{446,878},{446,824},{446,770},{446,716},{446,662},{446,608},
{392,554},{338,554},{284,554},{230,554},{176,554},{122,554},{122,500},{122,446}
};
std::vector<Position> PiecePath[4]{
{//red path
{176,446},{230,446},{284,446},{338,446},{392,446},{446,392},{446,338},{446,284},{446,230},{446,176},{446,122},{500,122},{554,122},{554,176},
{554,230},{554,284},{554,338},{554,392},{608,446},{662,446},{716,446},{770,446},{824,446},{878,446},{878,500},{878,554},{824,554},{770,554},{716,554},
{662,554},{608,554},{554,608},{554,662},{554,716},{554,770},{554,824},{554,878},{500,878},{446,878},{446,824},{446,770},{446,716},{446,662},{446,608},
{392,554},{338,554},{284,554},{230,554},{176,554},{122,554},{122,500},{176,500},{230,500},{284,500},{338,500},{392,500},{446,500}
},
{//Green path
{554,176},{554,230},{554,284},{554,338},{554,392},{608,446},{662,446},{716,446},{770,446},{824,446},{878,446},{878,500},{878,554},{824,554},{770,554},{716,554},
{662,554},{608,554},{554,608},{554,662},{554,716},{554,770},{554,824},{554,878},{500,878},{446,878},{446,824},{446,770},{446,716},{446,662},{446,608},
{392,554},{338,554},{284,554},{230,554},{176,554},{122,554},{122,500},{122,446},{176,446},{230,446},{284,446},{338,446},{392,446},{446,392},{446,338},
{446,284},{446,230},{446,176},{446,122},{500,122},{500,176},{500,230},{500,284},{500,338},{500,392},{500,446}
},
{// Yellow path
{824,554},{770,554},{716,554},{662,554},{608,554},{554,608},{554,662},{554,716},{554,770},{554,824},{554,878},{500,878},{446,878},{446,824},{446,770},{446,716},{446,662},{446,608},
{392,554},{338,554},{284,554},{230,554},{176,554},{122,554},{122,500},{122,446}, {176,446},{230,446},{284,446},{338,446},{392,446},{446,392},{446,338},{446,284},{446,230},{446,176},{446,122},{500,122},{554,122},{554,176},
{554,230},{554,284},{554,338},{554,392},{608,446},{662,446},{716,446},{770,446},{824,446},{878,446},{878,500},{824,500},{770,500},{716,500},{662,500},{608,500},{554,500}
},
{//blue path
{446,824},{446,770},{446,716},{446,662},{446,608},{392,554},{338,554},{284,554},{230,554},{176,554},{122,554},{122,500},{122,446},{176,446},{230,446},{284,446},{338,446},{392,446},{446,392},{446,338},{446,284},{446,230},{446,176},{446,122},{500,122},{554,122},{554,176},
{554,230},{554,284},{554,338},{554,392},{608,446},{662,446},{716,446},{770,446},{824,446},{878,446},{878,500},{878,554},{824,554},{770,554},{716,554},
{662,554},{608,554},{554,608},{554,662},{554,716},{554,770},{554,824},{554,878},{500,878},{500,824},{500,770},{500,716},{500,662},{500,608},{500,554}
}
};
std::vector<Position> Home[4]{
{//redHome
{203,203},{311,203},{203,311},{311,311}
},
{//greenHome
{689,203},{797,203},{689,311},{797,311}
},
{//yellowHome
{689,689},{797,689},{689,797},{797,797}
},
{//blueHome
{203,689},{311,689},{203,797},{311,797}
},
};
class Piece {
public:
Position position;
bool inPlay;
Color color;
bool inHome;
bool inFinalHome;
const Position InitialPosition;
int ColorIndex;
int PathIndex;
// initializer list
Piece(Color color, Position InitialPosition, int ci) : InitialPosition(InitialPosition), position(InitialPosition), inPlay(false), color(color), inFinalHome(false), inHome(true) {
ColorIndex = ci;
}
void setPosition(const Position& newPos) {
position = newPos;
}
void setInitialPos() {
position = InitialPosition;
inPlay = false;
inHome = true;
}
Position getPosition() {
return position;
}
void move(int steps) {
if ((56 - PathIndex) < steps) {
return;
}
if (inHome && steps == 1) {
inHome = false;
inPlay = true;
setPosition(PiecePath[ColorIndex][0]);
PathIndex = 0;
PlayPiece();
}
else {
PathIndex += steps;
if (PathIndex == 56) {
inFinalHome = true;
PlaySound(FinalHomeSound);
}
setPosition(PiecePath[ColorIndex][PathIndex]);
}
}
bool isInHome() {
return inHome;
}
bool isInPlay() const {
return inPlay;
}
};
class Player {
public:
Color color;
int colorIndex;
Piece pieces[4];
Player(Color color, int ci) : color(color), colorIndex(ci),
pieces{ Piece(color,Home[ci][0],ci) ,Piece(color,Home[ci][1],ci), Piece(color,Home[ci][2],ci), Piece(color,Home[ci][3],ci) } {
}
int rollDice() {
return (rand() % 6) + 1;
}
void movePiece(int pieceIndex, int steps) {
if (pieces[pieceIndex].isInPlay() || (steps == 1 && pieces[pieceIndex].isInHome())) {
pieces[pieceIndex].move(steps);
}
else {
}
}
};
class Board {
public:
Player players[4];
Board() : currentPlayerIndex(0),
players{ Player(playersColors[0],0), Player(playersColors[1],1), Player(playersColors[2],2), Player(playersColors[3],3) } {
}
void startGame() {
while (!gameOver()) {
update();
}
/* std::cout << "Player " << currentPlayerIndex + 1 << " has won the game!" << std::endl;*/
}
private:
int currentPlayerIndex;
static int count;
void update() {
Player& currentPlayer = players[currentPlayerIndex];
static int diceValue = 1;
bool rolling = false; // Flag for dice rolling state
float rollTime = 0.0f; // Timer for dice roll animation
int flag = 0;
bool faded = true;
while (!flag) {
if (IsKeyPressed(KEY_SPACE)) {
PlayDice();
rolling = true;
}
if (rolling) {
rollTime += GetFrameTime(); // Increment roll timer
if (rollTime >= rollDuration) {
rolling = false;
diceValue = currentPlayer.rollDice(); // Random dice value between 1 and 6
flag = 1;
faded = false;
}
else {
// Optional: Simulate rolling by randomly changing diceValue
diceValue = currentPlayer.rollDice();
}
}
if (faded == true) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawLudoBoard(currentPlayer.color);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
DrawPiece(players[i].pieces[j].getPosition(), playersColors[i]);
}
}
DrawDice((int)dicePosX, (int)dicePosY, diceValue, currentPlayer.color);
EndDrawing();
}
else {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawLudoBoard();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
DrawPiece(players[i].pieces[j].getPosition(), playersColors[i]);
}
}
DrawDice((int)dicePosX, (int)dicePosY, diceValue, currentPlayer.color);
EndDrawing();
}
}
int moveIndex = 0;
int flag1 = 0;
int flag3 = 1;
int PiecesInPlay = 0;
int PieceIndexInPlay = 0;
// Check if step is not equal to 1 and all pieces are in home then we have to skip turn for the current player
if (diceValue != 1) {
for (int i = 0; i < 4; i++) {
if (currentPlayer.pieces[i].isInPlay() && (56 - currentPlayer.pieces[i].PathIndex) >= diceValue) {
flag3 = 0;
PiecesInPlay++;
PieceIndexInPlay = i;
}
}
}
else {
flag3 = 0;
}
if (diceValue == 1) {
count++;
if (count == 3) {
int largest = 0;
int index = 0;
for (int i = 0; i < 4; i++) {
if ((largest < currentPlayer.pieces[i].PathIndex) && currentPlayer.pieces[i].PathIndex != 56) {
largest = currentPlayer.pieces[i].PathIndex;
index = i;
}
}
currentPlayer.pieces[index].setInitialPos();
nextTurn();
return;
}
}
if (PiecesInPlay == 1) {
moveIndex = PieceIndexInPlay;
}
// If at least one player is in play or dice value is 1
if (flag3 == 0 && PiecesInPlay != 1) {
while (true) {
int flag2 = 0;
BeginDrawing();
ClearBackground(RAYWHITE);
DrawLudoBoard();
DrawDice((int)dicePosX, (int)dicePosY, diceValue, currentPlayer.color);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
DrawPiece(players[i].pieces[j].getPosition(), playersColors[i]);
}
}
for (int i = 0; i < 4; i++) {
Position p = currentPlayer.pieces[i].getPosition();
Vector2 CircleCenter = { static_cast<float>(p.x), static_cast<float>(p.y) };
float CircleRadius1 = 23.0f;
if (CheckCollisionPointCircle(GetMousePosition(), CircleCenter, CircleRadius1)) {
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
moveIndex = i;
flag1 = 1; // User selects piece
// If dice not equal to 1 and piece user selecting is in home
if ((diceValue != 1 && currentPlayer.pieces[moveIndex].isInHome()) || ((56 - currentPlayer.pieces[moveIndex].PathIndex) < diceValue)) {
DrawText("Please select the allowed pieces", 100, 46, 20, BLACK);
// flag2 is 1 when unwanted piece is selected
flag2 = 1;
flag1 = 0; // As unwanted piece is selected then user should reselect valid piece
}
break;
}
}
}
EndDrawing();
// For the message of "please select the allowed pieces" to last 2 seconds on screen
if (flag2 == 1) {
std::this_thread::sleep_for(std::chrono::seconds(2));
}
// If one valid piece is selected then loop breaks
if (flag1 == 1) {
break;
}
}
}
// Move piece is only run when at least one piece is moveable
if (flag3 == 0) {
if (!(diceValue == 1 && currentPlayer.pieces[moveIndex].isInHome())) {
int InitialIndex = currentPlayer.pieces[moveIndex].PathIndex;
for (int k = 0; k < diceValue; k++) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawLudoBoard();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (!((i == currentPlayerIndex) && (j == moveIndex))) {
DrawPiece(players[i].pieces[j].getPosition(), playersColors[i]);
}
else {
DrawPiece(PiecePath[currentPlayer.colorIndex][InitialIndex + k], currentPlayer.color);
}
}
}
DrawDice((int)dicePosX, (int)dicePosY, diceValue, currentPlayer.color);
EndDrawing();
PlayPiece();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
currentPlayer.movePiece(moveIndex, diceValue);
}
bool kill = false;
int isSoundPlayed = 0;
if (isInStar(currentPlayer.pieces[moveIndex].getPosition())) {
if (isSoundPlayed == 0) {
PlaySound(OnStarSound);
isSoundPlayed = 1;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if ((i != currentPlayerIndex) && (!isInStar(players[i].pieces[j].getPosition()))) {
if (currentPlayer.pieces[moveIndex].getPosition() == players[i].pieces[j].getPosition()) {
players[i].pieces[j].setInitialPos();
kill = true;
PlaySound(KillSound);
}
}
}
}
if (diceValue == 1 || diceValue == 6 || kill) {
if (diceValue == 6) {
count = 0;
}
}
else {
if (!(gameOver())) {
nextTurn();
}
}
}
void nextTurn() {
count = 0;
currentPlayerIndex = (currentPlayerIndex + 1) % 4;
}
bool gameOver() const {
for (int i = 0; i < 4; i++) {
if (!(players[currentPlayerIndex].pieces[i].inFinalHome)) {
return false;
}
}
return true;
}
};
int Board::count = 0;
int main() {
srand(static_cast<unsigned int>(time(nullptr)));
InitWindow(screenWidth, screenHeight, "Ludo Game");
InitAudioDevice();
DiceSound = LoadSound("sounds/dice.mp3");
Texture2D background = LoadTexture("images/ludo4.png");
Font robotoMedium = LoadFont("font/Roboto-Medium.ttf");
Sound backgroundSound = LoadSound("sounds/background.wav");
PieceSound = LoadSound("sounds/piece.wav");
KillSound = LoadSound("sounds/kill.wav");
OnStarSound = LoadSound("sounds/star.wav");
FinalHomeSound = LoadSound("sounds/finalhome.wav");
SetTargetFPS(60);
Board board;
float textAlpha = 0.0f;
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(WHITE);
if (currentGameState == MENU) {
if (!IsSoundPlaying(backgroundSound)) {
PlaySound(backgroundSound); // Play sound only if not already playing
}
DrawTexture(background, 0, 0, WHITE);
textAlpha = sin(GetTime() * 2) * 0.5f + 0.5f;
DrawTextEx(robotoMedium, "Press ENTER to Start",
Vector2{ screenWidth / 3 , 300 },
24, 2, Fade(WHITE, textAlpha));
DrawTextEx(robotoMedium, "Papu Chaudhary", Vector2{
screenHeight / 2 - 120, 750
},
24, 2, WHITE);
DrawTextEx(robotoMedium, "Rhythm Adhikari", Vector2{
screenHeight / 2 - 120, 780
},
24, 2, WHITE);
DrawTextEx(robotoMedium, "Roshan Koirala", Vector2{
screenHeight / 2 - 120, 810
},
24, 2, WHITE);
DrawTextEx(robotoMedium, "Sabin Shrestha", Vector2{
screenHeight / 2 - 120, 840
},
24, 2, WHITE);
if (IsKeyPressed(KEY_ENTER)) {
StopSound(backgroundSound);
currentGameState = PLAYING;
}
}
else if (currentGameState == PLAYING) {
DrawLudoBoard();
board.startGame();
}
EndDrawing();
}
UnloadSound(OnStarSound);
UnloadSound(FinalHomeSound);
UnloadSound(KillSound);
UnloadSound(PieceSound);
UnloadSound(DiceSound);
UnloadTexture(background);
UnloadSound(backgroundSound);
CloseAudioDevice();
CloseWindow();
return 0;
}