commit cc107f975c9c551c5ffe9aff46f568477f30c037 Author: Julia Lange Date: Sun Jul 14 02:45:09 2024 -0700 Add meta-files, board structure, and alloc function Adds meta-files such as the Makefile and flake.lock/nix for easier project management. Adds board structure in connect4.h, as well as some helper functions such as the make_board function to create a board, and the print_board function to view the board after creation. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e8e07c9 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -g -Wall -pedantic + +connect4.c main.c: connect4.h + +TARGET = connect4 +CFILES = main.c connect4.c +OFILES = ${CFILES:.c=.o} + +all: ${OFILES} + $(CC) $(CFLAGS) -o ${TARGET} ${OFILES} + +clean: + rm -r ${TARGET} ${OFILES} diff --git a/connect4.c b/connect4.c new file mode 100644 index 0000000..8e397cb --- /dev/null +++ b/connect4.c @@ -0,0 +1,43 @@ +#include "connect4.h" +#include +#include +#include + +struct Board *make_board(size_t height, size_t width) +{ + struct Board *new_board = malloc(sizeof(*new_board)); + if (!new_board) + return NULL; + new_board->tilemap = calloc(height*width, sizeof(*(new_board->tilemap))); + if (!new_board->tilemap) + goto out_tilemap; + new_board->tile_heights = calloc(width, sizeof(*(new_board->tile_heights))); + if (!new_board->tile_heights) + goto out_tile_heights; + + new_board->height = height; + new_board->width = width; + new_board->next_player = RED; + + return new_board; + +out_tile_heights: + free(new_board->tilemap); +out_tilemap: + free(new_board); + return NULL; +} + +int drop_tile(struct Board *board, size_t drop_pos) { + return 0; +} + +void print_board(struct Board *board) { + for (int j = 0; j < board->height; j++) { + for (int i = 0; i < board->width; i++) { + printf("%d", IDX(i, j, board)); + } + printf("\n"); + } + +} diff --git a/connect4.h b/connect4.h new file mode 100644 index 0000000..0084e6d --- /dev/null +++ b/connect4.h @@ -0,0 +1,31 @@ +#ifndef CONNECT4_H_ +#define CONNECT4_H_ + +#include + +enum Tile { + EMPTY = 0, + RED, + BLACK +}; + +struct Board { + size_t height; + size_t width; + size_t *tile_heights; + enum Tile *tilemap; + enum Tile next_player; +}; + +#define IDX(i, j, board) (board->tilemap[j*(board->height) + i]) + +// Returns a board struct with height and width based on parameters +struct Board *make_board(size_t height, size_t width); + +// Drops a tile, returns 0 if successful, -1 if error, and 1 if the move won +int drop_tile(struct Board *board, size_t drop_pos); + +// Prints the supplied board +void print_board(struct Board *board); + +#endif // !CONNECT4_H_ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..40d4d4a --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1720768451, + "narHash": "sha256-EYekUHJE2gxeo2pM/zM9Wlqw1Uw2XTJXOSAO79ksc4Y=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "7e7c39ea35c5cdd002cd4588b03a3fb9ece6fad9", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..479c7f8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,28 @@ +{ + description = "C Development"; + + inputs = { + nixpkgs.url = "nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs }: + let + system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; + libs = with pkgs; [ + git + gnumake + gcc + gdb + # clang_16 + # clang-tools_16 + # lld_16 + # llvmPackages_16.libllvm + ]; + in { + devShells.${system}.default = pkgs.mkShell { + buildInputs = libs; + LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath libs; + }; + }; +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..eb5960a --- /dev/null +++ b/main.c @@ -0,0 +1,10 @@ +#include "connect4.h" +#include +#include + +int main(int argc, char *argv[]) +{ + struct Board *my_board = make_board(6, 7); + print_board(my_board); + return EXIT_SUCCESS; +}