From d82fa725fb03026ba073ae0fb5272ce2d221b8f4 Mon Sep 17 00:00:00 2001 From: xolatile Date: Thu, 26 Jun 2025 12:31:13 +0200 Subject: [PATCH] Dumping initial prototype... --- .gitignore | 1 + compile.sh | 7 ++ install.sh | 9 ++ x709.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 263 insertions(+) create mode 100644 .gitignore create mode 100644 compile.sh create mode 100644 install.sh create mode 100644 x709.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4a631e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +x709 diff --git a/compile.sh b/compile.sh new file mode 100644 index 0000000..36e14a4 --- /dev/null +++ b/compile.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -xe + +gcc -std=gnu99 -Wall -Wextra -Wpedantic -Werror -Ofast -o x709 x709.c -lraylib + +exit diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..e249c2f --- /dev/null +++ b/install.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -xe + +if [ -f x709 ]; then + cp -f x709 /usr/local/bin/x709 +fi + +exit diff --git a/x709.c b/x709.c new file mode 100644 index 0000000..27275f0 --- /dev/null +++ b/x709.c @@ -0,0 +1,246 @@ +#include + +#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); +} -- 2.39.5