1
0
Fork 0

logik : add a brand new game

logik is also known as mastermind. There is an optionnal flag to compile
with :
`make logik COLORMODE=BGCOLOR`
**NOTE** : only tested on FreeBSD, it suppose to work on UNIX-like. One
of the needed dependency is :
```
/bin/stty raw
/bin/stty cooked
```
it's hard coded in the line 75, 134 and 140.
This commit is contained in:
Philippe Krejčí 2021-07-26 12:33:54 +02:00
parent f1a58a9e4a
commit 3bb91c82ac
No known key found for this signature in database
GPG Key ID: D5A095F3C6A9BBF0
4 changed files with 183 additions and 2 deletions

View File

@ -9,8 +9,9 @@ MAN_DIR?=/usr/share/man/man6
CFLAGS+= -Wno-unused-result -D SCORES_DIR=\"$(SCORES_DIR)\"
LDFLAGS+= -lncurses -lm
COLORMODE:=FGCOLOR
ALL= jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks fisher muncher miketron redsquare darrt snakeduel
ALL= jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks fisher muncher miketron redsquare darrt snakeduel logik
SCORE_FILES= pipes_scores jewels_scores miketron_scores muncher_scores fisher_scores darrt_scores
all: $(ALL)
@ -53,9 +54,11 @@ redsquare: redsquare.c config.h
$(CC) redsquare.c $(LDFLAGS) $(CFLAGS) -o ./redsquare
darrt: darrt.c config.h common.h
$(CC) darrt.c $(LDFLAGS) $(CFLAGS) -o ./darrt
snakeduel: snakeduel.c config.h
$(CC) snakeduel.c $(LDFLAGS) $(CFLAGS) -o ./snakeduel
logik: logik.c
$(CC) $@.c -o $@ -D$(COLORMODE)
clean:
rm $(ALL)
uninstall:

View File

@ -29,6 +29,7 @@ The games include:
* Redsquare (Conway's Game of Life made playable!)
* Darrt (with original gameplay!)
* Snakeduel
* Logik (or also known as Mastermind)
The difficulty and/or dimensions are adjustable through simple command line options, you can play a minesweeper game that take hours to complete, or exprience hexadecimal sudoku and 8x8 fifteen-like puzzles!

148
logik.c Normal file
View File

@ -0,0 +1,148 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define NBHOLES 5
#define NBLINES 10
#define NBCOLORS 8
#define NBMINUS 5
unsigned char pion[] = "rgypmRGY";
int
convert(unsigned char a) {
for(int i=0; i<NBCOLORS; i++)
if( a == pion[i] )
return i;
return -1;
}
int
colorize(int a) {
return a % NBMINUS + 31;
}
int
bold(int a) {
return a <= 'Z';
}
int
pastel(unsigned char a) {
if( convert(a) < 0 )
return -1;
#ifdef BGCOLOR
/* if you want bg color define the macro BGCOLOR: */
printf("\033[0;%dm%c\033[0m", bold(a)*10 + colorize(convert(a)), a);
#else
printf("\033[%d;%dm%c\033[0m", bold(a), colorize(convert(a)), a);
#endif
return 1;
}
int
main(void) {
printf("\033[22;34m====================\n\r LOGIK \n\r====================\033[0m\n");
printf("\rcolors\t");
for( int i=0; i<NBCOLORS; i++)
pastel( pion[i] );
printf("\n\rx is backspace\n\rQQ to exit");
int sol[NBHOLES], mix[NBCOLORS];
srand( getpid() * ( 1 + getppid() ) + time(NULL) );
for(int i=0; i<NBMINUS; i++)
sol[i] = rand() % NBCOLORS;
printf("\r");
for(int i=0; i<NBCOLORS; i++)
mix[i]=0;
for(int i=0; i<NBHOLES; i++)
mix[sol[i]]++;
/* ----- */
char c;
char inc[NBHOLES], stop=0;
int black, white;
int tmp[NBCOLORS];
system("/bin/stty raw");
for(int l=1; l <= NBLINES; l++) {
printf("\n\r%d\t",l); /*due to stty raw*/
for( int i=0; i < NBHOLES ; ) {
c = getchar();
if( pastel(c) > 0 ) {
inc[i] = convert(c);
i++;
stop = 0;
}
if( i > 0 & c == 'x' ) { /*aimed to backspace*/
i--;
printf("\b");
stop = 0;
}
if( c == 'Q' ) {
stop++;
if( stop == 2)
break;
}
}
printf("\t");
if( stop == 2 )
break;
black=0; white=0;
/* black section : x : is in the right place */
for(int i=0; i<NBHOLES; i++)
if( inc[i] == sol[i] )
black++;
/* white section : o : the color is right but placed */
for(int i=0; i<NBCOLORS; i++)
tmp[i]=0;
for(int i=0; i<NBHOLES; i++)
tmp[(int) inc[i]] += 1;
white = 0;
for(int i=0; i<NBCOLORS; i++)
if( (mix[i] > 0) & (tmp[i] > 0) ) {
if( tmp[i] <= mix[i] )
white += tmp[i];
else if ( mix[i] < tmp[i] )
white += mix[i];
}
/* fmt */
for(int i=0; i < NBHOLES - white ; i++)
printf("-");
for(int i=0; i < black; i++)
printf("x");
for(int i=0; i < white - black; i++)
printf("o");
if( black == 5 ) {
system("/bin/stty cooked");
printf("\n\tYOU WIN\n");
return 0;
}
for(int i=0; i<NBHOLES; inc[i++]=-1)
;
}
system("/bin/stty cooked");
printf("\n\rYOU LOOSE\n\r");
for(int i=0; i<NBHOLES; i++)
pastel(pion[sol[i]]);
printf("\n");
return 0;
}

29
man/logik.6 Normal file
View File

@ -0,0 +1,29 @@
.TH "LOGIK" "" "May 2021" "" ""
.SH "NAME"
\fBlogik\fR
.SH "CONTROLS"
rgypmRGY : put a specific color
.P
QQ : quit
.P
x : backspace
.SH "GAMEPLAY"
The computer generate random pattern of 5 colors
that you must guess\. You have 8 colors, and 10 lines
to guess with each 5 holes\. After you entered your 5
colors the computer will print you a hint, to help you:
.P
\'-\' : one of the colors doesn't match with the correct answere\.
.P
\'x\' : one of the colors is on the right place\.
.P
\'o\' : one of the colors match but is missplaced\.
.SH "HISTORY"
This is game is known as
.B
Logik
in the eastern block (USSR),
and
.B
Mastermind
in the west block (USA)\.