From 4ddf6f1a0411ed147ba7a2ac178c186ebef374b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Apr 2017 15:21:04 +0200 Subject: [PATCH] Implement the othello board printing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit And commit the forgotten bits ... Signed-off-by: Jérôme Benoit --- Makefile | 183 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/constants.h | 4 ++ lib/othello.c | 19 +++++ lib/othello.h | 22 ++++++ lib/ui.c | 53 ++++++++------ src/main.c | 40 +++++++++++ src/othello.c | 17 ----- 7 files changed, 300 insertions(+), 38 deletions(-) create mode 100644 Makefile create mode 100644 lib/othello.c create mode 100644 lib/othello.h create mode 100644 src/main.c delete mode 100644 src/othello.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..584ff34 --- /dev/null +++ b/Makefile @@ -0,0 +1,183 @@ +# Sample Makefile to build simple project. +# +# This Makefile expect all source files (.c) to be at the same level, in the +# $(SRC_PATH) directory. +# +# This Makefile expect all embedded library source files (.c) to be at the same level, in the +# $(LIBRARY_PATH) directory. +# +# It will automatically generate dependencies, compile all files, and produce a +# binary using the provided name linked against the library if necessary. +# +# Set BINARY_NAME to the name of the binary file to build. +# Set LIBRARY_NAME to the name of the library file to build. +# The default path for the library code and object is lib. +# By default the linker will look for $(BINARY_NAME) library name. +# Set BUILD_TYPE to either debug or release. +# +# Automatic dependencies code from: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr +BINARY_NAME:=othello +SRC_PATH:=src +# will add -lothello automagically linking flags +LIBRARY_NAME:=libothello +LIBRARY_PATH:=lib +LDLIBS:=-lncurses -ltinfo +BUILD_TYPE:=debug +#BUILD_TYPE:=release + +# ==================================== +# DO NOT CHANGE STUFF BEYOND THIS LINE +# ==================================== + +CC=gcc +LD=gcc +AR=ar + +WARN_FLAGS = -Wall -Wextra +STD_FLAG = -std=c11 +UNAME := $(shell uname -o) + +ifeq ($(BUILD_TYPE),debug) +BUILDDIR := .build/debug +DEBUG_FLAG = -g +STRIP_FLAG = +OPTI_FLAG = -O0 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +else +BUILDDIR := .build/release +DEBUG_FLAG = +STRIP_FLAG = -s +OPTI_FLAG = -O3 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +endif + +BINARY_SUFFIX := +DLIB_PREFIX := +SDLIB_PREFIX := +DLIB_SUFFIX := .so +SLIB_SUFFIX := .a + +OBJDIR := $(BUILDDIR)/objs +$(shell mkdir -p $(OBJDIR)) + +SRCS=$(wildcard $(SRC_PATH)/*.c) +LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c) +OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS))) +LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS))) + +DEPDIR := $(BUILDDIR)/deps +$(shell mkdir -p $(DEPDIR)) +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td +POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d + +ifeq ($(UNAME),Cygwin) +CYGWIN_DLIBRARY_NAME := $(subst lib,cyg,$(LIBRARY_NAME)) +GOLD_SUPPORT = no +# Cygwin ncurses do not contain tinfo +LDLIBS := $(filter-out -ltinfo,$(LDLIBS)) +BINARY_SUFFIX := .exe +DLIB_SUFFIX := .dll +# do not archive external libraries +CYGWIN_LDLIBS_NOARCHIVE := $(LDLIBS) +CYGWIN_LIBLDFLAGS := -Wl,--out-implib=$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a \ + -Wl,--export-all-symbols \ + -Wl,--enable-auto-import +endif + +ifeq ($(LTO_SUPPORT),yes) +CFLAGS_LTO = -flto -ffat-lto-objects +LDFLAGS_LTO = -fuse-linker-plugin -flto +endif + +ifeq ($(GOLD_SUPPORT),yes) +LDFLAGS_GOLD = -fuse-ld=gold +endif + +# Putting header files in the source directory is not the purpose of this INCLUDES_PATH variable +INCLUDES_PATH := $(INCLUDES_PATH) -I$(LIBRARY_PATH) +CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES_PATH) +LIBCFLAGS := -fPIC $(CFLAGS) +LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) +LIBLDFLAGS := -shared $(LDFLAGS) +STATICLIBLDFLAGS := -static $(LDFLAGS) +LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME) + +all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX) $(BINARY_NAME).static$(BINARY_SUFFIX) + +$(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX): $(LIBOBJS) + @echo "[AR StO] $@" + @$(AR) rcs $@ $^ + +ifeq ($(UNAME),Cygwin) +$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a: $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX) +$(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS) + @echo "[LD DLL] $@" + @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $(CYGWIN_LIBLDFLAGS) -Wl,--whole-archive $^ -Wl,--no-whole-archive $(CYGWIN_LDLIBS_NOARCHIVE) -o $@ +else +$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS) + @echo "[LD ShO] $@" + @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@ +endif + +#$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBOBJS) +# @echo "[LD ] $@" +# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX) + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME).static$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX) + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@ + +ifeq ($(UNAME),Cygwin) +$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a +else +$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX) +endif + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d + @echo "[C ] $(notdir $*)" + @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) + +$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d + @echo "[C ] $(notdir $*)" + @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS)))) + +$(DEPDIR)/%.d: ; + +.PRECIOUS: $(DEPDIR)/%.d + +#FIXME: add an install target + +clean: + @echo "[CLN]" + -@rm -r $(BUILDDIR) + -@rm $(BINARY_NAME)$(BINARY_SUFFIX) + -@rm $(BINARY_NAME).static$(BINARY_SUFFIX) + -@rm $(BINARY_NAME).dynamic$(BINARY_SUFFIX) + -@rm $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX) +ifeq ($(UNAME),Cygwin) + -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX) + -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a +else + -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX) +endif + +disassemble: $(BINARY_NAME)$(BINARY_SUFFIX) + objdump -d $< | less + +symbols: $(BINARY_NAME)$(BINARY_SUFFIX) + objdump -t $< | sort | less diff --git a/lib/constants.h b/lib/constants.h index 431b796..74ea1d8 100644 --- a/lib/constants.h +++ b/lib/constants.h @@ -19,5 +19,9 @@ #ifndef CONSTANTS_H #define CONSTANTS_H +const int empty = 0; +const int white = 1; +const int black = 2; + #endif /* CONSTANTS_H */ diff --git a/lib/othello.c b/lib/othello.c new file mode 100644 index 0000000..0cd65a4 --- /dev/null +++ b/lib/othello.c @@ -0,0 +1,19 @@ +/* + * ===================================================================================== + * + * Filename: othello.c + * + * Description: Handle the othello board content + * + * Version: 1.0 + * Created: 25/04/2017 15:16:08 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + + diff --git a/lib/othello.h b/lib/othello.h new file mode 100644 index 0000000..e590b02 --- /dev/null +++ b/lib/othello.h @@ -0,0 +1,22 @@ +/* + * ===================================================================================== + * + * Filename: othello.h + * + * Description: Header for othello board handling functions + * + * Version: 1.0 + * Created: 25/04/2017 15:16:37 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#ifndef OTHELLO_H +#define OTHELLO_H + +#endif /* OTHELLO_H */ diff --git a/lib/ui.c b/lib/ui.c index cee08f1..c5f83b7 100644 --- a/lib/ui.c +++ b/lib/ui.c @@ -26,39 +26,52 @@ /* FIXME: one can split this shape in building blocks and build it using them */ void print_board(int y, int x) { - mvprintw(y, x, " | |"); - mvprintw(y+1, x, " | |"); - mvprintw(y+2, x, "----+----+----"); - mvprintw(y+3, x, " | |"); - mvprintw(y+4, x, " | |"); - mvprintw(y+5, x, "----+----+----"); - mvprintw(y+6, x, " | |"); - mvprintw(y+7, x, " | |"); + mvprintw(y, x, " A | B | C | D | E | F | G | H"); + mvprintw(y+1, x, " -----+----+----+----+----+----+----+-----"); + mvprintw(y+2, x, "1| | | | | | | | |"); + mvprintw(y+3, x, " | | | | | | | | |"); + mvprintw(y+4, x, " +----+----+----+----+----+----+----+----+"); + mvprintw(y+5, x, "2| | | | | | | | |"); + mvprintw(y+6, x, " | | | | | | | | |"); + mvprintw(y+7, x, " +----+----+----+----+----+----+----+----+"); + mvprintw(y+8, x, "3| | | | | | | | |"); + mvprintw(y+9, x, " | | | | | | | | |"); + mvprintw(y+10, x, " +----+----+----+----+----+----+----+----+"); + mvprintw(y+11, x, "4| | | | | | | | |"); + mvprintw(y+12, x, " | | | | | | | | |"); + mvprintw(y+13, x, " +----+----+----+----+----+----+----+----+"); + mvprintw(y+14, x, "5| | | | | | | | |"); + mvprintw(y+15, x, " | | | | | | | | |"); + mvprintw(y+16, x, " +----+----+----+----+----+----+----+----+"); + mvprintw(y+17, x, "7| | | | | | | | |"); + mvprintw(y+18, x, " | | | | | | | | |"); + mvprintw(y+19, x, " +----+----+----+----+----+----+----+----+"); + mvprintw(y+20, x, "8| | | | | | | | |"); + mvprintw(y+21, x, " | | | | | | | | |"); + mvprintw(y+22, x, " -----+----+----+----+----+----+----+-----"); } -static void print_x(int y, int x) { +static void print_x(int y, int x, int type) { mvprintw(y, x, "\\/"); mvprintw(y+1, x,"/\\"); } -static void print_o(int y, int x) { +static void print_o(int y, int x, int type) { mvprintw(y, x, "/\\"); mvprintw(y+1, x, "\\/"); } -/* y: 1 -> +0 x: 1 -> +1 - * 2 -> +3 2 -> +6 - * 3 -> +6 3 -> +11 */ +/* y = 1: y -> 0 x = 1: 1 -> 1 + * y > 2: y -> y + 3 2 -> +6 */ + static int remap_y(int y) { if (y == 1) { return 0; - } else if (y == 2) { - return 3; - } else { - return 6; + } else if (y > 1) { + return y + 3; } } @@ -66,9 +79,7 @@ static int remap_x(int x) { if (x == 1) { return 1; - } else if (x == 2) { - return 6; - } else { - return 11; + } else if (x > 2) { + return x + 5; } } diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ead55ef --- /dev/null +++ b/src/main.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +#include "ui.h" + +int main() { + int row = 0, col = 0; + int key_pressed = 0; + bool exit_condition = false; + + initscr(); + if (has_colors() == false) { + endwin(); + printf("Votre terminal ne supporte pas les couleurs\n"); + exit(EXIT_FAILURE); + } + start_color(); + getmaxyx(stdscr, row, col); + noecho(); + curs_set(0); + + /* center the board */ + int center_board_y = row/2 - 23/2; + int center_board_x = col/2 - 40/2; + + do { + print_board(center_board_y, center_board_x); + + key_pressed = getch(); + if (key_pressed == 'q') { + exit_condition = true; + } + } while (!exit_condition); + + endwin(); + + exit(EXIT_SUCCESS); +} diff --git a/src/othello.c b/src/othello.c deleted file mode 100644 index 650efa9..0000000 --- a/src/othello.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include -#include - -int main() { - int row = 0, col = 0; - - initscr(); - getmaxyx(stdscr, row, col); - noecho(); - curs_set(0); - - endwin(); - - exit(EXIT_SUCCESS); -} -- 2.34.1