From: Laurent Mazet Date: Mon, 23 Sep 2024 05:59:12 +0000 (+0200) Subject: ai (wip) X-Git-Tag: v1.1~3 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=60efe8c136b85bced2eeba03cf9a93225634a83b;p=reversi.git ai (wip) --- diff --git a/ai.c b/ai.c new file mode 100644 index 0000000..58da3bf --- /dev/null +++ b/ai.c @@ -0,0 +1,36 @@ +#include + +#include "debug.h" +#include "function.h" +#include "type.h" + +#include "ai.h" + +int play (board_t *board, int id) +{ + int x = -1; + int y = -1; + int s = 0; + + int i, j; + for (i = 0; i < board->width; i++) { + for (j = 0; j < board->height; j++) { + board_t *newboard = copyboard (board); + if (putdisk (newboard, i, j, id)) { + int ns = count (newboard, id); + if (ns > s) { + x = i; + y = j; + ns = s; + } + } + freeboard (newboard); + } + } + if (s > 0) { + putdisk (board, x, y, id); + } + return (s > 0); +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/ai.h b/ai.h new file mode 100644 index 0000000..c2a2a55 --- /dev/null +++ b/ai.h @@ -0,0 +1,8 @@ +#ifndef __AI_H__ +#define __AI_H__ + +#include "type.h" + +int play (board_t *board, int id); + +#endif /* __AI_H__ */ diff --git a/reversi.c b/reversi.c index edd2acd..6ccb640 100644 --- a/reversi.c +++ b/reversi.c @@ -1,14 +1,15 @@ /* depend: */ /* cflags: */ -/* linker: block.o color.c constant.o debug.o display.o function.o -lcurses */ -/* doslnk: block.o color.c constant.o debug.o display.o function.o -lpdc~1 */ -/* winlnk: block.o color.c constant.o debug.o display.o function.o -lpdcurses */ +/* linker: ai.o block.o color.c constant.o debug.o display.o function.o -lcurses */ +/* doslnk: ai.o block.o color.c constant.o debug.o display.o function.o -lpdc~1 */ +/* winlnk: ai.o block.o color.c constant.o debug.o display.o function.o -lpdcurses */ #include #include #include #include +#include "ai.h" #include "constant.h" #include "debug.h" #include "display.h" @@ -19,6 +20,7 @@ char *progname = NULL; char *version = "1.0"; +int ai = -1; char *boardname = "8x8"; char *filename = NULL; int maxnbrecords = 0; @@ -39,7 +41,8 @@ char *help = int usage (int ret) { FILE *fd = ret ? stderr : stdout; - fprintf (fd, "usage: %s [-b] [-f file] [-h] [-r int] [-s int] [-v int]\n", progname); + fprintf (fd, "usage: %s [-a] [-b] [-f file] [-h] [-r int] [-s int] [-v int]\n", progname); + fprintf (fd, " -a: automatic player (%s)\n", (ai == -1) ? "off" : ((ai == 0) ? "0" : "1")); fprintf (fd, " -b: board form [6x10, 5x12, 4x15, 3x20] (%s)\n", boardname); fprintf (fd, " -f: file name (%s)\n", (filename) ? filename : "none"); fprintf (fd, " -h: help message\n"); @@ -73,6 +76,9 @@ int main (int argc, char *argv[]) } char c = arg[1]; switch (c) { + case 'a': + ai = 1; + break; case 'b': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; if (arg == NULL) { @@ -202,15 +208,49 @@ int main (int argc, char *argv[]) /* board */ board_t *boardturn = copyboard (board); - sprintf (msg, "Player %s", id ? "Two" : "One"); - if (!find (boardturn, id)) { - sprintf (msg, "Skip %s", id ? "Two" : "One"); - id = id ? 0 : 1; + if (ai == -1) { if (!find (boardturn, id)) { + sprintf (msg, "Skip %s", id ? "Two" : "One"); + id = id ? 0 : 1; + if (!find (boardturn, id)) { + sprintf (msg, "End of game"); + mode = 1; + } + } + + /* ai player */ + } else { + int test = 0; + do { + if (id != ai) { + if (!find (boardturn, id)) { + sprintf (msg, "Skip %s", id ? "Two" : "One"); + id = id ? 0 : 1; + test++; + freeboard (boardturn); + boardturn = copyboard (board); + } else { + break; + } + } else { + if (!play (board, id)) { + sprintf (msg, "Skip %s", id ? "Two" : "One"); + id = id ? 0 : 1; + test++; + } else { + test = 0; + } + } + } while (test == 2); + if (test == 2) { sprintf (msg, "End of game"); mode = 1; } } + if (*msg) { + sprintf (msg, "Player %s", id ? "Two" : "One"); + } + boardwindow ((showpos) ? boardturn : board, 0); freeboard (boardturn); @@ -225,7 +265,7 @@ int main (int argc, char *argv[]) /* get key press */ int ch = getch (); - + /* record key pressed */ if (maxnbrecords) { if (nbrecords < maxnbrecords) {