diff options
| author | 2026-11-08 02:51:49 +0000 | |
|---|---|---|
| committer | 2026-11-08 02:51:49 +0000 | |
| commit | 80893fa3e272d9792ba1206f7006e8defeeccb6e (patch) | |
| tree | 1867dba062a481bf59313b0fbb16f416834a11a3 /src | |
| download | lab-02-master.tar.gz | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamic-array.h | 54 | ||||
| -rw-r--r-- | src/nim.hs | 65 | ||||
| -rw-r--r-- | src/peano-curve.c | 158 | ||||
| -rw-r--r-- | src/peano-curve.hs | 87 |
4 files changed, 364 insertions, 0 deletions
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 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 5 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 6 | #endif | ||
| 7 | |||
| 8 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ | ||
| 9 | ({ \ | ||
| 10 | if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ | ||
| 11 | if ((DYNAMIC_ARRAY).capacity == 0) \ | ||
| 12 | (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 13 | while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ | ||
| 14 | (DYNAMIC_ARRAY).capacity *= 2; \ | ||
| 15 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 16 | (DYNAMIC_ARRAY).capacity * \ | ||
| 17 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 18 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 19 | } \ | ||
| 20 | }) | ||
| 21 | |||
| 22 | #define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ | ||
| 23 | ({ \ | ||
| 24 | (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ | ||
| 25 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 26 | (DYNAMIC_ARRAY).capacity * \ | ||
| 27 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 28 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 29 | \ | ||
| 30 | }) | ||
| 31 | |||
| 32 | #define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ | ||
| 33 | ({ \ | ||
| 34 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ | ||
| 35 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ | ||
| 36 | }) | ||
| 37 | |||
| 38 | #define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 39 | ({ \ | ||
| 40 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ | ||
| 41 | memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ | ||
| 42 | (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 43 | (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ | ||
| 44 | }) | ||
| 45 | |||
| 46 | #define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) | ||
| 47 | |||
| 48 | #define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) | ||
| 49 | |||
| 50 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ | ||
| 51 | typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ | ||
| 52 | ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) | ||
| 53 | |||
| 54 | #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 @@ | |||
| 1 | module Nim where | ||
| 2 | |||
| 3 | import Data.Word (Word) | ||
| 4 | import Data.Bits (xor) | ||
| 5 | |||
| 6 | type Piles = [Word] | ||
| 7 | type Move = (Word, Word) | ||
| 8 | type Moves = [Move] | ||
| 9 | |||
| 10 | isGameOver :: Piles -> Bool | ||
| 11 | isGameOver = all (== 0) | ||
| 12 | |||
| 13 | makeMove :: Piles -> Move -> Piles | ||
| 14 | makeMove piles (index, take) = | ||
| 15 | [if i == fromIntegral index | ||
| 16 | then piles !! i - take | ||
| 17 | else piles !! i | ||
| 18 | | i <- [0..length piles - 1]] | ||
| 19 | |||
| 20 | computerMove :: Piles -> Move | ||
| 21 | computerMove piles = | ||
| 22 | let nimSum = foldl xor 0 piles | ||
| 23 | indexed = zip [0..] piles | ||
| 24 | viable = [(i, stones - (stones `xor` nimSum)) | | ||
| 25 | (i, stones) <- | ||
| 26 | indexed, 0 < stones && stones > stones `xor` nimSum] | ||
| 27 | fallback = head $ [(i, 1) | (i, stones) <- indexed, stones > 0] | ||
| 28 | in if (null viable) then fallback else head viable | ||
| 29 | |||
| 30 | playGame :: Piles -> Moves -> IO () | ||
| 31 | playGame piles userMoves = playGame' piles userMoves 0 | ||
| 32 | where | ||
| 33 | playGame' :: Piles -> Moves -> Word -> IO () | ||
| 34 | playGame' piles moves turn | ||
| 35 | | isGameOver piles = putStrLn "Игра окончена!" | ||
| 36 | | turn >= 100 = putStrLn "Игра завершена из-за лимита ходов." | ||
| 37 | | otherwise = do | ||
| 38 | putStrLn $ "Кучки: " ++ show piles | ||
| 39 | if turn `mod` 2 == 0 | ||
| 40 | then -- Ход пользователя | ||
| 41 | case dropWhile (\(index, take) -> | ||
| 42 | piles !! (fromIntegral index) < take) moves of | ||
| 43 | ((index, take):rest) -> do | ||
| 44 | putStrLn $ "Ход пользователя: убрать " ++ | ||
| 45 | show take ++ " из кучки " ++ show index | ||
| 46 | playGame' (makeMove piles (index, take)) rest (turn + 1) | ||
| 47 | _ -> putStrLn "У пользователя закончились ходы." | ||
| 48 | else -- Ход компьютера | ||
| 49 | let move = computerMove piles | ||
| 50 | (index, take) = move | ||
| 51 | piles' = makeMove piles move | ||
| 52 | in do | ||
| 53 | putStrLn $ "Ход компьютера: убрать " ++ | ||
| 54 | show take ++ " из кучки " ++ show index | ||
| 55 | playGame' piles' moves (turn + 1) | ||
| 56 | |||
| 57 | main :: IO () | ||
| 58 | main = do | ||
| 59 | let initialPiles = [1, 4, 5] | ||
| 60 | let userMoves = [(0, 2), (2, 1), (2, 1), (2, 1), (2, 1), (2, 1)] | ||
| 61 | putStrLn $ "У пользователя " ++ | ||
| 62 | if (foldl xor 0 initialPiles > 0) | ||
| 63 | then "есть выигрышная стратегия" | ||
| 64 | else "нет выигрышной стратегии" | ||
| 65 | 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 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <inttypes.h> | ||
| 5 | #include <assert.h> | ||
| 6 | #include <raylib.h> | ||
| 7 | #include <raymath.h> | ||
| 8 | #include <rlgl.h> | ||
| 9 | |||
| 10 | #define MALLOC malloc | ||
| 11 | #define REALLOC realloc | ||
| 12 | #define FREE free | ||
| 13 | #define ASSERT assert | ||
| 14 | |||
| 15 | #include "dynamic-array.h" | ||
| 16 | |||
| 17 | struct squares { | ||
| 18 | Vector2 *items; | ||
| 19 | float side; | ||
| 20 | uint16_t count; | ||
| 21 | uint16_t capacity; | ||
| 22 | }; | ||
| 23 | |||
| 24 | enum ordering { | ||
| 25 | ORDERING_NE = 0b00, | ||
| 26 | ORDERING_NW = 0b01, | ||
| 27 | ORDERING_SE = 0b10, | ||
| 28 | ORDERING_SW = 0b11, | ||
| 29 | }; | ||
| 30 | |||
| 31 | static enum ordering | ||
| 32 | ordering_next_vertical (enum ordering ord) | ||
| 33 | { | ||
| 34 | return ord ^ 0b01; | ||
| 35 | } | ||
| 36 | |||
| 37 | static enum ordering | ||
| 38 | ordering_next_horizontal (enum ordering ord) | ||
| 39 | { | ||
| 40 | return ord ^ 0b10; | ||
| 41 | } | ||
| 42 | |||
| 43 | static int | ||
| 44 | ordering_offset_vertical (enum ordering ord) | ||
| 45 | { | ||
| 46 | return 1 - (ord & 0b10); | ||
| 47 | } | ||
| 48 | |||
| 49 | static int | ||
| 50 | ordering_offset_horizontal (enum ordering ord) | ||
| 51 | { | ||
| 52 | return 1 - 2*(ord & 0b01); | ||
| 53 | } | ||
| 54 | |||
| 55 | static void | ||
| 56 | advance (struct squares *s) | ||
| 57 | { | ||
| 58 | struct squares tmp = { .side = s->side/3 }; | ||
| 59 | DYNAMIC_ARRAY_ENSURE_CAPACITY (tmp, 9*s->count); | ||
| 60 | |||
| 61 | enum ordering ord = ORDERING_NE; | ||
| 62 | Vector2 offset = { -1, -1 }; | ||
| 63 | for (uint32_t i = 0; i < s->count; ++i) { | ||
| 64 | for (int j = 0; j < 3; ++j) { | ||
| 65 | if (j) { | ||
| 66 | ord = ordering_next_horizontal (ord); | ||
| 67 | offset.x += ordering_offset_horizontal (ord); | ||
| 68 | } | ||
| 69 | for (int k = 0; k < 3; ++k) { | ||
| 70 | if (k) { | ||
| 71 | ord = ordering_next_vertical (ord); | ||
| 72 | offset.y += ordering_offset_vertical (ord); | ||
| 73 | } | ||
| 74 | |||
| 75 | Vector2 c = Vector2Add (s->items[i], Vector2Scale (offset, tmp.side)); | ||
| 76 | DYNAMIC_ARRAY_APPEND (tmp, c); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | if (i + 1 < s->count) { | ||
| 81 | if (s->items[i + 1].x != s->items[i + 0].x) { | ||
| 82 | ord = ordering_next_horizontal (ord); | ||
| 83 | offset.x += ordering_offset_horizontal (ord); | ||
| 84 | if (offset.x > +1) offset.x = -1; | ||
| 85 | else if (offset.x < -1) offset.x = +1; | ||
| 86 | } else { | ||
| 87 | ord = ordering_next_vertical (ord); | ||
| 88 | offset.y += ordering_offset_vertical (ord); | ||
| 89 | if (offset.y > +1) offset.y = -1; | ||
| 90 | else if (offset.y < -1) offset.y = +1; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | DYNAMIC_ARRAY_FREE (*s); | ||
| 96 | *s = tmp; | ||
| 97 | } | ||
| 98 | |||
| 99 | int | ||
| 100 | main (int argc, char *argv[]) | ||
| 101 | { | ||
| 102 | SetConfigFlags (FLAG_MSAA_4X_HINT | | ||
| 103 | FLAG_WINDOW_UNDECORATED | | ||
| 104 | FLAG_WINDOW_RESIZABLE | | ||
| 105 | FLAG_FULLSCREEN_MODE | | ||
| 106 | FLAG_VSYNC_HINT); | ||
| 107 | // InitWindow (600, 600, "Peano Curve"); | ||
| 108 | InitWindow (0, 0, "Peano Curve"); | ||
| 109 | |||
| 110 | Camera2D camera = { | ||
| 111 | .offset = { .5, .5 }, | ||
| 112 | }; | ||
| 113 | |||
| 114 | struct squares squares = { .side = 1. }; | ||
| 115 | DYNAMIC_ARRAY_APPEND (squares, ((Vector2) { .5, .5 })); | ||
| 116 | advance (&squares); | ||
| 117 | advance (&squares); | ||
| 118 | advance (&squares); | ||
| 119 | advance (&squares); | ||
| 120 | |||
| 121 | int draw_up_to = squares.count; | ||
| 122 | while (!WindowShouldClose ()) { | ||
| 123 | if (draw_up_to > 0 && IsKeyDown (KEY_DOWN)) | ||
| 124 | draw_up_to -= 1; | ||
| 125 | else if (draw_up_to < squares.count && IsKeyDown (KEY_UP)) | ||
| 126 | draw_up_to += 1; | ||
| 127 | |||
| 128 | Vector2 center = (Vector2) { GetScreenWidth ()/2, GetScreenHeight ()/2 }; | ||
| 129 | camera.zoom = fminf (center.x, center.y)*2; | ||
| 130 | |||
| 131 | BeginDrawing (); { | ||
| 132 | ClearBackground (BLACK); | ||
| 133 | BeginMode2D (camera); { | ||
| 134 | Vector2 head = Vector2Zero (); | ||
| 135 | for (uint32_t i = 0; i < squares.count; ++i) { | ||
| 136 | if (i == draw_up_to) break; | ||
| 137 | Vector2 c = squares.items[i]; | ||
| 138 | c.y = 1 - c.y; | ||
| 139 | Rectangle rec = { | ||
| 140 | .x = c.x - squares.side/2, | ||
| 141 | .y = c.y - squares.side/2, | ||
| 142 | .width = squares.side, | ||
| 143 | .height = squares.side, | ||
| 144 | }; | ||
| 145 | DrawRectangleLinesEx (rec, .0005, DARKGRAY); | ||
| 146 | if (i < squares.count - 1) { | ||
| 147 | Vector2 nc = squares.items[i + 1]; | ||
| 148 | nc.y = 1 - nc.y; | ||
| 149 | DrawLineEx (c, nc, 0.001, BLUE); | ||
| 150 | } | ||
| 151 | DrawCircleV (c, .0015, RED); | ||
| 152 | } | ||
| 153 | } EndMode2D (); | ||
| 154 | } EndDrawing (); | ||
| 155 | } | ||
| 156 | |||
| 157 | return EXIT_SUCCESS; | ||
| 158 | } | ||
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 @@ | |||
| 1 | module PeanoCurve (peanoCurve) where | ||
| 2 | |||
| 3 | import System.Environment (getArgs) | ||
| 4 | import Text.Read (readMaybe) | ||
| 5 | |||
| 6 | type Point = (Rational, Rational) | ||
| 7 | type Points = [Point] | ||
| 8 | |||
| 9 | splitNorthEast :: Point -> Rational -> Points | ||
| 10 | splitNorthEast (x, y) a = | ||
| 11 | [(x - a/3, y - a/3), | ||
| 12 | (x - a/3, y ), | ||
| 13 | (x - a/3, y + a/3), | ||
| 14 | (x, y + a/3), | ||
| 15 | (x, y ), | ||
| 16 | (x, y - a/3), | ||
| 17 | (x + a/3, y - a/3), | ||
| 18 | (x + a/3, y ), | ||
| 19 | (x + a/3, y + a/3)] | ||
| 20 | |||
| 21 | splitNorthWest :: Point -> Rational -> Points | ||
| 22 | splitNorthWest (x, y) a = | ||
| 23 | [(x + a/3, y - a/3), | ||
| 24 | (x + a/3, y ), | ||
| 25 | (x + a/3, y + a/3), | ||
| 26 | (x, y + a/3), | ||
| 27 | (x, y ), | ||
| 28 | (x, y - a/3), | ||
| 29 | (x - a/3, y - a/3), | ||
| 30 | (x - a/3, y ), | ||
| 31 | (x - a/3, y + a/3)] | ||
| 32 | |||
| 33 | splitSouthEast :: Point -> Rational -> Points | ||
| 34 | splitSouthEast (x, y) a = | ||
| 35 | [(x - a/3, y + a/3), | ||
| 36 | (x - a/3, y ), | ||
| 37 | (x - a/3, y - a/3), | ||
| 38 | (x, y - a/3), | ||
| 39 | (x, y ), | ||
| 40 | (x, y + a/3), | ||
| 41 | (x + a/3, y + a/3), | ||
| 42 | (x + a/3, y ), | ||
| 43 | (x + a/3, y - a/3)] | ||
| 44 | |||
| 45 | splitSouthWest :: Point -> Rational -> Points | ||
| 46 | splitSouthWest (x, y) a = | ||
| 47 | [(x + a/3, y + a/3), | ||
| 48 | (x + a/3, y ), | ||
| 49 | (x + a/3, y - a/3), | ||
| 50 | (x, y - a/3), | ||
| 51 | (x, y ), | ||
| 52 | (x, y + a/3), | ||
| 53 | (x - a/3, y + a/3), | ||
| 54 | (x - a/3, y ), | ||
| 55 | (x - a/3, y - a/3)] | ||
| 56 | |||
| 57 | advance :: Points -> Rational -> Int -> Int -> Points | ||
| 58 | advance points _ index 0 = [points !! index] | ||
| 59 | advance points a 0 iterations = | ||
| 60 | let advanced = splitNorthEast (head points) a | ||
| 61 | in concat [advance advanced (a/3) index (iterations - 1) | index <- [0..8]] | ||
| 62 | advance points a index 1 = | ||
| 63 | let (cx, cy) = (points !! index) | ||
| 64 | (lx, ly) = last $ advance points a (index - 1) 1 | ||
| 65 | (dx, dy) = (cx - lx, cy - ly) | ||
| 66 | in if dy > 0 && dx < 0 then splitNorthWest (cx, cy) a | ||
| 67 | else if dy > 0 && dx > 0 then splitNorthEast (cx, cy) a | ||
| 68 | else if dy < 0 && dx < 0 then splitSouthWest (cx, cy) a | ||
| 69 | else splitSouthEast (cx, cy) a | ||
| 70 | advance points a index iterations = | ||
| 71 | let advanced = concat [advance points a i 1 | i <- [0..index]] | ||
| 72 | from = 9*index | ||
| 73 | to = from + 8 | ||
| 74 | in concat [advance advanced (a/3) i (iterations - 1) | i <- [from..to]] | ||
| 75 | |||
| 76 | peanoCurve :: Int -> Points | ||
| 77 | peanoCurve iterations = advance [(1/2, 1/2)] 1 0 iterations | ||
| 78 | |||
| 79 | main :: IO () | ||
| 80 | main = do | ||
| 81 | args <- getArgs | ||
| 82 | case args of | ||
| 83 | (arg:_) -> do | ||
| 84 | case readMaybe arg :: Maybe Int of | ||
| 85 | Just i -> print $ map (\n -> peanoCurve n) [1..i] | ||
| 86 | Nothing -> error "Первый аргумент не является допустимым числом." | ||
| 87 | [] -> error "Никаких аргументов не было приведено." | ||
