-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettlers of Catan (with AI).t
1943 lines (1880 loc) · 66.5 KB
/
Settlers of Catan (with AI).t
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
setscreen ("graphics:1020,1000")
var c : int %used for assigning resources to the hexes
var x : int := 260 %initial position of the first hexagon in the board
var y : int := 105
var l : int := 100 %the length of the hexagon
var n : int := 0 %used for assigning number to the hexes
var pics : array 1 .. 37 of int
var colours : array 1 .. 19 of int := init (1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6) %array of resources
var numbers : array 1 .. 19, 1 .. 2 of int := init (1, 5, 3, 2, 6, 6, 11, 3, 16, 8, 18, 10, 19, 9, 17, 12, 14, 11, 9, 4, 4, 8, 2, 10, 5, 9, 8, 4, 13, 5, 15, 6, 12,
3, 7, 11, 10, 0) %the numbers for each hex and their order: first order, second number
/* order of the hexagons
19
17 18
14 15 16
12 13
9 10 11
7 8
4 5 6
2 3
1 */
var vertices : array 1 .. 54, 1 .. 4 of int % 3rd index: 1. red settlement 2. red city 3. blue settlement 4. blue city, 4th is either 1 or 0 for the harbors
var hexagons : array 1 .. 19, 1 .. 9 of int % 1) x 2) y 3) colour 4) number(dice) 5th index: 1. red settlement 2. red city 3. blue settlement 4. blue city 6) robber
var roads:array 1..72,1..5 of int % 1) x of beginning 2) y of beginiing 3) x of end 4) y of end 5) colour
var sides:array 1..72,1..4 of int %keeps track of the position of all the sides
var harbors:array 1..9 of int:=init(1,2,3,4,5,6,6,6,6) %grain, ore, brick, wood, sheep, anything
var players:array 1..2 of int:=init(64,79) %keeps track of the colour of the roads (red and blue)
var resources:array 1..5 of int:=init(19,19,19,19,19) % wood, sheep, wheet, brick, metal
var rs:array 1..5 of string:=init("wood","wool","grain","clay","ore") % wood, sheep, wheet, brick, metal
var p1_hand:array 1..18 of int:=init(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) % wood, sheep, wheet, brick, metal, knight, victory point, monopoly, year of plenty, road building, harbours 1 to 6,largest_army, longest_road
var p2_hand:array 1..18 of int:=init(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) % wood, sheep, wheet, brick, metal, knight, victory point, monopoly, year of plenty, road building, harbours 1 to 6,largest_army, longest_road
var victory_cards:array 1..27 of int:=init(1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,4,4,5,5,6,7) %14 knights, 5 victory cards, 2 monopoly, 2 year of plenty, 2 road building, largest army, longest road
var init_resources:array 1..5 of int:=init(0,0,0,0,0) %used for assigning resources to the players after they put their last settlement
var order:array 1..5 of int:=init(2,1,4,3,5) %the order of signifance of for the resources
var used:array 1..72 of boolean %used to keep track of the used roads for the longest road
var dice:array 1..2 of int:=init(4,4) %used for the dice
var army:array 1..2 of int:=init(-1,2) %person and number of knights
var long_road:array 1..2 of int:=init(-1,4) %person and number of roads
var ind, res, upp, upp2,hex_count, desert_flag,turn : int := 0
var mousex, mousey, button : int
var road_up,build,road,p1_army_count,p2_army_count,p1_road_count,p2_road_count,r_count,s_count:int:=0 %r_count and s_count is used for initial placements, The rest is used for longest road and largest army
var p1_vp,p2_vp,resource,road_build,play_one,init_build,last_settlement:int:=0
var v_top:int:=1 %counter of development cards
var test_mode:boolean:=false %used for testing
var comp_c,comp_s,comp_r:int:=0 %keeps track of computer's roads, settlements, and cities
proc load_images %loads the pictures
pics (1) := Pic.FileNew ("images/wood.bmp")
pics (2) := Pic.FileNew ("images/sheep.bmp")
pics (3) := Pic.FileNew ("images/wheet.bmp")
pics (4) := Pic.FileNew ("images/brick.bmp")
pics (5) := Pic.FileNew ("images/metal.bmp")
pics (6) := Pic.FileNew ("images/desert.bmp")
pics (7) := Pic.FileNew ("images/rubber.bmp")
pics (8) := Pic.FileNew ("images/red_settlement.bmp")
pics (9) := Pic.FileNew ("images/red_city.bmp")
pics (10) := Pic.FileNew ("images/blue_settlement.bmp")
pics (11) := Pic.FileNew ("images/blue_city.bmp")
pics (12) := Pic.FileNew ("images/magnifier.bmp")
pics (13) := Pic.FileNew ("images/building_costs.bmp")
pics (14) := Pic.FileNew ("images/harbor_1.bmp")
pics (15) := Pic.FileNew ("images/harbor_2.bmp")
pics (16) := Pic.FileNew ("images/harbor_3.bmp")
pics (17) := Pic.FileNew ("images/harbor_4.bmp")
pics (18) := Pic.FileNew ("images/harbor_5.bmp")
pics (19) := Pic.FileNew ("images/harbor_6.bmp")
pics (20) := Pic.FileNew ("images/wood_card.bmp")
pics (21) := Pic.FileNew ("images/wool_card.bmp")
pics (22) := Pic.FileNew ("images/grain_card.bmp")
pics (23) := Pic.FileNew ("images/brick_card.bmp")
pics (24) := Pic.FileNew ("images/ore_card.bmp")
pics (25) := Pic.FileNew ("images/knight.bmp")
pics (26) := Pic.FileNew ("images/victory_point_1.bmp")
pics (27) := Pic.FileNew ("images/monopoly.bmp")
pics (28) := Pic.FileNew ("images/year_of_plenty.bmp")
pics (29) := Pic.FileNew ("images/road_building.bmp")
pics (30) := Pic.FileNew ("images/largest_army.bmp")
pics (31) := Pic.FileNew ("images/longest_road.bmp")
pics (32) := Pic.FileNew ("images/1.bmp")
pics (33) := Pic.FileNew ("images/2.bmp")
pics (34) := Pic.FileNew ("images/3.bmp")
pics (35) := Pic.FileNew ("images/4.bmp")
pics (36) := Pic.FileNew ("images/5.bmp")
pics (37) := Pic.FileNew ("images/6.bmp")
end load_images
function find_colour (f : string) : int %assigns a resource to each hexagon
if f = "i" then
loop
ind := Rand.Int (1, 19)
exit when colours (ind) ~= 0
end loop
res := colours (ind)
colours (ind) := 0
else
res := hexagons (hex_count, 3)
end if
result res
end find_colour
proc add_vertex (x_v, y_v : int, var upp : int,f:string) %keeps track of all the vertices in an array and adds the vertices
var flag : int := 0
for i : 1 .. upp
if abs (vertices (i, 1) - x_v) < 5 and abs (vertices (i, 2) - y_v) < 5 then
flag := 1
end if
end for
if flag = 0 then
upp += 1
vertices (upp, 1) := x_v
vertices (upp, 2) := y_v
if f="i" then
vertices(upp,3):=0
vertices(upp,4):=0
end if
end if
end add_vertex
proc add_side(x1,y1,x2,y2:int,var upp2:int) %keeps track of all the sides in an array and adds the sides
var flag2:int:=0
for i:1..upp2
if (abs (sides (i, 1) - x1) < 5 and abs (sides (i, 2) - y1) < 5 and abs (sides (i, 3) - x2) < 5 and abs (sides (i, 4) - y2) < 5) or (abs (sides (i, 1) - x2) < 5 and abs (sides (i, 2) - y2) < 5 and abs (sides (i, 3) - x1) < 5 and abs (sides (i, 4) - y1) < 5) then
flag2:=1
end if
end for
if flag2=0 then
upp2+=1
sides(upp2,1):=x1
sides(upp2,2):=y1
sides(upp2,3):=x2
sides(upp2,4):=y2
end if
end add_side
proc find_number(f:string) %assigns the number to each hex
for i : 1 .. 19
if hexagons (numbers (i, 1), 3) ~= 6 then
%draws the number
Draw.FillOval (hexagons (numbers (i, 1), 1), hexagons (numbers (i, 1), 2), 25, 25, white)
Draw.Text (intstr (numbers (i - desert_flag, 2)), hexagons (numbers (i, 1), 1) - 7 * (numbers (i - desert_flag, 2) div 10 + 1), hexagons (numbers (i, 1), 2) - 7, Font.New ("arial:18"),
9)
hexagons (numbers (i, 1), 4) := numbers (i - desert_flag, 2)
if f="i" then %in the initial phase assigns zero for robber of all resources except desert
hexagons(numbers(i,1),9):=0
end if
elsif hexagons (numbers (i, 1), 3) = 6 then %keeps track of the robber and the desert
desert_flag := 1
hexagons (numbers (i, 1), 4) := 0
if f="i" then
Pic.Draw (pics (7), hexagons (numbers (i, 1), 1)+10, hexagons (numbers (i, 1), 2) - 44, picMerge)
hexagons(numbers(i,1),9):=1
end if
end if
if f~="i" then
if hexagons(numbers(i,1),9)=1 then
Pic.Draw (pics (7), hexagons (numbers (i, 1), 1)+10, hexagons (numbers (i, 1), 2) - 44, picMerge)
end if
end if
end for
end find_number
proc draw_hex (n_x, n_y, l, c : int,f:string) %draws a hexagon
var last_x : int := n_x
var last_y : int := n_y
add_vertex (last_x, last_y, upp,f)
var a : int := 0
Pic.Draw (pics (c), n_x - l div 2, n_y, picMerge)
for i : 1 .. 6 %draws a line and adds 60 degrees
Draw.ThickLine (last_x, last_y, floor (last_x + cosd (a) * l), floor (last_y + sind (a) * l), 15, black)
add_side(last_x, last_y, floor (last_x + cosd (a) * l), floor (last_y + sind (a) * l),upp2)
last_x := floor (last_x + cosd (a) * l)
last_y := floor (last_y + sind (a) * l)
add_vertex (last_x, last_y, upp,f)
a += 60
end for
end draw_hex
proc draw_board (f : string) %draws the board
Draw.FillBox (0, 0, 1000, 1000, 54)
if f="i" then %this is only for initialzing
for i:1..19 %sets all the buildings for all hexes to zero
hexagons(i,5):=0
hexagons(i,6):=0
hexagons(i,7):=0
hexagons(i,8):=0
end for
for i:1..72 %the colour for all the roads is zero
roads(i,5):=0
end for
var ind1,ind2,ind3:int
for i:1..9 %shuffles the array of the harbours so they will be random
ind1:=Rand.Int(1,9)
ind2:=Rand.Int(1,9)
ind3:=harbors(ind1)
harbors(ind1):=harbors(ind2)
harbors(ind2):=ind3
end for
for i:1..25 %shuffles the development cards
ind1:=Rand.Int(1,25)
ind2:=Rand.Int(1,25)
ind3:=victory_cards(ind1)
victory_cards(ind1):=victory_cards(ind2)
victory_cards(ind2):=ind3
end for
end if
Pic.Draw (pics (harbors(1)+13),x, y-100, picMerge) %draws all the pictures of the harbors
Pic.Draw (pics (harbors(2)+13),x+3*l+20, y-100, picMerge)
Pic.Draw (pics (harbors(3)+13),x-3*l div 2, y+640, picMerge)
Pic.Draw (pics (harbors(4)+13),x+5*l div 2+50, y+720, picMerge)
Pic.Draw (pics (harbors(5)+13),-10, 330, picMerge)
Pic.Draw (pics (harbors(6)+13),-10, 520, picMerge)
Pic.Draw (pics (harbors(7)+13),x+6*l+10, 320, picMerge)
Pic.Draw (pics (harbors(8)+13),x+6*l+10, 500, picMerge)
Pic.Draw (pics (harbors(9)+13),x, y+710, picMerge)
%draws the hexagons draws the bottom one first : 1
upp := 0
desert_flag := 0
var y2 : int := y
hex_count := 1
c := find_colour (f)
hexagons (1, 1) := x + 2 * l
hexagons (1, 2) := y
hexagons (1, 3) := c
draw_hex (x + 3 * l div 2, y - floor (sqrt (3) * l / 2), l, c,f)
hex_count += 1
/* order of the hexagons
17 18
12 13
7 8
2 3 */
for i : 1 .. 4 %draws the middle column first
c := find_colour (f)
hexagons (hex_count, 1) := x + l div 2
hexagons (hex_count, 2) := y2 + floor (sqrt (3) * l) div 2
hexagons (hex_count, 3) := c
draw_hex (x, y2, l, c,f)
hex_count += 1
c := find_colour (f)
hexagons (hex_count, 1) := x + 7 * l div 2
hexagons (hex_count, 2) := y2 + floor (sqrt (3) * l) div 2
hexagons (hex_count, 3) := c
draw_hex (x + 3 * l, y2, l, c,f)
hex_count += 4
y2 += floor (sqrt (3) * l)
end for
hex_count := 4
y2 := y + floor (sqrt (3) * l / 2)
/* order of the hexagons
14 15 16
9 10 11
4 5 6 */
for i : 1 .. 3 %then it draws the wider column
c := find_colour (f)
hexagons (hex_count, 1) := x - l
hexagons (hex_count, 2) := y2 + floor (sqrt (3) * l) div 2
hexagons (hex_count, 3) := c
draw_hex (x - 3 * l div 2, y2, l, c,f)
hex_count += 1
c := find_colour (f)
hexagons (hex_count, 1) := x + 2 * l
hexagons (hex_count, 2) := y2 + floor (sqrt (3) * l) div 2
hexagons (hex_count, 3) := c
draw_hex (x + 3 * l div 2, y2, l, c,f)
hex_count += 1
c := find_colour (f)
hexagons (hex_count, 1) := x + 5 * l
hexagons (hex_count, 2) := y2 + floor (sqrt (3) * l) div 2
hexagons (hex_count, 3) := c
draw_hex (x + 9 * l div 2, y2, l, c,f)
hex_count += 3
y2 += floor (sqrt (3) * l)
end for
c := find_colour (f) %then draws the last hexagons: 19
hexagons (19, 1) := x + 2 * l
hexagons (19, 2) := y2 + floor (sqrt (3) * l) div 2
hexagons (19, 3) := c
draw_hex (x + 3 * l div 2, y2, l, c,f)
find_number(f)
%draws lines that conncet all the harbors
Draw.ThickLine(vertices(6,1),vertices(6,2),x+40,y-40,12,66)
Draw.ThickLine(vertices(7,1),vertices(7,2),x+40,y-40,12,66)
Draw.ThickLine(vertices(3,1),vertices(3,2),x+3*l+60,y-40,12,66)
Draw.ThickLine(vertices(11,1),vertices(11,2),x+3*l+60,y-40,12,66)
Draw.ThickLine(vertices(49,1),vertices(49,2),x-3*l div 2+40,y+650,12,66)
Draw.ThickLine(vertices(34,1),vertices(34,2),x-3*l div 2+40,y+650,12,66)
Draw.ThickLine(vertices(54,1),vertices(54,2),x-3*l div 2+220,y+750,12,66)
Draw.ThickLine(vertices(32,1),vertices(32,2),x-3*l div 2+220,y+750,12,66)
Draw.ThickLine(vertices(36,1),vertices(36,2),x-3*l div 2+500,y+720,12,66)
Draw.ThickLine(vertices(37,1),vertices(37,2),x-3*l div 2+500,y+720,12,66)
Draw.ThickLine(vertices(51,1),vertices(51,2),x-3*l div 2+757,y+450,12,66)
Draw.ThickLine(vertices(48,1),vertices(48,2),x-3*l div 2+757,y+450,12,66)
Draw.ThickLine(vertices(43,1),vertices(43,2),x-3*l div 2+757,y+250,12,66)
Draw.ThickLine(vertices(44,1),vertices(44,2),x-3*l div 2+757,y+250,12,66)
Draw.ThickLine(vertices(40,1),vertices(40,2),x-3*l div 2-40,y+250,12,66)
Draw.ThickLine(vertices(41,1),vertices(41,2),x-3*l div 2-40,y+250,12,66)
Draw.ThickLine(vertices(50,1),vertices(50,2),x-3*l div 2-40,y+450,12,66)
Draw.ThickLine(vertices(45,1),vertices(45,2),x-3*l div 2-40,y+450,12,66)
%adds the harbors to the vertices that have a harbor on them
vertices(6,4):=harbors(1)
vertices(7,4):=harbors(1)
vertices(3,4):=harbors(2)
vertices(11,4):=harbors(2)
vertices(49,4):=harbors(3)
vertices(34,4):=harbors(3)
vertices(36,4):=harbors(4)
vertices(37,4):=harbors(4)
vertices(40,4):=harbors(5)
vertices(41,4):=harbors(5)
vertices(45,4):=harbors(6)
vertices(50,4):=harbors(6)
vertices(44,4):=harbors(7)
vertices(43,4):=harbors(7)
vertices(48,4):=harbors(8)
vertices(51,4):=harbors(8)
vertices(32,4):=harbors(9)
vertices(54,4):=harbors(9)
for i : 1 .. upp %draws the vertices
Draw.FillOval (vertices (i, 1), vertices (i, 2), 15, 15, 27)
end for
%draws the texts on the screen
Draw.Text ("Next Turn", 800,900, Font.New ("arial:18"),yellow)
Draw.Text ("Player 1's Victory Points: "+intstr(p1_vp), 10,950, Font.New ("arial:18"),yellow)
Draw.Text ("Player 2's Victory Points: "+intstr(p2_vp), 700,950, Font.New ("arial:18"),yellow)
Draw.Text ("Player " + intstr(turn mod 2+1)+"' s turn", 400,950, Font.New ("arial:18"),yellow)
Pic.Draw (pics (12),100, 835, picMerge)
end draw_board
function vertex_find(x,y:int):int %finds the index of a vertex for a given x and y
for i:1..54
if x=vertices(i,1) and y=vertices(i,2) then
result i
end if
end for
end vertex_find
function exists(n:int,var check:array 1..* of int):boolean %checks to see if a number exists in an array
for i:1..upper(check)
if check(i)=n then
result true
end if
end for
result false
end exists
proc display_board %display the board
draw_board("d") %draws the board in a display mode not initialized mode
if turn>2 then %shows the pictures of the dice
Pic.Draw (pics (31+dice(1)),746, 775, picMerge)
Pic.Draw (pics (31+dice(2)),872, 775, picMerge)
end if
for i:1..54 %draws all the settlements and all the cities
if vertices(i,3)=1 then
Pic.Draw (pics (8),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
elsif vertices(i,3)=2 then
Pic.Draw (pics (9),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
elsif vertices(i,3)=3 then
Pic.Draw (pics (10),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
elsif vertices(i,3)=4 then
Pic.Draw (pics (11),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
end if
end for
for i:1..road_up %draws all the roads
Draw.ThickLine (roads (i, 1)+30*(roads(i,3)-roads(i,1)) div l, roads (i, 2)+30*(roads(i,4)-roads(i,2)) div l, roads (i, 3)+30*(roads(i,1)-roads(i,3)) div l, roads (i, 4)+30*(roads(i,2)-roads(i,4)) div l, 15, roads(i,5))
end for
end display_board
function longest_road(n1,r:int):int %finds the length of a road for a given node
var n2:int:=0
if roads(r,1)=vertices(n1,1) and roads(r,2)=vertices(n1,2) then
n2:=vertex_find(roads(r,3),roads(r,4))
elsif roads(r,3)=vertices(n1,1) and roads(r,4)=vertices(n1,2) then
n2:=vertex_find(roads(r,1),roads(r,2))
end if
var current,best:int:=0
for i:1..72
if roads(i,5)=players(turn mod 2 +1) and used(i)=false and (vertices(n1,3)=0 or vertices(n1,3) div 3= turn mod 2) and (vertex_find(roads(i,1),roads(i,2))=n2 or vertex_find(roads(i,3),roads(i,4))=n2) then
used(i):=true
current:=1+longest_road(n2,i)
if current>best then
best:=current
end if
end if
end for
used(r):=false
result best
end longest_road
proc max_road %goes over all the roads to find the longest road
for i:1..72
used(i):=false
end for
var current:int:=0
for i:1..72
if roads(i,5)=players(turn mod 2 +1) and used(i)=false then
current:=longest_road(vertex_find(roads(i,1),roads(i,2)),i)
if current>p1_road_count and turn mod 2=0 then
p1_road_count:=current
elsif current>p2_road_count and turn mod 2=1 then
p2_road_count:=current
end if
current:=longest_road(vertex_find(roads(i,3),roads(i,4)),i)
if current>p1_road_count and turn mod 2=0 then
p1_road_count:=current
elsif current>p2_road_count and turn mod 2=1 then
p2_road_count:=current
end if
end if
end for
%finds punt which player has the longest road and gives them victory points
if p1_road_count>long_road(2) and turn mod 2=0 then
p1_hand(18):=1
if long_road(1)~=turn mod 2 then
if long_road(1)>=0 then
p2_hand(18):=0
p2_vp-=2
end if
p1_vp+=2
long_road(1):=turn mod 2
long_road(2):=p1_road_count
end if
elsif p2_road_count>long_road(2) and turn mod 2=1 then
p2_hand(18):=1
if long_road(1)~=turn mod 2 then
Draw.FillBox(0,0,1000,1000,28)
Draw.Text ("Computer got the longest road", 50,540, Font.New ("arial:18"),black)
delay(1000)
display_board
if long_road(1)>=0 then
p1_hand(18):=0
p1_vp-=2
end if
p2_vp+=2
long_road(1):=turn mod 2
long_road(2):=p2_road_count
end if
end if
end max_road
function vertex_valid (x1,y1,turn:int):boolean %checks to see if a settlement can be placed on a point
if turn<=2 then
for i:1..54
if sqrt ((vertices (i, 1) - x1) ** 2 + (vertices (i, 2) - y1) ** 2) <= l+10 and vertices(i,3)~=0 then
result false
end if
end for
result true
else
for i:1..54
if sqrt ((vertices (i, 1) - x1) ** 2 + (vertices (i, 2) - y1) ** 2) <= l+10 and vertices(i,3)~=0 then
result false
end if
end for
for i:1..road_up
if (x1=roads(i,1) and y1=roads(i,2) and roads(i,5)=players(turn mod 2+1)) or (x1=roads(i,3) and y1=roads(i,4) and roads(i,5)=players(turn mod 2+1)) then
result true
end if
end for
result false
end if
end vertex_valid
function check_vertex (x, y : int,var f:int,h:int) : boolean %puts down a settlement
%checks if they can pay for it
if (f=1 and p1_hand(1)>=1 and p1_hand(2)>=1 and p1_hand(3)>=1 and p1_hand(4)>=1) or (f=2 and p1_hand(3)>=2 and p1_hand(5)>=3) or (f=3 and p2_hand(1)>=1 and p2_hand(2)>=1 and p2_hand(3)>=1 and p2_hand(4)>=1) or (f=4 and p2_hand(3)>=2 and p2_hand(5)>=3) or turn<=2 then
for i : 1 .. 54
if abs (vertices (i, 1) - x) <= 20 and abs (vertices (i, 2) - y) <= 20 then %finds the vertex based on distance
if vertex_valid(vertices(i,1),vertices(i,2),turn)=true or f=2 or f=4 then
if turn<=2 then
s_count+=1
end if
last_settlement:=i
init_build+=1
if f=1 then
if h=1 then %red settlement
Pic.Draw (pics (8),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
if turn>2 then
p1_hand(1)-=1 %takes resources from the players
p1_hand(2)-=1
p1_hand(3)-=1
p1_hand(4)-=1
resources(1)+=1
resources(2)+=1
resources(3)+=1
resources(4)+=1
end if
p1_vp+=1
end if
elsif f=2 and vertices(i,3)=1 then %red city: for the cities it makes sure there is a settlement already there
if h=1 then
Draw.Oval(vertices(i,1),vertices(i,2),15,15,27)
Pic.Draw (pics (9),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
if turn>2 then
p1_hand(3)-=2 %takes resources from the players
p1_hand(5)-=3
resources(3)+=2
resources(5)+=3
end if
p1_vp+=1
end if
elsif f=3 then %blue settlement
if h=1 then
Pic.Draw (pics (10),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
if turn>2 then
p2_hand(1)-=1 %takes resources from the players
p2_hand(2)-=1
p2_hand(3)-=1
p2_hand(4)-=1
resources(1)+=1
resources(2)+=1
resources(3)+=1
resources(4)+=1
end if
p2_vp+=1
end if
elsif f=4 and vertices(i,3)=3 then %blue city: for the cities it makes sure there is a settlement already there
if h=1 then
Draw.Oval(vertices(i,1),vertices(i,2),15,15,27)
Pic.Draw (pics (11),vertices (i, 1)-20, vertices (i, 2)-20, picMerge)
if turn>2 then
p2_hand(3)-=2 %takes resources from the players
p2_hand(5)-=3
resources(3)+=2
resources(5)+=3
end if
p2_vp+=1
end if
else
result false
end if
if h=1 then
vertices(i,3):=f
if vertices(i,4)~=0 then
if turn mod 2=0 then
p1_hand(10+vertices(i,4)):=1
else
p2_hand(10+vertices(i,4)):=1
end if
end if
for j:1..19 %finds put which hexagons are connceted to that vertex
if sqrt ((vertices (i, 1) - hexagons (j, 1)) ** 2 + (vertices (i, 2) - hexagons (j, 2)) ** 2) <= floor(l*sqrt(3))+5 then
hexagons(j,4+f)+=1
if (init_build=1 and turn=2) or (init_build=2 and turn=1) and hexagons(j,3)<=5 then
init_resources(hexagons(j,3))+=1
end if
end if
end for
end if
result true
end if
end if
end for
end if
result false
end check_vertex
function road_valid(x1,x2,y1,y2,turn,road_up:int):boolean %it checks to see if a road can be placed there
var v1,v2:int
if turn<=2 then
if last_settlement=0 then
result false
end if
if (x1=vertices(last_settlement,1) and y1=vertices(last_settlement,2)) or (x2=vertices(last_settlement,1) and y2=vertices(last_settlement,2)) then
last_settlement:=0
result true
end if
result false
end if
for i:1..road_up
if x1=roads(i,1) and y1=roads(i,2) and x2=roads(i,3) and y2=roads(i,4) then
result false
end if
end for
for i:1..54
if x1=vertices(i,1) and y1=vertices(i,2) then
v1:=i
elsif x2=vertices(i,1) and y2=vertices(i,2) then
v2:=i
end if
end for
for i:1..road_up
if (x1=roads(i,1) and y1=roads(i,2) and roads(i,5)=players(turn mod 2+1)) or (x1=roads(i,3) and y1=roads(i,4) and roads(i,5)=players(turn mod 2+1)) or (x2=roads(i,1) and y2=roads(i,2) and roads(i,5)=players(turn mod 2+1)) or(x2=roads(i,3) and y2=roads(i,4) and roads(i,5)=players(turn mod 2+1)) then
if ((x1=roads(i,1) and y1=roads(i,2)) or (x1=roads(i,3) and y1=roads(i,4))) and vertices(v1,3)~=0 and turn mod 2~=vertices(v1,3) div 3 and vertices(v2,3)=0 then
result false
elsif ((x2=roads(i,1) and y2=roads(i,2)) or (x2=roads(i,3) and y2=roads(i,4))) and vertices(v2,3)~=0 and turn mod 2~=vertices(v2,3) div 3 and vertices(v1,3)=0 then
result false
end if
result true
end if
end for
for i:1..54
if vertices(i,3)~=0 and ((x1=vertices(i,1) and y1=vertices(i,2) and turn mod 2=vertices(i,3) div 3) or (x2=vertices(i,1) and y2=vertices(i,2)and turn mod 2=vertices(i,3) div 3)) then
result true
end if
end for
result false
end road_valid
function check_side (x, y : int,var road_up,f:int,h:int) : boolean
%checks to see if there are enough resources for a road to be built
if (turn mod 2=0 and p1_hand(1)>=1 and p1_hand(4)>=1) or (turn mod 2=1 and p2_hand(1)>=1 and p2_hand(4)>=1) or road_build>0 or turn<=2 then
for i : 1 .. 53
for j : i+1 .. 54
if sqrt ((vertices (i, 1) - x) ** 2 + (vertices (i, 2) - y) ** 2) <= l+2 and sqrt ((vertices (j, 1) - x) ** 2 + (vertices (j, 2) - y) ** 2) <= l+2 then
if road_valid(vertices (i, 1),vertices (j, 1),vertices (i, 2),vertices (j, 2),turn,road_up)=true then
if turn<=2 then
r_count+=1
end if
if h=1 then %draws a road based on player and cost
Draw.ThickLine (vertices (i, 1)+30*(vertices(j,1)-vertices(i,1)) div l, vertices (i, 2)+30*(vertices(j,2)-vertices(i,2)) div l, vertices (j, 1)+30*(vertices(i,1)-vertices(j,1)) div l, vertices (j, 2)+30*(vertices(i,2)-vertices(j,2)) div l, 15, players(turn mod 2 +1))
road_up+=1
roads(road_up,1):=vertices (i, 1)
roads(road_up,2):=vertices (i, 2)
roads(road_up,3):=vertices (j, 1)
roads(road_up,4):=vertices (j, 2)
roads(road_up,5):=players(turn mod 2 +1)
if turn mod 2=0 and road_build=0 and turn>2 then
p1_hand(1)-=1 %players pay based on resources
p1_hand(4)-=1
resources(1)+=1
resources(4)+=1
elsif turn mod 2=1 and road_build=0 and turn>2 then
p2_hand(1)-=1 %players pay based on resources
p2_hand(4)-=1
resources(1)+=1
resources(4)+=1
end if
if road_build>0 then
road_build-=1
end if
max_road
end if
result true
end if
end if
end for
end for
end if
result false
end check_side
proc largest_army %counts the number of army cards plays
if turn mod 2=0 then
p1_army_count+=1
p1_hand(6)-=1
else
p2_army_count+=1
p2_hand(6)-=1
Draw.FillBox(0,0,1000,1000,28)
Draw.Text ("Army Card was played", 50,540, Font.New ("arial:18"),black) %tells the other player that he played an army card
delay(1000)
end if
if p1_army_count>army(2) and turn mod 2=0 then %finds who has the largest army
p1_hand(17):=1
if army(1)~=turn mod 2 then
if army(1)>=0 then
p2_hand(17):=0
p2_vp-=2
end if
p1_vp+=2
army(1):=turn mod 2
army(2):=p1_army_count
end if
elsif p2_army_count>army(2) and turn mod 2=1 then
p2_hand(17):=1
if army(1)~=turn mod 2 then
Draw.FillBox(0,0,1000,1000,28)
Draw.Text ("Computer got the largest army", 50,540, Font.New ("arial:18"),black)
delay(1000)
if army(1)>=0 then
p1_hand(17):=0
p1_vp-=2
end if
p2_vp+=2
army(1):=turn mod 2
army(2):=p2_army_count
end if
end if
delay(300)
end largest_army
proc monopoly
%checks to see if the other player has enough resources for monopoly to be played
if (turn mod 2=0 and (p2_hand(1)>0 or p2_hand(2)>0 or p2_hand(3)>0 or p2_hand(4)>0 or p2_hand(5)>0)) or (turn mod 2=1 and (p1_hand(1)>0 or p1_hand(2)>0 or p1_hand(3)>0 or p1_hand(4)>0 or p1_hand(5)>0)) then
Draw.FillBox(0,0,1000,1000,28)
play_one:=1
if turn mod 2=0 then
p1_hand(8)-=1
else
p2_hand(8)-=1
end if
if turn mod 2=0 then
var x_c:int:=460
var y_c:int:=820
Draw.Text ("Pick one of your opponent's resources and take all of the resources for that card", 50,940, Font.New ("arial:18"),black)
for i:1..5
if turn mod 2=0 and p2_hand(i)>0 then
Pic.Draw (pics (19+i),x_c,y_c, picMerge)
Draw.Text ("* "+intstr(p2_hand(i)), x_c+100,y_c, Font.New ("arial:18"),black)
elsif turn mod 2=1 and p1_hand(i)>0 then
Pic.Draw (pics (19+i),x_c,y_c, picMerge)
Draw.Text ("* "+intstr(p1_hand(i)), x_c+100,y_c, Font.New ("arial:18"),black)
end if
y_c-=180
end for
y_c:=820
button:=0
resource:=0
loop
Mouse.Where(mousex,mousey,button) %waits for the player to pick a resource
if button=1 then
if mousex>=x_c and mousex<=x_c+70 and mousey>=y_c and mousey<=y_c+80 and ((turn mod 2=0 and p2_hand(1)>0)or(turn mod 2=1 and p1_hand(1)>0)) then
resource:=1
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-180 and mousey<=y_c-100 and ((turn mod 2=0 and p2_hand(2)>0)or(turn mod 2=1 and p1_hand(2)>0)) then
resource:=2
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-360 and mousey<=y_c-280 and ((turn mod 2=0 and p2_hand(3)>0)or(turn mod 2=1 and p1_hand(3)>0))then
resource:=3
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-540 and mousey<=y_c-460 and ((turn mod 2=0 and p2_hand(4)>0)or(turn mod 2=1 and p1_hand(4)>0))then
resource:=4
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-720 and mousey<=y_c-640 and ((turn mod 2=0 and p2_hand(5)>0)or(turn mod 2=1 and p1_hand(5)>0))then
resource:=5
exit
end if
end if
end loop
elsif turn mod 2=1 then
Draw.Text ("The monopoly card was chosen", 50,540, Font.New ("arial:18"),black)
loop %the computer picks a random resource for monopoly
resource:=Rand.Int(1,5)
exit when p1_hand(resource)>0
end loop
Draw.Text ("Computer got all the "+rs(resource)+" cards", 50,500, Font.New ("arial:18"),black)
delay(1000)
end if
if turn mod 2=0 then
p1_hand(resource)+=p2_hand(resource)
p2_hand(resource):=0
else
p2_hand(resource)+=p1_hand(resource)
p1_hand(resource):=0
end if
end if
end monopoly
proc year_of_plenty
Draw.FillBox(0,0,1000,1000,28)
play_one:=1
if turn mod 2=0 then
p1_hand(9)-=1
else
p2_hand(9)-=1
end if
if turn mod 2=0 then %checks to see if the computer
var x_c:int:=460
var y_c:int:=820
for i:1..5
Pic.Draw (pics (19+i),x_c,y_c, picMerge)
y_c-=180
end for
Draw.Text ("Pick a resource and recieve two cards for it", 250,940, Font.New ("arial:18"),black)
y_c:=820
button:=0
resource:=0
loop
Mouse.Where(mousex,mousey,button)
if button=1 then
if mousex>=x_c and mousex<=x_c+70 and mousey>=y_c and mousey<=y_c+80 then
resource:=1
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-180 and mousey<=y_c-100 then
resource:=2
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-360 and mousey<=y_c-280 then
resource:=3
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-540 and mousey<=y_c-460 then
resource:=4
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-720 and mousey<=y_c-640 then
resource:=5
exit
end if
end if
end loop
else
Draw.Text ("Year of Plenty Card was selected", 50,540, Font.New ("arial:18"),black) %computer gets two neww resources at random
resource:=Rand.Int(1,5)
Draw.Text ("Computer got 2 new "+rs(resource)+" cards", 50,500, Font.New ("arial:18"),black)
delay(1000)
end if
if turn mod 2=0 then
p1_hand(resource)+=2
resources(resource)-=2
else
p2_hand(resource)+=2
resources(resource)-=2
end if
end year_of_plenty
proc road_building
var possible_road_count:int:=0
for i:1..upper(sides)
if road_valid(sides (i, 1),sides (i, 3),sides (i, 2),sides (i, 4),turn,road_up)=true then
possible_road_count+=1
end if
end for
if possible_road_count>=2 then %it makes sure the player can build two new roads
play_one:=1
if turn mod 2=0 then
p1_hand(10)-=1
else
p2_hand(10)-=1
end if
display_board
button:=0
road_build:=2
if turn mod 2=0 then
loop
Mouse.Where(mousex,mousey,button)
if button=1 then
if View.WhatDotColour (mousex, mousey) = 7 then
if check_side (mousex, mousey, road_up,road,1) = true then
%side selected
end if
end if
delay(500)
end if
exit when road_build=0
end loop
else
Draw.FillBox(0,0,1000,1000,28)
Draw.Text ("Road Building Card was played", 50,540, Font.New ("arial:18"),black)%tells the player that road building card was played
delay(1000)
var check4:flexible array 1..0 of int
road:=1
var rnd:int
loop
loop
rnd:=Rand.Int(1,upper(roads))
if exists(rnd,check4)=false then
new check4,upper(check4)+1
check4(upper(check4)):=rnd
exit
end if
exit when upper(check4)=72
end loop
if roads(rnd,5)=0 then
if check_side ((sides(rnd,1)+sides(rnd,3)) div 2, (sides(rnd,2)+sides(rnd,4)) div 2, road_up,road,1) and comp_r<=comp_s+5 then
comp_r+=1
end if
end if
exit when road_build=0
end loop
end if
end if
end road_building
proc victory %gives a development card to each player
if turn mod 2=0 then
p1_hand(2)-=1
p1_hand(3)-=1
p1_hand(5)-=1
resources(2)+=1
resources(3)+=1
resources(5)+=1
else
p2_hand(2)-=1
p2_hand(3)-=1
p2_hand(5)-=1
resources(2)+=1
resources(3)+=1
resources(5)+=1
end if
var hand_index:int:=0
if victory_cards(v_top)=1 then
hand_index:=6
elsif victory_cards(v_top)=2 then
hand_index:=7
elsif victory_cards(v_top)=3 then
hand_index:=8
elsif victory_cards(v_top)=4 then
hand_index:=9
elsif victory_cards(v_top)=5 then
hand_index:=10
end if
if hand_index~=0 then %adds the victory card to the hand
if turn mod 2=0 then
p1_hand(hand_index)+=1
if hand_index=7 then
p1_vp+=1
end if
else
p2_hand(hand_index)+=1
if hand_index=7 then
p2_vp+=1
end if
end if
end if
v_top+=1
delay(500)
end victory
proc harbor (f:int) %manages the harbors: the player lose resources and gain resources based on their trades
resource:=0
button:=0
if f=1 and ((turn mod 2=0 and p1_hand(3)>=2) or (turn mod 2=1 and p2_hand(3)>=2)) then %2 to 1 grain
Draw.FillBox(0,0,1000,1000,28)
if turn mod 2=0 then
p1_hand(3)-=2
else
p2_hand(3)-=2
Draw.Text ("2 to 1 trade of grain was done", 50,520, Font.New ("arial:18"),black)
end if
resource:=3
elsif f=2 and ((turn mod 2=0 and p1_hand(5)>=2) or (turn mod 2=1 and p2_hand(5)>=2)) then %2 to 1 ore
Draw.FillBox(0,0,1000,1000,28)
if turn mod 2=0 then
p1_hand(5)-=2
else
p2_hand(5)-=2
Draw.Text ("2 to 1 trade of ore was done", 50,520, Font.New ("arial:18"),black)
end if
resource:=5
elsif f=3 and ((turn mod 2=0 and p1_hand(4)>=2) or (turn mod 2=1 and p2_hand(4)>=2)) then %2 to 1 clay
Draw.FillBox(0,0,1000,1000,28)
if turn mod 2=0 then
p1_hand(4)-=2
else
p2_hand(4)-=2
Draw.Text ("2 to 1 trade of clay was done", 50,520, Font.New ("arial:18"),black)
end if
resource:=4
elsif f=4 and ((turn mod 2=0 and p1_hand(1)>=2) or (turn mod 2=1 and p2_hand(1)>=2)) then %2 to 1 wood
Draw.FillBox(0,0,1000,1000,28)
if turn mod 2=0 then
p1_hand(1)-=2
else
p2_hand(1)-=2
Draw.Text ("2 to 1 trade of wood was done", 50,520, Font.New ("arial:18"),black)
end if
resource:=1
elsif f=5 and ((turn mod 2=0 and p1_hand(2)>=2) or (turn mod 2=1 and p2_hand(2)>=2)) then %2 to 1 wool
Draw.FillBox(0,0,1000,1000,28)
if turn mod 2=0 then
p1_hand(2)-=2
else
p2_hand(2)-=2
Draw.Text ("2 to 1 trade of wool was done", 50,520, Font.New ("arial:18"),black)
end if
resource:=2
elsif f=6 and ((turn mod 2=0 and (p1_hand(1)>=3 or p1_hand(2)>=3 or p1_hand(3)>=3 or p1_hand(4)>=3 or p1_hand(5)>=3)) or (turn mod 2=1 and (p2_hand(1)>=3 or p2_hand(2)>=3 or p2_hand(3)>=3 or p2_hand(4)>=3 or p2_hand(5)>=3))) then
%3 to 1 trade
Draw.FillBox(0,0,1000,1000,28)
if turn mod 2=0 then
var x_c:int:=460
var y_c:int:=820
for i:1..5
if (turn mod 2=0 and p1_hand(i)>=3) or (turn mod 2=1 and p2_hand(i)>=3) then
Pic.Draw (pics (19+i),x_c,y_c, picMerge)
end if
y_c-=180
end for
Draw.Text ("Pick a resource to take three cards from it", 250,940, Font.New ("arial:18"),black)
y_c:=820
loop
Mouse.Where(mousex,mousey,button) %lets the player choose the card they want to give up
if button=1 then
if mousex>=x_c and mousex<=x_c+70 and mousey>=y_c and mousey<=y_c+80 and ((turn mod 2=0 and p1_hand(1)>=3)or(turn mod 2=1 and p2_hand(1)>=3)) then
resource:=1
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-180 and mousey<=y_c-100 and ((turn mod 2=0 and p1_hand(2)>=3)or(turn mod 2=1 and p2_hand(2)>=3)) then
resource:=2
exit
elsif mousex>=x_c and mousex<=x_c+70 and mousey>=y_c-360 and mousey<=y_c-280 and ((turn mod 2=0 and p1_hand(3)>=3)or(turn mod 2=1 and p2_hand(3)>=3)) then
resource:=3
exit