ref: 2e3c458a12df98ae916ac92712fad87f81aabdce
parent: 9671de025471a5ee4f39d5acb1434161a21ed317
author: mkf <mkf@cloud9p.org>
date: Thu May 23 11:40:45 EDT 2024
engine: move client enums to clients, add some handy functions
--- a/engine.c
+++ b/engine.c
@@ -6,9 +6,7 @@
#include <draw.h>
#endif
-// XXX
#include "util.h"
-
#include "engine.h"
#include "netclient.h"
@@ -15,7 +13,7 @@
int difficulty = DEasy;
int state;
int turn = 0;
-int ptype[2] = {Human, Computer}; /* human? computer? */
+int ptype[2] = {Human, Computer};
int grid[SzX][SzY];
int pgrid[SzX][SzY]; /* for undo */
@@ -169,7 +167,7 @@
grid[src.x][src.y] = Prev;
turn++;
- nextglenda();
+ checkstate();
return Ok;
}
@@ -206,7 +204,10 @@
grid[x][y] = 100;
/* we need it to check game state, even if not playing with computer */
- nextglenda();
+ if(ptype[1] == Computer)
+ nextglenda();
+ else if(ptype[1] == Human)
+ checkstate();
return Ok;
}
@@ -286,6 +287,20 @@
grid[x][y] = score1(Pt(x, y));
}
+int
+findmin(void)
+{
+ int next, min = 1000;
+ Point p = findglenda();
+
+ for(int dir = NE; dir <= NW; dir++)
+ {
+ next = checknext(dir, p);
+ if(next < min)
+ min = next;
+ }
+ return min;
+}
void
nextglenda(void)
{
@@ -298,7 +313,7 @@
calc();
calc();
calc();
-
+
for(dir = NE; dir <= NW; dir++)
{
next = checknext(dir, p);
@@ -310,15 +325,29 @@
}
else if(next == min)
nextdir = (nrand(++count) == 0) ? dir : nextdir;
- }
+ }
+
if(min > 100)
state = Won;
- else if(ptype[1] == Computer)
- domove(nextdir);
+ domove(nextdir);
+
p = findglenda();
if(p.x == 0 || p.x == SzX-1 || p.y == 0 || p.y == SzY-1)
state = Lost;
+}
+
+int
+checkstate(void)
+{
+ Point p = findglenda();
+
+ if(findmin() > 100)
+ state = Won;
+ else if(p.x == 0 || p.x == SzX-1 || p.y == 0 || p.y == SzY-1)
+ state = Lost;
+
+ return state;
}
void
--- a/engine.h
+++ b/engine.h
@@ -8,17 +8,10 @@
DHard, /* 1≤x<5 */
DImp, /* 0 */
- New = 0,
- Undo,
- Restart,
- Exit,
-
/* dynamic? original game has a fixed grid size, but we don't need to abide by it */
SzX = 11,
- SzY = 11,
+ SzY = 11,
- Border = 3,
-
/* movement directions */
NE,
E,
@@ -35,9 +28,10 @@
PTrapper = 0,
PGlenda,
PEither,
-
+
+ /* game states */
Init = 0, /* setting up the map */
- Start, /* game states */
+ Start,
Playing,
Won,
Lost,
@@ -72,12 +66,19 @@
void initlevel(void);
Point movedir(int dir, Point p);
int pointdir(Point src, Point dst);
+
int domove(int dir);
int doput(Point p);
+
Point findglenda(void);
int checknext(int dir, Point p);
+
int score1(Point p);
void calc(void);
void nextglenda(void);
+
+int findmin(void);
+int checkstate(void);
+
void restart(void);
void undo(void);