--- /dev/null
+#include <raylib.h>
+
+#define SevenHundredAndNine (709)
+
+#define BoardLimit (4)
+#define StartState (7)
+
+static int BoardState [BoardLimit] [BoardLimit] = { 0 };
+
+static bool IsPrimeNumber (int Number) {
+ if (Number <= 1) return (false);
+ if (Number <= 3) return (true);
+ if (Number % 2 == 0) return (false);
+
+ for (int Iterator = 3; Iterator < Number / 2; ++Iterator) {
+ if (Number % Iterator == 0) {
+ return (false);
+ }
+ }
+
+ return (true);
+}
+
+static bool IsGameOver (void) {
+ for (int Vertical = 0; Vertical < BoardLimit; ++Vertical) {
+ for (int Horizontal = 0; Horizontal < BoardLimit; ++Horizontal) {
+ if (BoardState [Vertical] [Horizontal] == 0) {
+ return (false);
+ }
+ }
+ }
+
+ return (true);
+}
+
+static void SetRandomField (void) {
+ int Spawner [] = { 2, 3, 5, 7 };
+
+ int X = 0, Y = 0;
+
+ Repeat:
+
+ X = GetRandomValue (0, BoardLimit - 1);
+ Y = GetRandomValue (0, BoardLimit - 1);
+
+ if (BoardState [Y] [X] == 0) {
+ BoardState [Y] [X] = Spawner [GetRandomValue (0, (int) (sizeof (Spawner) / sizeof (* Spawner)) - 1)];
+ } else {
+ goto Repeat;
+ }
+}
+
+static void SetRandomBoard (void) {
+ for (int Index = 0; Index < StartState; ++Index) {
+ SetRandomField ();
+ }
+}
+
+static void DrawState (void) {
+ int Margin = 7;
+ int Width = (GetScreenWidth () - BoardLimit * Margin) / BoardLimit - Margin;
+ int Height = (GetScreenHeight () - BoardLimit * Margin) / BoardLimit - Margin;
+ int FontSize = Height - 2 * Margin;
+
+ ClearBackground ((Color) { 23, 23, 23, 255 });
+
+ for (int Vertical = 0; Vertical < BoardLimit; ++Vertical) {
+ for (int Horizontal = 0; Horizontal < BoardLimit; ++Horizontal) {
+ int Number = BoardState [Vertical] [Horizontal];
+ int X = Margin + Horizontal * (Margin + Width);
+ int Y = Margin + Vertical * (Margin + Height);
+
+ Rectangle Data = { X, Y, Width, Height };
+
+ Color Tint = { 63, 63, 63, 255 };
+
+ //~Color Tint = {
+ //~(unsigned char) (0.9 * (float) ((Number * 255) / SevenHundredAndNine)),
+ //~(unsigned char) (0.6 * (float) ((Number * 255) / SevenHundredAndNine)),
+ //~(unsigned char) (0.3 * (float) ((Number * 255) / SevenHundredAndNine)),
+ //~255
+ //~};
+
+ DrawRectangleRounded (Data, 0.3, 3, Tint);
+
+ DrawText (TextFormat ("%i", Number), X + Margin, Y + Margin, FontSize, (Color) { 255, 255, 255, 255 });
+ }
+ }
+}
+
+static void DrawScore (void) {
+ int TotalScore = 0;
+
+ for (int Horizontal = 0; Horizontal < BoardLimit; ++Horizontal) {
+ for (int Vertical = 0; Vertical < BoardLimit; ++Vertical) {
+ TotalScore += BoardState [Vertical] [Horizontal];
+ }
+ }
+
+ DrawText (TextFormat ("%i", TotalScore), 0, 0, GetScreenHeight () / 3, (Color) { 255, 0, 0, 255 });
+}
+
+static int SumVertically (int Horizontal, int From, int To) {
+ int Sum = 0;
+
+ for (int Index = From; Index < To; ++Index) {
+ Sum += BoardState [Index] [Horizontal];
+ }
+
+ return (Sum);
+}
+
+static int SumHorizontally (int Vertical, int From, int To) {
+ int Sum = 0;
+
+ for (int Index = From; Index < To; ++Index) {
+ Sum += BoardState [Vertical] [Index];
+ }
+
+ return (Sum);
+}
+
+static void SumToTop (void) {
+ for (int Horizontal = 0; Horizontal < BoardLimit; ++Horizontal) {
+ for (int Vertical = 0; Vertical < BoardLimit; ++Vertical) {
+ for (int Index = BoardLimit; Index >= Vertical; --Index) {
+ int Sum = SumVertically (Horizontal, Vertical, Index);
+
+ if ((IsPrimeNumber (Sum)) && (Sum <= SevenHundredAndNine)) {
+ BoardState [Vertical] [Horizontal] = Sum;
+
+ for (int Replace = Vertical + 1; Replace < Index; ++Replace) {
+ BoardState [Replace] [Horizontal] = 0;
+ }
+
+ break;
+ }
+ }
+ }
+ }
+}
+
+static void SumToBottom (void) {
+ for (int Horizontal = BoardLimit - 1; Horizontal >= 0; --Horizontal) {
+ for (int Vertical = BoardLimit - 1; Vertical >= 0; --Vertical) {
+ for (int Index = 0; Index <= Vertical; ++Index) {
+ int Sum = SumVertically (Horizontal, Index, Vertical + 1);
+
+ if ((IsPrimeNumber (Sum)) && (Sum <= SevenHundredAndNine)) {
+ BoardState [Vertical] [Horizontal] = Sum;
+
+ for (int Replace = Vertical - 1; Replace >= Index; --Replace) {
+ BoardState [Replace] [Horizontal] = 0;
+ }
+
+ break;
+ }
+ }
+ }
+ }
+}
+
+static void SumToLeft (void) {
+ for (int Vertical = 0; Vertical < BoardLimit; ++Vertical) {
+ for (int Horizontal = 0; Horizontal < BoardLimit; ++Horizontal) {
+ for (int Index = BoardLimit; Index >= Horizontal; --Index) {
+ int Sum = SumHorizontally (Vertical, Horizontal, Index);
+
+ if ((IsPrimeNumber (Sum)) && (Sum <= SevenHundredAndNine)) {
+ BoardState [Vertical] [Horizontal] = Sum;
+
+ for (int Replace = Horizontal + 1; Replace < Index; ++Replace) {
+ BoardState [Vertical] [Replace] = 0;
+ }
+
+ break;
+ }
+ }
+ }
+ }
+}
+
+static void SumToRight (void) {
+ for (int Vertical = BoardLimit - 1; Vertical >= 0; --Vertical) {
+ for (int Horizontal = BoardLimit - 1; Horizontal >= 0; --Horizontal) {
+ for (int Index = 0; Index <= Horizontal; ++Index) {
+ int Sum = SumHorizontally (Vertical, Index, Horizontal + 1);
+
+ if ((IsPrimeNumber (Sum)) && (Sum <= SevenHundredAndNine)) {
+ BoardState [Vertical] [Horizontal] = Sum;
+
+ for (int Replace = Horizontal - 1; Replace >= Index; --Replace) {
+ BoardState [Vertical] [Replace] = 0;
+ }
+
+ break;
+ }
+ }
+ }
+ }
+}
+
+int main (void) {
+ SetTraceLogLevel (LOG_NONE);
+
+ InitWindow (960, 480, "x709 --- Raylib");
+
+ SetRandomSeed ((unsigned int) GetTime ());
+ SetRandomBoard ();
+
+ SetTargetFPS (60);
+ SetExitKey (KEY_Q);
+
+ while (! WindowShouldClose ()) {
+ BeginDrawing ();
+
+ DrawState ();
+
+ if (IsGameOver ()) {
+ DrawScore ();
+ EndDrawing ();
+
+ continue;
+ }
+
+ if (IsKeyPressed (KEY_W) || IsKeyPressed (KEY_UP)) {
+ SumToTop ();
+ SetRandomField ();
+ } else if (IsKeyPressed (KEY_S) || IsKeyPressed (KEY_DOWN)) {
+ SumToBottom ();
+ SetRandomField ();
+ } else if (IsKeyPressed (KEY_A) || IsKeyPressed (KEY_LEFT)) {
+ SumToLeft ();
+ SetRandomField ();
+ } else if (IsKeyPressed (KEY_D) || IsKeyPressed (KEY_RIGHT)) {
+ SumToRight ();
+ SetRandomField ();
+ }
+
+ EndDrawing ();
+ }
+
+ CloseWindow ();
+
+ return (0);
+}