diff --git a/.gitignore b/.gitignore index ce733a9..fd8eb11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -/2048* -*~ +2048* +*.o +*.core diff --git a/.replit b/.replit new file mode 100644 index 0000000..560dc58 --- /dev/null +++ b/.replit @@ -0,0 +1,2 @@ +language = "bash" +run = "make && ./2048" diff --git a/18n/es_AR/LC_MESSAGES/gfx_curses.mo b/18n/es_AR/LC_MESSAGES/gfx_curses.mo new file mode 100644 index 0000000..1e442ee Binary files /dev/null and b/18n/es_AR/LC_MESSAGES/gfx_curses.mo differ diff --git a/18n/es_AR/LC_MESSAGES/gfx_terminal.mo b/18n/es_AR/LC_MESSAGES/gfx_terminal.mo new file mode 100644 index 0000000..36069f3 Binary files /dev/null and b/18n/es_AR/LC_MESSAGES/gfx_terminal.mo differ diff --git a/18n/es_AR/LC_MESSAGES/highscore.mo b/18n/es_AR/LC_MESSAGES/highscore.mo new file mode 100644 index 0000000..bccdd3d Binary files /dev/null and b/18n/es_AR/LC_MESSAGES/highscore.mo differ diff --git a/18n/fr_FR/LC_MESSAGES/gfx_curses.mo b/18n/fr_FR/LC_MESSAGES/gfx_curses.mo new file mode 100644 index 0000000..45cf837 Binary files /dev/null and b/18n/fr_FR/LC_MESSAGES/gfx_curses.mo differ diff --git a/18n/fr_FR/LC_MESSAGES/gfx_terminal.mo b/18n/fr_FR/LC_MESSAGES/gfx_terminal.mo new file mode 100644 index 0000000..01989e4 Binary files /dev/null and b/18n/fr_FR/LC_MESSAGES/gfx_terminal.mo differ diff --git a/18n/fr_FR/LC_MESSAGES/highscore.mo b/18n/fr_FR/LC_MESSAGES/highscore.mo new file mode 100644 index 0000000..555ae33 Binary files /dev/null and b/18n/fr_FR/LC_MESSAGES/highscore.mo differ diff --git a/HowToTranslate.md b/HowToTranslate.md new file mode 100644 index 0000000..f06e8b4 --- /dev/null +++ b/HowToTranslate.md @@ -0,0 +1,53 @@ + +# How to translate 2048 to another language. + +First, if you do not have Gettext, get it by entering the following command by terminal + +``` +sudo apt-get install gettext +``` + +We aim to have the following structure for our directory: + +./po/language_name/file.po + +So, go to the ./po directory and then create a directory with your language standard name. + +for example: + +``` +mkdir po/es_AR +``` + +The next step is to generate the .po files with the following command (change 'es_AR' by your language standard name): +``` +msginit --input=po/gfx_curses.pot --locale=es_AR --output=po/es_AR/gfx_curses.po +msginit --input=po/gfx_terminal.pot --locale=es_AR --output=po/es_AR/gfx_terminal.po +msginit --input=po/highscore.pot --locale=es_AR --output=po/es_AR/highscore.po +``` + +### Translation + +Now, we can translate the strings: + +so, go to ./po/language_name/ and translate the strings in the .po files with a text editor. + +for example: +![Screenshot](https://k62.kn3.net/5/0/D/B/4/4/870.png) + + +Nwe should create the directory where we'll place the .mo files (change es_AR by your language standard name): + +``` +mkdir -p 18n/es_AR/LC_MESSAGES +``` + +The last step is to generate the .mo file: + +``` +msgfmt --output-file=18n/es_AR/LC_MESSAGES/gfx_curses.mo po/es_AR/gfx_curses.po +msgfmt --output-file=18n/es_AR/LC_MESSAGES/gfx_terminal.mo po/es_AR/gfx_terminal.po +msgfmt --output-file=18n/es_AR/LC_MESSAGES/highscore.mo po/es_AR/highscore.po +``` + +## And this is it! diff --git a/Makefile b/Makefile index 4ba965f..0003ea0 100644 --- a/Makefile +++ b/Makefile @@ -1,25 +1,24 @@ -ASCIIDOC ?= a2x -CC ?= gcc -CFLAGS := -Wall -Wextra $(CFLAGS) -LIBS = -lcurses +CC ?= clang +CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra +CFLAGS += -DINVERT_COLORS -DVT100 -O2 +LFLAGS += -.PHONY: clean man man-nc +PROGRAM := 2048 +C_FILES := $(wildcard src/*.c) +MERGE_FILE := src/merge_std.c +FILTERED_C_FILES := $(filter-out src/gfx%.c src/merge%.c, $(C_FILES)) -2048: src/2048.c - $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) src/2048.c -o 2048 +all: terminal -2048nc: src/2048.c - $(CC) -DNO_CURSES=1 $(CFLAGS) $(LDFLAGS) src/2048.c -o 2048nc +curses: $(FILTERED_C_FILES) src/gfx_curses.c + $(CC) $(CFLAGS) $(FILTERED_C_FILES) $(MERGE_FILE) src/gfx_curses.c -o $(PROGRAM) $(LDFLAGS) -lcurses -all: 2048 +terminal: $(FILTERED_C_FILES) src/gfx_terminal.c + $(CC) $(CFLAGS) $(FILTERED_C_FILES) $(MERGE_FILE) src/gfx_terminal.c -o $(PROGRAM) $(LDFLAGS) -clean: - rm -f 2048 2048nc +remake: clean all -man: - rm -f man/2048.1 - $(ASCIIDOC) -d manpage -f manpage man/2048.1.txt +clean: + rm -f $(PROGRAM) -man-nc: - rm -f man/2048nc.1 - $(ASCIIDOC) -d manpage -f manpage man/2048nc.1.txt +.PHONY: clean remake diff --git a/README.md b/README.md index 71e3392..e7484ac 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,60 @@ -#2048-cli +# 2048-cli -A cli version of the game [2048](https://github.com/gabrielecirulli/2048) for your Linux -terminal. +[![run on repl.it](http://repl.it/badge/github/tiehuis/2048-cli)](https://repl.it/github/tiehuis/2048-cli) -#####2048_curses.c -![Screenshot](http://i.imgur.com/QU7t5mH.png) +A cli version/engine of the game [2048](https://github.com/gabrielecirulli/2048)= +for your Linux terminal. -#####2048_no_curses.c -![Screenshot](http://i.imgur.com/fwZEvdh.png) +![Screenshot](http://i.imgur.com/qTfXP4J.png) -## Installation -This requires the ncurses library to link against during compilation. It is available -in most package managers. The program creates and uses a file name `.hs2048g` in the -working directory. Any file with this name will be modified and replaced. +There currently are 2 versions that can be run. This includes a straight-forward +terminal based version and one using ncurses. To add a new graphical interface, +simply create a .c file which implements all the functions in gfx.h and add a +Makefile entry. ### Get - git clone https://github.com/Tiehuis/2048-cli.git + git clone https://github.com/tiehuis/2048-cli.git make -### Run - ./2048 + +By default, termios.h is expected to be present for the standard terminal +version, and a VT100 compatible terminal is being used. If a VT100 terminal is +not available, simply alter the makefile and remove the '-DVT100' flag. +Compiling with ncurses requires the development libraries for it. + +On an ubuntu/debian machine for example, these can be obtained via + +``` +apt-get install libncurses5-dev +``` + +You can also also easily install this on el >= 5 (CentOS, RedHat Enterprise Linux, +Scientific Linux, Oracle) and Fedora >= 19 using the package-manager: + +``` +sudo yum install 2048-cli[-nocurses] +``` + +For el you will need to have the +[EPEL-repository](https://fedoraproject.org/wiki/EPEL/FAQ#How_can_I_install_the_packages_from_the_EPEL_software_repository.3F) +enabled. + +## Controls + hjkl and wasd Default movement keys + q Quit the current game ## Options - -s Set the grid border length - -b Set the block spawn rate - -r Resets hiscore. Will prompt user - -c Enables color support (ncurses version only) - -C Disables color support (ncurses version only) + -h Print the program usage. + -H Print the current highscore. + -C Disable color support (default). + -c Enable color support if supported. + -a Enable animations (default). + -A Disable animations. + -i Enable ai without displaying game. + -I Enable ai and display game. + -r Reset highscore. Will prompt user. + -s SIZE Set the size of the playing field. + -b RATE Set the rate at which blocks spawn per turn. ## License -This code is licensed under the [MIT License](https://github.com/Tiehuis/2048-cli/blob/master/LICENSE). +This code is licensed under the +[MIT License](https://github.com/Tiehuis/2048-cli/blob/master/LICENSE). diff --git a/man/2048.1 b/man/2048.1 deleted file mode 100644 index e1448f5..0000000 --- a/man/2048.1 +++ /dev/null @@ -1,92 +0,0 @@ -'\" t -.\" Title: \e20\e4\e8 -.\" Author: [see the "AUTHOR" section] -.\" Generator: DocBook XSL Stylesheets v1.78.1 -.\" Date: 12/05/2014 -.\" Manual: \ \& -.\" Source: \ \& -.\" Language: English -.\" -.TH "\E20\E4\E8" "1" "12/05/2014" "\ \&" "\ \&" -.\" ----------------------------------------------------------------- -.\" * Define some portability stuff -.\" ----------------------------------------------------------------- -.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.\" http://bugs.debian.org/507673 -.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html -.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.ie \n(.g .ds Aq \(aq -.el .ds Aq ' -.\" ----------------------------------------------------------------- -.\" * set default formatting -.\" ----------------------------------------------------------------- -.\" disable hyphenation -.nh -.\" disable justification (adjust text to left margin only) -.ad l -.\" ----------------------------------------------------------------- -.\" * MAIN CONTENT STARTS HERE * -.\" ----------------------------------------------------------------- -.SH "NAME" -2048 \- The game 2048 for your Linux terminal -.SH "SYNOPSIS" -.sp -\fB2048\fR [\fIOPTIONS\fR] -.SH "DESCRIPTION" -.sp -A cli version of the game 2048 for your Linux terminal\&. -.SH "OPTIONS" -.PP -\fB\-s\fR \fIsize\fR -.RS 4 -Set the grid border length to -\fIsize\fR\&. -.RE -.PP -\fB\-b\fR \fIrate\fR -.RS 4 -Set the block spawn rate to -\fIrate\fR\&. -.RE -.PP -\fB\-r\fR -.RS 4 -Resets hiscore\&. Will prompt user\&. -.RE -.PP -\fB\-c\fR -.RS 4 -Enables color support\&. -.RE -.PP -\fB\-C\fR -.RS 4 -Disables color support\&. -.RE -.PP -\fB\-h\fR -.RS 4 -Display usage information\&. -.RE -.SH "CONTROLS" -.PP -\fBhjkl or *wasd\fR or \fBarrow keys\fR -.RS 4 -Movement -.RE -.PP -\fBq\fR -.RS 4 -Quit the game\&. -.RE -.SH "AUTHOR" -.sp -2048 was originally written by Thiehuis\&. Some people have contributed to it\&. -.sp -This manpage is Copyright (C) 2014 Björn Esser and licensed under the terms of the MIT License (MIT)\&. -.SH "RESOURCES" -.sp -github: https://github\&.com/Tiehuis/2048\-cli/ -.SH "COPYING" -.sp -Copyright (C) 2014 Tiehuis\&. Free use of this software is granted under the terms of the MIT License (MIT)\&. diff --git a/man/2048.1.txt b/man/2048.1.txt deleted file mode 100644 index 50754fe..0000000 --- a/man/2048.1.txt +++ /dev/null @@ -1,62 +0,0 @@ -\20\4\8(1) -=========== -:doctype: manpage - - -NAME ----- -2048 - The game 2048 for your Linux terminal - - -SYNOPSIS --------- -*2048* ['OPTIONS'] - - -DESCRIPTION ------------ -A cli version of the game 2048 for your Linux terminal. - - -OPTIONS -------- -*-s* 'size':: - Set the grid border length to 'size'. -*-b* 'rate':: - Set the block spawn rate to 'rate'. -*-r*:: - Resets hiscore. Will prompt user. -*-c*:: - Enables color support. -*-C*:: - Disables color support. -*-h*:: - Display usage information. - - -CONTROLS --------- -*hjkl or *wasd* or *arrow keys*:: - Movement -*q*:: - Quit the game. - - -AUTHOR ------- -2048 was originally written by Thiehuis. Some people have -contributed to it. - -This manpage is Copyright \(C) 2014 Björn Esser and licensed -under the terms of the MIT License (MIT). - - -RESOURCES ---------- -github: - - -COPYING -------- -Copyright \(C) 2014 Tiehuis. Free use of this software is -granted under the terms of the MIT License (MIT). diff --git a/man/2048.6 b/man/2048.6 new file mode 100644 index 0000000..d8cba9c --- /dev/null +++ b/man/2048.6 @@ -0,0 +1,76 @@ +.Dd December 10, 2017 +.Dt 2048 6 +.Os +. +.Sh NAME +.Nm 2048 +.Nd play the game 2048 in your terminal +. +.Sh SYNOPSIS +.Nm 2048 +.Op Fl hrcCaA +.Op Fl s Ar size +.Op Fl b Ar rate +. +.Sh DESCRIPTION +.Nm +is an implementation of the popular game, 2048, designed to be run on a +terminal. +It is designed to be easy to understand and extend. +. +.Sh CONTROLS +.Bl -tag +.It Ic hjkl , wasd +Default movement keys. +Depending on the graphics implementation, there may be extra alternatives. +.It Ic q +Quit the current game. +.El +. +.Sh OPTIONS +.Bl -tag +.It Fl h +Print the program usage. +.It Fl H +Print the current highscore. +.It Fl c +Enable color support if supported. +.It Fl C +Disable color support (default). +.It Fl a +Enable animations (default). +.It Fl A +Disable animations. +.It Fl i +Enable ai without displaying game. +.It Fl I +Enable ai and display game. +.It Fl r +Reset highscore. Will prompt user. +.It Fl s Ar size +Set the size of the playing field. +Default is 4. +Maximum value is 16, minimum is 4. +.It Fl b Ar rate +Set the rate at which blocks are spawned. +Default is 1. +. +.Sh FILES +.Bl -tag +.It Pa $XDG_DATA_HOME/2048/highscore +High score file. +If +.Ev XDG_DATA_HOME +is unset, defaults to +.Pa ~/.local/share/2048/highscore . +.El +. +.Sh AUTHORS +.An -nosplit +Originally written by +.An Marc Tiehuis . +All contributions can be found at +.Lk https://github.com/Tiehuis/2048-cli . +. +.Sh COPYRIGHT +MIT License (2014) diff --git a/man/2048nc.1 b/man/2048nc.1 deleted file mode 100644 index d0151de..0000000 --- a/man/2048nc.1 +++ /dev/null @@ -1,82 +0,0 @@ -'\" t -.\" Title: \e20\e4\e8nc -.\" Author: [see the "AUTHOR" section] -.\" Generator: DocBook XSL Stylesheets v1.78.1 -.\" Date: 12/05/2014 -.\" Manual: \ \& -.\" Source: \ \& -.\" Language: English -.\" -.TH "\E20\E4\E8NC" "1" "12/05/2014" "\ \&" "\ \&" -.\" ----------------------------------------------------------------- -.\" * Define some portability stuff -.\" ----------------------------------------------------------------- -.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.\" http://bugs.debian.org/507673 -.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html -.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.ie \n(.g .ds Aq \(aq -.el .ds Aq ' -.\" ----------------------------------------------------------------- -.\" * set default formatting -.\" ----------------------------------------------------------------- -.\" disable hyphenation -.nh -.\" disable justification (adjust text to left margin only) -.ad l -.\" ----------------------------------------------------------------- -.\" * MAIN CONTENT STARTS HERE * -.\" ----------------------------------------------------------------- -.SH "NAME" -2048nc \- The game 2048 for your Linux terminal -.SH "SYNOPSIS" -.sp -\fB2048nc\fR [\fIOPTIONS\fR] -.SH "DESCRIPTION" -.sp -A non\-ncurses cli version of the game 2048 for your Linux terminal\&. -.SH "OPTIONS" -.PP -\fB\-s\fR \fIsize\fR -.RS 4 -Set the grid border length to -\fIsize\fR\&. -.RE -.PP -\fB\-b\fR \fIrate\fR -.RS 4 -Set the block spawn rate to -\fIrate\fR\&. -.RE -.PP -\fB\-r\fR -.RS 4 -Resets hiscore\&. Will prompt user\&. -.RE -.PP -\fB\-h\fR -.RS 4 -Display usage information\&. -.RE -.SH "CONTROLS" -.PP -\fBhjkl\fR or \fBwasd\fR -.RS 4 -Movement -.RE -.PP -\fBq\fR -.RS 4 -Quit the game\&. -.RE -.SH "AUTHOR" -.sp -2048nc was originally written by Thiehuis\&. Some people have contributed to it\&. -.sp -This manpage is Copyright (C) 2014 Björn Esser and licensed under the terms of the MIT License (MIT)\&. -.SH "RESOURCES" -.sp -github: https://github\&.com/Tiehuis/2048\-cli/ -.SH "COPYING" -.sp -Copyright (C) 2014 Tiehuis\&. Free use of this software is granted under the terms of the MIT License (MIT)\&. diff --git a/man/2048nc.1.txt b/man/2048nc.1.txt deleted file mode 100644 index a961892..0000000 --- a/man/2048nc.1.txt +++ /dev/null @@ -1,58 +0,0 @@ -\20\4\8nc(1) -=========== -:doctype: manpage - - -NAME ----- -2048nc - The game 2048 for your Linux terminal - - -SYNOPSIS --------- -*2048nc* ['OPTIONS'] - - -DESCRIPTION ------------ -A non-ncurses cli version of the game 2048 for your Linux terminal. - - -OPTIONS -------- -*-s* 'size':: - Set the grid border length to 'size'. -*-b* 'rate':: - Set the block spawn rate to 'rate'. -*-r*:: - Resets hiscore. Will prompt user. -*-h*:: - Display usage information. - - -CONTROLS --------- -*hjkl* or *wasd*:: - Movement -*q*:: - Quit the game. - - -AUTHOR ------- -2048nc was originally written by Thiehuis. Some people have -contributed to it. - -This manpage is Copyright \(C) 2014 Björn Esser and licensed -under the terms of the MIT License (MIT). - - -RESOURCES ---------- -github: - - -COPYING -------- -Copyright \(C) 2014 Tiehuis. Free use of this software is -granted under the terms of the MIT License (MIT). diff --git a/po/es_AR/gfx_curses.po b/po/es_AR/gfx_curses.po new file mode 100644 index 0000000..0616ffd --- /dev/null +++ b/po/es_AR/gfx_curses.po @@ -0,0 +1,33 @@ +# Spanish translations for 2048 package. +# Copyright (C) 2018 THE 2048'S COPYRIGHT HOLDER +# This file is distributed under the same license as the 2048 package. +# Matias , 2018. +# +msgid "" +msgstr "" +"Project-Id-Version: 2048 v1.0.1\n" +"Report-Msgid-Bugs-To: matiasezequielbian@gmail.com\n" +"POT-Creation-Date: 2018-04-26 18:58-0300\n" +"PO-Revision-Date: 2018-04-26 19:00-0300\n" +"Last-Translator: Matias \n" +"Language-Team: Spanish\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: src/gfx_curses.c:70 +#, c-format +msgid " Hi: %d\n" +msgstr "Puntuación Máxima: %d\n" + +#: src/gfx_curses.c:68 +#, c-format +msgid "Score: %d\n" +msgstr "Puntuación: %d\n" + +#: src/gfx_curses.c:66 +#, c-format +msgid "Score: %d (+%d)\n" +msgstr "Puntuación: %d (+%d)\n" diff --git a/po/es_AR/gfx_terminal.po b/po/es_AR/gfx_terminal.po new file mode 100644 index 0000000..471f98e --- /dev/null +++ b/po/es_AR/gfx_terminal.po @@ -0,0 +1,33 @@ +# Spanish translations for PACKAGE package. +# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# Matias , 2018. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-04-26 19:02-0300\n" +"PO-Revision-Date: 2018-04-26 19:03-0300\n" +"Last-Translator: Matias \n" +"Language-Team: Spanish\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: src/gfx_terminal.c:46 +#, c-format +msgid " Hi: %ld\n" +msgstr "Puntuación Máxima: %ld\n" + +#: src/gfx_terminal.c:44 +#, c-format +msgid "Score: %ld\n" +msgstr "Puntuación: %ld\n" + +#: src/gfx_terminal.c:42 +#, c-format +msgid "Score: %ld (+%ld)\n" +msgstr "Puntuación: %ld (+%ld)\n" diff --git a/po/es_AR/highscore.po b/po/es_AR/highscore.po new file mode 100644 index 0000000..0af49c7 --- /dev/null +++ b/po/es_AR/highscore.po @@ -0,0 +1,56 @@ +# Spanish translations for PACKAGE package. +# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# Matias , 2018. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-05-17 18:12-0300\n" +"PO-Revision-Date: 2018-05-17 18:19-0300\n" +"Last-Translator: Matias \n" +"Language-Team: Argentinian \n" +"Language: es_AR\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: src/highscore.c:58 +#, c-format +msgid "Are you sure you want to reset your scores? Y(es) or N(o)\n" +msgstr "¿Estás seguro que quieres reiniciar tus puntuaciones? (S)i o (N)o\n" + +#: src/highscore.c:76 +#, c-format +msgid "Please enter Yes or No\n" +msgstr "Por favor ingresa Si o No\n" + +#: src/highscore.c:95 +#, c-format +msgid "load: Failed to open highscore file\n" +msgstr "Carga: Error al abrir el archivo de puntuaciones máximas\n" + +#: src/highscore.c:100 +#, c-format +msgid "load: Failed to parse highscore file\n" +msgstr "Carga: Error al parsear el archivo de puntuaciones máximas\n" + +#: src/highscore.c:122 +#, c-format +msgid "save: Failed to open highscore file\n" +msgstr "Guardado: Error al abrir el archivo de puntuaciones máximas\n" + +#: src/highscore.c:127 +#, c-format +msgid "save: Failed to write highscore file\n" +msgstr "Guardado: Error al escribir el archivo de puntuaciones máximas\n" + +#: src/highscore.c:71 +msgid "y" +msgstr "s" + +#: src/highscore.c:71 +msgid "yes" +msgstr "si" diff --git a/po/fr_FR/gfx_curses.po b/po/fr_FR/gfx_curses.po new file mode 100644 index 0000000..9db6412 --- /dev/null +++ b/po/fr_FR/gfx_curses.po @@ -0,0 +1,33 @@ +# French translations for 2048 package. +# Copyright (C) 2019 THE 2048'S COPYRIGHT HOLDER +# This file is distributed under the same license as the 2048 package. +# Thomas , 2019. +# +msgid "" +msgstr "" +"Project-Id-Version: 2048 v1.0.1\n" +"Report-Msgid-Bugs-To: matiasezequielbian@gmail.com\n" +"POT-Creation-Date: 2018-04-26 18:58-0300\n" +"PO-Revision-Date: 2019-10-24 23:37+0200\n" +"Last-Translator: Thomas \n" +"Language-Team: French\n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=ASCII\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +#: src/gfx_curses.c:70 +#, c-format +msgid " Hi: %d\n" +msgstr "Meilleur:%d\n" + +#: src/gfx_curses.c:68 +#, c-format +msgid "Score: %d\n" +msgstr " Score: %d\n" + +#: src/gfx_curses.c:66 +#, c-format +msgid "Score: %d (+%d)\n" +msgstr " Score: %d (+%d)\n" diff --git a/po/fr_FR/gfx_terminal.po b/po/fr_FR/gfx_terminal.po new file mode 100644 index 0000000..6822588 --- /dev/null +++ b/po/fr_FR/gfx_terminal.po @@ -0,0 +1,33 @@ +# French translations for PACKAGE package. +# Copyright (C) 2019 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# Thomas , 2019. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-04-26 19:02-0300\n" +"PO-Revision-Date: 2019-10-24 23:38+0200\n" +"Last-Translator: Thomas \n" +"Language-Team: French\n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=ASCII\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +#: src/gfx_terminal.c:46 +#, c-format +msgid " Hi: %ld\n" +msgstr "Meilleur: %ld\n" + +#: src/gfx_terminal.c:44 +#, c-format +msgid "Score: %ld\n" +msgstr " Score: %ld\n" + +#: src/gfx_terminal.c:42 +#, c-format +msgid "Score: %ld (+%ld)\n" +msgstr " Score: %ld (+%ld)\n" diff --git a/po/fr_FR/highscore.po b/po/fr_FR/highscore.po new file mode 100644 index 0000000..f5b348a --- /dev/null +++ b/po/fr_FR/highscore.po @@ -0,0 +1,56 @@ +# French translations for PACKAGE package. +# Copyright (C) 2019 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# Thomas , 2019. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-05-17 18:12-0300\n" +"PO-Revision-Date: 2019-10-24 23:38+0200\n" +"Last-Translator: Thomas \n" +"Language-Team: French\n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=ASCII\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +#: src/highscore.c:58 +#, c-format +msgid "Are you sure you want to reset your scores? Y(es) or N(o)\n" +msgstr "Voulez-vous vraiment remettre a zero vos scores ? (Y)Oui ou (N)on\n" + +#: src/highscore.c:76 +#, c-format +msgid "Please enter Yes or No\n" +msgstr "Veuillez entrer (Y)Oui ou (N)on\n" + +#: src/highscore.c:95 +#, c-format +msgid "load: Failed to open highscore file\n" +msgstr "chargement: Echec dans l'ouverture du fichier de score\n" + +#: src/highscore.c:100 +#, c-format +msgid "load: Failed to parse highscore file\n" +msgstr "chargement: Echec dans la lecture du fichier de score\n" + +#: src/highscore.c:122 +#, c-format +msgid "save: Failed to open highscore file\n" +msgstr "sauvegarde: Echec dans l'ouverture du fichier de score\n" + +#: src/highscore.c:127 +#, c-format +msgid "save: Failed to write highscore file\n" +msgstr "sauvegarde: Echec dans l'ecriture du fichier de score\n" + +#: src/highscore.c:71 +msgid "y" +msgstr "y" + +#: src/highscore.c:71 +msgid "yes" +msgstr "yes" diff --git a/po/gfx_curses.pot b/po/gfx_curses.pot new file mode 100644 index 0000000..ccb9233 --- /dev/null +++ b/po/gfx_curses.pot @@ -0,0 +1,33 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: 2048 v1.0.1\n" +"Report-Msgid-Bugs-To: matiasezequielbian@gmail.com\n" +"POT-Creation-Date: 2018-04-26 18:58-0300\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: src/gfx_curses.c:70 +#, c-format +msgid " Hi: %d\n" +msgstr "" + +#: src/gfx_curses.c:68 +#, c-format +msgid "Score: %d\n" +msgstr "" + +#: src/gfx_curses.c:66 +#, c-format +msgid "Score: %d (+%d)\n" +msgstr "" diff --git a/po/gfx_terminal.pot b/po/gfx_terminal.pot new file mode 100644 index 0000000..fb745c6 --- /dev/null +++ b/po/gfx_terminal.pot @@ -0,0 +1,33 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-04-26 19:02-0300\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: src/gfx_terminal.c:46 +#, c-format +msgid " Hi: %ld\n" +msgstr "" + +#: src/gfx_terminal.c:44 +#, c-format +msgid "Score: %ld\n" +msgstr "" + +#: src/gfx_terminal.c:42 +#, c-format +msgid "Score: %ld (+%ld)\n" +msgstr "" diff --git a/po/highscore.pot b/po/highscore.pot new file mode 100644 index 0000000..151d4e2 --- /dev/null +++ b/po/highscore.pot @@ -0,0 +1,56 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-05-17 18:12-0300\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: src/highscore.c:58 +#, c-format +msgid "Are you sure you want to reset your scores? Y(es) or N(o)\n" +msgstr "" + +#: src/highscore.c:76 +#, c-format +msgid "Please enter Yes or No\n" +msgstr "" + +#: src/highscore.c:95 +#, c-format +msgid "load: Failed to open highscore file\n" +msgstr "" + +#: src/highscore.c:100 +#, c-format +msgid "load: Failed to parse highscore file\n" +msgstr "" + +#: src/highscore.c:122 +#, c-format +msgid "save: Failed to open highscore file\n" +msgstr "" + +#: src/highscore.c:127 +#, c-format +msgid "save: Failed to write highscore file\n" +msgstr "" + +#: src/highscore.c:71 +msgid "y" +msgstr "" + +#: src/highscore.c:71 +msgid "yes" +msgstr "" diff --git a/src/2048.c b/src/2048.c deleted file mode 100644 index b2c8713..0000000 --- a/src/2048.c +++ /dev/null @@ -1,596 +0,0 @@ -/* - * 2048.c - The game 2048 for your Linux terminal. - * - * Copyright (c) 2014 Tiehuis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - * - * Things to do: - * - Finish cleaning up code. - * - Ask user if they would like to play again instead of exiting immediately. - * - Add an actual goal (what happens when we reach 2048?). - * - Keep track list of past scores, and store in a better fashion independent - * of cwd. Also, store not in plaintext. - * */ - -#include /* for malloc */ -#include /* for printf, FILE */ -#include /* for strlen, strrchr */ -#include /* for mkdir */ -#include /* for time */ -#include /* for getopts */ - -#ifndef NO_CURSES - #include /* for ncurses terminal */ -#else /* ifndef NO_CURSES */ - #include /* for plain terminal */ -#endif /* ifndef NO_CURSES */ - - -/* Constants */ -#define DATADIR_NAME "2048" -#define HIGHSCORE_FILE_NAME "highscore" -#define DEFAULT_GRID_SIZE 4 -#define HISCORE_FILE get_highscore_file() -#define USAGE_STR\ - "Usage:\n"\ - " ./2048 [options]\n"\ - "\n"\ - "Controls:\n"\ - " hjkl, wasd Movement\n"\ - " q Quit\n"\ - "\n"\ - "Options:\n"\ - " -s Set the grid border length\n"\ - " -b Set the block spawn rate\n"\ - " -c Enables color support (ncurses version only)\n"\ - " -C Disables color support (ncurses version only)\n" - - -/* Globals */ -int **grid; /* grid pointer */ -int grid_size; /* grid size */ -int score; /* score */ -int score_last; /* Score for last move */ -int score_high; /* Hiscore */ -int printwidth; /* maximum length of any value in grid, for printing */ - - -/* Typedefs */ -typedef enum { - DIR_UP, - DIR_RIGHT, - DIR_DOWN, - DIR_LEFT -} dir_t; - - -/* Macros */ -/* Repeat an expression y, x times */ -#define ITER(x, expr)\ - do {\ - int i;\ - for (i = 0; i < (x); i++){ expr;}\ - } while (0) - -/* Allocates a square pointer of array of arrays onto heap */ -#define CALLOC2D(ptr, sz)\ - do {\ - int i;\ - ptr = calloc((sz), sizeof(*ptr));\ - for (i = 0; i < (sz); i++)\ - ptr[i] = calloc((sz), sizeof(*ptr));\ - } while (0) - -/* Frees a square pointer of arrays to arrays */ -#define FREE2D(ptr, sz)\ - do {\ - int i;\ - for (i = 0; i < (sz); i++)\ - free(ptr[i]);\ - free(ptr);\ - } while (0) - -/* What occurs during a 'turn' of execution */ -#define TURN(x) (gravitate(x) + merge(x) + gravitate(x)) - - -/* Functions */ -const char* get_highscore_file() { - static char buffer[4096]; - if (getenv("XDG_DATA_HOME") != NULL) { - snprintf(buffer, sizeof(buffer), "%s/%s/%s", getenv("XDG_DATA_HOME"), DATADIR_NAME, HIGHSCORE_FILE_NAME); - } else if (getenv("HOME") != NULL) { - snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", getenv("HOME"), DATADIR_NAME, HIGHSCORE_FILE_NAME); - } else { - return HIGHSCORE_FILE_NAME; - } - - // create hierarrchy of directories up to highscore file location - char* sep = strrchr(buffer, '/'); - while (sep != NULL) { - *sep = '\0'; - if (strlen(buffer) != 0) - mkdir(buffer, 0777); - char* tmpsep = sep; - sep = strrchr(buffer, '/'); - *tmpsep = '/'; - } - return buffer; -} - -/* Merges adjacent squares of the same value together in a certain direction */ -int merge(dir_t d) -{ - int moved = 0; - int i, j; - - if (d == DIR_LEFT) { - /* Move from upper left, across rows until second to second-last elem each row */ - for (i = 0; i < grid_size; i++) { - for (j = 0; j < grid_size - 1; j++) { - if (grid[i][j] == grid[i][j + 1]) { - grid[i][j] <<= 1; - grid[i][j + 1] = 0; - score_last += grid[i][j]; - score += grid[i][j]; - moved = 1; - } - } - } - } - else if (d == DIR_UP) { - /* Move from upper left, across rows until final row which is skipped */ - for (i = 0; i < grid_size - 1; i++) { - for (j = 0; j < grid_size; j++) { - if (grid[i][j] == grid[i + 1][j]) { - grid[i][j] <<= 1; - grid[i + 1][j] = 0; - score_last += grid[i][j]; - score += grid[i][j]; - moved = 1; - } - } - } - } - else if (d == DIR_RIGHT) { - /* Move from lower right, backwards across rows until first elem each row */ - for (i = grid_size - 1; i >= 0; i--) { - for (j = grid_size - 1; j > 0; j--) { - if (grid[i][j] == grid[i][j - 1]) { - grid[i][j] <<= 1; - grid[i][j - 1] = 0; - score_last += grid[i][j]; - score += grid[i][j]; - moved = 1; - } - } - } - } - else if (d == DIR_DOWN) { - /* Move from lower right, across rows until first row which is skipped */ - for (i = grid_size - 1; i > 0; i--) { - for (j = grid_size - 1; j >= 0; j--) { - if (grid[i][j] == grid[i - 1][j]) { - grid[i][j] <<= 1; - grid[i - 1][j] = 0; - score_last += grid[i][j]; - score += grid[i][j]; - moved = 1; - } - } - } - } - - return moved; -} - - -/* move all values in the grid to the edge given by the direction pressed */ -/* would be nice to generalize this code a little so didn't need four loops */ -/* if animations are wanted, then need to alter this so it moves each square one at a time */ -int gravitate(dir_t d) -{ - int moved = 0; - int i, j; - - if (d == DIR_LEFT) { - for (i = 0; i < grid_size; i++) { - for (j = 0; j < grid_size - 1; j++) { - if (grid[i][j]) continue; - int st = 1; - while (j + st < grid_size && !grid[i][j + st]) st++; - if (j + st < grid_size) { - grid[i][j] = grid[i][j + st]; - grid[i][j + st] = 0; - moved = 1; - } - } - } - } - else if (d == DIR_UP) { - for (i = 0; i < grid_size; i++) { - for (j = 0; j < grid_size - 1; j++) { - if (grid[j][i]) continue; - int st = 1; - while (j + st < grid_size && !grid[j + st][i]) st++; - if (j + st < grid_size) { - grid[j][i] = grid[j + st][i]; - grid[j + st][i] = 0; - moved = 1; - } - } - } - } - else if (d == DIR_RIGHT) { - for (i = 0; i < grid_size; i++) { - for (j = grid_size - 1; j > 0; j--) { - if (grid[i][j]) continue; - int st = 1; - while (j - st >= 0 && !grid[i][j - st]) st++; - if (j - st >= 0) { - grid[i][j] = grid[i][j - st]; - grid[i][j - st] = 0; - moved = 1; - } - } - } - } - else if (d == DIR_DOWN) { - for (i = 0; i < grid_size; i++) { - for (j = grid_size - 1; j > 0; j--) { - if (grid[j][i]) continue; - int st = 1; - while (j - st >= 0 && !grid[j - st][i]) st++; - if (j - st >= 0) { - grid[j][i] = grid[j - st][i]; - grid[j - st][i] = 0; - moved = 1; - } - } - } - } - return moved; -} - -/* Return the current highscore */ -int get_score_high() -{ - int s = 0; - FILE *fd = fopen(HISCORE_FILE, "r"); - if (fd == NULL) - fd = fopen(HISCORE_FILE, "w+"); - - if (fscanf(fd, "%d", &s) == 1) {}; - fclose(fd); - return s; -} - -/* saves hiscore, but only if playing on standard size grid */ -void save_score_high() -{ - if (score > score_high && grid_size == 4) { - score_high = score; - FILE *fd = fopen(HISCORE_FILE, "w+"); - fprintf(fd, "%d", score_high); - fclose(fd); - } -} - -/* returns if there are any possible moves */ -int moves_available() -{ - int i, j; - for (i = 0; i < grid_size; i++) - for (j = 0; j < grid_size; j++) - if (!grid[i][j] - || ((i + 1 < grid_size) && (grid[i][j] == grid[i + 1][j])) - || ((j + 1 < grid_size) && (grid[i][j] == grid[i][j + 1]))) - return 1; - return 0; -} - -/* places a random block onto the grid - either a 4, or a 2 with a chance of 1:3 respectively */ -/* could do this in a much smarter fashion by finding which spaces are free */ -void rand_block() -{ - int x, y; - while (grid[x = rand() % grid_size][y = rand() % grid_size]); - grid[x][y] = (rand() & 3) ? 2 : 4; -} - -/* quick floor log2(n) */ -int flog2(unsigned int n) -{ - int k = 0; - while (n) - k++, n >>= 1; - return k; -} - -#ifndef NO_CURSES - -/* draws the grid and fills it with the current values */ -/* colors just rotate around, works for now, can be confusing when you have some fairly high values on the board */ -void draw_grid(WINDOW *gamewin) -{ - // mvwprintw will sometimes have a useless arg, this is warned, but doesn't affect the program - char *scr = score_last ? "SCORE: %d (+%d)\n" : "SCORE: %d\n"; - mvwprintw(gamewin, 0, 0, scr, score, score_last); - mvwprintw(gamewin, 1, 0, "HISCR: %d\n", score_high); - - ITER(grid_size*(printwidth + 2) + 1, waddch(gamewin, '-')); - int i, j, xps = 0, yps = 3; - for (i = 0; i < grid_size; i++, xps = 0, yps++) { - mvwprintw(gamewin, yps, xps++, "|"); - for (j = 0; j < grid_size; j++) { - if (grid[i][j]) { - wattron(gamewin, COLOR_PAIR(flog2(grid[i][j]) % 7)); - mvwprintw(gamewin, yps, xps, "%*d", printwidth, grid[i][j]); - wattroff(gamewin, COLOR_PAIR(flog2(grid[i][j]) % 7)); - mvwprintw(gamewin, yps, xps + printwidth, " |"); - } - else { - ITER(printwidth + 1, waddch(gamewin, ' ')); - waddch(gamewin, '|'); - } - xps += (printwidth + 2); - } - } - ITER(grid_size*(printwidth + 2) + 1, waddch(gamewin, '-')); - wrefresh(gamewin); -} - -#else /* ifndef NO_CURSES */ - -/* draws the grid and fills it with the current values */ -void draw_grid() -{ - printf("HISCORE: %d |", score_high); - printf("| SCORE: %d ", score); - if (score_last) printf("(+%d)", score_last); - printf("\n"); - - // alter this grid_size + 1 to match abitrary grid size - ITER(grid_size, printf("------")); - printf("-\n"); - int i, j; - for (i = 0; i < grid_size; i++) { - printf("|"); - for (j = 0; j < grid_size; j++) { - if (grid[i][j]) - printf("%*d |", 4, grid[i][j]); - else - printf(" |"); - } - printf("\n"); - } - ITER(grid_size, printf("------")); - printf("-\n\n"); -} - -/* store the terminal settings and call this function on exit to restore */ -struct termios sattr; -void reset_term() -{ - tcsetattr(STDIN_FILENO, TCSANOW, &sattr); -} - -#endif /* ifndef NO_CURSES */ - - -/* entry point for the program */ -/* parses options and stores the main game loop */ -int main(int argc, char **argv) -{ - - /* init variables */ - score = 0; - score_last = 0; - score_high = get_score_high(); - grid_size = DEFAULT_GRID_SIZE; - printwidth = DEFAULT_GRID_SIZE; - - int n_blocks = 1; - -#ifndef NO_CURSES - /* init ncurses environment */ - initscr(); - cbreak(); - noecho(); - curs_set(FALSE); - int enable_color = has_colors(); -#endif /* ifndef NO_CURSES */ - - /* parse options */ - int c; - while ((c = getopt(argc, argv, "rcChs:b:")) != -1) { - switch (c) { -#ifndef NO_CURSES - /* Color support */ - case 'c': - enable_color = 1; - break; - case 'C': - enable_color = 0; - break; -#endif /* ifndef NO_CURSES */ - // different board sizes - case 's':; - int optint = strtol(optarg, NULL, 10); - grid_size = optint > DEFAULT_GRID_SIZE ? optint : DEFAULT_GRID_SIZE; - break; - // different block spawn rate - case 'b': - n_blocks = strtol(optarg, NULL, 10); - break; - // reset hiscores - case 'r': -#ifndef NO_CURSES - endwin(); -#endif /* ifndef NO_CURSES */ - printf("Are you sure you want to reset your highscores? (Y)es or (N)o\n"); - int response; - if ((response = getchar()) == 'y' || response == 'Y') { - FILE *fd = fopen(HISCORE_FILE, "w+"); - fclose(fd); - } - exit(EXIT_SUCCESS); - // help menu - case 'h': -#ifndef NO_CURSES - endwin(); -#endif /* ifndef NO_CURSES */ - printf(USAGE_STR); - exit(EXIT_SUCCESS); - } - } - - /* Allocate memory once we actually know amount */ - CALLOC2D(grid, grid_size); - -#ifndef NO_CURSES - int width = grid_size * (printwidth + 2) + 1; - int height = grid_size * (printwidth + 2) + 3; - - if (enable_color) { - if (!has_colors()) { - fprintf(stderr, "Terminal does not support color\n"); - } else { - start_color(); - init_pair(0, 1, 0); - init_pair(1, 2, 0); - init_pair(2, 3, 0); - init_pair(3, 4, 0); - init_pair(4, 5, 0); - init_pair(5, 6, 0); - init_pair(6, 7, 0); - } - } - - // might center in middle of screen - WINDOW *gamewin = newwin(height, width, 1, 1); - keypad(gamewin, TRUE); -#else /* ifndef NO_CURSES */ - - /* store term settings so we can restore on exit */ - tcgetattr(STDIN_FILENO, &sattr); - atexit(reset_term); - - /* alters terminal stdin to not echo and doesn't need \n before reading getchar */ - struct termios tattr; - tcgetattr(STDIN_FILENO, &tattr); - tattr.c_lflag &= ~(ICANON | ECHO); - tcsetattr(STDOUT_FILENO, TCSANOW, &tattr); -#endif /* ifndef NO_CURSES */ - - /* random seed */ - srand((unsigned int)time(NULL)); - ITER(2, rand_block()); -#ifndef NO_CURSES - draw_grid(gamewin); -#else /* ifndef NO_CURSES */ - draw_grid(); -#endif /* ifndef NO_CURSES */ - - int key, moved; - while (1) { - /* will goto this if we didn't get a valid keypress */ - retry:; - - moved = 0; - score_last = 0; - -#ifndef NO_CURSES - key = wgetch(gamewin); -#else /* ifndef NO_CURSES */ - key = getchar(); -#endif /* ifndef NO_CURSES */ - - /* should check if anything changed during merge and if not retry */ - switch (key) { - - case 'h': - case 'a': -#ifndef NO_CURSES - case KEY_LEFT: -#endif /* ifndef NO_CURSES */ - moved = TURN(DIR_LEFT); - break; - - case 'l': - case 'd': -#ifndef NO_CURSES - case KEY_RIGHT: -#endif /* ifndef NO_CURSES */ - moved = TURN(DIR_RIGHT); - break; - - case 'j': - case 's': -#ifndef NO_CURSES - case KEY_DOWN: -#endif /* ifndef NO_CURSES */ - moved = TURN(DIR_DOWN); - break; - - case 'k': - case 'w': -#ifndef NO_CURSES - case KEY_UP: -#endif /* ifndef NO_CURSES */ - moved = TURN(DIR_UP); - break; - - case 'q': - FREE2D(grid, grid_size); -#ifndef NO_CURSES - erase(); - refresh(); - endwin(); -#endif /* ifndef NO_CURSES */ - save_score_high(); - exit(EXIT_SUCCESS); - - default: - goto retry; - } - - if (!moves_available()) { -#ifndef NO_CURSES - endwin(); -#endif /* ifndef NO_CURSES */ - printf("\n" - "YOU LOSE! - Your score was %d\n", score); - save_score_high(); - exit(EXIT_SUCCESS); - } - - if (moved) { - ITER(n_blocks, rand_block()); -#ifndef NO_CURSES - draw_grid(gamewin); -#else /* ifndef NO_CURSES */ - draw_grid(); -#endif /* ifndef NO_CURSES */ - } - } - - return 0; -} diff --git a/src/ai.c b/src/ai.c new file mode 100644 index 0000000..12d5ea8 --- /dev/null +++ b/src/ai.c @@ -0,0 +1,13 @@ +#include +#include "ai.h" +#include "engine.h" +#include "gfx.h" + +const char moves[] = {'w', 'a', 's', 'd'}; + +int ai_move(struct gamestate *g) +{ + /* Ensure srand is called somewhere prior */ + if (g->opts->interactive) gfx_sleep(50); + return moves[rand() % 4]; +} diff --git a/src/ai.h b/src/ai.h new file mode 100644 index 0000000..05e437f --- /dev/null +++ b/src/ai.h @@ -0,0 +1,8 @@ +#ifndef AI_H +#define AI_H + +#include "engine.h" + +int ai_move(struct gamestate *g); + +#endif diff --git a/src/engine.c b/src/engine.c new file mode 100644 index 0000000..0afc777 --- /dev/null +++ b/src/engine.c @@ -0,0 +1,291 @@ +#include +#include +#include +#include "merge.h" +#include "engine.h" +#include "highscore.h" + +/* Utilize block counter to improve some of the functions so they can run + * quicker */ + +/* This function will move all blocks in the given game the given direction. + * The callback function is called after each single move. It can be used to + * animate the movement of the board. */ +static void gravitate(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state *s, struct gamestate *g)) +{ + +#define swap_if_space(xoff, yoff)\ + do {\ + if (g->grid[x][y] == 0 && g->grid[x+xoff][y+yoff] != 0) {\ + g->grid[x][y] = g->grid[x+xoff][y+yoff];\ + g->grid[x+xoff][y+yoff] = 0;\ + done = 0;\ + g->moved = 1;\ + }\ + } while (0) + + int x, y; + int done = 0; + + if (d == dir_left) { + while (!done) { + done = 1; + for (x = 0; x < g->opts->grid_width - 1; ++x) { + for (y = 0; y < g->opts->grid_height; ++y) { + swap_if_space(1, 0); + } + } + if (callback) + callback(s, g); + } + } + else if (d == dir_right) { + while (!done) { + done = 1; + for (x = g->opts->grid_width - 1; x > 0; --x) { + for (y = 0; y < g->opts->grid_height; ++y) { + swap_if_space(-1, 0); + } + } + if (callback) + callback(s, g); + } + } + else if (d == dir_down) { + while (!done) { + done = 1; + for (y = g->opts->grid_height - 1; y > 0; --y) { + for (x = 0; x < g->opts->grid_width; ++x) { + swap_if_space(0, -1); + } + } + if (callback) + callback(s, g); + } + } + else if (d == dir_up) { + while (!done) { + done = 1; + for (y = 0; y < g->opts->grid_height - 1; ++y) { + for (x = 0; x < g->opts->grid_width; ++x) { + swap_if_space(0, 1); + } + } + if (callback) + callback(s, g); + } + } + else { + fatal("Invalid direction passed to gravitate()"); + /* Not reached */ + } + +#undef swap_if_space +} + +/* The merge function will combine adjacent blocks with the same value for + * the given direction. Note, left and right merges will merge in a different + * order, so they are not identical in all cases. + * + * Consider 2 2 2 0 + * + * Right merging: 4 0 2 0 + * Left merging: 2 0 4 0 + */ +static void merge(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state *s, struct gamestate *g)) +{ + +#define merge_if_equal(xoff, yoff)\ + do {\ + if (g->grid[x][y] && (merge_possible(g->grid[x][y], g->grid[x+xoff][y+yoff]))) {\ + g->grid[x][y] = merge_result(g->grid[x][y], g->grid[x+xoff][y+yoff]);\ + g->grid[x+xoff][y+yoff] = 0;\ + g->blocks_in_play -= 1;\ + g->score_last += merge_value(g->grid[x][y]);\ + g->score += merge_value(g->grid[x][y]);\ + g->moved = 1;\ + }\ + } while (0) + + int x, y; + g->score_last = 0; + + if (d == dir_left) { + for (x = 0; x < g->opts->grid_width - 1; ++x) { + for (y = 0; y < g->opts->grid_height; ++y) { + merge_if_equal(1, 0); + } + } + } + else if (d == dir_right) { + for (x = g->opts->grid_width - 1; x > 0; --x) { + for (y = 0; y < g->opts->grid_height; ++y) { + merge_if_equal(-1, 0); + } + } + } + else if (d == dir_down) { + for (y = g->opts->grid_height - 1; y > 0; --y) { + for (x = 0; x < g->opts->grid_width; ++x) { + merge_if_equal(0, -1); + } + } + } + else if (d == dir_up) { + for (y = 0; y < g->opts->grid_height - 1; ++y) { + for (x = 0; x < g->opts->grid_width; ++x) { + merge_if_equal(0, 1); + } + } + } + else { + fatal("Invalid direction passed to merge()"); + /* Not reached */ + } + + if (callback) + callback(s, g); + +#undef merge_if_equal +} + +/* Scan the current board and determine if an end outcome has been reached. + * -1 indicates a lose condition, 1 indicates a win condition, 0 indicates + * end has not yet been reached. */ +int gamestate_end_condition(struct gamestate *g) +{ + int ret = -1; + //size_t blocks_counted = 0; + int x, y; + + for (x = 0; x < g->opts->grid_width; ++x) { + for (y = 0; y < g->opts->grid_height; ++y) { + if (g->grid[x][y] == merge_goal()) + return 1; + if (!g->grid[x][y] || ((x + 1 < g->opts->grid_width) && + merge_possible(g->grid[x][y], g->grid[x+1][y])) + || ((y + 1 < g->opts->grid_height) && + merge_possible(g->grid[x][y], g->grid[x][y+1]))) + ret = 0; + } + } + + return ret; +} + +/* Place a random block into the current grid */ +void gamestate_new_block(struct gamestate *g) +{ + /* Exit early if there are no spaces to place a block */ + if (g->blocks_in_play >= g->gridsize) return; + + int block_number = rand() % (g->gridsize - g->blocks_in_play); + + int x, y, p = 0; + for (y = 0; y < g->opts->grid_height; ++y) { + for (x = 0; x < g->opts->grid_width; ++x) { + if (!g->grid[x][y]) { + if (p == block_number) { + g->grid[x][y] = rand() & 3 ? 1 : 2; + g->blocks_in_play += 1; + return; + } + else { + ++p; + } + } + } + } + + /* This should never be reached; but just in case */ + assert(0); +} + +/* This returns the number of digits in the base10 rep of n. The ceiling is + * taken so this will be one greater than required */ +static int digits_ceiling(unsigned int n) +{ + int l = 0; + while (n) n /= 10, ++l; + return l + 1; +} + +/* Return NULL if we couldn't allocate space for the gamestate. initializating the + * gamestate will parse the options internally, so any caller should pass argc and argv + * through this function */ +struct gamestate* gamestate_init(int argc, char **argv) +{ + struct gameoptions *opt = gameoptions_default(); + if (!opt) return NULL; + + if (argc != 0) parse_options(opt, argc, argv); + + srand(time(NULL)); + + struct gamestate *g = malloc(sizeof(struct gamestate)); + if (!g) goto gamestate_alloc_fail; + g->gridsize = opt->grid_width * opt->grid_height; + + g->grid_data_ptr = calloc(g->gridsize, sizeof(int)); + if (!g->grid_data_ptr) goto grid_data_alloc_fail; + + g->grid = malloc(opt->grid_height * sizeof(int*)); + if (!g->grid) goto grid_alloc_fail; + + /* Switch to two allocation version */ + int i; + int **iterator = g->grid; + for (i = 0; i < g->gridsize; i += opt->grid_width) + *iterator++ = &g->grid_data_ptr[i]; + + g->moved = 0; + g->score = 0; + g->score_high = 0; + g->score_last = 0; + g->print_width = digits_ceiling(merge_value(merge_goal())); + g->blocks_in_play = 0; + g->opts = opt; + + /* Clamp spawn rate to maximum to avoid possible excessive calculation + * int generation of blocks */ + if (g->opts->spawn_rate > g->gridsize) + g->opts->spawn_rate = g->gridsize; + + highscore_load(g); + + /* Initial 3 random blocks */ + gamestate_new_block(g); + gamestate_new_block(g); + gamestate_new_block(g); + return g; + +grid_alloc_fail: + free(g->grid_data_ptr); +grid_data_alloc_fail: + free(g); +gamestate_alloc_fail: + return NULL; +} + +/* A tick is a gravitate, merge then gravitate all in the same direction. + * the moved variable is set to 0 initially and if the gravitate of merge + * functions modify it, we can determine which action to take. */ +int gamestate_tick(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state*, struct gamestate*)) +{ + /* Reset move. Altered by gravitate and merge if we do move */ + g->moved = 0; + gravitate(s, g, d, callback); + merge(s, g, d, callback); + gravitate(s, g, d, callback); + return g->moved; +} + +/* Free all data associated with the gamestate */ +void gamestate_clear(struct gamestate *g) +{ + highscore_save(g); + gameoptions_destroy(g->opts); + free(g->grid_data_ptr); /* Free grid data */ + free(g->grid); /* Free pointers to data slots */ + free(g); +} diff --git a/src/engine.h b/src/engine.h new file mode 100644 index 0000000..0d50fad --- /dev/null +++ b/src/engine.h @@ -0,0 +1,45 @@ +#ifndef ENGINE_H +#define ENGINE_H + +#include +#include +#include "options.h" + +#define fatal(msg)\ + do {\ + fprintf(stderr, "line %d: %s\n", __LINE__, msg);\ + abort();\ + } while (0) + +struct gamestate { + /* Game state */ + int *grid_data_ptr; + int **grid; + int gridsize; + int moved; + long score; + long score_high; + long score_last; + int print_width; + int blocks_in_play; + /* Variable command line options */ + struct gameoptions *opts; +}; + +enum { + dir_invalid, + dir_down, + dir_left, + dir_right, + dir_up +}; + +struct gfx_state; + +int gamestate_end_condition(struct gamestate*); +void gamestate_new_block(struct gamestate*); +int gamestate_tick(struct gfx_state*, struct gamestate*, int, void (*callback)(struct gfx_state*, struct gamestate*)); +void gamestate_clear(struct gamestate*); +struct gamestate* gamestate_init(int argc, char **argv); + +#endif diff --git a/src/gfx.h b/src/gfx.h new file mode 100644 index 0000000..3974763 --- /dev/null +++ b/src/gfx.h @@ -0,0 +1,28 @@ +#ifndef GFX_H +#define GFX_H + +#define INPUT_UP 0 +#define INPUT_DOWN 1 +#define INPUT_LEFT 2 +#define INPUT_RIGHT 3 + +#include "engine.h" + +struct gfx_state; + +/* Initialization of a graphics context */ +struct gfx_state* gfx_init(struct gamestate *); + +/* Drawing of a game_state onto a graphics context */ +void gfx_draw(struct gfx_state *, struct gamestate *); + +/* Blocking get character. Should not be buffered for best results */ +int gfx_getch(struct gfx_state *); + +/* Destruction of a graphics context */ +void gfx_destroy(struct gfx_state *); + +/* Sleep for a specifed millisecond period */ +void gfx_sleep(int ms); + +#endif diff --git a/src/gfx_curses.c b/src/gfx_curses.c new file mode 100644 index 0000000..0a2682f --- /dev/null +++ b/src/gfx_curses.c @@ -0,0 +1,157 @@ +#include +#include +#include +#include "gfx.h" +#include "merge.h" +#include +#include + +#define NUMBER_OF_COLORS 7 + +#define iterate(n, expression)\ + do {\ + int i;\ + for (i = 0; i < n; ++i) { expression; }\ + } while (0) + +struct gfx_state { + WINDOW *window; + size_t window_height, window_width; +}; + +struct gfx_state* gfx_init(struct gamestate *g) +{ + initscr(); + cbreak(); + keypad(stdscr, true); + noecho(); + curs_set(FALSE); + refresh(); + + struct gfx_state *s = malloc(sizeof(struct gfx_state)); + if (!s) return NULL; + + s->window_height = g->opts->grid_height * (g->print_width + 2) + 3; + s->window_width = g->opts->grid_width * (g->print_width + 2) + 1; + s->window = newwin(s->window_height, s->window_width, 1, 1); + keypad(s->window, TRUE); + + if (g->opts->enable_color && has_colors()) { + start_color(); + +#ifdef INVERT_COLORS + init_pair(1, COLOR_BLACK, COLOR_RED); + init_pair(2, COLOR_BLACK, COLOR_GREEN); + init_pair(3, COLOR_BLACK, COLOR_YELLOW); + init_pair(4, COLOR_BLACK, COLOR_BLUE); + init_pair(5, COLOR_BLACK, COLOR_MAGENTA); + init_pair(6, COLOR_BLACK, COLOR_CYAN); + init_pair(7, COLOR_BLACK, COLOR_WHITE); +#else + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_YELLOW, COLOR_BLACK); + init_pair(4, COLOR_BLUE, COLOR_BLACK); + init_pair(5, COLOR_MAGENTA, COLOR_BLACK); + init_pair(6, COLOR_CYAN, COLOR_BLACK); + init_pair(7, COLOR_WHITE, COLOR_BLACK); +#endif + } + + return s; +} + +void gfx_draw(struct gfx_state *s, struct gamestate *g) +{ + if (g->score_last) + mvwprintw(s->window, 0, 0, gettext("Score: %d (+%d)\n"), g->score, g->score_last); + else + mvwprintw(s->window, 0, 0, gettext("Score: %d\n"), g->score); + + if (g->score >= g->score_high) + g->score_high = g->score; + + mvwprintw(s->window, 1, 0, gettext(" Hil: %d\n"), g->score_high); + + wattron(s->window, A_DIM); + iterate(g->opts->grid_width * (g->print_width + 2) + 1, waddch(s->window, '-')); + wattroff(s->window, A_DIM); + + int x, y, + xpos = 0, + ypos = 3; + + for (y = 0; y < g->opts->grid_height; ++y, ++ypos, xpos = 0) { + wattron(s->window, A_DIM); + mvwprintw(s->window, ypos, xpos++, "|"); + wattroff(s->window, A_DIM); + + for (x = 0; x < g->opts->grid_width; ++x) { + if (g->grid[x][y]) { + wattron(s->window, COLOR_PAIR(g->grid[x][y] % NUMBER_OF_COLORS + 1)); + mvwprintw(s->window, ypos, xpos, "%-*ld", g->print_width, merge_value(g->grid[x][y])); + mvwprintw(s->window, ypos, xpos + g->print_width, " "); + wattroff(s->window, COLOR_PAIR(g->grid[x][y] % NUMBER_OF_COLORS + 1)); + + wattron(s->window, A_DIM); + mvwprintw(s->window, ypos, xpos + g->print_width + 1, "|"); + wattroff(s->window, A_DIM); + } + else { + wattron(s->window, A_DIM); + iterate(g->print_width + 1, waddch(s->window, ' ')); + waddch(s->window, '|'); + wattroff(s->window, A_DIM); + } + + xpos += (g->print_width + 2); + } + } + + wattron(s->window, A_DIM); + iterate(g->opts->grid_height * (g->print_width + 2) + 1, waddch(s->window, '-')); + wattroff(s->window, A_DIM); + wrefresh(s->window); +} + +int gfx_getch(struct gfx_state *s) +{ + int c = wgetch(s->window); + + /* Flush buffer */ + nodelay(s->window, TRUE); + while (wgetch(s->window) != ERR); + nodelay(s->window, FALSE); + + switch (c) + { + case KEY_UP: + return INPUT_UP; + break; + case KEY_DOWN: + return INPUT_DOWN; + break; + case KEY_RIGHT: + return INPUT_RIGHT; + break; + case KEY_LEFT: + return INPUT_LEFT; + break; + + default: + return c; + break; + + } +} + +void gfx_sleep(int ms) +{ + usleep(ms * 1000); +} + +void gfx_destroy(struct gfx_state *s) +{ + free(s); + endwin(); +} diff --git a/src/gfx_terminal.c b/src/gfx_terminal.c new file mode 100644 index 0000000..93d0520 --- /dev/null +++ b/src/gfx_terminal.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include "merge.h" +#include "gfx.h" +#include +#include + +#define iterate(n, expression)\ + do {\ + int i;\ + for (i = 0; i < n; ++i) { expression; }\ + } while (0) + +struct gfx_state { + struct termios oldt, newt; +}; + +struct gfx_state* gfx_init(struct gamestate *g) +{ + (void) g; + + struct gfx_state *s = malloc(sizeof(struct gfx_state)); + if (!s) return NULL; + tcgetattr(STDIN_FILENO, &s->oldt); + s->newt = s->oldt; + s->newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &s->newt); + return s; +} + +void gfx_draw(struct gfx_state *s, struct gamestate *g) +{ + (void) s; + +#ifdef VT100 + printf("\033[2J\033[H"); +#endif + + if (g->score_last) + printf(gettext("Score: %ld (+%ld)\n"), g->score, g->score_last); + else + printf(gettext("Score: %ld\n"), g->score); + + if (g->score >= g->score_high) + g->score_high = g->score; + + printf(gettext(" Hi: %ld\n"), g->score_high); + + iterate((g->print_width + 2) * g->opts->grid_width + 1, printf("-")); printf("\n"); + + int x, y; + for (y = 0; y < g->opts->grid_width; ++y) { + printf("|"); + + for (x = 0; x < g->opts->grid_width; ++x) { + if (g->grid[x][y]) + printf("%*zd |", g->print_width, merge_value(g->grid[x][y])); + else + printf("%*s |", g->print_width, ""); + } + printf("\n"); + } + + iterate((g->print_width + 2) * g->opts->grid_width + 1, printf("-")); printf("\n\n"); +} + +int gfx_getch(struct gfx_state *s) +{ + (void) s; + return getchar(); +} + +void gfx_sleep(int ms) +{ + usleep(ms * 1000); +} + +void gfx_destroy(struct gfx_state *s) +{ + tcsetattr(STDIN_FILENO, TCSANOW, &s->oldt); + free(s); +} diff --git a/src/highscore.c b/src/highscore.c new file mode 100644 index 0000000..fbf51e3 --- /dev/null +++ b/src/highscore.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include "engine.h" +#include +#include + +const char *hs_dir_name = "2048"; +const char *hs_file_name = "highscore"; + +static const char* highscore_retrieve_file(void) +{ + static char buffer[4096]; + + if (getenv("XDG_DATA_HOME") != NULL) { + snprintf(buffer, sizeof(buffer), "%s/%s/%s", + getenv("XDG_DATA_HOME"), hs_dir_name, hs_file_name); + } + else if (getenv("HOME")) { + snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", + getenv("HOME"), hs_dir_name, hs_file_name); + } + else { + return hs_file_name; + } + + /* Create file only if it doesn't exist */ + if (access(buffer, F_OK) != -1) + return buffer; + + char *sep = strrchr(buffer, '/'); + while (sep != NULL) { + *sep = '\0'; + if (strlen(buffer) != 0) + mkdir(buffer, 0777); + char *tmpsep = sep; + sep = strrchr(buffer, '/'); + *tmpsep = '/'; + } + + return buffer; +} + +static inline void string_to_lower(char *str) +{ + for (; *str; ++str) *str = tolower(*str); +} + +void highscore_reset(void) +{ + const char *hsfile = highscore_retrieve_file(); + const size_t resp_length = 16; + char resp[resp_length]; + + printf(gettext("Are you sure you want to reset your scores? Y(es) or N(o)\n")); + + while (1) { + /* fgets is used to avoid queuing that may occur with getchar */ + if (fgets(resp, resp_length, stdin) == NULL) + return; + + string_to_lower(resp); + + const size_t sl = strlen(resp); + if (sl < resp_length) + resp[sl - 1] = '\0'; + + if (!strncmp(resp, gettext("yes"), resp_length) || !strncmp(resp, gettext("y"), resp_length)) + goto reset_scores; + else if (!strncmp(resp, "no", resp_length) || !strncmp(resp, "n", resp_length)) + return; + + printf(gettext("Please enter Yes or No\n")); + } + +reset_scores:; + FILE *fd = fopen(hsfile, "w+"); + fprintf(fd, "%d", 0); + fclose(fd); +} + +long highscore_load(struct gamestate *g) +{ + const char *hsfile = highscore_retrieve_file(); + long result = 0; + + FILE *fd = fopen(hsfile, "r"); + if (fd == NULL) + fd = fopen(hsfile, "w+"); + + if (fd == NULL) { + fprintf(stderr, gettext("load: Failed to open highscore file\n")); + return 0; + } + + if (fscanf(fd, "%ld", &result) != 1) { + fprintf(stderr, gettext("load: Failed to parse highscore file\n")); + result = 0; + } + + fclose(fd); + + if (g) g->score_high = result; + return result; +} + +void highscore_save(struct gamestate *g) +{ + /* Someone could make their own merge rules for highscores and this could be meaningless, + * howeverhighscores are in plaintext, so that isn't that much of a concern */ + if (g->score < g->score_high || g->opts->grid_width != 4 || + g->opts->grid_height != 4 || g->opts->ai == true) + return; + + const char *hsfile = highscore_retrieve_file(); + + FILE *fd = fopen(hsfile, "w"); + if (fd == NULL) { + fprintf(stderr, gettext("save: Failed to open highscore file\n")); + return; + } + + if (fprintf(fd, "%ld", g->score) < 0) { + fprintf(stderr, gettext("save: Failed to write highscore file\n")); + } + fclose(fd); +} diff --git a/src/highscore.h b/src/highscore.h new file mode 100644 index 0000000..e1b4e19 --- /dev/null +++ b/src/highscore.h @@ -0,0 +1,10 @@ +#ifndef HIGHSCORE_H +#define HIGHSCORE_H + +#include "engine.h" + +void highscore_reset(void); +long highscore_load(struct gamestate *g); +void highscore_save(struct gamestate *g); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..794f1cf --- /dev/null +++ b/src/main.c @@ -0,0 +1,114 @@ +#include +#include +#include "ai.h" +#include "engine.h" +#include "gfx.h" +#include +#include +#include + +void draw_then_sleep(struct gfx_state *s, struct gamestate *g) +{ + gfx_draw(s, g); + /* Have a fixed time for each turn to animate (160 default) */ + gfx_sleep(160 / g->opts->grid_width); +} + +char *targetDir(char *env, char *path) +{ + char *dir; + char *dirEnv; + dirEnv = getenv(env); + dir = malloc(strlen(dirEnv) + strlen(path) + 1); + strcpy(dir, dirEnv); + strcat(dir,path); + return dir; +} + +int main(int argc, char **argv) +{ + + + setlocale (LC_ALL, ""); + bindtextdomain ("gfx_terminal", targetDir("PWD","/18n/")); + textdomain ("gfx_terminal"); + + struct gamestate *g = gamestate_init(argc, argv); + if (!g) { + fatal("failed to allocate gamestate"); + } + + struct gfx_state *s = NULL; + + if (g->opts->interactive) { + s = gfx_init(g); + if (!s) { + fatal("failed to allocate gfx state"); + } + } + + int game_running = true; + while (game_running) { + + if (g->opts->interactive) + gfx_draw(s, g); + +get_new_key:; + int direction = dir_invalid; + int value = !g->opts->ai ? gfx_getch(s) : ai_move(g); + switch (value) { + case 'h': + case 'a': + case INPUT_LEFT: + direction = dir_left; + break; + case 'l': + case 'd': + case INPUT_RIGHT: + direction = dir_right; + break; + case 'j': + case 's': + case INPUT_DOWN: + direction = dir_down; + break; + case 'k': + case 'w': + case INPUT_UP: + direction = dir_up; + break; + case 'q': + game_running = false; + break; + default: + goto get_new_key; + } + + /* Game will only end if 0 moves available */ + if (game_running) { + gamestate_tick(s, g, direction, g->opts->animate && g->opts->interactive + ? draw_then_sleep : NULL); + + if (g->moved == 0) + goto get_new_key; + + int spawned; + for (spawned = 0; spawned < g->opts->spawn_rate; spawned++) + gamestate_new_block(g); + + if (gamestate_end_condition(g)) { + game_running = false; + } + } + } + + if (g->opts->interactive) { + // gfx_getch(s); // getch here would be good, + // need an exit message for each graphical output + gfx_destroy(s); + } + + printf("%ld\n", g->score); + gamestate_clear(g); + return 0; +} diff --git a/src/merge.h b/src/merge.h new file mode 100644 index 0000000..47f2459 --- /dev/null +++ b/src/merge.h @@ -0,0 +1,21 @@ +#ifndef MERGE_H +#define MERGE_H + +/* When defining a new set of rules, remember that the grid values (and the + * therefore the arguments v1, v2) are index values and should be treated as + * such */ + +/* This should return the value of a given index. 0 should default to 0 (empty) */ +long merge_value(const int v1); + +/* This should return the goal index value */ +long merge_goal(void); + +/* Return if a merge is possible between two indices */ +int merge_possible(const int v1, const int v2); + +/* The result of a merge of two values. If the two parameters are not mergeable, + * then a -1 error code is the suggested return value */ +int merge_result(const int v1, const int v2); + +#endif diff --git a/src/merge_fib.c b/src/merge_fib.c new file mode 100644 index 0000000..f1b8975 --- /dev/null +++ b/src/merge_fib.c @@ -0,0 +1,30 @@ +#include "merge.h" + +#define MERGE_GOAL (int)((sizeof(merge_values)/sizeof(merge_values[0]))-1) + +const long merge_values[] = { + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, + 233, 377, 610, 987, 1597 +}; + +inline long merge_value(const int v1) +{ + return v1 <= MERGE_GOAL ? merge_values[v1] : -1; +} + +inline long merge_goal(void) +{ + return MERGE_GOAL; +} + +inline int merge_possible(const int v1, const int v2) +{ + return v1 == v2 - 1 || v2 == v1 - 1 || + ((v1 == 1 || v1 == 2) && (v2 == 1 || v2 == 2)); +} + +inline int merge_result(const int v1, const int v2) +{ + int max = v1 > v2 ? v1 : v2; + return merge_possible(v1, v2) ? max + 1 : -1; +} diff --git a/src/merge_std.c b/src/merge_std.c new file mode 100644 index 0000000..57d0a0b --- /dev/null +++ b/src/merge_std.c @@ -0,0 +1,28 @@ +#include "merge.h" + +#define MERGE_GOAL (int)((sizeof(merge_values)/sizeof(merge_values[0]))-1) + +const long merge_values[] = { + 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, + 1024, 2048 +}; + +inline long merge_value(const int v1) +{ + return v1 <= MERGE_GOAL ? merge_values[v1] : -1; +} + +inline long merge_goal(void) +{ + return MERGE_GOAL; +} + +inline int merge_possible(const int v1, const int v2) +{ + return v1 == v2; +} + +inline int merge_result(const int v1, const int v2) +{ + return merge_possible(v1, v2) ? v1 + 1 : -1; +} diff --git a/src/options.c b/src/options.c new file mode 100644 index 0000000..053f498 --- /dev/null +++ b/src/options.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include "highscore.h" +#include "options.h" + +void print_usage(void) +{ + printf("usage: 2048 [-cCaAiIrh] [-s SIZE] [-b RATE]\n"); +} + + +/* Initial game options */ +struct gameoptions* gameoptions_default(void) +{ + struct gameoptions *opt = malloc(sizeof(struct gameoptions)); + if (!opt) return NULL; + + opt->grid_height = DEFAULT_GRID_HEIGHT; + opt->grid_width = DEFAULT_GRID_WIDTH; + opt->spawn_value = DEFAULT_SPAWN_VALUE; + opt->spawn_rate = DEFAULT_SPAWN_RATE; + opt->enable_color = DEFAULT_COLOR_FLAG; + opt->animate = DEFAULT_ANIMATE_FLAG; + opt->ai = DEFAULT_AI_FLAG; + opt->interactive = DEFAULT_INTERACTIVE_FLAG; + + return opt; +} + +void gameoptions_destroy(struct gameoptions *opt) +{ + free(opt); +} + +struct gameoptions* parse_options(struct gameoptions *opt, int argc, char **argv) +{ + int c; + while ((c = getopt(argc, argv, "aArcCiIhHs:b:")) != -1) { + switch (c) { + case 'a': + opt->animate = true; + break; + case 'A': + opt->animate = false; + break; + case 'c': + opt->enable_color = true; + break; + case 'C': + opt->enable_color = false; + break; + case 'i': + opt->ai = true; + opt->interactive = false; + break; + case 'I': + opt->ai = true; + opt->interactive = true; + break; + case 's':; + /* Stick with square for now */ + int optint = strtol(optarg, NULL, 10); + if (optint < CONSTRAINT_GRID_MAX && optint > CONSTRAINT_GRID_MIN) { + opt->grid_height = optint; + opt->grid_width = optint; + } + break; + case 'b': + opt->spawn_rate = strtol(optarg, NULL, 10); + break; + case 'r': + highscore_reset(); + exit(0); + case 'h': + print_usage(); + exit(0); + case 'H': + printf("%ld\n", highscore_load(NULL)); + exit(0); + } + } + + return opt; +} diff --git a/src/options.h b/src/options.h new file mode 100644 index 0000000..2126cd3 --- /dev/null +++ b/src/options.h @@ -0,0 +1,34 @@ +#ifndef OPTIONS_H +#define OPTIONS_H + +#include +#include + +#define CONSTRAINT_GRID_MIN 4 +#define CONSTRAINT_GRID_MAX 20 +#define DEFAULT_GRID_HEIGHT 4 +#define DEFAULT_GRID_WIDTH 4 +#define DEFAULT_SPAWN_VALUE 2 +#define DEFAULT_SPAWN_RATE 1 +#define DEFAULT_COLOR_FLAG false +#define DEFAULT_ANIMATE_FLAG true +#define DEFAULT_AI_FLAG false +#define DEFAULT_INTERACTIVE_FLAG true + +struct gameoptions { + int grid_height; + int grid_width; + long spawn_value; + int spawn_rate; + bool enable_color; + bool animate; + bool ai; + bool interactive; +}; + +void print_usage(void); +struct gameoptions* parse_options(struct gameoptions*, int, char**); +struct gameoptions* gameoptions_default(void); +void gameoptions_destroy(struct gameoptions *opt); + +#endif