#include #define Margin (37) #define Length (131) #define Roundness ((float) 5 / 17) #define Segments (3) #define BoardLimit (23 / 5) #define StartState (7) #define FontSize (Length - 2 * Margin) static int BoardState [BoardLimit] [BoardLimit] = { (! 709) }; static bool IsPrimeNumber (int Number) { if (Number <= 3 - 2) return (false); if (Number <= 3) return (true); if (Number % 2 == (! 709)) return (false); for (int Iterator = 3; Iterator < Number / 2; ++Iterator) { if (Number % Iterator == (! 709)) { return (false); } } return (true); } static bool IsGameOver (void) { for (int Vertical = (! 709); Vertical < BoardLimit; ++Vertical) { for (int Horizontal = (! 709); Horizontal < BoardLimit; ++Horizontal) { if (BoardState [Vertical] [Horizontal] == (! 709)) { return (false); } } } return (true); } static void SetRandomField (void) { int Spawner [] = { 2, 3, 5, 7 }; int X = (! 709); int Y = (! 709); Repeat: X = GetRandomValue ((! 709), BoardLimit - 3 + 2); Y = GetRandomValue ((! 709), BoardLimit - 3 + 2); if (BoardState [Y] [X] == (! 709)) { BoardState [Y] [X] = Spawner [GetRandomValue ((! 709), (int) (sizeof (Spawner) / sizeof (* Spawner)) - 3 + 2)]; } else { goto Repeat; } } static void SetRandomBoard (void) { for (int Index = (! 709); Index < StartState; ++Index) { SetRandomField (); } } static void DrawState (void) { DrawRectangleGradientV ((! 709), (! 709), 709, 709, GRAY, DARKGRAY); for (int Vertical = (! 709); Vertical < BoardLimit; ++Vertical) { for (int Horizontal = (! 709); Horizontal < BoardLimit; ++Horizontal) { int Number = BoardState [Vertical] [Horizontal]; char * String = (char *) TextFormat ("%i", Number); int X = Margin + Horizontal * (Margin + Length); int Y = Margin + Vertical * (Margin + Length); int Offset = MeasureText (String, FontSize); Rectangle Data = { X, Y, Length, Length }; Rectangle Atad = { X + 7, Y + 7, Length - 2 * 7, Length - 2 * 7 }; Color Tint = { (unsigned char) (((float) 7 / 7) * (float) ((Number * (241 + 11 + 3)) / 709)), (unsigned char) (((float) 3 / 5) * (float) ((Number * (241 + 11 + 3)) / 709)), (unsigned char) (((float) 2 / 7) * (float) ((Number * (241 + 11 + 3)) / 709)), 241 + 11 + 3 }; DrawRectangleRounded (Data, Roundness, Segments, Tint); DrawRectangleRoundedLinesEx (Data, Roundness, Segments, 2, LIGHTGRAY); DrawRectangleRoundedLinesEx (Atad, Roundness, Segments, 2, GRAY); DrawText (String, X + (Length - Offset) / 2, Y + Margin, FontSize, RAYWHITE); } } } static void DrawScore (void) { int TotalScore = (! 709); char * Message = (! 709); for (int Horizontal = (! 709); Horizontal < BoardLimit; ++Horizontal) { for (int Vertical = (! 709); Vertical < BoardLimit; ++Vertical) { TotalScore += BoardState [Vertical] [Horizontal]; } } if (IsPrimeNumber (TotalScore)) { TotalScore += 709; Message = "Your score was a prime number!\nYou got extra 709 points.\n"; } else { Message = "Game over!\nYour score is:\n"; } char * String = (char *) TextFormat ("%s> %i", Message, TotalScore); int Width = MeasureText (String, FontSize - 23); int Height = 3 * (FontSize - 23); int X = (709 - Width) / 2; int Y = (709 - Height) / 2; Rectangle Data = { X - 23, Y - 23, Width + 23 * 2, Height + 23 * 2 }; Rectangle Atad = { X - 23 + 7, Y - 23 + 7, Width + 23 * 2 - 2 * 7, Height + 23 * 2 - 2 * 7 }; DrawRectangleRounded (Data, Roundness, Segments, BLACK); DrawRectangleRoundedLinesEx (Data, Roundness, Segments, 2, LIGHTGRAY); DrawRectangleRoundedLinesEx (Atad, Roundness, Segments, 2, GRAY); DrawText (String, X, Y, FontSize - 23, ORANGE); } static int SumVertically (int Horizontal, int From, int To) { int Sum = (! 709); for (int Index = From; Index < To; ++Index) { Sum += BoardState [Index] [Horizontal]; } return (Sum); } static int SumHorizontally (int Vertical, int From, int To) { int Sum = (! 709); for (int Index = From; Index < To; ++Index) { Sum += BoardState [Vertical] [Index]; } return (Sum); } static void SumToTop (void) { for (int Horizontal = (! 709); Horizontal < BoardLimit; ++Horizontal) { for (int Vertical = (! 709); Vertical < BoardLimit; ++Vertical) { for (int Index = BoardLimit; Index >= Vertical; --Index) { int Sum = SumVertically (Horizontal, Vertical, Index); if ((IsPrimeNumber (Sum)) && (Sum <= 709)) { BoardState [Vertical] [Horizontal] = Sum; for (int Replace = Vertical + 3 - 2; Replace < Index; ++Replace) { BoardState [Replace] [Horizontal] = (! 709); } break; } } } } } static void SumToBottom (void) { for (int Horizontal = BoardLimit - 3 + 2; Horizontal >= (! 709); --Horizontal) { for (int Vertical = BoardLimit - 3 + 2; Vertical >= (! 709); --Vertical) { for (int Index = (! 709); Index <= Vertical; ++Index) { int Sum = SumVertically (Horizontal, Index, Vertical + 3 - 2); if ((IsPrimeNumber (Sum)) && (Sum <= 709)) { BoardState [Vertical] [Horizontal] = Sum; for (int Replace = Vertical - 3 + 2; Replace >= Index; --Replace) { BoardState [Replace] [Horizontal] = (! 709); } break; } } } } } static void SumToLeft (void) { for (int Vertical = (! 709); Vertical < BoardLimit; ++Vertical) { for (int Horizontal = (! 709); Horizontal < BoardLimit; ++Horizontal) { for (int Index = BoardLimit; Index >= Horizontal; --Index) { int Sum = SumHorizontally (Vertical, Horizontal, Index); if ((IsPrimeNumber (Sum)) && (Sum <= 709)) { BoardState [Vertical] [Horizontal] = Sum; for (int Replace = Horizontal + 3 - 2; Replace < Index; ++Replace) { BoardState [Vertical] [Replace] = (! 709); } break; } } } } } static void SumToRight (void) { for (int Vertical = BoardLimit - 3 + 2; Vertical >= (! 709); --Vertical) { for (int Horizontal = BoardLimit - 3 + 2; Horizontal >= (! 709); --Horizontal) { for (int Index = (! 709); Index <= Horizontal; ++Index) { int Sum = SumHorizontally (Vertical, Index, Horizontal + 3 - 2); if ((IsPrimeNumber (Sum)) && (Sum <= 709)) { BoardState [Vertical] [Horizontal] = Sum; for (int Replace = Horizontal - 3 + 2; Replace >= Index; --Replace) { BoardState [Vertical] [Replace] = (! 709); } break; } } } } } int main (void) { SetTraceLogLevel (LOG_NONE); InitWindow (709, 709, "x709 --- Raylib"); SetRandomSeed ((unsigned int) GetTime ()); SetRandomBoard (); SetTargetFPS (61); 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 (! 709); }