--- /dev/null
+#include <stdio.h>
+
+#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: */
/* 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 <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "ai.h"
#include "constant.h"
#include "debug.h"
#include "display.h"
char *progname = NULL;
char *version = "1.0";
+int ai = -1;
char *boardname = "8x8";
char *filename = NULL;
int maxnbrecords = 0;
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");
}
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) {
/* 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);
/* get key press */
int ch = getch ();
-
+
/* record key pressed */
if (maxnbrecords) {
if (nbrecords < maxnbrecords) {