tents.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  1. /*
  2. * tents.c: Puzzle involving placing tents next to trees subject to
  3. * some confusing conditions.
  4. *
  5. * TODO:
  6. *
  7. * - it might be nice to make setter-provided tent/nontent clues
  8. * inviolable?
  9. * * on the other hand, this would introduce considerable extra
  10. * complexity and size into the game state; also inviolable
  11. * clues would have to be marked as such somehow, in an
  12. * intrusive and annoying manner. Since they're never
  13. * generated by _my_ generator, I'm currently more inclined
  14. * not to bother.
  15. *
  16. * - more difficult levels at the top end?
  17. * * for example, sometimes we can deduce that two BLANKs in
  18. * the same row are each adjacent to the same unattached tree
  19. * and to nothing else, implying that they can't both be
  20. * tents; this enables us to rule out some extra combinations
  21. * in the row-based deduction loop, and hence deduce more
  22. * from the number in that row than we could otherwise do.
  23. * * that by itself doesn't seem worth implementing a new
  24. * difficulty level for, but if I can find a few more things
  25. * like that then it might become worthwhile.
  26. * * I wonder if there's a sensible heuristic for where to
  27. * guess which would make a recursive solver viable?
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <assert.h>
  33. #include <ctype.h>
  34. #include <limits.h>
  35. #ifdef NO_TGMATH_H
  36. # include <math.h>
  37. #else
  38. # include <tgmath.h>
  39. #endif
  40. #include "puzzles.h"
  41. #include "matching.h"
  42. /*
  43. * Design discussion
  44. * -----------------
  45. *
  46. * The rules of this puzzle as available on the WWW are poorly
  47. * specified. The bits about tents having to be orthogonally
  48. * adjacent to trees, tents not being even diagonally adjacent to
  49. * one another, and the number of tents in each row and column
  50. * being given are simple enough; the difficult bit is the
  51. * tent-to-tree matching.
  52. *
  53. * Some sources use simplistic wordings such as `each tree is
  54. * exactly connected to only one tent', which is extremely unclear:
  55. * it's easy to read erroneously as `each tree is _orthogonally
  56. * adjacent_ to exactly one tent', which is definitely incorrect.
  57. * Even the most coherent sources I've found don't do a much better
  58. * job of stating the rule.
  59. *
  60. * A more precise statement of the rule is that it must be possible
  61. * to find a bijection f between tents and trees such that each
  62. * tree T is orthogonally adjacent to the tent f(T), but that a
  63. * tent is permitted to be adjacent to other trees in addition to
  64. * its own. This slightly non-obvious criterion is what gives this
  65. * puzzle most of its subtlety.
  66. *
  67. * However, there's a particularly subtle ambiguity left over. Is
  68. * the bijection between tents and trees required to be _unique_?
  69. * In other words, is that bijection conceptually something the
  70. * player should be able to exhibit as part of the solution (even
  71. * if they aren't actually required to do so)? Or is it sufficient
  72. * to have a unique _placement_ of the tents which gives rise to at
  73. * least one suitable bijection?
  74. *
  75. * The puzzle shown to the right of this .T. 2 *T* 2
  76. * paragraph illustrates the problem. There T.T 0 -> T-T 0
  77. * are two distinct bijections available. .T. 2 *T* 2
  78. * The answer to the above question will
  79. * determine whether it's a valid puzzle. 202 202
  80. *
  81. * This is an important question, because it affects both the
  82. * player and the generator. Eventually I found all the instances
  83. * of this puzzle I could Google up, solved them all by hand, and
  84. * verified that in all cases the tree/tent matching was uniquely
  85. * determined given the tree and tent positions. Therefore, the
  86. * puzzle as implemented in this source file takes the following
  87. * policy:
  88. *
  89. * - When checking a user-supplied solution for correctness, only
  90. * verify that there exists _at least_ one matching.
  91. * - When generating a puzzle, enforce that there must be
  92. * _exactly_ one.
  93. *
  94. * Algorithmic implications
  95. * ------------------------
  96. *
  97. * Another way of phrasing the tree/tent matching criterion is to
  98. * say that the bipartite adjacency graph between trees and tents
  99. * has a perfect matching. That is, if you construct a graph which
  100. * has a vertex per tree and a vertex per tent, and an edge between
  101. * any tree and tent which are orthogonally adjacent, it is
  102. * possible to find a set of N edges of that graph (where N is the
  103. * number of trees and also the number of tents) which between them
  104. * connect every tree to every tent.
  105. *
  106. * The most efficient known algorithms for finding such a matching
  107. * given a graph, as far as I'm aware, are the Munkres assignment
  108. * algorithm (also known as the Hungarian algorithm) and the
  109. * Ford-Fulkerson algorithm (for finding optimal flows in
  110. * networks). Each of these takes O(N^3) running time; so we're
  111. * talking O(N^3) time to verify any candidate solution to this
  112. * puzzle. That's just about OK if you're doing it once per mouse
  113. * click (and in fact not even that, since the sensible thing to do
  114. * is check all the _other_ puzzle criteria and only wade into this
  115. * quagmire if none are violated); but if the solver had to keep
  116. * doing N^3 work internally, then it would probably end up with
  117. * more like N^5 or N^6 running time, and grid generation would
  118. * become very clunky.
  119. *
  120. * Fortunately, I've been able to prove a very useful property of
  121. * _unique_ perfect matchings, by adapting the proof of Hall's
  122. * Marriage Theorem. For those unaware of Hall's Theorem, I'll
  123. * recap it and its proof: it states that a bipartite graph
  124. * contains a perfect matching iff every set of vertices on the
  125. * left side of the graph have a neighbourhood _at least_ as big on
  126. * the right.
  127. *
  128. * This condition is obviously satisfied if a perfect matching does
  129. * exist; each left-side node has a distinct right-side node which
  130. * is the one assigned to it by the matching, and thus any set of n
  131. * left vertices must have a combined neighbourhood containing at
  132. * least the n corresponding right vertices, and possibly others
  133. * too. Alternatively, imagine if you had (say) three left-side
  134. * nodes all of which were connected to only two right-side nodes
  135. * between them: any perfect matching would have to assign one of
  136. * those two right nodes to each of the three left nodes, and still
  137. * give the three left nodes a different right node each. This is
  138. * of course impossible.
  139. *
  140. * To prove the converse (that if every subset of left vertices
  141. * satisfies the Hall condition then a perfect matching exists),
  142. * consider trying to find a proper subset of the left vertices
  143. * which _exactly_ satisfies the Hall condition: that is, its right
  144. * neighbourhood is precisely the same size as it. If we can find
  145. * such a subset, then we can split the bipartite graph into two
  146. * smaller ones: one consisting of the left subset and its right
  147. * neighbourhood, the other consisting of everything else. Edges
  148. * from the left side of the former graph to the right side of the
  149. * latter do not exist, by construction; edges from the right side
  150. * of the former to the left of the latter cannot be part of any
  151. * perfect matching because otherwise the left subset would not be
  152. * left with enough distinct right vertices to connect to (this is
  153. * exactly the same deduction used in Solo's set analysis). You can
  154. * then prove (left as an exercise) that both these smaller graphs
  155. * still satisfy the Hall condition, and therefore the proof will
  156. * follow by induction.
  157. *
  158. * There's one other possibility, which is the case where _no_
  159. * proper subset of the left vertices has a right neighbourhood of
  160. * exactly the same size. That is, every left subset has a strictly
  161. * _larger_ right neighbourhood. In this situation, we can simply
  162. * remove an _arbitrary_ edge from the graph. This cannot reduce
  163. * the size of any left subset's right neighbourhood by more than
  164. * one, so if all neighbourhoods were strictly bigger than they
  165. * needed to be initially, they must now still be _at least as big_
  166. * as they need to be. So we can keep throwing out arbitrary edges
  167. * until we find a set which exactly satisfies the Hall condition,
  168. * and then proceed as above. []
  169. *
  170. * That's Hall's theorem. I now build on this by examining the
  171. * circumstances in which a bipartite graph can have a _unique_
  172. * perfect matching. It is clear that in the second case, where no
  173. * left subset exactly satisfies the Hall condition and so we can
  174. * remove an arbitrary edge, there cannot be a unique perfect
  175. * matching: given one perfect matching, we choose our arbitrary
  176. * removed edge to be one of those contained in it, and then we can
  177. * still find a perfect matching in the remaining graph, which will
  178. * be a distinct perfect matching in the original.
  179. *
  180. * So it is a necessary condition for a unique perfect matching
  181. * that there must be at least one proper left subset which
  182. * _exactly_ satisfies the Hall condition. But now consider the
  183. * smaller graph constructed by taking that left subset and its
  184. * neighbourhood: if the graph as a whole had a unique perfect
  185. * matching, then so must this smaller one, which means we can find
  186. * a proper left subset _again_, and so on. Repeating this process
  187. * must eventually reduce us to a graph with only one left-side
  188. * vertex (so there are no proper subsets at all); this vertex must
  189. * be connected to only one right-side vertex, and hence must be so
  190. * in the original graph as well (by construction). So we can
  191. * discard this vertex pair from the graph, and any other edges
  192. * that involved it (which will by construction be from other left
  193. * vertices only), and the resulting smaller graph still has a
  194. * unique perfect matching which means we can do the same thing
  195. * again.
  196. *
  197. * In other words, given any bipartite graph with a unique perfect
  198. * matching, we can find that matching by the following extremely
  199. * simple algorithm:
  200. *
  201. * - Find a left-side vertex which is only connected to one
  202. * right-side vertex.
  203. * - Assign those vertices to one another, and therefore discard
  204. * any other edges connecting to that right vertex.
  205. * - Repeat until all vertices have been matched.
  206. *
  207. * This algorithm can be run in O(V+E) time (where V is the number
  208. * of vertices and E is the number of edges in the graph), and the
  209. * only way it can fail is if there is not a unique perfect
  210. * matching (either because there is no matching at all, or because
  211. * it isn't unique; but it can't distinguish those cases).
  212. *
  213. * Thus, the internal solver in this source file can be confident
  214. * that if the tree/tent matching is uniquely determined by the
  215. * tree and tent positions, it can find it using only this kind of
  216. * obvious and simple operation: assign a tree to a tent if it
  217. * cannot possibly belong to any other tent, and vice versa. If the
  218. * solver were _only_ trying to determine the matching, even that
  219. * `vice versa' wouldn't be required; but it can come in handy when
  220. * not all the tents have been placed yet. I can therefore be
  221. * reasonably confident that as long as my solver doesn't need to
  222. * cope with grids that have a non-unique matching, it will also
  223. * not need to do anything complicated like set analysis between
  224. * trees and tents.
  225. */
  226. /*
  227. * In standalone solver mode, `verbose' is a variable which can be
  228. * set by command-line option; in debugging mode it's simply always
  229. * true.
  230. */
  231. #if defined STANDALONE_SOLVER
  232. #define SOLVER_DIAGNOSTICS
  233. static bool verbose = false;
  234. #elif defined SOLVER_DIAGNOSTICS
  235. #define verbose true
  236. #endif
  237. /*
  238. * Difficulty levels. I do some macro ickery here to ensure that my
  239. * enum and the various forms of my name list always match up.
  240. */
  241. #define DIFFLIST(A) \
  242. A(EASY,Easy,e) \
  243. A(TRICKY,Tricky,t)
  244. #define ENUM(upper,title,lower) DIFF_ ## upper,
  245. #define TITLE(upper,title,lower) #title,
  246. #define ENCODE(upper,title,lower) #lower
  247. #define CONFIG(upper,title,lower) ":" #title
  248. enum { DIFFLIST(ENUM) DIFFCOUNT };
  249. static char const *const tents_diffnames[] = { DIFFLIST(TITLE) };
  250. static char const tents_diffchars[] = DIFFLIST(ENCODE);
  251. #define DIFFCONFIG DIFFLIST(CONFIG)
  252. enum {
  253. COL_BACKGROUND,
  254. COL_GRID,
  255. COL_GRASS,
  256. COL_TREETRUNK,
  257. COL_TREELEAF,
  258. COL_TENT,
  259. COL_ERROR,
  260. COL_ERRTEXT,
  261. COL_ERRTRUNK,
  262. NCOLOURS
  263. };
  264. enum { BLANK, TREE, TENT, NONTENT, MAGIC };
  265. struct game_params {
  266. int w, h;
  267. int diff;
  268. };
  269. struct numbers {
  270. int refcount;
  271. int *numbers;
  272. };
  273. struct game_state {
  274. game_params p;
  275. char *grid;
  276. struct numbers *numbers;
  277. bool completed, used_solve;
  278. };
  279. static game_params *default_params(void)
  280. {
  281. game_params *ret = snew(game_params);
  282. ret->w = ret->h = 8;
  283. ret->diff = DIFF_EASY;
  284. return ret;
  285. }
  286. static const struct game_params tents_presets[] = {
  287. {8, 8, DIFF_EASY},
  288. {8, 8, DIFF_TRICKY},
  289. {10, 10, DIFF_EASY},
  290. {10, 10, DIFF_TRICKY},
  291. {15, 15, DIFF_EASY},
  292. {15, 15, DIFF_TRICKY},
  293. };
  294. static bool game_fetch_preset(int i, char **name, game_params **params)
  295. {
  296. game_params *ret;
  297. char str[80];
  298. if (i < 0 || i >= lenof(tents_presets))
  299. return false;
  300. ret = snew(game_params);
  301. *ret = tents_presets[i];
  302. sprintf(str, "%dx%d %s", ret->w, ret->h, tents_diffnames[ret->diff]);
  303. *name = dupstr(str);
  304. *params = ret;
  305. return true;
  306. }
  307. static void free_params(game_params *params)
  308. {
  309. sfree(params);
  310. }
  311. static game_params *dup_params(const game_params *params)
  312. {
  313. game_params *ret = snew(game_params);
  314. *ret = *params; /* structure copy */
  315. return ret;
  316. }
  317. static void decode_params(game_params *params, char const *string)
  318. {
  319. params->w = params->h = atoi(string);
  320. while (*string && isdigit((unsigned char)*string)) string++;
  321. if (*string == 'x') {
  322. string++;
  323. params->h = atoi(string);
  324. while (*string && isdigit((unsigned char)*string)) string++;
  325. }
  326. if (*string == 'd') {
  327. int i;
  328. string++;
  329. for (i = 0; i < DIFFCOUNT; i++)
  330. if (*string == tents_diffchars[i])
  331. params->diff = i;
  332. if (*string) string++;
  333. }
  334. }
  335. static char *encode_params(const game_params *params, bool full)
  336. {
  337. char buf[120];
  338. sprintf(buf, "%dx%d", params->w, params->h);
  339. if (full)
  340. sprintf(buf + strlen(buf), "d%c",
  341. tents_diffchars[params->diff]);
  342. return dupstr(buf);
  343. }
  344. static config_item *game_configure(const game_params *params)
  345. {
  346. config_item *ret;
  347. char buf[80];
  348. ret = snewn(4, config_item);
  349. ret[0].name = "Width";
  350. ret[0].type = C_STRING;
  351. sprintf(buf, "%d", params->w);
  352. ret[0].u.string.sval = dupstr(buf);
  353. ret[1].name = "Height";
  354. ret[1].type = C_STRING;
  355. sprintf(buf, "%d", params->h);
  356. ret[1].u.string.sval = dupstr(buf);
  357. ret[2].name = "Difficulty";
  358. ret[2].type = C_CHOICES;
  359. ret[2].u.choices.choicenames = DIFFCONFIG;
  360. ret[2].u.choices.selected = params->diff;
  361. ret[3].name = NULL;
  362. ret[3].type = C_END;
  363. return ret;
  364. }
  365. static game_params *custom_params(const config_item *cfg)
  366. {
  367. game_params *ret = snew(game_params);
  368. ret->w = atoi(cfg[0].u.string.sval);
  369. ret->h = atoi(cfg[1].u.string.sval);
  370. ret->diff = cfg[2].u.choices.selected;
  371. return ret;
  372. }
  373. static const char *validate_params(const game_params *params, bool full)
  374. {
  375. /*
  376. * Generating anything under 4x4 runs into trouble of one kind
  377. * or another.
  378. */
  379. if (params->w < 4 || params->h < 4)
  380. return "Width and height must both be at least four";
  381. if (params->w > (INT_MAX - 1) / params->h)
  382. return "Width times height must not be unreasonably large";
  383. return NULL;
  384. }
  385. /*
  386. * Scratch space for solver.
  387. */
  388. enum { N, U, L, R, D, MAXDIR }; /* link directions */
  389. #define dx(d) ( ((d)==R) - ((d)==L) )
  390. #define dy(d) ( ((d)==D) - ((d)==U) )
  391. #define F(d) ( U + D - (d) )
  392. struct solver_scratch {
  393. char *links; /* mapping between trees and tents */
  394. int *locs;
  395. char *place, *mrows, *trows;
  396. };
  397. static struct solver_scratch *new_scratch(int w, int h)
  398. {
  399. struct solver_scratch *ret = snew(struct solver_scratch);
  400. ret->links = snewn(w*h, char);
  401. ret->locs = snewn(max(w, h), int);
  402. ret->place = snewn(max(w, h), char);
  403. ret->mrows = snewn(3 * max(w, h), char);
  404. ret->trows = snewn(3 * max(w, h), char);
  405. return ret;
  406. }
  407. static void free_scratch(struct solver_scratch *sc)
  408. {
  409. sfree(sc->trows);
  410. sfree(sc->mrows);
  411. sfree(sc->place);
  412. sfree(sc->locs);
  413. sfree(sc->links);
  414. sfree(sc);
  415. }
  416. /*
  417. * Solver. Returns 0 for impossibility, 1 for success, 2 for
  418. * ambiguity or failure to converge.
  419. */
  420. static int tents_solve(int w, int h, const char *grid, int *numbers,
  421. char *soln, struct solver_scratch *sc, int diff)
  422. {
  423. int x, y, d, i, j;
  424. char *mrow, *trow, *trow1, *trow2;
  425. /*
  426. * Set up solver data.
  427. */
  428. memset(sc->links, N, w*h);
  429. /*
  430. * Set up solution array.
  431. */
  432. memcpy(soln, grid, w*h);
  433. /*
  434. * Main solver loop.
  435. */
  436. while (1) {
  437. bool done_something = false;
  438. /*
  439. * Any tent which has only one unattached tree adjacent to
  440. * it can be tied to that tree.
  441. */
  442. for (y = 0; y < h; y++)
  443. for (x = 0; x < w; x++)
  444. if (soln[y*w+x] == TENT && !sc->links[y*w+x]) {
  445. int linkd = 0;
  446. for (d = 1; d < MAXDIR; d++) {
  447. int x2 = x + dx(d), y2 = y + dy(d);
  448. if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h &&
  449. soln[y2*w+x2] == TREE &&
  450. !sc->links[y2*w+x2]) {
  451. if (linkd)
  452. break; /* found more than one */
  453. else
  454. linkd = d;
  455. }
  456. }
  457. if (d == MAXDIR && linkd == 0) {
  458. #ifdef SOLVER_DIAGNOSTICS
  459. if (verbose)
  460. printf("tent at %d,%d cannot link to anything\n",
  461. x, y);
  462. #endif
  463. return 0; /* no solution exists */
  464. } else if (d == MAXDIR) {
  465. int x2 = x + dx(linkd), y2 = y + dy(linkd);
  466. #ifdef SOLVER_DIAGNOSTICS
  467. if (verbose)
  468. printf("tent at %d,%d can only link to tree at"
  469. " %d,%d\n", x, y, x2, y2);
  470. #endif
  471. sc->links[y*w+x] = linkd;
  472. sc->links[y2*w+x2] = F(linkd);
  473. done_something = true;
  474. }
  475. }
  476. if (done_something)
  477. continue;
  478. if (diff < 0)
  479. break; /* don't do anything else! */
  480. /*
  481. * Mark a blank square as NONTENT if it is not orthogonally
  482. * adjacent to any unmatched tree.
  483. */
  484. for (y = 0; y < h; y++)
  485. for (x = 0; x < w; x++)
  486. if (soln[y*w+x] == BLANK) {
  487. bool can_be_tent = false;
  488. for (d = 1; d < MAXDIR; d++) {
  489. int x2 = x + dx(d), y2 = y + dy(d);
  490. if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h &&
  491. soln[y2*w+x2] == TREE &&
  492. !sc->links[y2*w+x2])
  493. can_be_tent = true;
  494. }
  495. if (!can_be_tent) {
  496. #ifdef SOLVER_DIAGNOSTICS
  497. if (verbose)
  498. printf("%d,%d cannot be a tent (no adjacent"
  499. " unmatched tree)\n", x, y);
  500. #endif
  501. soln[y*w+x] = NONTENT;
  502. done_something = true;
  503. }
  504. }
  505. if (done_something)
  506. continue;
  507. /*
  508. * Mark a blank square as NONTENT if it is (perhaps
  509. * diagonally) adjacent to any other tent.
  510. */
  511. for (y = 0; y < h; y++)
  512. for (x = 0; x < w; x++)
  513. if (soln[y*w+x] == BLANK) {
  514. int dx, dy;
  515. bool imposs = false;
  516. for (dy = -1; dy <= +1; dy++)
  517. for (dx = -1; dx <= +1; dx++)
  518. if (dy || dx) {
  519. int x2 = x + dx, y2 = y + dy;
  520. if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h &&
  521. soln[y2*w+x2] == TENT)
  522. imposs = true;
  523. }
  524. if (imposs) {
  525. #ifdef SOLVER_DIAGNOSTICS
  526. if (verbose)
  527. printf("%d,%d cannot be a tent (adjacent tent)\n",
  528. x, y);
  529. #endif
  530. soln[y*w+x] = NONTENT;
  531. done_something = true;
  532. }
  533. }
  534. if (done_something)
  535. continue;
  536. /*
  537. * Any tree which has exactly one {unattached tent, BLANK}
  538. * adjacent to it must have its tent in that square.
  539. */
  540. for (y = 0; y < h; y++)
  541. for (x = 0; x < w; x++)
  542. if (soln[y*w+x] == TREE && !sc->links[y*w+x]) {
  543. int linkd = 0, linkd2 = 0, nd = 0;
  544. for (d = 1; d < MAXDIR; d++) {
  545. int x2 = x + dx(d), y2 = y + dy(d);
  546. if (!(x2 >= 0 && x2 < w && y2 >= 0 && y2 < h))
  547. continue;
  548. if (soln[y2*w+x2] == BLANK ||
  549. (soln[y2*w+x2] == TENT && !sc->links[y2*w+x2])) {
  550. if (linkd)
  551. linkd2 = d;
  552. else
  553. linkd = d;
  554. nd++;
  555. }
  556. }
  557. if (nd == 0) {
  558. #ifdef SOLVER_DIAGNOSTICS
  559. if (verbose)
  560. printf("tree at %d,%d cannot link to anything\n",
  561. x, y);
  562. #endif
  563. return 0; /* no solution exists */
  564. } else if (nd == 1) {
  565. int x2 = x + dx(linkd), y2 = y + dy(linkd);
  566. #ifdef SOLVER_DIAGNOSTICS
  567. if (verbose)
  568. printf("tree at %d,%d can only link to tent at"
  569. " %d,%d\n", x, y, x2, y2);
  570. #endif
  571. soln[y2*w+x2] = TENT;
  572. sc->links[y*w+x] = linkd;
  573. sc->links[y2*w+x2] = F(linkd);
  574. done_something = true;
  575. } else if (nd == 2 && (!dx(linkd) != !dx(linkd2)) &&
  576. diff >= DIFF_TRICKY) {
  577. /*
  578. * If there are two possible places where
  579. * this tree's tent can go, and they are
  580. * diagonally separated rather than being
  581. * on opposite sides of the tree, then the
  582. * square (other than the tree square)
  583. * which is adjacent to both of them must
  584. * be a non-tent.
  585. */
  586. int x2 = x + dx(linkd) + dx(linkd2);
  587. int y2 = y + dy(linkd) + dy(linkd2);
  588. assert(x2 >= 0 && x2 < w && y2 >= 0 && y2 < h);
  589. if (soln[y2*w+x2] == BLANK) {
  590. #ifdef SOLVER_DIAGNOSTICS
  591. if (verbose)
  592. printf("possible tent locations for tree at"
  593. " %d,%d rule out tent at %d,%d\n",
  594. x, y, x2, y2);
  595. #endif
  596. soln[y2*w+x2] = NONTENT;
  597. done_something = true;
  598. }
  599. }
  600. }
  601. if (done_something)
  602. continue;
  603. /*
  604. * If localised deductions about the trees and tents
  605. * themselves haven't helped us, it's time to resort to the
  606. * numbers round the grid edge. For each row and column, we
  607. * go through all possible combinations of locations for
  608. * the unplaced tents, rule out any which have adjacent
  609. * tents, and spot any square which is given the same state
  610. * by all remaining combinations.
  611. */
  612. for (i = 0; i < w+h; i++) {
  613. int start, step, len, start1, start2, n, k;
  614. if (i < w) {
  615. /*
  616. * This is the number for a column.
  617. */
  618. start = i;
  619. step = w;
  620. len = h;
  621. if (i > 0)
  622. start1 = start - 1;
  623. else
  624. start1 = -1;
  625. if (i+1 < w)
  626. start2 = start + 1;
  627. else
  628. start2 = -1;
  629. } else {
  630. /*
  631. * This is the number for a row.
  632. */
  633. start = (i-w)*w;
  634. step = 1;
  635. len = w;
  636. if (i > w)
  637. start1 = start - w;
  638. else
  639. start1 = -1;
  640. if (i+1 < w+h)
  641. start2 = start + w;
  642. else
  643. start2 = -1;
  644. }
  645. if (diff < DIFF_TRICKY) {
  646. /*
  647. * In Easy mode, we don't look at the effect of one
  648. * row on the next (i.e. ruling out a square if all
  649. * possibilities for an adjacent row place a tent
  650. * next to it).
  651. */
  652. start1 = start2 = -1;
  653. }
  654. k = numbers[i];
  655. /*
  656. * Count and store the locations of the free squares,
  657. * and also count the number of tents already placed.
  658. */
  659. n = 0;
  660. for (j = 0; j < len; j++) {
  661. if (soln[start+j*step] == TENT)
  662. k--; /* one fewer tent to place */
  663. else if (soln[start+j*step] == BLANK)
  664. sc->locs[n++] = j;
  665. }
  666. if (n == 0)
  667. continue; /* nothing left to do here */
  668. /*
  669. * Now we know we're placing k tents in n squares. Set
  670. * up the first possibility.
  671. */
  672. for (j = 0; j < n; j++)
  673. sc->place[j] = (j < k ? TENT : NONTENT);
  674. /*
  675. * We're aiming to find squares in this row which are
  676. * invariant over all valid possibilities. Thus, we
  677. * maintain the current state of that invariance. We
  678. * start everything off at MAGIC to indicate that it
  679. * hasn't been set up yet.
  680. */
  681. mrow = sc->mrows;
  682. trow = sc->trows;
  683. trow1 = sc->trows + len;
  684. trow2 = sc->trows + 2*len;
  685. memset(mrow, MAGIC, 3*len);
  686. /*
  687. * And iterate over all possibilities.
  688. */
  689. while (1) {
  690. int p;
  691. bool valid;
  692. /*
  693. * See if this possibility is valid. The only way
  694. * it can fail to be valid is if it contains two
  695. * adjacent tents. (Other forms of invalidity, such
  696. * as containing a tent adjacent to one already
  697. * placed, will have been dealt with already by
  698. * other parts of the solver.)
  699. */
  700. valid = true;
  701. for (j = 0; j+1 < n; j++)
  702. if (sc->place[j] == TENT &&
  703. sc->place[j+1] == TENT &&
  704. sc->locs[j+1] == sc->locs[j]+1) {
  705. valid = false;
  706. break;
  707. }
  708. if (valid) {
  709. /*
  710. * Merge this valid combination into mrow.
  711. */
  712. memset(trow, MAGIC, len);
  713. memset(trow+len, BLANK, 2*len);
  714. for (j = 0; j < n; j++) {
  715. trow[sc->locs[j]] = sc->place[j];
  716. if (sc->place[j] == TENT) {
  717. int jj;
  718. for (jj = sc->locs[j]-1; jj <= sc->locs[j]+1; jj++)
  719. if (jj >= 0 && jj < len)
  720. trow1[jj] = trow2[jj] = NONTENT;
  721. }
  722. }
  723. for (j = 0; j < 3*len; j++) {
  724. if (trow[j] == MAGIC)
  725. continue;
  726. if (mrow[j] == MAGIC || mrow[j] == trow[j]) {
  727. /*
  728. * Either this is the first valid
  729. * placement we've found at all, or
  730. * this square's contents are
  731. * consistent with every previous valid
  732. * combination.
  733. */
  734. mrow[j] = trow[j];
  735. } else {
  736. /*
  737. * This square's contents fail to match
  738. * what they were in a different
  739. * combination, so we cannot deduce
  740. * anything about this square.
  741. */
  742. mrow[j] = BLANK;
  743. }
  744. }
  745. }
  746. /*
  747. * Find the next combination of k choices from n.
  748. * We do this by finding the rightmost tent which
  749. * can be moved one place right, doing so, and
  750. * shunting all tents to the right of that as far
  751. * left as they can go.
  752. */
  753. p = 0;
  754. for (j = n-1; j > 0; j--) {
  755. if (sc->place[j] == TENT)
  756. p++;
  757. if (sc->place[j] == NONTENT && sc->place[j-1] == TENT) {
  758. sc->place[j-1] = NONTENT;
  759. sc->place[j] = TENT;
  760. while (p--)
  761. sc->place[++j] = TENT;
  762. while (++j < n)
  763. sc->place[j] = NONTENT;
  764. break;
  765. }
  766. }
  767. if (j <= 0)
  768. break; /* we've finished */
  769. }
  770. /*
  771. * It's just possible that _no_ placement was valid, in
  772. * which case we have an internally inconsistent
  773. * puzzle.
  774. */
  775. if (mrow[sc->locs[0]] == MAGIC)
  776. return 0; /* inconsistent */
  777. /*
  778. * Now go through mrow and see if there's anything
  779. * we've deduced which wasn't already mentioned in soln.
  780. */
  781. for (j = 0; j < len; j++) {
  782. int whichrow;
  783. for (whichrow = 0; whichrow < 3; whichrow++) {
  784. char *mthis = mrow + whichrow * len;
  785. int tstart = (whichrow == 0 ? start :
  786. whichrow == 1 ? start1 : start2);
  787. if (tstart >= 0 &&
  788. mthis[j] != MAGIC && mthis[j] != BLANK &&
  789. soln[tstart+j*step] == BLANK) {
  790. int pos = tstart+j*step;
  791. #ifdef SOLVER_DIAGNOSTICS
  792. if (verbose)
  793. printf("%s %d forces %s at %d,%d\n",
  794. step==1 ? "row" : "column",
  795. step==1 ? start/w : start,
  796. mthis[j] == TENT ? "tent" : "non-tent",
  797. pos % w, pos / w);
  798. #endif
  799. soln[pos] = mthis[j];
  800. done_something = true;
  801. }
  802. }
  803. }
  804. }
  805. if (done_something)
  806. continue;
  807. if (!done_something)
  808. break;
  809. }
  810. /*
  811. * The solver has nothing further it can do. Return 1 if both
  812. * soln and sc->links are completely filled in, or 2 otherwise.
  813. */
  814. for (y = 0; y < h; y++)
  815. for (x = 0; x < w; x++) {
  816. if (soln[y*w+x] == BLANK)
  817. return 2;
  818. if (soln[y*w+x] != NONTENT && sc->links[y*w+x] == 0)
  819. return 2;
  820. }
  821. return 1;
  822. }
  823. static char *new_game_desc(const game_params *params_in, random_state *rs,
  824. char **aux, bool interactive)
  825. {
  826. game_params params_copy = *params_in; /* structure copy */
  827. game_params *params = &params_copy;
  828. int w = params->w, h = params->h;
  829. int ntrees = w * h / 5;
  830. char *grid = snewn(w*h, char);
  831. char *puzzle = snewn(w*h, char);
  832. int *numbers = snewn(w+h, int);
  833. char *soln = snewn(w*h, char);
  834. int *order = snewn(w*h, int);
  835. int *treemap = snewn(w*h, int);
  836. int maxedges = ntrees*4 + w*h;
  837. int *adjdata = snewn(maxedges, int);
  838. int **adjlists = snewn(ntrees, int *);
  839. int *adjsizes = snewn(ntrees, int);
  840. int *outr = snewn(4*ntrees, int);
  841. struct solver_scratch *sc = new_scratch(w, h);
  842. char *ret, *p;
  843. int i, j, nl, nr;
  844. int *adjptr;
  845. /*
  846. * Since this puzzle has many global deductions and doesn't
  847. * permit limited clue sets, generating grids for this puzzle
  848. * is hard enough that I see no better option than to simply
  849. * generate a solution and see if it's unique and has the
  850. * required difficulty. This turns out to be computationally
  851. * plausible as well.
  852. *
  853. * We chose our tree count (hence also tent count) by dividing
  854. * the total grid area by five above. Why five? Well, w*h/4 is
  855. * the maximum number of tents you can _possibly_ fit into the
  856. * grid without violating the separation criterion, and to
  857. * achieve that you are constrained to a very small set of
  858. * possible layouts (the obvious one with a tent at every
  859. * (even,even) coordinate, and trivial variations thereon). So
  860. * if we reduce the tent count a bit more, we enable more
  861. * random-looking placement; 5 turns out to be a plausible
  862. * figure which yields sensible puzzles. Increasing the tent
  863. * count would give puzzles whose solutions were too regimented
  864. * and could be solved by the use of that knowledge (and would
  865. * also take longer to find a viable placement); decreasing it
  866. * would make the grids emptier and more boring.
  867. *
  868. * Actually generating a grid is a matter of first placing the
  869. * tents, and then placing the trees by the use of matching.c
  870. * (finding a distinct square adjacent to every tent). We do it
  871. * this way round because otherwise satisfying the tent
  872. * separation condition would become onerous: most randomly
  873. * chosen tent layouts do not satisfy this condition, so we'd
  874. * have gone to a lot of work before finding that a candidate
  875. * layout was unusable. Instead, we place the tents first and
  876. * ensure they meet the separation criterion _before_ doing
  877. * lots of computation; this works much better.
  878. *
  879. * This generation strategy can fail at many points, including
  880. * as early as tent placement (if you get a bad random order in
  881. * which to greedily try the grid squares, you won't even
  882. * manage to find enough mutually non-adjacent squares to put
  883. * the tents in). Then it can fail if matching.c doesn't manage
  884. * to find a good enough matching (i.e. the tent placements don't
  885. * admit any adequate tree placements); and finally it can fail
  886. * if the solver finds that the problem has the wrong
  887. * difficulty (including being actually non-unique). All of
  888. * these, however, are insufficiently frequent to cause
  889. * trouble.
  890. */
  891. if (params->diff > DIFF_EASY && params->w <= 4 && params->h <= 4)
  892. params->diff = DIFF_EASY; /* downgrade to prevent tight loop */
  893. while (1) {
  894. /*
  895. * Make a list of grid squares which we'll permute as we pick
  896. * the tent locations.
  897. *
  898. * We'll also need to index all the potential tree squares,
  899. * i.e. the ones adjacent to the tents.
  900. */
  901. for (i = 0; i < w*h; i++) {
  902. order[i] = i;
  903. treemap[i] = -1;
  904. }
  905. /*
  906. * Place tents at random without making any two adjacent.
  907. */
  908. memset(grid, BLANK, w*h);
  909. j = ntrees;
  910. nr = 0;
  911. /* Loop end condition: either j==0 (we've placed all the
  912. * tents), or the number of grid squares we have yet to try
  913. * is too few to fit the remaining tents into. */
  914. for (i = 0; j > 0 && i+j <= w*h; i++) {
  915. int which, x, y, d, tmp;
  916. int dy, dx;
  917. bool ok = true;
  918. which = i + random_upto(rs, w*h - i);
  919. tmp = order[which];
  920. order[which] = order[i];
  921. order[i] = tmp;
  922. x = order[i] % w;
  923. y = order[i] / w;
  924. for (dy = -1; dy <= +1; dy++)
  925. for (dx = -1; dx <= +1; dx++)
  926. if (x+dx >= 0 && x+dx < w &&
  927. y+dy >= 0 && y+dy < h &&
  928. grid[(y+dy)*w+(x+dx)] == TENT)
  929. ok = false;
  930. if (ok) {
  931. grid[order[i]] = TENT;
  932. for (d = 1; d < MAXDIR; d++) {
  933. int x2 = x + dx(d), y2 = y + dy(d);
  934. if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h &&
  935. treemap[y2*w+x2] == -1) {
  936. treemap[y2*w+x2] = nr++;
  937. }
  938. }
  939. j--;
  940. }
  941. }
  942. if (j > 0)
  943. continue; /* couldn't place all the tents */
  944. /*
  945. * Build up the graph for matching.c.
  946. */
  947. adjptr = adjdata;
  948. nl = 0;
  949. for (i = 0; i < w*h; i++) {
  950. if (grid[i] == TENT) {
  951. int d, x = i % w, y = i / w;
  952. adjlists[nl] = adjptr;
  953. for (d = 1; d < MAXDIR; d++) {
  954. int x2 = x + dx(d), y2 = y + dy(d);
  955. if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h) {
  956. assert(treemap[y2*w+x2] != -1);
  957. *adjptr++ = treemap[y2*w+x2];
  958. }
  959. }
  960. adjsizes[nl] = adjptr - adjlists[nl];
  961. nl++;
  962. }
  963. }
  964. /*
  965. * Call the matching algorithm to actually place the trees.
  966. */
  967. j = matching(ntrees, nr, adjlists, adjsizes, rs, NULL, outr);
  968. if (j < ntrees)
  969. continue; /* couldn't place all the trees */
  970. /*
  971. * Fill in the trees in the grid, by cross-referencing treemap
  972. * (which maps a grid square to its index as known to
  973. * matching()) against the output from matching().
  974. *
  975. * Note that for these purposes we don't actually care _which_
  976. * tent each potential tree square is assigned to - we only
  977. * care whether it was assigned to any tent at all, in order
  978. * to decide whether to put a tree in it.
  979. */
  980. for (i = 0; i < w*h; i++)
  981. if (treemap[i] != -1 && outr[treemap[i]] != -1)
  982. grid[i] = TREE;
  983. /*
  984. * I think it looks ugly if there isn't at least one of
  985. * _something_ (tent or tree) in each row and each column
  986. * of the grid. This doesn't give any information away
  987. * since a completely empty row/column is instantly obvious
  988. * from the clues (it has no trees and a zero).
  989. */
  990. for (i = 0; i < w; i++) {
  991. for (j = 0; j < h; j++) {
  992. if (grid[j*w+i] != BLANK)
  993. break; /* found something in this column */
  994. }
  995. if (j == h)
  996. break; /* found empty column */
  997. }
  998. if (i < w)
  999. continue; /* a column was empty */
  1000. for (j = 0; j < h; j++) {
  1001. for (i = 0; i < w; i++) {
  1002. if (grid[j*w+i] != BLANK)
  1003. break; /* found something in this row */
  1004. }
  1005. if (i == w)
  1006. break; /* found empty row */
  1007. }
  1008. if (j < h)
  1009. continue; /* a row was empty */
  1010. /*
  1011. * Now set up the numbers round the edge.
  1012. */
  1013. for (i = 0; i < w; i++) {
  1014. int n = 0;
  1015. for (j = 0; j < h; j++)
  1016. if (grid[j*w+i] == TENT)
  1017. n++;
  1018. numbers[i] = n;
  1019. }
  1020. for (i = 0; i < h; i++) {
  1021. int n = 0;
  1022. for (j = 0; j < w; j++)
  1023. if (grid[i*w+j] == TENT)
  1024. n++;
  1025. numbers[w+i] = n;
  1026. }
  1027. /*
  1028. * And now actually solve the puzzle, to see whether it's
  1029. * unique and has the required difficulty.
  1030. */
  1031. for (i = 0; i < w*h; i++)
  1032. puzzle[i] = grid[i] == TREE ? TREE : BLANK;
  1033. i = tents_solve(w, h, puzzle, numbers, soln, sc, params->diff-1);
  1034. j = tents_solve(w, h, puzzle, numbers, soln, sc, params->diff);
  1035. /*
  1036. * We expect solving with difficulty params->diff to have
  1037. * succeeded (otherwise the problem is too hard), and
  1038. * solving with diff-1 to have failed (otherwise it's too
  1039. * easy).
  1040. */
  1041. if (i == 2 && j == 1)
  1042. break;
  1043. }
  1044. /*
  1045. * That's it. Encode as a game ID.
  1046. */
  1047. ret = snewn((w+h)*40 + ntrees + (w*h)/26 + 1, char);
  1048. p = ret;
  1049. j = 0;
  1050. for (i = 0; i <= w*h; i++) {
  1051. bool c = (i < w*h ? grid[i] == TREE : true);
  1052. if (c) {
  1053. *p++ = (j == 0 ? '_' : j-1 + 'a');
  1054. j = 0;
  1055. } else {
  1056. j++;
  1057. while (j > 25) {
  1058. *p++ = 'z';
  1059. j -= 25;
  1060. }
  1061. }
  1062. }
  1063. for (i = 0; i < w+h; i++)
  1064. p += sprintf(p, ",%d", numbers[i]);
  1065. *p++ = '\0';
  1066. ret = sresize(ret, p - ret, char);
  1067. /*
  1068. * And encode the solution as an aux_info.
  1069. */
  1070. *aux = snewn(ntrees * 40, char);
  1071. p = *aux;
  1072. *p++ = 'S';
  1073. for (i = 0; i < w*h; i++)
  1074. if (grid[i] == TENT)
  1075. p += sprintf(p, ";T%d,%d", i%w, i/w);
  1076. *p++ = '\0';
  1077. *aux = sresize(*aux, p - *aux, char);
  1078. free_scratch(sc);
  1079. sfree(outr);
  1080. sfree(adjdata);
  1081. sfree(adjlists);
  1082. sfree(adjsizes);
  1083. sfree(treemap);
  1084. sfree(order);
  1085. sfree(soln);
  1086. sfree(numbers);
  1087. sfree(puzzle);
  1088. sfree(grid);
  1089. return ret;
  1090. }
  1091. /*
  1092. * Grid description format:
  1093. *
  1094. * _ = tree
  1095. * a = 1 BLANK then TREE
  1096. * ...
  1097. * y = 25 BLANKs then TREE
  1098. * z = 25 BLANKs
  1099. * ! = set previous square to TENT
  1100. * - = set previous square to NONTENT
  1101. *
  1102. * Last character must be one that would insert a tree as the first
  1103. * square after the grid.
  1104. */
  1105. static const char *validate_desc(const game_params *params, const char *desc)
  1106. {
  1107. int w = params->w, h = params->h;
  1108. int area, i;
  1109. area = 0;
  1110. while (*desc && *desc != ',') {
  1111. if (*desc == '_')
  1112. area++;
  1113. else if (*desc >= 'a' && *desc < 'z')
  1114. area += *desc - 'a' + 2;
  1115. else if (*desc == 'z')
  1116. area += 25;
  1117. else if (*desc == '!' || *desc == '-') {
  1118. if (area == 0 || area > w * h)
  1119. return "Tent or non-tent placed off the grid";
  1120. } else
  1121. return "Invalid character in grid specification";
  1122. desc++;
  1123. }
  1124. if (area < w * h + 1)
  1125. return "Not enough data to fill grid";
  1126. else if (area > w * h + 1)
  1127. return "Too much data to fill grid";
  1128. for (i = 0; i < w+h; i++) {
  1129. if (!*desc)
  1130. return "Not enough numbers given after grid specification";
  1131. else if (*desc != ',')
  1132. return "Invalid character in number list";
  1133. desc++;
  1134. while (*desc && isdigit((unsigned char)*desc)) desc++;
  1135. }
  1136. if (*desc)
  1137. return "Unexpected additional data at end of game description";
  1138. return NULL;
  1139. }
  1140. static game_state *new_game(midend *me, const game_params *params,
  1141. const char *desc)
  1142. {
  1143. int w = params->w, h = params->h;
  1144. game_state *state = snew(game_state);
  1145. int i;
  1146. state->p = *params; /* structure copy */
  1147. state->grid = snewn(w*h, char);
  1148. state->numbers = snew(struct numbers);
  1149. state->numbers->refcount = 1;
  1150. state->numbers->numbers = snewn(w+h, int);
  1151. state->completed = state->used_solve = false;
  1152. i = 0;
  1153. memset(state->grid, BLANK, w*h);
  1154. while (*desc) {
  1155. int run, type;
  1156. type = TREE;
  1157. if (*desc == '_')
  1158. run = 0;
  1159. else if (*desc >= 'a' && *desc < 'z')
  1160. run = *desc - ('a'-1);
  1161. else if (*desc == 'z') {
  1162. run = 25;
  1163. type = BLANK;
  1164. } else {
  1165. assert(*desc == '!' || *desc == '-');
  1166. run = -1;
  1167. type = (*desc == '!' ? TENT : NONTENT);
  1168. }
  1169. desc++;
  1170. i += run;
  1171. assert(i >= 0 && i <= w*h);
  1172. if (i == w*h) {
  1173. assert(type == TREE);
  1174. break;
  1175. } else {
  1176. if (type != BLANK)
  1177. state->grid[i++] = type;
  1178. }
  1179. }
  1180. for (i = 0; i < w+h; i++) {
  1181. assert(*desc == ',');
  1182. desc++;
  1183. state->numbers->numbers[i] = atoi(desc);
  1184. while (*desc && isdigit((unsigned char)*desc)) desc++;
  1185. }
  1186. assert(!*desc);
  1187. return state;
  1188. }
  1189. static game_state *dup_game(const game_state *state)
  1190. {
  1191. int w = state->p.w, h = state->p.h;
  1192. game_state *ret = snew(game_state);
  1193. ret->p = state->p; /* structure copy */
  1194. ret->grid = snewn(w*h, char);
  1195. memcpy(ret->grid, state->grid, w*h);
  1196. ret->numbers = state->numbers;
  1197. state->numbers->refcount++;
  1198. ret->completed = state->completed;
  1199. ret->used_solve = state->used_solve;
  1200. return ret;
  1201. }
  1202. static void free_game(game_state *state)
  1203. {
  1204. if (--state->numbers->refcount <= 0) {
  1205. sfree(state->numbers->numbers);
  1206. sfree(state->numbers);
  1207. }
  1208. sfree(state->grid);
  1209. sfree(state);
  1210. }
  1211. static char *solve_game(const game_state *state, const game_state *currstate,
  1212. const char *aux, const char **error)
  1213. {
  1214. int w = state->p.w, h = state->p.h;
  1215. if (aux) {
  1216. /*
  1217. * If we already have the solution, save ourselves some
  1218. * time.
  1219. */
  1220. return dupstr(aux);
  1221. } else {
  1222. struct solver_scratch *sc = new_scratch(w, h);
  1223. char *soln;
  1224. int ret;
  1225. char *move, *p;
  1226. int i;
  1227. soln = snewn(w*h, char);
  1228. ret = tents_solve(w, h, state->grid, state->numbers->numbers,
  1229. soln, sc, DIFFCOUNT-1);
  1230. free_scratch(sc);
  1231. if (ret != 1) {
  1232. sfree(soln);
  1233. if (ret == 0)
  1234. *error = "This puzzle is not self-consistent";
  1235. else
  1236. *error = "Unable to find a unique solution for this puzzle";
  1237. return NULL;
  1238. }
  1239. /*
  1240. * Construct a move string which turns the current state
  1241. * into the solved state.
  1242. */
  1243. move = snewn(w*h * 40, char);
  1244. p = move;
  1245. *p++ = 'S';
  1246. for (i = 0; i < w*h; i++)
  1247. if (soln[i] == TENT)
  1248. p += sprintf(p, ";T%d,%d", i%w, i/w);
  1249. *p++ = '\0';
  1250. move = sresize(move, p - move, char);
  1251. sfree(soln);
  1252. return move;
  1253. }
  1254. }
  1255. static bool game_can_format_as_text_now(const game_params *params)
  1256. {
  1257. return params->w <= 1998 && params->h <= 1998; /* 999 tents */
  1258. }
  1259. static char *game_text_format(const game_state *state)
  1260. {
  1261. int w = state->p.w, h = state->p.h, r, c;
  1262. int cw = 4, ch = 2, gw = (w+1)*cw + 2, gh = (h+1)*ch + 1, len = gw * gh;
  1263. char *board = snewn(len + 1, char);
  1264. sprintf(board, "%*s\n", len - 2, "");
  1265. for (r = 0; r <= h; ++r) {
  1266. for (c = 0; c <= w; ++c) {
  1267. int cell = r*ch*gw + cw*c, center = cell + gw*ch/2 + cw/2;
  1268. int i = r*w + c, n = 1000;
  1269. if (r == h && c == w) /* NOP */;
  1270. else if (c == w) n = state->numbers->numbers[w + r];
  1271. else if (r == h) n = state->numbers->numbers[c];
  1272. else switch (state->grid[i]) {
  1273. case BLANK: board[center] = '.'; break;
  1274. case TREE: board[center] = 'T'; break;
  1275. case TENT: memcpy(board + center - 1, "//\\", 3); break;
  1276. case NONTENT: break;
  1277. default: memcpy(board + center - 1, "wtf", 3);
  1278. }
  1279. if (n < 100) {
  1280. board[center] = '0' + n % 10;
  1281. if (n >= 10) board[center - 1] = '0' + n / 10;
  1282. } else if (n < 1000) {
  1283. board[center + 1] = '0' + n % 10;
  1284. board[center] = '0' + n / 10 % 10;
  1285. board[center - 1] = '0' + n / 100;
  1286. }
  1287. board[cell] = '+';
  1288. memset(board + cell + 1, '-', cw - 1);
  1289. for (i = 1; i < ch; ++i) board[cell + i*gw] = '|';
  1290. }
  1291. for (c = 0; c < ch; ++c) {
  1292. board[(r*ch+c)*gw + gw - 2] =
  1293. c == 0 ? '+' : r < h ? '|' : ' ';
  1294. board[(r*ch+c)*gw + gw - 1] = '\n';
  1295. }
  1296. }
  1297. memset(board + len - gw, '-', gw - 2 - cw);
  1298. for (c = 0; c <= w; ++c) board[len - gw + cw*c] = '+';
  1299. return board;
  1300. }
  1301. struct game_ui {
  1302. int dsx, dsy; /* coords of drag start */
  1303. int dex, dey; /* coords of drag end */
  1304. int drag_button; /* -1 for none, or a button code */
  1305. bool drag_ok; /* dragged off the window, to cancel */
  1306. int cx, cy; /* cursor position. */
  1307. bool cdisp; /* is cursor displayed? */
  1308. };
  1309. static game_ui *new_ui(const game_state *state)
  1310. {
  1311. game_ui *ui = snew(game_ui);
  1312. ui->dsx = ui->dsy = -1;
  1313. ui->dex = ui->dey = -1;
  1314. ui->drag_button = -1;
  1315. ui->drag_ok = false;
  1316. ui->cx = ui->cy = 0;
  1317. ui->cdisp = getenv_bool("PUZZLES_SHOW_CURSOR", false);
  1318. return ui;
  1319. }
  1320. static void free_ui(game_ui *ui)
  1321. {
  1322. sfree(ui);
  1323. }
  1324. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  1325. const game_state *newstate)
  1326. {
  1327. }
  1328. static const char *current_key_label(const game_ui *ui,
  1329. const game_state *state, int button)
  1330. {
  1331. int w = state->p.w;
  1332. int v = state->grid[ui->cy*w+ui->cx];
  1333. if (IS_CURSOR_SELECT(button) && ui->cdisp) {
  1334. switch (v) {
  1335. case BLANK:
  1336. return button == CURSOR_SELECT ? "Tent" : "Green";
  1337. case TENT: case NONTENT: return "Clear";
  1338. }
  1339. }
  1340. return "";
  1341. }
  1342. struct game_drawstate {
  1343. int tilesize;
  1344. bool started;
  1345. game_params p;
  1346. int *drawn, *numbersdrawn;
  1347. int cx, cy; /* last-drawn cursor pos, or (-1,-1) if absent. */
  1348. };
  1349. #define PREFERRED_TILESIZE 32
  1350. #define TILESIZE (ds->tilesize)
  1351. #define TLBORDER (TILESIZE/2)
  1352. #define BRBORDER (TILESIZE*3/2)
  1353. #define COORD(x) ( (x) * TILESIZE + TLBORDER )
  1354. #define FROMCOORD(x) ( ((x) - TLBORDER + TILESIZE) / TILESIZE - 1 )
  1355. #define FLASH_TIME 0.30F
  1356. static int drag_xform(const game_ui *ui, int x, int y, int v)
  1357. {
  1358. int xmin, ymin, xmax, ymax;
  1359. xmin = min(ui->dsx, ui->dex);
  1360. xmax = max(ui->dsx, ui->dex);
  1361. ymin = min(ui->dsy, ui->dey);
  1362. ymax = max(ui->dsy, ui->dey);
  1363. #ifndef STYLUS_BASED
  1364. /*
  1365. * Left-dragging has no effect, so we treat a left-drag as a
  1366. * single click on dsx,dsy.
  1367. */
  1368. if (ui->drag_button == LEFT_BUTTON) {
  1369. xmin = xmax = ui->dsx;
  1370. ymin = ymax = ui->dsy;
  1371. }
  1372. #endif
  1373. if (x < xmin || x > xmax || y < ymin || y > ymax)
  1374. return v; /* no change outside drag area */
  1375. if (v == TREE)
  1376. return v; /* trees are inviolate always */
  1377. if (xmin == xmax && ymin == ymax) {
  1378. /*
  1379. * Results of a simple click. Left button sets blanks to
  1380. * tents; right button sets blanks to non-tents; either
  1381. * button clears a non-blank square.
  1382. * If stylus-based however, it loops instead.
  1383. */
  1384. if (ui->drag_button == LEFT_BUTTON)
  1385. #ifdef STYLUS_BASED
  1386. v = (v == BLANK ? TENT : (v == TENT ? NONTENT : BLANK));
  1387. else
  1388. v = (v == BLANK ? NONTENT : (v == NONTENT ? TENT : BLANK));
  1389. #else
  1390. v = (v == BLANK ? TENT : BLANK);
  1391. else
  1392. v = (v == BLANK ? NONTENT : BLANK);
  1393. #endif
  1394. } else {
  1395. /*
  1396. * Results of a drag. Left-dragging has no effect.
  1397. * Right-dragging sets all blank squares to non-tents and
  1398. * has no effect on anything else.
  1399. */
  1400. if (ui->drag_button == RIGHT_BUTTON)
  1401. v = (v == BLANK ? NONTENT : v);
  1402. else
  1403. #ifdef STYLUS_BASED
  1404. v = (v == BLANK ? NONTENT : v);
  1405. #else
  1406. /* do nothing */;
  1407. #endif
  1408. }
  1409. return v;
  1410. }
  1411. static char *interpret_move(const game_state *state, game_ui *ui,
  1412. const game_drawstate *ds,
  1413. int x, int y, int button)
  1414. {
  1415. int w = state->p.w, h = state->p.h;
  1416. char tmpbuf[80];
  1417. bool shift = button & MOD_SHFT, control = button & MOD_CTRL;
  1418. button &= ~MOD_MASK;
  1419. if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
  1420. x = FROMCOORD(x);
  1421. y = FROMCOORD(y);
  1422. if (x < 0 || y < 0 || x >= w || y >= h)
  1423. return NULL;
  1424. ui->drag_button = button;
  1425. ui->dsx = ui->dex = x;
  1426. ui->dsy = ui->dey = y;
  1427. ui->drag_ok = true;
  1428. ui->cdisp = false;
  1429. return MOVE_UI_UPDATE;
  1430. }
  1431. if ((IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) &&
  1432. ui->drag_button > 0) {
  1433. int xmin, ymin, xmax, ymax;
  1434. char *buf;
  1435. const char *sep;
  1436. int buflen, bufsize, tmplen;
  1437. x = FROMCOORD(x);
  1438. y = FROMCOORD(y);
  1439. if (x < 0 || y < 0 || x >= w || y >= h) {
  1440. ui->drag_ok = false;
  1441. } else {
  1442. /*
  1443. * Drags are limited to one row or column. Hence, we
  1444. * work out which coordinate is closer to the drag
  1445. * start, and move it _to_ the drag start.
  1446. */
  1447. if (abs(x - ui->dsx) < abs(y - ui->dsy))
  1448. x = ui->dsx;
  1449. else
  1450. y = ui->dsy;
  1451. ui->dex = x;
  1452. ui->dey = y;
  1453. ui->drag_ok = true;
  1454. }
  1455. if (IS_MOUSE_DRAG(button))
  1456. return MOVE_UI_UPDATE;
  1457. /*
  1458. * The drag has been released. Enact it.
  1459. */
  1460. if (!ui->drag_ok) {
  1461. ui->drag_button = -1;
  1462. return MOVE_UI_UPDATE; /* drag was just cancelled */
  1463. }
  1464. xmin = min(ui->dsx, ui->dex);
  1465. xmax = max(ui->dsx, ui->dex);
  1466. ymin = min(ui->dsy, ui->dey);
  1467. ymax = max(ui->dsy, ui->dey);
  1468. assert(0 <= xmin && xmin <= xmax && xmax < w);
  1469. assert(0 <= ymin && ymin <= ymax && ymax < h);
  1470. buflen = 0;
  1471. bufsize = 256;
  1472. buf = snewn(bufsize, char);
  1473. sep = "";
  1474. for (y = ymin; y <= ymax; y++)
  1475. for (x = xmin; x <= xmax; x++) {
  1476. int v = drag_xform(ui, x, y, state->grid[y*w+x]);
  1477. if (state->grid[y*w+x] != v) {
  1478. tmplen = sprintf(tmpbuf, "%s%c%d,%d", sep,
  1479. (int)(v == BLANK ? 'B' :
  1480. v == TENT ? 'T' : 'N'),
  1481. x, y);
  1482. sep = ";";
  1483. if (buflen + tmplen >= bufsize) {
  1484. bufsize = buflen + tmplen + 256;
  1485. buf = sresize(buf, bufsize, char);
  1486. }
  1487. strcpy(buf+buflen, tmpbuf);
  1488. buflen += tmplen;
  1489. }
  1490. }
  1491. ui->drag_button = -1; /* drag is terminated */
  1492. if (buflen == 0) {
  1493. sfree(buf);
  1494. return MOVE_UI_UPDATE; /* drag was terminated */
  1495. } else {
  1496. buf[buflen] = '\0';
  1497. return buf;
  1498. }
  1499. }
  1500. if (IS_CURSOR_MOVE(button)) {
  1501. ui->cdisp = true;
  1502. if (shift || control) {
  1503. int len = 0, i, indices[2];
  1504. indices[0] = ui->cx + w * ui->cy;
  1505. move_cursor(button, &ui->cx, &ui->cy, w, h, false);
  1506. indices[1] = ui->cx + w * ui->cy;
  1507. /* NONTENTify all unique traversed eligible squares */
  1508. for (i = 0; i <= (indices[0] != indices[1]); ++i)
  1509. if (state->grid[indices[i]] == BLANK ||
  1510. (control && state->grid[indices[i]] == TENT)) {
  1511. len += sprintf(tmpbuf + len, "%sN%d,%d", len ? ";" : "",
  1512. indices[i] % w, indices[i] / w);
  1513. assert(len < lenof(tmpbuf));
  1514. }
  1515. tmpbuf[len] = '\0';
  1516. if (len) return dupstr(tmpbuf);
  1517. } else
  1518. move_cursor(button, &ui->cx, &ui->cy, w, h, false);
  1519. return MOVE_UI_UPDATE;
  1520. }
  1521. if (ui->cdisp) {
  1522. char rep = 0;
  1523. int v = state->grid[ui->cy*w+ui->cx];
  1524. if (v != TREE) {
  1525. #ifdef SINGLE_CURSOR_SELECT
  1526. if (button == CURSOR_SELECT)
  1527. /* SELECT cycles T, N, B */
  1528. rep = v == BLANK ? 'T' : v == TENT ? 'N' : 'B';
  1529. #else
  1530. if (button == CURSOR_SELECT)
  1531. rep = v == BLANK ? 'T' : 'B';
  1532. else if (button == CURSOR_SELECT2)
  1533. rep = v == BLANK ? 'N' : 'B';
  1534. else if (button == 'T' || button == 'N' || button == 'B')
  1535. rep = (char)button;
  1536. #endif
  1537. }
  1538. if (rep) {
  1539. sprintf(tmpbuf, "%c%d,%d", (int)rep, ui->cx, ui->cy);
  1540. return dupstr(tmpbuf);
  1541. }
  1542. } else if (IS_CURSOR_SELECT(button)) {
  1543. ui->cdisp = true;
  1544. return MOVE_UI_UPDATE;
  1545. }
  1546. return NULL;
  1547. }
  1548. static game_state *execute_move(const game_state *state, const char *move)
  1549. {
  1550. int w = state->p.w, h = state->p.h;
  1551. char c;
  1552. int x, y, m, n, i, j;
  1553. game_state *ret = dup_game(state);
  1554. while (*move) {
  1555. c = *move;
  1556. if (c == 'S') {
  1557. int i;
  1558. ret->used_solve = true;
  1559. /*
  1560. * Set all non-tree squares to NONTENT. The rest of the
  1561. * solve move will fill the tents in over the top.
  1562. */
  1563. for (i = 0; i < w*h; i++)
  1564. if (ret->grid[i] != TREE)
  1565. ret->grid[i] = NONTENT;
  1566. move++;
  1567. } else if (c == 'B' || c == 'T' || c == 'N') {
  1568. move++;
  1569. if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
  1570. x < 0 || y < 0 || x >= w || y >= h) {
  1571. free_game(ret);
  1572. return NULL;
  1573. }
  1574. if (ret->grid[y*w+x] == TREE) {
  1575. free_game(ret);
  1576. return NULL;
  1577. }
  1578. ret->grid[y*w+x] = (c == 'B' ? BLANK : c == 'T' ? TENT : NONTENT);
  1579. move += n;
  1580. } else {
  1581. free_game(ret);
  1582. return NULL;
  1583. }
  1584. if (*move == ';')
  1585. move++;
  1586. else if (*move) {
  1587. free_game(ret);
  1588. return NULL;
  1589. }
  1590. }
  1591. /*
  1592. * Check for completion.
  1593. */
  1594. for (i = n = m = 0; i < w*h; i++) {
  1595. if (ret->grid[i] == TENT)
  1596. n++;
  1597. else if (ret->grid[i] == TREE)
  1598. m++;
  1599. }
  1600. if (n == m) {
  1601. int *gridids, *adjdata, **adjlists, *adjsizes, *adjptr;
  1602. /*
  1603. * We have the right number of tents, which is a
  1604. * precondition for the game being complete. Now check that
  1605. * the numbers add up.
  1606. */
  1607. for (i = 0; i < w; i++) {
  1608. n = 0;
  1609. for (j = 0; j < h; j++)
  1610. if (ret->grid[j*w+i] == TENT)
  1611. n++;
  1612. if (ret->numbers->numbers[i] != n)
  1613. goto completion_check_done;
  1614. }
  1615. for (i = 0; i < h; i++) {
  1616. n = 0;
  1617. for (j = 0; j < w; j++)
  1618. if (ret->grid[i*w+j] == TENT)
  1619. n++;
  1620. if (ret->numbers->numbers[w+i] != n)
  1621. goto completion_check_done;
  1622. }
  1623. /*
  1624. * Also, check that no two tents are adjacent.
  1625. */
  1626. for (y = 0; y < h; y++)
  1627. for (x = 0; x < w; x++) {
  1628. if (x+1 < w &&
  1629. ret->grid[y*w+x] == TENT && ret->grid[y*w+x+1] == TENT)
  1630. goto completion_check_done;
  1631. if (y+1 < h &&
  1632. ret->grid[y*w+x] == TENT && ret->grid[(y+1)*w+x] == TENT)
  1633. goto completion_check_done;
  1634. if (x+1 < w && y+1 < h) {
  1635. if (ret->grid[y*w+x] == TENT &&
  1636. ret->grid[(y+1)*w+(x+1)] == TENT)
  1637. goto completion_check_done;
  1638. if (ret->grid[(y+1)*w+x] == TENT &&
  1639. ret->grid[y*w+(x+1)] == TENT)
  1640. goto completion_check_done;
  1641. }
  1642. }
  1643. /*
  1644. * OK; we have the right number of tents, they match the
  1645. * numeric clues, and they satisfy the non-adjacency
  1646. * criterion. Finally, we need to verify that they can be
  1647. * placed in a one-to-one matching with the trees such that
  1648. * every tent is orthogonally adjacent to its tree.
  1649. *
  1650. * This bit is where the hard work comes in: we have to do
  1651. * it by finding such a matching using matching.c.
  1652. */
  1653. gridids = snewn(w*h, int);
  1654. adjdata = snewn(m*4, int);
  1655. adjlists = snewn(m, int *);
  1656. adjsizes = snewn(m, int);
  1657. /* Assign each tent and tree a consecutive vertex id for
  1658. * matching(). */
  1659. for (i = n = 0; i < w*h; i++) {
  1660. if (ret->grid[i] == TENT)
  1661. gridids[i] = n++;
  1662. }
  1663. assert(n == m);
  1664. for (i = n = 0; i < w*h; i++) {
  1665. if (ret->grid[i] == TREE)
  1666. gridids[i] = n++;
  1667. }
  1668. assert(n == m);
  1669. /* Build the vertices' adjacency lists. */
  1670. adjptr = adjdata;
  1671. for (y = 0; y < h; y++)
  1672. for (x = 0; x < w; x++)
  1673. if (ret->grid[y*w+x] == TREE) {
  1674. int d, treeid = gridids[y*w+x];
  1675. adjlists[treeid] = adjptr;
  1676. /*
  1677. * Here we use the direction enum declared for
  1678. * the solver. We make use of the fact that the
  1679. * directions are declared in the order
  1680. * U,L,R,D, meaning that we go through the four
  1681. * neighbours of any square in numerically
  1682. * increasing order.
  1683. */
  1684. for (d = 1; d < MAXDIR; d++) {
  1685. int x2 = x + dx(d), y2 = y + dy(d);
  1686. if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h &&
  1687. ret->grid[y2*w+x2] == TENT) {
  1688. *adjptr++ = gridids[y2*w+x2];
  1689. }
  1690. }
  1691. adjsizes[treeid] = adjptr - adjlists[treeid];
  1692. }
  1693. n = matching(m, m, adjlists, adjsizes, NULL, NULL, NULL);
  1694. sfree(gridids);
  1695. sfree(adjdata);
  1696. sfree(adjlists);
  1697. sfree(adjsizes);
  1698. if (n != m)
  1699. goto completion_check_done;
  1700. /*
  1701. * We haven't managed to fault the grid on any count. Score!
  1702. */
  1703. ret->completed = true;
  1704. }
  1705. completion_check_done:
  1706. return ret;
  1707. }
  1708. /* ----------------------------------------------------------------------
  1709. * Drawing routines.
  1710. */
  1711. static void game_compute_size(const game_params *params, int tilesize,
  1712. const game_ui *ui, int *x, int *y)
  1713. {
  1714. /* fool the macros */
  1715. struct dummy { int tilesize; } dummy, *ds = &dummy;
  1716. dummy.tilesize = tilesize;
  1717. *x = TLBORDER + BRBORDER + TILESIZE * params->w;
  1718. *y = TLBORDER + BRBORDER + TILESIZE * params->h;
  1719. }
  1720. static void game_set_size(drawing *dr, game_drawstate *ds,
  1721. const game_params *params, int tilesize)
  1722. {
  1723. ds->tilesize = tilesize;
  1724. }
  1725. static float *game_colours(frontend *fe, int *ncolours)
  1726. {
  1727. float *ret = snewn(3 * NCOLOURS, float);
  1728. frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
  1729. ret[COL_GRID * 3 + 0] = 0.0F;
  1730. ret[COL_GRID * 3 + 1] = 0.0F;
  1731. ret[COL_GRID * 3 + 2] = 0.0F;
  1732. ret[COL_GRASS * 3 + 0] = 0.7F;
  1733. ret[COL_GRASS * 3 + 1] = 1.0F;
  1734. ret[COL_GRASS * 3 + 2] = 0.5F;
  1735. ret[COL_TREETRUNK * 3 + 0] = 0.6F;
  1736. ret[COL_TREETRUNK * 3 + 1] = 0.4F;
  1737. ret[COL_TREETRUNK * 3 + 2] = 0.0F;
  1738. ret[COL_TREELEAF * 3 + 0] = 0.0F;
  1739. ret[COL_TREELEAF * 3 + 1] = 0.7F;
  1740. ret[COL_TREELEAF * 3 + 2] = 0.0F;
  1741. ret[COL_TENT * 3 + 0] = 0.8F;
  1742. ret[COL_TENT * 3 + 1] = 0.7F;
  1743. ret[COL_TENT * 3 + 2] = 0.0F;
  1744. ret[COL_ERROR * 3 + 0] = 1.0F;
  1745. ret[COL_ERROR * 3 + 1] = 0.0F;
  1746. ret[COL_ERROR * 3 + 2] = 0.0F;
  1747. ret[COL_ERRTEXT * 3 + 0] = 1.0F;
  1748. ret[COL_ERRTEXT * 3 + 1] = 1.0F;
  1749. ret[COL_ERRTEXT * 3 + 2] = 1.0F;
  1750. ret[COL_ERRTRUNK * 3 + 0] = 0.6F;
  1751. ret[COL_ERRTRUNK * 3 + 1] = 0.0F;
  1752. ret[COL_ERRTRUNK * 3 + 2] = 0.0F;
  1753. *ncolours = NCOLOURS;
  1754. return ret;
  1755. }
  1756. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  1757. {
  1758. int w = state->p.w, h = state->p.h;
  1759. struct game_drawstate *ds = snew(struct game_drawstate);
  1760. int i;
  1761. ds->tilesize = 0;
  1762. ds->started = false;
  1763. ds->p = state->p; /* structure copy */
  1764. ds->drawn = snewn(w*h, int);
  1765. for (i = 0; i < w*h; i++)
  1766. ds->drawn[i] = MAGIC;
  1767. ds->numbersdrawn = snewn(w+h, int);
  1768. for (i = 0; i < w+h; i++)
  1769. ds->numbersdrawn[i] = 2;
  1770. ds->cx = ds->cy = -1;
  1771. return ds;
  1772. }
  1773. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  1774. {
  1775. sfree(ds->drawn);
  1776. sfree(ds->numbersdrawn);
  1777. sfree(ds);
  1778. }
  1779. enum {
  1780. ERR_ADJ_TOPLEFT = 4,
  1781. ERR_ADJ_TOP,
  1782. ERR_ADJ_TOPRIGHT,
  1783. ERR_ADJ_LEFT,
  1784. ERR_ADJ_RIGHT,
  1785. ERR_ADJ_BOTLEFT,
  1786. ERR_ADJ_BOT,
  1787. ERR_ADJ_BOTRIGHT,
  1788. ERR_OVERCOMMITTED
  1789. };
  1790. static int *find_errors(const game_state *state, char *grid)
  1791. {
  1792. int w = state->p.w, h = state->p.h;
  1793. int *ret = snewn(w*h + w + h, int);
  1794. int *tmp = snewn(w*h, int);
  1795. DSF *dsf;
  1796. int x, y;
  1797. /*
  1798. * This function goes through a grid and works out where to
  1799. * highlight play errors in red. The aim is that it should
  1800. * produce at least one error highlight for any complete grid
  1801. * (or complete piece of grid) violating a puzzle constraint, so
  1802. * that a grid containing no BLANK squares is either a win or is
  1803. * marked up in some way that indicates why not.
  1804. *
  1805. * So it's easy enough to highlight errors in the numeric clues
  1806. * - just light up any row or column number which is not
  1807. * fulfilled - and it's just as easy to highlight adjacent
  1808. * tents. The difficult bit is highlighting failures in the
  1809. * tent/tree matching criterion.
  1810. *
  1811. * A natural approach would seem to be to apply the matching.c
  1812. * algorithm to find the tent/tree matching; if this fails, it
  1813. * could be made to produce as a by-product some set of trees
  1814. * which have too few tents between them (or vice versa). However,
  1815. * it's bad for localising errors, because it's not easy to make
  1816. * the algorithm narrow down to the _smallest_ such set of trees:
  1817. * if trees A and B have only one tent between them, for instance,
  1818. * it might perfectly well highlight not only A and B but also
  1819. * trees C and D which are correctly matched on the far side of
  1820. * the grid, on the grounds that those four trees between them
  1821. * have only three tents.
  1822. *
  1823. * Also, that approach fares badly when you introduce the
  1824. * additional requirement that incomplete grids should have
  1825. * errors highlighted only when they can be proved to be errors
  1826. * - so that trees should not be marked as having too few tents
  1827. * if there are enough BLANK squares remaining around them that
  1828. * could be turned into the missing tents (to do so would be
  1829. * patronising, since the overwhelming likelihood is not that
  1830. * the player has forgotten to put a tree there but that they
  1831. * have merely not put one there _yet_). However, tents with too
  1832. * few trees can be marked immediately, since those are
  1833. * definitely player error.
  1834. *
  1835. * So I adopt an alternative approach, which is to consider the
  1836. * bipartite adjacency graph between trees and tents
  1837. * ('bipartite' in the sense that for these purposes I
  1838. * deliberately ignore two adjacent trees or two adjacent
  1839. * tents), divide that graph up into its connected components
  1840. * using a dsf, and look for components which contain different
  1841. * numbers of trees and tents. This allows me to highlight
  1842. * groups of tents with too few trees between them immediately,
  1843. * and then in order to find groups of trees with too few tents
  1844. * I redo the same process but counting BLANKs as potential
  1845. * tents (so that the only trees highlighted are those
  1846. * surrounded by enough NONTENTs to make it impossible to give
  1847. * them enough tents).
  1848. *
  1849. * However, this technique is incomplete: it is not a sufficient
  1850. * condition for the existence of a perfect matching that every
  1851. * connected component of the graph has the same number of tents
  1852. * and trees. An example of a graph which satisfies the latter
  1853. * condition but still has no perfect matching is
  1854. *
  1855. * A B C
  1856. * | / ,/|
  1857. * | / ,'/ |
  1858. * | / ,' / |
  1859. * |/,' / |
  1860. * 1 2 3
  1861. *
  1862. * which can be realised in Tents as
  1863. *
  1864. * B
  1865. * A 1 C 2
  1866. * 3
  1867. *
  1868. * The matching-error highlighter described above will not mark
  1869. * this construction as erroneous. However, something else will:
  1870. * the three tents in the above diagram (let us suppose A,B,C
  1871. * are the tents, though it doesn't matter which) contain two
  1872. * diagonally adjacent pairs. So there will be _an_ error
  1873. * highlighted for the above layout, even though not all types
  1874. * of error will be highlighted.
  1875. *
  1876. * And in fact we can prove that this will always be the case:
  1877. * that the shortcomings of the matching-error highlighter will
  1878. * always be made up for by the easy tent adjacency highlighter.
  1879. *
  1880. * Lemma: Let G be a bipartite graph between n trees and n
  1881. * tents, which is connected, and in which no tree has degree
  1882. * more than two (but a tent may). Then G has a perfect matching.
  1883. *
  1884. * (Note: in the statement and proof of the Lemma I will
  1885. * consistently use 'tree' to indicate a type of graph vertex as
  1886. * opposed to a tent, and not to indicate a tree in the graph-
  1887. * theoretic sense.)
  1888. *
  1889. * Proof:
  1890. *
  1891. * If we can find a tent of degree 1 joined to a tree of degree
  1892. * 2, then any perfect matching must pair that tent with that
  1893. * tree. Hence, we can remove both, leaving a smaller graph G'
  1894. * which still satisfies all the conditions of the Lemma, and
  1895. * which has a perfect matching iff G does.
  1896. *
  1897. * So, wlog, we may assume G contains no tent of degree 1 joined
  1898. * to a tree of degree 2; if it does, we can reduce it as above.
  1899. *
  1900. * If G has no tent of degree 1 at all, then every tent has
  1901. * degree at least two, so there are at least 2n edges in the
  1902. * graph. But every tree has degree at most two, so there are at
  1903. * most 2n edges. Hence there must be exactly 2n edges, so every
  1904. * tree and every tent must have degree exactly two, which means
  1905. * that the whole graph consists of a single loop (by
  1906. * connectedness), and therefore certainly has a perfect
  1907. * matching.
  1908. *
  1909. * Alternatively, if G does have a tent of degree 1 but it is
  1910. * not connected to a tree of degree 2, then the tree it is
  1911. * connected to must have degree 1 - and, by connectedness, that
  1912. * must mean that that tent and that tree between them form the
  1913. * entire graph. This trivial graph has a trivial perfect
  1914. * matching. []
  1915. *
  1916. * That proves the lemma. Hence, in any case where the matching-
  1917. * error highlighter fails to highlight an erroneous component
  1918. * (because it has the same number of tents as trees, but they
  1919. * cannot be matched up), the above lemma tells us that there
  1920. * must be a tree with degree more than 2, i.e. a tree
  1921. * orthogonally adjacent to at least three tents. But in that
  1922. * case, there must be some pair of those three tents which are
  1923. * diagonally adjacent to each other, so the tent-adjacency
  1924. * highlighter will necessarily show an error. So any filled
  1925. * layout in Tents which is not a correct solution to the puzzle
  1926. * must have _some_ error highlighted by the subroutine below.
  1927. *
  1928. * (Of course it would be nicer if we could highlight all
  1929. * errors: in the above example layout, we would like to
  1930. * highlight tents A,B as having too few trees between them, and
  1931. * trees 2,3 as having too few tents, in addition to marking the
  1932. * adjacency problems. But I can't immediately think of any way
  1933. * to find the smallest sets of such tents and trees without an
  1934. * O(2^N) loop over all subsets of a given component.)
  1935. */
  1936. /*
  1937. * ret[0] through to ret[w*h-1] give error markers for the grid
  1938. * squares. After that, ret[w*h] to ret[w*h+w-1] give error
  1939. * markers for the column numbers, and ret[w*h+w] to
  1940. * ret[w*h+w+h-1] for the row numbers.
  1941. */
  1942. /*
  1943. * Spot tent-adjacency violations.
  1944. */
  1945. for (x = 0; x < w*h; x++)
  1946. ret[x] = 0;
  1947. for (y = 0; y < h; y++) {
  1948. for (x = 0; x < w; x++) {
  1949. if (y+1 < h && x+1 < w &&
  1950. ((grid[y*w+x] == TENT &&
  1951. grid[(y+1)*w+(x+1)] == TENT) ||
  1952. (grid[(y+1)*w+x] == TENT &&
  1953. grid[y*w+(x+1)] == TENT))) {
  1954. ret[y*w+x] |= 1 << ERR_ADJ_BOTRIGHT;
  1955. ret[(y+1)*w+x] |= 1 << ERR_ADJ_TOPRIGHT;
  1956. ret[y*w+(x+1)] |= 1 << ERR_ADJ_BOTLEFT;
  1957. ret[(y+1)*w+(x+1)] |= 1 << ERR_ADJ_TOPLEFT;
  1958. }
  1959. if (y+1 < h &&
  1960. grid[y*w+x] == TENT &&
  1961. grid[(y+1)*w+x] == TENT) {
  1962. ret[y*w+x] |= 1 << ERR_ADJ_BOT;
  1963. ret[(y+1)*w+x] |= 1 << ERR_ADJ_TOP;
  1964. }
  1965. if (x+1 < w &&
  1966. grid[y*w+x] == TENT &&
  1967. grid[y*w+(x+1)] == TENT) {
  1968. ret[y*w+x] |= 1 << ERR_ADJ_RIGHT;
  1969. ret[y*w+(x+1)] |= 1 << ERR_ADJ_LEFT;
  1970. }
  1971. }
  1972. }
  1973. /*
  1974. * Spot numeric clue violations.
  1975. */
  1976. for (x = 0; x < w; x++) {
  1977. int tents = 0, maybetents = 0;
  1978. for (y = 0; y < h; y++) {
  1979. if (grid[y*w+x] == TENT)
  1980. tents++;
  1981. else if (grid[y*w+x] == BLANK)
  1982. maybetents++;
  1983. }
  1984. ret[w*h+x] = (tents > state->numbers->numbers[x] ||
  1985. tents + maybetents < state->numbers->numbers[x]);
  1986. }
  1987. for (y = 0; y < h; y++) {
  1988. int tents = 0, maybetents = 0;
  1989. for (x = 0; x < w; x++) {
  1990. if (grid[y*w+x] == TENT)
  1991. tents++;
  1992. else if (grid[y*w+x] == BLANK)
  1993. maybetents++;
  1994. }
  1995. ret[w*h+w+y] = (tents > state->numbers->numbers[w+y] ||
  1996. tents + maybetents < state->numbers->numbers[w+y]);
  1997. }
  1998. /*
  1999. * Identify groups of tents with too few trees between them,
  2000. * which we do by constructing the connected components of the
  2001. * bipartite adjacency graph between tents and trees
  2002. * ('bipartite' in the sense that we deliberately ignore
  2003. * adjacency between tents or between trees), and highlighting
  2004. * all the tents in any component which has a smaller tree
  2005. * count.
  2006. */
  2007. dsf = dsf_new(w*h);
  2008. /* Construct the equivalence classes. */
  2009. for (y = 0; y < h; y++) {
  2010. for (x = 0; x < w-1; x++) {
  2011. if ((grid[y*w+x] == TREE && grid[y*w+x+1] == TENT) ||
  2012. (grid[y*w+x] == TENT && grid[y*w+x+1] == TREE))
  2013. dsf_merge(dsf, y*w+x, y*w+x+1);
  2014. }
  2015. }
  2016. for (y = 0; y < h-1; y++) {
  2017. for (x = 0; x < w; x++) {
  2018. if ((grid[y*w+x] == TREE && grid[(y+1)*w+x] == TENT) ||
  2019. (grid[y*w+x] == TENT && grid[(y+1)*w+x] == TREE))
  2020. dsf_merge(dsf, y*w+x, (y+1)*w+x);
  2021. }
  2022. }
  2023. /* Count up the tent/tree difference in each one. */
  2024. for (x = 0; x < w*h; x++)
  2025. tmp[x] = 0;
  2026. for (x = 0; x < w*h; x++) {
  2027. y = dsf_canonify(dsf, x);
  2028. if (grid[x] == TREE)
  2029. tmp[y]++;
  2030. else if (grid[x] == TENT)
  2031. tmp[y]--;
  2032. }
  2033. /* And highlight any tent belonging to an equivalence class with
  2034. * a score less than zero. */
  2035. for (x = 0; x < w*h; x++) {
  2036. y = dsf_canonify(dsf, x);
  2037. if (grid[x] == TENT && tmp[y] < 0)
  2038. ret[x] |= 1 << ERR_OVERCOMMITTED;
  2039. }
  2040. /*
  2041. * Identify groups of trees with too few tents between them.
  2042. * This is done similarly, except that we now count BLANK as
  2043. * equivalent to TENT, i.e. we only highlight such trees when
  2044. * the user hasn't even left _room_ to provide tents for them
  2045. * all. (Otherwise, we'd highlight all trees red right at the
  2046. * start of the game, before the user had done anything wrong!)
  2047. */
  2048. #define TENT(x) ((x)==TENT || (x)==BLANK)
  2049. dsf_reinit(dsf);
  2050. /* Construct the equivalence classes. */
  2051. for (y = 0; y < h; y++) {
  2052. for (x = 0; x < w-1; x++) {
  2053. if ((grid[y*w+x] == TREE && TENT(grid[y*w+x+1])) ||
  2054. (TENT(grid[y*w+x]) && grid[y*w+x+1] == TREE))
  2055. dsf_merge(dsf, y*w+x, y*w+x+1);
  2056. }
  2057. }
  2058. for (y = 0; y < h-1; y++) {
  2059. for (x = 0; x < w; x++) {
  2060. if ((grid[y*w+x] == TREE && TENT(grid[(y+1)*w+x])) ||
  2061. (TENT(grid[y*w+x]) && grid[(y+1)*w+x] == TREE))
  2062. dsf_merge(dsf, y*w+x, (y+1)*w+x);
  2063. }
  2064. }
  2065. /* Count up the tent/tree difference in each one. */
  2066. for (x = 0; x < w*h; x++)
  2067. tmp[x] = 0;
  2068. for (x = 0; x < w*h; x++) {
  2069. y = dsf_canonify(dsf, x);
  2070. if (grid[x] == TREE)
  2071. tmp[y]++;
  2072. else if (TENT(grid[x]))
  2073. tmp[y]--;
  2074. }
  2075. /* And highlight any tree belonging to an equivalence class with
  2076. * a score more than zero. */
  2077. for (x = 0; x < w*h; x++) {
  2078. y = dsf_canonify(dsf, x);
  2079. if (grid[x] == TREE && tmp[y] > 0)
  2080. ret[x] |= 1 << ERR_OVERCOMMITTED;
  2081. }
  2082. #undef TENT
  2083. sfree(tmp);
  2084. dsf_free(dsf);
  2085. return ret;
  2086. }
  2087. static void draw_err_adj(drawing *dr, game_drawstate *ds, int x, int y)
  2088. {
  2089. int coords[8];
  2090. int yext, xext;
  2091. /*
  2092. * Draw a diamond.
  2093. */
  2094. coords[0] = x - TILESIZE*2/5;
  2095. coords[1] = y;
  2096. coords[2] = x;
  2097. coords[3] = y - TILESIZE*2/5;
  2098. coords[4] = x + TILESIZE*2/5;
  2099. coords[5] = y;
  2100. coords[6] = x;
  2101. coords[7] = y + TILESIZE*2/5;
  2102. draw_polygon(dr, coords, 4, COL_ERROR, COL_GRID);
  2103. /*
  2104. * Draw an exclamation mark in the diamond. This turns out to
  2105. * look unpleasantly off-centre if done via draw_text, so I do
  2106. * it by hand on the basis that exclamation marks aren't that
  2107. * difficult to draw...
  2108. */
  2109. xext = TILESIZE/16;
  2110. yext = TILESIZE*2/5 - (xext*2+2);
  2111. draw_rect(dr, x-xext, y-yext, xext*2+1, yext*2+1 - (xext*3),
  2112. COL_ERRTEXT);
  2113. draw_rect(dr, x-xext, y+yext-xext*2+1, xext*2+1, xext*2, COL_ERRTEXT);
  2114. }
  2115. static void draw_tile(drawing *dr, game_drawstate *ds,
  2116. int x, int y, int v, bool cur, bool printing)
  2117. {
  2118. int err;
  2119. int tx = COORD(x), ty = COORD(y);
  2120. int cx = tx + TILESIZE/2, cy = ty + TILESIZE/2;
  2121. err = v & ~15;
  2122. v &= 15;
  2123. clip(dr, tx, ty, TILESIZE, TILESIZE);
  2124. if (!printing) {
  2125. draw_rect(dr, tx, ty, TILESIZE, TILESIZE, COL_GRID);
  2126. draw_rect(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1,
  2127. (v == BLANK ? COL_BACKGROUND : COL_GRASS));
  2128. }
  2129. if (v == TREE) {
  2130. int i;
  2131. (printing ? draw_rect_outline : draw_rect)
  2132. (dr, cx-TILESIZE/15, ty+TILESIZE*3/10,
  2133. 2*(TILESIZE/15)+1, (TILESIZE*9/10 - TILESIZE*3/10),
  2134. (err & (1<<ERR_OVERCOMMITTED) ? COL_ERRTRUNK : COL_TREETRUNK));
  2135. for (i = 0; i < (printing ? 2 : 1); i++) {
  2136. int col = (i == 1 ? COL_BACKGROUND :
  2137. (err & (1<<ERR_OVERCOMMITTED) ? COL_ERROR :
  2138. COL_TREELEAF));
  2139. int sub = i * (TILESIZE/32);
  2140. draw_circle(dr, cx, ty+TILESIZE*4/10, TILESIZE/4 - sub,
  2141. col, col);
  2142. draw_circle(dr, cx+TILESIZE/5, ty+TILESIZE/4, TILESIZE/8 - sub,
  2143. col, col);
  2144. draw_circle(dr, cx-TILESIZE/5, ty+TILESIZE/4, TILESIZE/8 - sub,
  2145. col, col);
  2146. draw_circle(dr, cx+TILESIZE/4, ty+TILESIZE*6/13, TILESIZE/8 - sub,
  2147. col, col);
  2148. draw_circle(dr, cx-TILESIZE/4, ty+TILESIZE*6/13, TILESIZE/8 - sub,
  2149. col, col);
  2150. }
  2151. } else if (v == TENT) {
  2152. int coords[6];
  2153. int col;
  2154. coords[0] = cx - TILESIZE/3;
  2155. coords[1] = cy + TILESIZE/3;
  2156. coords[2] = cx + TILESIZE/3;
  2157. coords[3] = cy + TILESIZE/3;
  2158. coords[4] = cx;
  2159. coords[5] = cy - TILESIZE/3;
  2160. col = (err & (1<<ERR_OVERCOMMITTED) ? COL_ERROR : COL_TENT);
  2161. draw_polygon(dr, coords, 3, (printing ? -1 : col), col);
  2162. }
  2163. if (err & (1 << ERR_ADJ_TOPLEFT))
  2164. draw_err_adj(dr, ds, tx, ty);
  2165. if (err & (1 << ERR_ADJ_TOP))
  2166. draw_err_adj(dr, ds, tx+TILESIZE/2, ty);
  2167. if (err & (1 << ERR_ADJ_TOPRIGHT))
  2168. draw_err_adj(dr, ds, tx+TILESIZE, ty);
  2169. if (err & (1 << ERR_ADJ_LEFT))
  2170. draw_err_adj(dr, ds, tx, ty+TILESIZE/2);
  2171. if (err & (1 << ERR_ADJ_RIGHT))
  2172. draw_err_adj(dr, ds, tx+TILESIZE, ty+TILESIZE/2);
  2173. if (err & (1 << ERR_ADJ_BOTLEFT))
  2174. draw_err_adj(dr, ds, tx, ty+TILESIZE);
  2175. if (err & (1 << ERR_ADJ_BOT))
  2176. draw_err_adj(dr, ds, tx+TILESIZE/2, ty+TILESIZE);
  2177. if (err & (1 << ERR_ADJ_BOTRIGHT))
  2178. draw_err_adj(dr, ds, tx+TILESIZE, ty+TILESIZE);
  2179. if (cur) {
  2180. int coff = TILESIZE/8;
  2181. draw_rect_outline(dr, tx + coff, ty + coff,
  2182. TILESIZE - coff*2 + 1, TILESIZE - coff*2 + 1,
  2183. COL_GRID);
  2184. }
  2185. unclip(dr);
  2186. draw_update(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1);
  2187. }
  2188. /*
  2189. * Internal redraw function, used for printing as well as drawing.
  2190. */
  2191. static void int_redraw(drawing *dr, game_drawstate *ds,
  2192. const game_state *oldstate, const game_state *state,
  2193. int dir, const game_ui *ui,
  2194. float animtime, float flashtime, bool printing)
  2195. {
  2196. int w = state->p.w, h = state->p.h;
  2197. int x, y;
  2198. bool flashing;
  2199. int cx = -1, cy = -1;
  2200. bool cmoved = false;
  2201. char *tmpgrid;
  2202. int *errors;
  2203. if (ui) {
  2204. if (ui->cdisp) { cx = ui->cx; cy = ui->cy; }
  2205. if (cx != ds->cx || cy != ds->cy) cmoved = true;
  2206. }
  2207. if (printing || !ds->started) {
  2208. if (printing)
  2209. print_line_width(dr, TILESIZE/64);
  2210. /*
  2211. * Draw the grid.
  2212. */
  2213. for (y = 0; y <= h; y++)
  2214. draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y), COL_GRID);
  2215. for (x = 0; x <= w; x++)
  2216. draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h), COL_GRID);
  2217. }
  2218. if (flashtime > 0)
  2219. flashing = (int)(flashtime * 3 / FLASH_TIME) != 1;
  2220. else
  2221. flashing = false;
  2222. /*
  2223. * Find errors. For this we use _part_ of the information from a
  2224. * currently active drag: we transform dsx,dsy but not anything
  2225. * else. (This seems to strike a good compromise between having
  2226. * the error highlights respond instantly to single clicks, but
  2227. * not giving constant feedback during a right-drag.)
  2228. */
  2229. if (ui && ui->drag_button >= 0) {
  2230. tmpgrid = snewn(w*h, char);
  2231. memcpy(tmpgrid, state->grid, w*h);
  2232. tmpgrid[ui->dsy * w + ui->dsx] =
  2233. drag_xform(ui, ui->dsx, ui->dsy, tmpgrid[ui->dsy * w + ui->dsx]);
  2234. errors = find_errors(state, tmpgrid);
  2235. sfree(tmpgrid);
  2236. } else {
  2237. errors = find_errors(state, state->grid);
  2238. }
  2239. /*
  2240. * Draw the grid.
  2241. */
  2242. for (y = 0; y < h; y++) {
  2243. for (x = 0; x < w; x++) {
  2244. int v = state->grid[y*w+x];
  2245. bool credraw = false;
  2246. /*
  2247. * We deliberately do not take drag_ok into account
  2248. * here, because user feedback suggests that it's
  2249. * marginally nicer not to have the drag effects
  2250. * flickering on and off disconcertingly.
  2251. */
  2252. if (ui && ui->drag_button >= 0)
  2253. v = drag_xform(ui, x, y, v);
  2254. if (flashing && (v == TREE || v == TENT))
  2255. v = NONTENT;
  2256. if (cmoved) {
  2257. if ((x == cx && y == cy) ||
  2258. (x == ds->cx && y == ds->cy)) credraw = true;
  2259. }
  2260. v |= errors[y*w+x];
  2261. if (printing || ds->drawn[y*w+x] != v || credraw) {
  2262. draw_tile(dr, ds, x, y, v, (x == cx && y == cy), printing);
  2263. if (!printing)
  2264. ds->drawn[y*w+x] = v;
  2265. }
  2266. }
  2267. }
  2268. /*
  2269. * Draw (or redraw, if their error-highlighted state has
  2270. * changed) the numbers.
  2271. */
  2272. for (x = 0; x < w; x++) {
  2273. if (printing || ds->numbersdrawn[x] != errors[w*h+x]) {
  2274. char buf[80];
  2275. draw_rect(dr, COORD(x), COORD(h)+1, TILESIZE, BRBORDER-1,
  2276. COL_BACKGROUND);
  2277. sprintf(buf, "%d", state->numbers->numbers[x]);
  2278. draw_text(dr, COORD(x) + TILESIZE/2, COORD(h+1),
  2279. FONT_VARIABLE, TILESIZE/2, ALIGN_HCENTRE|ALIGN_VNORMAL,
  2280. (errors[w*h+x] ? COL_ERROR : COL_GRID), buf);
  2281. draw_update(dr, COORD(x), COORD(h)+1, TILESIZE, BRBORDER-1);
  2282. if (!printing)
  2283. ds->numbersdrawn[x] = errors[w*h+x];
  2284. }
  2285. }
  2286. for (y = 0; y < h; y++) {
  2287. if (printing || ds->numbersdrawn[w+y] != errors[w*h+w+y]) {
  2288. char buf[80];
  2289. draw_rect(dr, COORD(w)+1, COORD(y), BRBORDER-1, TILESIZE,
  2290. COL_BACKGROUND);
  2291. sprintf(buf, "%d", state->numbers->numbers[w+y]);
  2292. draw_text(dr, COORD(w+1), COORD(y) + TILESIZE/2,
  2293. FONT_VARIABLE, TILESIZE/2, ALIGN_HRIGHT|ALIGN_VCENTRE,
  2294. (errors[w*h+w+y] ? COL_ERROR : COL_GRID), buf);
  2295. draw_update(dr, COORD(w)+1, COORD(y), BRBORDER-1, TILESIZE);
  2296. if (!printing)
  2297. ds->numbersdrawn[w+y] = errors[w*h+w+y];
  2298. }
  2299. }
  2300. if (cmoved) {
  2301. ds->cx = cx;
  2302. ds->cy = cy;
  2303. }
  2304. sfree(errors);
  2305. }
  2306. static void game_redraw(drawing *dr, game_drawstate *ds,
  2307. const game_state *oldstate, const game_state *state,
  2308. int dir, const game_ui *ui,
  2309. float animtime, float flashtime)
  2310. {
  2311. int_redraw(dr, ds, oldstate, state, dir, ui, animtime, flashtime, false);
  2312. }
  2313. static float game_anim_length(const game_state *oldstate,
  2314. const game_state *newstate, int dir, game_ui *ui)
  2315. {
  2316. return 0.0F;
  2317. }
  2318. static float game_flash_length(const game_state *oldstate,
  2319. const game_state *newstate, int dir, game_ui *ui)
  2320. {
  2321. if (!oldstate->completed && newstate->completed &&
  2322. !oldstate->used_solve && !newstate->used_solve)
  2323. return FLASH_TIME;
  2324. return 0.0F;
  2325. }
  2326. static void game_get_cursor_location(const game_ui *ui,
  2327. const game_drawstate *ds,
  2328. const game_state *state,
  2329. const game_params *params,
  2330. int *x, int *y, int *w, int *h)
  2331. {
  2332. if(ui->cdisp) {
  2333. *x = COORD(ui->cx);
  2334. *y = COORD(ui->cy);
  2335. *w = *h = TILESIZE;
  2336. }
  2337. }
  2338. static int game_status(const game_state *state)
  2339. {
  2340. return state->completed ? +1 : 0;
  2341. }
  2342. static void game_print_size(const game_params *params, const game_ui *ui,
  2343. float *x, float *y)
  2344. {
  2345. int pw, ph;
  2346. /*
  2347. * I'll use 6mm squares by default.
  2348. */
  2349. game_compute_size(params, 600, ui, &pw, &ph);
  2350. *x = pw / 100.0F;
  2351. *y = ph / 100.0F;
  2352. }
  2353. static void game_print(drawing *dr, const game_state *state, const game_ui *ui,
  2354. int tilesize)
  2355. {
  2356. int c;
  2357. /* Ick: fake up `ds->tilesize' for macro expansion purposes */
  2358. game_drawstate ads, *ds = &ads;
  2359. game_set_size(dr, ds, NULL, tilesize);
  2360. c = print_mono_colour(dr, 1); assert(c == COL_BACKGROUND);
  2361. c = print_mono_colour(dr, 0); assert(c == COL_GRID);
  2362. c = print_mono_colour(dr, 1); assert(c == COL_GRASS);
  2363. c = print_mono_colour(dr, 0); assert(c == COL_TREETRUNK);
  2364. c = print_mono_colour(dr, 0); assert(c == COL_TREELEAF);
  2365. c = print_mono_colour(dr, 0); assert(c == COL_TENT);
  2366. int_redraw(dr, ds, NULL, state, +1, NULL, 0.0F, 0.0F, true);
  2367. }
  2368. #ifdef COMBINED
  2369. #define thegame tents
  2370. #endif
  2371. const struct game thegame = {
  2372. "Tents", "games.tents", "tents",
  2373. default_params,
  2374. game_fetch_preset, NULL,
  2375. decode_params,
  2376. encode_params,
  2377. free_params,
  2378. dup_params,
  2379. true, game_configure, custom_params,
  2380. validate_params,
  2381. new_game_desc,
  2382. validate_desc,
  2383. new_game,
  2384. dup_game,
  2385. free_game,
  2386. true, solve_game,
  2387. true, game_can_format_as_text_now, game_text_format,
  2388. NULL, NULL, /* get_prefs, set_prefs */
  2389. new_ui,
  2390. free_ui,
  2391. NULL, /* encode_ui */
  2392. NULL, /* decode_ui */
  2393. NULL, /* game_request_keys */
  2394. game_changed_state,
  2395. current_key_label,
  2396. interpret_move,
  2397. execute_move,
  2398. PREFERRED_TILESIZE, game_compute_size, game_set_size,
  2399. game_colours,
  2400. game_new_drawstate,
  2401. game_free_drawstate,
  2402. game_redraw,
  2403. game_anim_length,
  2404. game_flash_length,
  2405. game_get_cursor_location,
  2406. game_status,
  2407. true, false, game_print_size, game_print,
  2408. false, /* wants_statusbar */
  2409. false, NULL, /* timing_state */
  2410. REQUIRE_RBUTTON, /* flags */
  2411. };
  2412. #ifdef STANDALONE_SOLVER
  2413. #include <stdarg.h>
  2414. int main(int argc, char **argv)
  2415. {
  2416. game_params *p;
  2417. game_state *s, *s2;
  2418. char *id = NULL, *desc;
  2419. const char *err;
  2420. bool grade = false;
  2421. int ret, diff;
  2422. bool really_verbose = false;
  2423. struct solver_scratch *sc;
  2424. while (--argc > 0) {
  2425. char *p = *++argv;
  2426. if (!strcmp(p, "-v")) {
  2427. really_verbose = true;
  2428. } else if (!strcmp(p, "-g")) {
  2429. grade = true;
  2430. } else if (*p == '-') {
  2431. fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
  2432. return 1;
  2433. } else {
  2434. id = p;
  2435. }
  2436. }
  2437. if (!id) {
  2438. fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
  2439. return 1;
  2440. }
  2441. desc = strchr(id, ':');
  2442. if (!desc) {
  2443. fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
  2444. return 1;
  2445. }
  2446. *desc++ = '\0';
  2447. p = default_params();
  2448. decode_params(p, id);
  2449. err = validate_desc(p, desc);
  2450. if (err) {
  2451. fprintf(stderr, "%s: %s\n", argv[0], err);
  2452. return 1;
  2453. }
  2454. s = new_game(NULL, p, desc);
  2455. s2 = new_game(NULL, p, desc);
  2456. sc = new_scratch(p->w, p->h);
  2457. /*
  2458. * When solving an Easy puzzle, we don't want to bother the
  2459. * user with Hard-level deductions. For this reason, we grade
  2460. * the puzzle internally before doing anything else.
  2461. */
  2462. ret = -1; /* placate optimiser */
  2463. for (diff = 0; diff < DIFFCOUNT; diff++) {
  2464. ret = tents_solve(p->w, p->h, s->grid, s->numbers->numbers,
  2465. s2->grid, sc, diff);
  2466. if (ret < 2)
  2467. break;
  2468. }
  2469. if (diff == DIFFCOUNT) {
  2470. if (grade)
  2471. printf("Difficulty rating: too hard to solve internally\n");
  2472. else
  2473. printf("Unable to find a unique solution\n");
  2474. } else {
  2475. if (grade) {
  2476. if (ret == 0)
  2477. printf("Difficulty rating: impossible (no solution exists)\n");
  2478. else if (ret == 1)
  2479. printf("Difficulty rating: %s\n", tents_diffnames[diff]);
  2480. } else {
  2481. verbose = really_verbose;
  2482. ret = tents_solve(p->w, p->h, s->grid, s->numbers->numbers,
  2483. s2->grid, sc, diff);
  2484. if (ret == 0)
  2485. printf("Puzzle is inconsistent\n");
  2486. else
  2487. fputs(game_text_format(s2), stdout);
  2488. }
  2489. }
  2490. return 0;
  2491. }
  2492. #endif
  2493. /* vim: set shiftwidth=4 tabstop=8: */