From 80893fa3e272d9792ba1206f7006e8defeeccb6e Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 05:51:49 +0300 Subject: publish --- src/dynamic-array.h | 54 ++++++++++++++++++ src/nim.hs | 65 +++++++++++++++++++++ src/peano-curve.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/peano-curve.hs | 87 +++++++++++++++++++++++++++++ 4 files changed, 364 insertions(+) create mode 100644 src/dynamic-array.h create mode 100644 src/nim.hs create mode 100644 src/peano-curve.c create mode 100644 src/peano-curve.hs (limited to 'src') diff --git a/src/dynamic-array.h b/src/dynamic-array.h new file mode 100644 index 0000000..9c90837 --- /dev/null +++ b/src/dynamic-array.h @@ -0,0 +1,54 @@ +#ifndef INCLUDED_DYNAMIC_ARRAY_H +#define INCLUDED_DYNAMIC_ARRAY_H + +#ifndef DYNAMIC_ARRAY_INIT_CAP +#define DYNAMIC_ARRAY_INIT_CAP 256 +#endif + +#define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ + ({ \ + if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ + if ((DYNAMIC_ARRAY).capacity == 0) \ + (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ + while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ + (DYNAMIC_ARRAY).capacity *= 2; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + } \ + }) + +#define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ + ({ \ + (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + \ + }) + +#define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ + }) + +#define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ + memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ + (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ + (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ + }) + +#define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) + +#define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) + +#define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ + typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ + ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) + +#endif diff --git a/src/nim.hs b/src/nim.hs new file mode 100644 index 0000000..040c167 --- /dev/null +++ b/src/nim.hs @@ -0,0 +1,65 @@ +module Nim where + +import Data.Word (Word) +import Data.Bits (xor) + +type Piles = [Word] +type Move = (Word, Word) +type Moves = [Move] + +isGameOver :: Piles -> Bool +isGameOver = all (== 0) + +makeMove :: Piles -> Move -> Piles +makeMove piles (index, take) = + [if i == fromIntegral index + then piles !! i - take + else piles !! i + | i <- [0..length piles - 1]] + +computerMove :: Piles -> Move +computerMove piles = + let nimSum = foldl xor 0 piles + indexed = zip [0..] piles + viable = [(i, stones - (stones `xor` nimSum)) | + (i, stones) <- + indexed, 0 < stones && stones > stones `xor` nimSum] + fallback = head $ [(i, 1) | (i, stones) <- indexed, stones > 0] + in if (null viable) then fallback else head viable + +playGame :: Piles -> Moves -> IO () +playGame piles userMoves = playGame' piles userMoves 0 + where + playGame' :: Piles -> Moves -> Word -> IO () + playGame' piles moves turn + | isGameOver piles = putStrLn "Игра окончена!" + | turn >= 100 = putStrLn "Игра завершена из-за лимита ходов." + | otherwise = do + putStrLn $ "Кучки: " ++ show piles + if turn `mod` 2 == 0 + then -- Ход пользователя + case dropWhile (\(index, take) -> + piles !! (fromIntegral index) < take) moves of + ((index, take):rest) -> do + putStrLn $ "Ход пользователя: убрать " ++ + show take ++ " из кучки " ++ show index + playGame' (makeMove piles (index, take)) rest (turn + 1) + _ -> putStrLn "У пользователя закончились ходы." + else -- Ход компьютера + let move = computerMove piles + (index, take) = move + piles' = makeMove piles move + in do + putStrLn $ "Ход компьютера: убрать " ++ + show take ++ " из кучки " ++ show index + playGame' piles' moves (turn + 1) + +main :: IO () +main = do + let initialPiles = [1, 4, 5] + let userMoves = [(0, 2), (2, 1), (2, 1), (2, 1), (2, 1), (2, 1)] + putStrLn $ "У пользователя " ++ + if (foldl xor 0 initialPiles > 0) + then "есть выигрышная стратегия" + else "нет выигрышной стратегии" + playGame initialPiles userMoves diff --git a/src/peano-curve.c b/src/peano-curve.c new file mode 100644 index 0000000..0b4a4ec --- /dev/null +++ b/src/peano-curve.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define MALLOC malloc +#define REALLOC realloc +#define FREE free +#define ASSERT assert + +#include "dynamic-array.h" + +struct squares { + Vector2 *items; + float side; + uint16_t count; + uint16_t capacity; +}; + +enum ordering { + ORDERING_NE = 0b00, + ORDERING_NW = 0b01, + ORDERING_SE = 0b10, + ORDERING_SW = 0b11, +}; + +static enum ordering +ordering_next_vertical (enum ordering ord) +{ + return ord ^ 0b01; +} + +static enum ordering +ordering_next_horizontal (enum ordering ord) +{ + return ord ^ 0b10; +} + +static int +ordering_offset_vertical (enum ordering ord) +{ + return 1 - (ord & 0b10); +} + +static int +ordering_offset_horizontal (enum ordering ord) +{ + return 1 - 2*(ord & 0b01); +} + +static void +advance (struct squares *s) +{ + struct squares tmp = { .side = s->side/3 }; + DYNAMIC_ARRAY_ENSURE_CAPACITY (tmp, 9*s->count); + + enum ordering ord = ORDERING_NE; + Vector2 offset = { -1, -1 }; + for (uint32_t i = 0; i < s->count; ++i) { + for (int j = 0; j < 3; ++j) { + if (j) { + ord = ordering_next_horizontal (ord); + offset.x += ordering_offset_horizontal (ord); + } + for (int k = 0; k < 3; ++k) { + if (k) { + ord = ordering_next_vertical (ord); + offset.y += ordering_offset_vertical (ord); + } + + Vector2 c = Vector2Add (s->items[i], Vector2Scale (offset, tmp.side)); + DYNAMIC_ARRAY_APPEND (tmp, c); + } + } + + if (i + 1 < s->count) { + if (s->items[i + 1].x != s->items[i + 0].x) { + ord = ordering_next_horizontal (ord); + offset.x += ordering_offset_horizontal (ord); + if (offset.x > +1) offset.x = -1; + else if (offset.x < -1) offset.x = +1; + } else { + ord = ordering_next_vertical (ord); + offset.y += ordering_offset_vertical (ord); + if (offset.y > +1) offset.y = -1; + else if (offset.y < -1) offset.y = +1; + } + } + } + + DYNAMIC_ARRAY_FREE (*s); + *s = tmp; +} + +int +main (int argc, char *argv[]) +{ + SetConfigFlags (FLAG_MSAA_4X_HINT | + FLAG_WINDOW_UNDECORATED | + FLAG_WINDOW_RESIZABLE | + FLAG_FULLSCREEN_MODE | + FLAG_VSYNC_HINT); + // InitWindow (600, 600, "Peano Curve"); + InitWindow (0, 0, "Peano Curve"); + + Camera2D camera = { + .offset = { .5, .5 }, + }; + + struct squares squares = { .side = 1. }; + DYNAMIC_ARRAY_APPEND (squares, ((Vector2) { .5, .5 })); + advance (&squares); + advance (&squares); + advance (&squares); + advance (&squares); + + int draw_up_to = squares.count; + while (!WindowShouldClose ()) { + if (draw_up_to > 0 && IsKeyDown (KEY_DOWN)) + draw_up_to -= 1; + else if (draw_up_to < squares.count && IsKeyDown (KEY_UP)) + draw_up_to += 1; + + Vector2 center = (Vector2) { GetScreenWidth ()/2, GetScreenHeight ()/2 }; + camera.zoom = fminf (center.x, center.y)*2; + + BeginDrawing (); { + ClearBackground (BLACK); + BeginMode2D (camera); { + Vector2 head = Vector2Zero (); + for (uint32_t i = 0; i < squares.count; ++i) { + if (i == draw_up_to) break; + Vector2 c = squares.items[i]; + c.y = 1 - c.y; + Rectangle rec = { + .x = c.x - squares.side/2, + .y = c.y - squares.side/2, + .width = squares.side, + .height = squares.side, + }; + DrawRectangleLinesEx (rec, .0005, DARKGRAY); + if (i < squares.count - 1) { + Vector2 nc = squares.items[i + 1]; + nc.y = 1 - nc.y; + DrawLineEx (c, nc, 0.001, BLUE); + } + DrawCircleV (c, .0015, RED); + } + } EndMode2D (); + } EndDrawing (); + } + + return EXIT_SUCCESS; +} diff --git a/src/peano-curve.hs b/src/peano-curve.hs new file mode 100644 index 0000000..54461bd --- /dev/null +++ b/src/peano-curve.hs @@ -0,0 +1,87 @@ +module PeanoCurve (peanoCurve) where + +import System.Environment (getArgs) +import Text.Read (readMaybe) + +type Point = (Rational, Rational) +type Points = [Point] + +splitNorthEast :: Point -> Rational -> Points +splitNorthEast (x, y) a = + [(x - a/3, y - a/3), + (x - a/3, y ), + (x - a/3, y + a/3), + (x, y + a/3), + (x, y ), + (x, y - a/3), + (x + a/3, y - a/3), + (x + a/3, y ), + (x + a/3, y + a/3)] + +splitNorthWest :: Point -> Rational -> Points +splitNorthWest (x, y) a = + [(x + a/3, y - a/3), + (x + a/3, y ), + (x + a/3, y + a/3), + (x, y + a/3), + (x, y ), + (x, y - a/3), + (x - a/3, y - a/3), + (x - a/3, y ), + (x - a/3, y + a/3)] + +splitSouthEast :: Point -> Rational -> Points +splitSouthEast (x, y) a = + [(x - a/3, y + a/3), + (x - a/3, y ), + (x - a/3, y - a/3), + (x, y - a/3), + (x, y ), + (x, y + a/3), + (x + a/3, y + a/3), + (x + a/3, y ), + (x + a/3, y - a/3)] + +splitSouthWest :: Point -> Rational -> Points +splitSouthWest (x, y) a = + [(x + a/3, y + a/3), + (x + a/3, y ), + (x + a/3, y - a/3), + (x, y - a/3), + (x, y ), + (x, y + a/3), + (x - a/3, y + a/3), + (x - a/3, y ), + (x - a/3, y - a/3)] + +advance :: Points -> Rational -> Int -> Int -> Points +advance points _ index 0 = [points !! index] +advance points a 0 iterations = + let advanced = splitNorthEast (head points) a + in concat [advance advanced (a/3) index (iterations - 1) | index <- [0..8]] +advance points a index 1 = + let (cx, cy) = (points !! index) + (lx, ly) = last $ advance points a (index - 1) 1 + (dx, dy) = (cx - lx, cy - ly) + in if dy > 0 && dx < 0 then splitNorthWest (cx, cy) a + else if dy > 0 && dx > 0 then splitNorthEast (cx, cy) a + else if dy < 0 && dx < 0 then splitSouthWest (cx, cy) a + else splitSouthEast (cx, cy) a +advance points a index iterations = + let advanced = concat [advance points a i 1 | i <- [0..index]] + from = 9*index + to = from + 8 + in concat [advance advanced (a/3) i (iterations - 1) | i <- [from..to]] + +peanoCurve :: Int -> Points +peanoCurve iterations = advance [(1/2, 1/2)] 1 0 iterations + +main :: IO () +main = do + args <- getArgs + case args of + (arg:_) -> do + case readMaybe arg :: Maybe Int of + Just i -> print $ map (\n -> peanoCurve n) [1..i] + Nothing -> error "Первый аргумент не является допустимым числом." + [] -> error "Никаких аргументов не было приведено." -- cgit v1.3.1