Repository navigation
Expand file tree
/
Copy pathKnightWGMap.py
More file actions
1242 lines (983 loc) · 45.2 KB
/
Copy pathKnightWGMap.py
File metadata and controls
1242 lines (983 loc) · 45.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Created on Jan 21, 2014
@author: presto
'''
import random
import re
from PIL import Image
from math import floor, ceil, sqrt, sin, cos, fabs
import string
# import ImageFont, ImageDraw
from General import SquareGridWGMap, WGMap
class KnightWGMap(SquareGridWGMap):
"""
Extends the SquareGridWGMap.
Creates a 'chessboard map' in which players
gain bonuses for 4-squares & borders are connected
like a knight moves in chess.
"""
def __init__(self):
"""Constructor"""
super(KnightWGMap,self).__init__()
self.doWrap = False
def addBordersViaRegex(self):
'''
Adds borders as a knight could attack for a grid created
by :func:`SquareGridWGMap.createTerritories.
todo: via regex? Rename this function.
Args are both ints.
'''
if self.DOM.getElementsByTagName("borders") == None:
newBordersElement = self.DOM.createElement("borders")
self.DOM.getElementsByTagName("board")[0].appendChild(newBordersElement)
for row in range(self.rows):
for col in range(self.cols):
# skip half the squares since borders are two way
if (row+col)%2 == 0:
continue
fromTerritoryName = self.getTerritoryName(row, col)
fromID = self.getTerritoryIDFromName(fromTerritoryName)
self.addBorderToCoordinate(fromID,row-2,col-1)
self.addBorderToCoordinate(fromID,row-2,col+1)
self.addBorderToCoordinate(fromID,row-1,col-2)
self.addBorderToCoordinate(fromID,row-1,col+2)
self.addBorderToCoordinate(fromID,row+1,col-2)
self.addBorderToCoordinate(fromID,row+2,col-1)
self.addBorderToCoordinate(fromID,row+1,col+2)
self.addBorderToCoordinate(fromID,row+2,col+1)
def createFunctionGame(self,filePath,placeKnightFunc,rowHeight=40,colWidth=40,):
'''Be sure to set the board name by hand'''
xOrigin = colWidth/2
yOrigin = rowHeight/2
#print "Creating Function Knight's Tour:",filePath,rows,cols,rowHeight,colWidth,xOrigin,yOrigin
print("Knight's Tour (" + str(self.rows) + "x" + str(self.cols) + ")")#+str(placeKnightFunc)
self.deleteAllBorders()
self.deleteAllTerritories()
self.deleteAllContinents()
#self.setBoardName("Knight's Tour (" + str(rows) + "x" + str(cols) + ")")
self.createTerritories(xOrigin, yOrigin, rowHeight, colWidth)
self.addBordersViaRegex()
#self.createBlockContinents(1)
print("Deleting Territories", end=' ')
territoriesDeleted = []
inper = .7
outper = .1
for r in range(self.rows):
for c in range(self.cols):
(doDelete, (inper, outper)) = placeKnightFunc(r, c, (inper, outper))
#print doDelete
if doDelete:
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
if (self.checkOneTerritoryCanReachAll() == False):
return False
territoriesDeleted.extend(self.removeUselessTerritories())
allTerritories = self.getTerritoryIDsFromNameRegex(".*")
self.addChainContinents(allTerritories)
#print "td1",territoriesDeleted
# find all territories with only one border & no continents - then delete them.
#print "td2",territoriesDeleted
self.setNumAttacks(int(ceil(sqrt(self.rows*self.cols))))
self.setNumFortifies(int(ceil(sqrt(sqrt(self.rows*self.cols)))))
self.setAllSoleContinentTerritoriesToNeutral()
self.addViewBordersToNeighbors(2)
self.createPNGs(filePath, rowHeight, colWidth,
xOrigin, yOrigin, territoriesDeleted)
self.save_map_to_file(filePath + ".xml")
return True
def createStringGame(self,filePath,knightString,rowHeight=40,colWidth=40):
'''Be sure to set the board name by hand'''
self.rows = knightString.count("\n")
self.cols = knightString.find("\n")
xOrigin = colWidth/2
yOrigin = rowHeight/2
#print "Creating Function Knight's Tour:",filePath,rows,cols,rowHeight,colWidth,xOrigin,yOrigin
print("Knight's Tour (" + str(self.rows) + "x" + str(self.cols) + ")")#+str(placeKnightFunc)
self.deleteAllBorders()
self.deleteAllTerritories()
self.deleteAllContinents()
#self.setBoardName("Knight's Tour (" + str(rows) + "x" + str(cols) + ")")
self.createTerritories(xOrigin, yOrigin, rowHeight, colWidth)
self.addBordersViaRegex()
#self.createBlockContinents(1)
print("Deleting Territories", end=' ')
territoriesDeleted = []
for r in range(self.rows):
for c in range(self.cols):
ix = r * (self.cols + 1) + c
doDelete = knightString[ix] != "K"
#print doDelete
if doDelete:
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
#territoriesDeleted.extend(self.removeUselessTerritories())
allTerritories = self.getTerritoryIDsFromNameRegex(".*")
self.addChainContinents(allTerritories)
#print "td1",territoriesDeleted
# find all territories with only one border & no continents - then delete them.
#print "td2",territoriesDeleted
self.setNumAttacks(int(ceil(sqrt(self.rows*self.cols))))
self.setNumFortifies(int(ceil(sqrt(sqrt(self.rows*self.cols)))))
self.setAllSoleContinentTerritoriesToNeutral()
self.addViewBordersToNeighbors(2)
#TODO: set card values.
#set # of players
self.createPNGs(filePath, rowHeight, colWidth,
xOrigin, yOrigin, territoriesDeleted)
self.save_map_to_file(filePath + ".xml")
if (self.checkOneTerritoryCanReachAll() == False):
return False
return True
def placeRandomVerticalStripes(self,r,c):
if(c%5 == 3):
return (random.random() < .3)
if(c%5 == 4):
return (random.random() < .5)
if(c%5 == 0):
return True;
if(c%5 == 1):
if (random.random() < .5):
return True;
if(c%5 == 2):
if (random.random() < .3):
return True;
def placeStripes(self,r,c, xxx_todo_changeme):
# inper = chance to keep a knight in cell
# outper = chance to keep a knight out of cell
(inper,outper) = xxx_todo_changeme
isKnight = False
if ( r%6 == 1 or r%6 == 2 or c%7 == 2 or c%7 == 1):
isKnight = True
# try to even out the random extremes, by increasing the odds if we miss.
doDelete = not isKnight
return (doDelete, (inper, outper))
def placeRandomSnake(self,r,c):
if(c%8 == 0) or (c%8 == 7):
if (r < self.rows-2):
return random.random() < .85
if(c%8 ==1) or (c%8 == 6):
if (r < self.rows-2):
return random.random() < .15
if(c%8 == 3) or (c%8 == 4):
if (r > 1):
return random.random() < .85
if(c%8 ==2) or (c%8 == 5):
if (r > 1):
return random.random() < .15
return False
def placeGrid(self,r,c):
rFlag = False
cFlag = False
if (r%3 == 0):
rFlag = True
if (c%3 == 0):
cFlag = True
if (rFlag and cFlag):
return False
if (rFlag or cFlag):
return True
return False
# return true to delete
def placeCells(self,r,c, xxx_todo_changeme1):
(inper,outper) = xxx_todo_changeme1
inCell = True
# edges
#if(c==0 or c == self.cols-1 or r==0 or r == self.rows-1):
# inCell = False
# dividing rows
# if( r == self.rows/2-1 or r == self.rows/2):
# if( r == self.rows/3+1 or r == self.rows/3 or r == 2*self.rows/3+1 or r == 2*self.rows/3):
if( r % 7 == 4 or r % 7 == 6 or r % 7== 5):
inCell = False
# dividing cols
if( c % 7 == 6 or c % 7 == 4 or c % 7 == 5):
inCell = False
# try to even out the random extremes, by increasing the odds if we miss.
if (inCell):
doDelete = (random.random() > inper)
if doDelete: #delete knight
inper += .15
else: #keep knight
inper = .7
else:
doDelete = (random.random() > outper)
if doDelete: # delete knight
outper += .1
else: # keep knight
outper = .05
print(doDelete)
return (doDelete, (inper, outper))
# return true to delete
def placeFourSquare(self,r,c, xxx_todo_changeme2):
# inper = chance to keep a knight in cell
# outper = chance to keep a knight out of cell
(inper,outper) = xxx_todo_changeme2
inCell = True
# edges
#if(c==0 or c == self.cols-1 or r==0 or r == self.rows-1):
# inCell = False
# dividing rows
# if( r == self.rows/2-1 or r == self.rows/2):
# if( r == self.rows/3+1 or r == self.rows/3 or r == 2*self.rows/3+1 or r == 2*self.rows/3):
if( r >= 5 and r <= 9):
inCell = False
# dividing cols
if(c >= 5 and c <= 9):
inCell = False
# try to even out the random extremes, by increasing the odds if we miss.
if (inCell):
doDelete = (random.random() > inper)
if doDelete: #delete knight
inper += .3
else: #keep knight
inper -= .10
else:
doDelete = (random.random() > outper)
if doDelete: # delete knight
outper += .25
else: # keep knight
outper -= .5
#print doDelete
print("P4S incell=",inCell, doDelete, (inper, outper))
return (doDelete, (inper, outper))
def placeKnightsSpots(self,r,c):
'''
012345678
0
1 x x x
2
3 x x x
4
5 x x x
6
7 x x x
8
'''
if (r%3==1 and c%3 == 1):
return True
else:
return False
def createCellsGame(self,filePath,rowHeight=40,colWidth=40):
xOrigin = colWidth/2
yOrigin = rowHeight/2
print("Creating Cells Knight's Tour:",filePath,self.rows,self.cols,rowHeight,colWidth,xOrigin,yOrigin)
self.deleteAllBorders()
self.deleteAllTerritories()
self.deleteAllContinents()
self.setBoardName("Knight's Tour Cells" + str(self.rows) + "x" + str(self.cols))
if (self.rows%5 != 0 ):
return -1;
self.createTerritories(xOrigin, yOrigin, rowHeight, colWidth)
self.addBordersViaRegex()
self.createBlockContinents(1)
print("Deleting Territories", end=' ')
territoriesDeleted = []
#Set knights on all edges.
for r in range(self.rows):
c=0
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
#print r,c," ",
c = self.cols-1
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
#print r,c," ",
for c in range(self.cols):
r=0
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
#print r,c," ",
r = self.rows-1
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
#print r,c," ",
# Draw two cols of knights at 4,5,9,10,14,15,...
# with one open spot on the top half & one in the bottom half.
for c in range(4,self.cols,5):
print("")
rSkip = random.randint(1, self.rows/2-2)
for r in range(1,self.rows/2-1):
if r != rSkip:
#print r,c," ",
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
rSkip = random.randint(self.rows/2+1, self.rows-2)
for r in range(self.rows/2+1,self.rows-1):
if r != rSkip:
#print r,c," ",
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
c=c+1
rSkip = random.randint(1, self.rows/2-2)
for r in range(1,self.rows/2-1):
if r != rSkip:
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
rSkip = random.randint(self.rows/2+1, self.rows-2)
for r in range(self.rows/2+1,self.rows-1):
if r != rSkip:
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
# Draw two rows of knights halfway through the board
# with 90% coverage.
r = self.rows/2-1;
for c in range(self.cols):
if (random.random() < 0.9):
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
r = self.rows/2;
for c in range(self.cols):
if (random.random() < 0.9):
self.deleteTerritory(self.getTerritoryName(r,c))
territoriesDeleted.append([r, c])
self.deleteEmptyContinents()
self.setAllSoleContinentTerritoriesToNeutral()
self.setNumAttacks(int(ceil(sqrt(self.rows*self.cols))))
self.setNumFortifies(int(ceil(sqrt(sqrt(self.rows*self.cols)))))
self.createPNG(filePath, self.rows, self.cols, rowHeight, colWidth,
xOrigin, yOrigin, territoriesDeleted)
self.save_map_to_file(filePath + ".xml")
if (self.checkOneTerritoryCanReachAll() == False):
return False
return True
def createTerritories(self, xOrigin=10, yOrigin=10, xOffset=20, yOffset=20, blackXOffset=-3, blackYOffset=-7, whiteXOffset=3, whiteYOffset=-7):
'''
Args:
rows,cols (int): The number of rows/columns of territories.
xOrigin,yOrigin (int): The position of the upper left grid box.
xOffset,yOffset (int): The width of the grid boxes.
'''
xpos = xOrigin
for col in range(self.cols):
ypos = yOrigin
for row in range(self.rows):
territoryName = self.getTerritoryName(row,col)
if ((row+col) % 2 == 0): #black square
tx = xpos + blackXOffset
ty = ypos + blackYOffset
else:
tx = xpos + whiteXOffset
ty = ypos + whiteYOffset
self.addTerritory(territoryName, str(tx), str(ty))
ypos += yOffset
xpos += xOffset
def genGeoNeighbors(self,territoryID):
name = self.get_territory_name_from_ID(territoryID)
(row,col) = name.split("_")
print(name,row,col)
for x in self.genGeoNeighborsRC(int(row),int(col)):
yield x
def genGeoNeighborsRC(self,row,col):
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col+1))
if (neighbor != None):
yield neighbor
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col-1))
if (neighbor != None):
yield neighbor
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row+1,col))
if (neighbor != None):
yield neighbor
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row-1,col))
if (neighbor != None):
yield neighbor
def getRCFromID(self,territoryID):
name = self.get_territory_name_from_ID(territoryID)
(row,col) = name.split("_")
return [row,col]
def getGeoNeighbors(self,territoryID):
(row,col) = self.getRCFromID(territoryID)
#print name,row,col
ret = []
for x in self.genGeoNeighborsRC(int(row),int(col)):
ret.append(x)
return ret
def getGeoNeighborsRC(self,row,col):
ret = []
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col+1))
if (neighbor != None):
ret.append(neighbor)
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col-1))
if (neighbor != None):
ret.append(neighbor)
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row+1,col))
if (neighbor != None):
ret.append(neighbor)
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row-1,col))
if (neighbor != None):
ret.append(neighbor)
return ret
def getNumGeoNeigbors(self, row,col):
neighbors = 0
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col+1))
if (neighbor != None):
neighbors += 1
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col-1))
if (neighbor != None):
neighbors += 1
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row+1,col))
if (neighbor != None):
neighbors += 1
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row-1,col))
if (neighbor != None):
neighbors += 1
return neighbors
def addChainContinents(self,baseIDSet,value=1): #,length=2):
'''
# this function adds continent chains. Just works for length=2 now
'''
chains = set()
for base in baseIDSet:
ggn = self.getGeoNeighbors(base)
#print ggn
for neighbor in ggn:
chain = frozenset([neighbor, base])
chains.add(chain)
print(chains)
for chain in chains:
name = "chain"
members = set()
for link in chain:
name += "." + self.get_territory_name_from_ID(link)
members.add(link)
#print "adding",name
self.addContinent(name,members,bonus=value)
def removeUselessTerritories(self):
t2d = []
allTerritories = self.getTerritoryIDsFromNameRegex(".*")
for territory in allTerritories:
#print "numcont",self.getContinentsWithTerritory(territory)
if self.getBorderCount(territory) < 2 and len(self.getContinentsWithTerritory(territory)) < 1:
#print "delete2", territory
t2d.append(list(map(int,self.getRCFromID(territory))))
self.deleteTerritory(territory)
#print "T2d",t2d
return t2d
def isIsthmus(self, row,col):
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col+1))
#print "neighbor",neighbor
if (neighbor != None):
#print "bc",self.getNumGeoNeigbors(row,col+1)
if (self.getNumGeoNeigbors(row,col+1) == 1):
return True
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col-1))
#print "neighbor",neighbor
if (neighbor != None):
#print "bc",self.getNumGeoNeigbors(row,col-1)
if (self.getNumGeoNeigbors(row,col-1) == 1):
return True
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row+1,col))
#print "neighbor",neighbor
if (neighbor != None):
#print "bc",self.getNumGeoNeigbors(row+1,col)
if (self.getNumGeoNeigbors(row+1,col) == 1):
return True
neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row-1,col))
#print "neighbor",neighbor
if (neighbor != None):
#print "bc",self.getNumGeoNeigbors(row-1,col)
if (self.getNumGeoNeigbors(row-1,col) == 1):
return True
return False
def createRandomGame(self, filePath, rowHeight=45, colWidth=45,
percentDeadSquares=.45, xOrigin=-1, yOrigin=-1):
'''
returns True if a board was created, False if board creation failed.
'''
if xOrigin < 0:
xOrigin = colWidth/2
if yOrigin < 0:
yOrigin = rowHeight/2
print("Creating Random Knight's Tour:",filePath,self.rows,self.cols,rowHeight,colWidth,percentDeadSquares,xOrigin,yOrigin)
self.setBoardName("Knight's Tour " + str(self.rows) + "x" + str(self.cols))
self.deleteAllBorders()
self.deleteAllTerritories()
self.deleteAllContinents()
#self.printDOM()
self.createTerritories(xOrigin, yOrigin, rowHeight, colWidth, blackXOffset=-3, blackYOffset=-7,whiteXOffset=3, whiteYOffset=-7)
self.addBordersViaRegex()
#self.createBlockContinents(1)
#print "here1",self.DOM.toprettyxml()
print("Deleting Territories")
territoriesToDelete = floor(percentDeadSquares*self.rows*self.cols)
territoriesDeleted = []
while(territoriesToDelete > 0):
# todo: Can we sort territories in some way to make good boards more likely?
#
row = random.randint(0, self.rows-1)
col = random.randint(0, self.cols-1)
# don't use this square if it is one of the two that
# attack a corner square
if ((row == 1 and col == 2) or
(row == 2 and col == 1) or
(row == self.rows-2 and col == self.cols-3) or
(row == self.rows-3 and col == self.cols-2) or
(row == 1 and col == self.cols-3) or
(row == 2 and col == self.cols-2) or
(row == self.rows-2 and col == 2) or
(row == self.rows-3 and col == 1)):
#print "skipping",row,col
continue
# Also don't use this square if it would create a continent with only
# one territory in it.
#if self.getMinContinentSize(self.getTerritoryName(row,col)) <= 2:
#continue
# don't delete if it would leave a lone territory
if self.isIsthmus(row,col):
continue
if (self.deleteTerritory(self.getTerritoryName(row,col))):
territoriesToDelete -= 1
territoriesDeleted.append([row, col])
#print ".",
#self.printDOM()
allTerritories = self.getTerritoryIDsFromNameRegex(".*")
#self.setAllSoleContinentTerritoriesToNeutral(neutralBase=2,neutralMultiplier=3)
if (self.checkOneTerritoryCanReachAll() == False):
return False
self.addChainContinents(allTerritories)
self.setNumAttacks(int(ceil(sqrt(self.rows*self.cols))))
self.setNumFortifies(int(ceil(sqrt(sqrt(self.rows*self.cols)))))
#print filePath + ".xml"
self.addViewBordersToNeighbors(2)
#print filePath + ".xml"
self.createPNGs(filePath, rowHeight, colWidth,
xOrigin, yOrigin, territoriesDeleted)
#print filePath + ".xml"
self.save_map_to_file(filePath + ".xml")
print(filePath + ".xml")
#print filePath + ".xml"
return True
# @todo: territoriesDeleted needs to be handled
# @todo: change putpixel to the faster way
def createPNGs(self, filePath, rowHeight, colWidth,
xOrigin, yOrigin, territoriesDeleted):
'''
Creates a PNG of a chessboard w/knights in dead squares..
'''
imBoard = Image.new("RGBA", ( self.cols*colWidth,self.rows*rowHeight), (0, 0, 0, 0))
imFill = Image.new("RGB", ( self.cols*colWidth,self.rows*rowHeight), "white")
imFog = Image.new("RGB", ( self.cols*colWidth,self.rows*rowHeight), "white")
#print "create new RGB image:", self.cols*colWidth,self.rows*rowHeight
# create fill image
borderSize = 1; #borderSize = 3; #borders are actually twice this.
for x in range(self.cols*colWidth):
for y in range(self.rows*rowHeight):
row = floor(y/rowHeight)
col = floor(x/colWidth)
#print x,y
#print im.getpixel((x,y))
#print "putpixel",x,y
if ((row+col) % 2 == 0): #black square
if (x - col*colWidth) >= borderSize and (y - row*rowHeight) >= borderSize and ((col+1)*colWidth - x) > borderSize and ((row+1)*rowHeight - y) > borderSize:
imFill.putpixel((x,y), (1, 1, 1)) #off color centers
else:
imFill.putpixel((x, y), (0, 0, 0))
else: #white square
if (x - col*colWidth) >= borderSize and (y - row*rowHeight) >= borderSize and ((col+1)*colWidth - x) > borderSize and ((row+1)*rowHeight - y) > borderSize:
imFill.putpixel((x,y), (254, 254, 254)) #off color centers
else:
imFill.putpixel((x, y), (255, 255, 255))
print("pasting")
#paste in the un/deleted territories to the Board Image
knightBorder=0
print("territoriesDeleted", territoriesDeleted)
for col in range(self.cols):
for row in range(self.rows):
print(territoriesDeleted)
print([ (r,c) for (r,c) in territoriesDeleted])
found = [(r,c) for (r,c) in territoriesDeleted if r == row if c == col]
print("found",found)
if found:
print("found hole!")
if ((row+col) % 2 == 0): #black square
tileImage = self.holeDarkImage
else:
tileImage = self.holeLightImage
else:
if ((row+col) % 2 == 0): #black square
tileImage = self.playerDarkImage
#tileFImage = self.playerDarkFogImage
else:
tileImage = self.playerLightImage
#tileFImage = self.playerLightFogImage
px = col*colWidth + knightBorder
py = row*rowHeight + knightBorder
imBoard.paste(tileImage, (px, py))
#fog now
for col in range(self.cols):
for row in range(self.rows):
if ((row+col) % 2 == 0): #black square
tileImage = self.darkFogImage
else:
tileImage = self.lightFogImage
px = col*colWidth
py = row*rowHeight
imFog.paste(tileImage, (px, py))
imBoard.save(filePath + "-BOARD.png")
imFill.save(filePath + "-FILLFOG.png")
imFog.save(filePath+ "-FOG.png")
def create1LayerPNG(self, filePath, rowHeight, colWidth,
xOrigin, yOrigin, territoriesDeleted):
'''
Creates a PNG of a chessboard w/knights in dead squares..
'''
im = Image.new("RGB", ( self.cols*colWidth,self.rows*rowHeight), "white")
#print "create new RGB image:", self.cols*colWidth,self.rows*rowHeight
# @todo: these should all be rowHeight
borderSize = 10; #borderSize = 3; #borders are actually twice this.
for x in range(self.cols*colWidth):
for y in range(self.rows*rowHeight):
row = floor(y/rowHeight)
col = floor(x/colWidth)
#print x,y
#print im.getpixel((x,y))
#print "putpixel",x,y
if ((row+col) % 2 == 0): #black square
if (x - col*colWidth) >= borderSize and (y - row*rowHeight) >= borderSize and ((col+1)*colWidth - x) > borderSize and ((row+1)*rowHeight - y) > borderSize:
im.putpixel((x,y), (1, 1, 1)) #off color centers
else:
im.putpixel((x, y), (0, 0, 0))
else: #white square
if (x - col*colWidth) >= borderSize and (y - row*rowHeight) >= borderSize and ((col+1)*colWidth - x) > borderSize and ((row+1)*rowHeight - y) > borderSize:
im.putpixel((x,y), (245, 254, 254)) #off color centers
else:
im.putpixel((x, y), (255, 255, 255))
#paste in the knight icons to the deleted territories
knightborder=3
for territory in territoriesDeleted:
(delr, delc) = territory
px = delc*colWidth
py = delr*rowHeight
if ((delr+delc) % 2 == 0): #black square
im.paste(self.whiteKnightImage, (px+knightborder, py+knightborder))
else:
im.paste(self.blackKnightImage, (px+knightborder, py+knightborder))
# add some text
#draw = ImageDraw.Draw(im)
# use a truetype font
#font = ImageFont.truetype("//BHO/data/wargear development/scripts/nayla.ttf", 15)
#draw.text((10, 25), "TESETING world TSETING", font=font)
im.save(filePath + ".png")
# todo: maybe create knights tour config object
def setKnightIcons(self, wkpath, bkpath):
'''
setter
TODO: It would be great if rowHeight & colWidth were class members (instance variables), and were set
by the width/height of these images.
'''
self.whiteKnightImage = Image.open(wkpath)
self.blackKnightImage = Image.open(bkpath)
def setMoreKnightIcons(self, playerLightImagePath, playerDarkImagePath, holeLightPath, holeDarkPath):
'''
setter
TODO: It would be great if rowHeight & colWidth were class members (instance variables), and were set
by the width/height of these images.
'''
self.holeDarkImage = Image.open(holeDarkPath)
self.holeLightImage = Image.open(holeLightPath)
self.playerDarkImage = Image.open(playerDarkImagePath)
self.playerLightImage = Image.open(playerLightImagePath)
def setMoreerKnightIcons(self, playerLightImagePath, playerDarkImagePath, holeLightPath, holeDarkPath, lightImageFogPath, darkImageFogPath, ):
'''
setter
TODO: It would be great if rowHeight & colWidth were class members (instance variables), and were set
by the width/height of these images.
'''
self.holeDarkImage = Image.open(holeDarkPath)
self.holeLightImage = Image.open(holeLightPath)
self.playerDarkImage = Image.open(playerDarkImagePath)
self.playerLightImage = Image.open(playerLightImagePath)
self.darkFogImage = Image.open(darkImageFogPath)
self.lightFogImage = Image.open(lightImageFogPath)
def addKnightViewBorders():
wgmap = WGMap()
wgmap.load_map_from_file('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 20x20 - Random.xml')
wgmap.addViewBordersToNeighbors(2)
wgmap.save_map_to_file('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 20x20 - Random - Out.xml', False)
'''
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 25x4 - Random.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 25x4 - Random - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 26x6 - Stripes.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 26x6 - Stripes - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 25x6 - Random.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 25x6 - Random.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 20x21 - Cells.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 20x21 - Cells - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 20x20 - Snake.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 20x20 - Snake - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 19x19 - Grid.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 19x19 - Grid - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Stripes.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Stripes - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Snake.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Snake - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Random.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Random - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Grid.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x16 - Grid - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x15 - Cells.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 16x15 - Cells - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 13x13 - Grid.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 13x13 - Grid - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 12x12 - Snake.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 12x12 - Snake - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 12x12 - Random.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 12x12 - Random - Out.xml',False)
wgmap.loadMapFromFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 10x10 - Random.xml')
wgmap.addViewBordersTo2Neighbors()
wgmap.saveMapToFile('//DISKSTATION/data/wargear development/Knights Tour/Knights Tour NxN - 10x10 - Random - Out.xml',False)
'''
def createRandomKnightTour():
''' Create a Knight Tour's map '''
wgmap = KnightWGMap()
wgmap.load_map_from_file('//DISKSTATION/data/wargear development/Knights Tour/Knight\'s Tour(1).xml')
# wgmap.setKnightIcons('//DISKSTATION/data/wargear development/Knights Tour/WhiteKnightIcon34.png',
# '//DISKSTATION/data/wargear development/Knights Tour/BlackKnightIcon34.png')
wgmap.setKnightIcons('//DISKSTATION/data/wargear development/Knights Tour/Parquet1.png',
'//DISKSTATION/data/wargear development/Knights Tour/Parquet2.png')
wgmap.setMoreKnightIcons( '//DISKSTATION/data/wargear development/Knights Tour/PlayerKnightLightWood45.png',
'//DISKSTATION/data/wargear development/Knights Tour/PlayerKnightDarkWood45.png',
'//DISKSTATION/data/wargear development/Knights Tour/WoodWhite45.png',
'//DISKSTATION/data/wargear development/Knights Tour/WoodBlack45.png'
)
wgmap.setMoreerKnightIcons( '//DISKSTATION/data/wargear development/Knights Tour/PlayerKnightLightWood45.png',
'//DISKSTATION/data/wargear development/Knights Tour/PlayerKnightDarkWood45.png',
'//DISKSTATION/data/wargear development/Knights Tour/DeadKnightLight.png',
'//DISKSTATION/data/wargear development/Knights Tour/DeadKnightDark.png',
'//DISKSTATION/data/wargear development/Knights Tour/WoodWhite45.png',
'//DISKSTATION/data/wargear development/Knights Tour/WoodBlack45.png'
)
wgmap.setMoreerKnightIcons( '//DISKSTATION/data/wargear development/Knights Tour/PlayerKnightLightWood45.png',
'//DISKSTATION/data/wargear development/Knights Tour/PlayerKnightDarkWood45.png',
'//DISKSTATION/data/wargear development/Knights Tour/WoodWhite45.png',
'//DISKSTATION/data/wargear development/Knights Tour/WoodBlack45.png' ,
'//DISKSTATION/data/wargear development/Knights Tour/WoodBlack45.png',
'//DISKSTATION/data/wargear development/Knights Tour/WoodWhite45.png'
)
wgmap.rows = 16
wgmap.cols = 32