-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdots-and-boxes.scm
1846 lines (1428 loc) · 78.7 KB
/
dots-and-boxes.scm
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
;Defining the required macros...
(define-syntax for
(syntax-rules (:)
[(for initzn : condn : incrmt)
(begin
initzn
(define (iter)
(cond (condn (begin
incrmt
(iter)))))
(iter))]
[(for initzn : condn : incrmt : body ...)
(begin
initzn
(define (iter)
(cond (condn (begin
body ...
incrmt
(iter)))))
(iter))]))
(define-syntax while
(syntax-rules ()
[(while bool-exp stats ...)
(begin
(define (iter)
(cond (bool-exp (begin stats ...
(iter)))))
(iter))]))
(define-syntax do-while
(syntax-rules ()
[(do-while bool-exp stats ...)
(begin
(define (iter)
(cond (bool-exp (begin stats ...
(iter)))))
stats ...
(iter))]))
;initilizing variables...
(define m 0)
(define n 0)
(define help 0)
(define (make-2d-vector r c)
(build-vector r
(lambda (x) (make-vector c 0))))
(define (2d-vector-ref vec r c)
(vector-ref (vector-ref vec r) c))
(define (2d-vector-set! vec r c val)
(let ((v (vector-ref vec r)))
(begin
(vector-set! v c val)
(vector-set! vec r v))))
(define horizontal (make-2d-vector 1 1))
(define vertical (make-2d-vector 1 1))
(define boxs (make-2d-vector 1 1))
(define score (build-vector 2 (lambda (x) 0)))
(define nn 0)
(define x 0)
(define y 0)
(define zz 0)
(define count 0)
(define loop 0)
(define u 0)
(define v 0)
(define player 1)
(define (setting-horizontal a2 b2)
(2d-vector-set! horizontal (- a2 1) (- b2 1) 1))
(define (setting-vertical a2 b2)
(2d-vector-set! vertical (- a2 1) (- b2 1) 1))
;This function is called when a two-player game is initialized...
(define (dots2 m n a1 b1)
(define frame1 (new frame% [label "dots and boxes"]
[min-width (+ (* (+ (+ n 1) 2) a1) b1)]
[min-height (+ (* (+ (+ m 1) 2) a1) b1)]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
;text field for displaying scores....
(define scores1 (new text-field%
[label "Player1"]
[parent frame1]
[init-value "0"]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores (new text-field%
[label "Player2"]
[parent frame1]
[init-value "0"]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores2 (new text-field%
[label "Remaining"]
[parent frame1]
[init-value (number->string (* m n))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
;useful brushes...
(define red-brush (make-object brush% "RED" 'solid))
(define white-brush (make-object brush% "WHITE" 'solid))
(define black-brush (make-object brush% "BLACK" 'solid))
(define green-brush (make-object brush% "GREEN" 'solid))
;defining new class
(define my-canvas%
(class canvas%
(define/override (on-event event)
;defining end frames player1 wins
(define (end-frame21)
(define frame (new frame% [label "End of game"]
[min-width 100]
[min-height 100]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
(define msg (new message% [parent frame]
[label "Player1 wins!"]))
(define scores1 (new text-field%
[label "Player1"]
[parent frame]
[init-value (number->string (vector-ref score 1))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores (new text-field%
[label "Player2"]
[parent frame]
[init-value (number->string (vector-ref score 0))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define button (new button% [parent frame]
[label "OK"]
[callback (lambda (button event) (send frame show #f)
(send frame1 show #f))]))
(send frame show #t))
;defining end frames player1 wins
(define (end-frame22)
(define frame (new frame% [label "End of game"]
[min-width 100]
[min-height 100]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
(define msg (new message% [parent frame]
[label "Player2 wins!"]))
(define scores1 (new text-field%
[label "Player1"]
[parent frame]
[init-value (number->string (vector-ref score 1))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores (new text-field%
[label "Player2"]
[parent frame]
[init-value (number->string (vector-ref score 0))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define button (new button% [parent frame]
[label "OK"]
[callback (lambda (button event) (send frame show #f)
(send frame1 show #f))]))
(send frame show #t))
;if the game is tied...
(define (end-frame0)
(define frame (new frame% [label "End of game"]
[min-width 100]
[min-height 100]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
(define msg (new message% [parent frame]
[label "It's a tie!"]))
(define scores1 (new text-field%
[label "Player1"]
[parent frame]
[init-value (number->string (vector-ref score 1))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores (new text-field%
[label "Player2"]
[parent frame]
[init-value (number->string (vector-ref score 0))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define button (new button% [parent frame]
[label "OK"]
[callback (lambda (button event) (send frame show #f)
(send frame1 show #f))]))
(send frame show #t))
;for the given value of z it will take either green or red brush....
(define (color-p z)
(if (= z 1) (send dc set-brush green-brush)
(send dc set-brush red-brush)))
;this will set board to white color except mouse clicked positions....................
(define (set-white k)
(define (set-h a2 b2)
(if (= a2 (+ (+ m 1) 1)) '()
(set-h1 a2 1)))
(define (set-h1 a2 b2)
(if (= b2 (+ n 1)) (set-h (+ a2 1) 1)
(if (= (2d-vector-ref horizontal (- a2 1) (- b2 1)) 1) (set-h1 a2 (+ b2 1))
(begin (send dc set-brush white-brush)
(send dc draw-rectangle (+ (* a1 b2) b1) (* a1 a2) (- a1 b1) b1)
(set-h1 a2 (+ b2 1))))))
(define (set-v a2 b2)
(if (= a2 (+ m 1)) '()
(set-v1 a2 1)))
(define (set-v1 a2 b2)
(if (= b2 (+ (+ n 1) 1)) (set-v (+ a2 1) 1)
(if (= (2d-vector-ref vertical (- a2 1) (- b2 1)) 1) (set-v1 a2 (+ b2 1))
(begin (send dc set-brush white-brush)
(send dc draw-rectangle (* a1 b2) (+ (* a1 a2) b1) b1 (- a1 b1))
(set-v1 a2 (+ b2 1))))))
(begin (set-h 1 1)
(set-v 1 1)))
;this fun will check if we already clicked at that position and he again clicks on it it will return yes else no...
(define (check l)
(cond ((equal? (car l) "H")
(if (= (2d-vector-ref horizontal (- (car (cdr l)) 1) (- (car (cdr (cdr l))) 1)) 1) "YES"
"NO"))
((equal? (car l) "V")
(if (= (2d-vector-ref vertical (- (car (cdr l)) 1) (- (car (cdr (cdr l))) 1)) 1) "YES"
"NO"))))
;convert function converts x,y to list of horizontal or vertical and corresponding position of rectangle
(define (convert a3 b3)
(define (ver-or-hor a3 b3)
(define (vertical a3 b3 a2)
(if (= a2 (+ m 1)) #f
(if (and (> b3 (+ (* a1 a2) b1)) (< b3 (* a1 (+ a2 1)))) #t
(vertical a3 b3 (+ a2 1)))))
(define (horizontal a3 b3 b2)
(if (= b2 (+ n 1)) #f
(if (and (> a3 (+ (* a1 b2) b1)) (< a3 (* a1 (+ b2 1)))) #t
(horizontal a3 b3 (+ b2 1)))))
(cond ((eq? (vertical a3 b3 1) #t) (ver a3 b3 1 1 (list "V")))
((eq? (horizontal a3 b3 1) #t) (hor a3 b3 1 1 (list "H")))
(else (list 1 2 3))))
(define (ver a3 b3 a2 b2 l)
(if (= b2 (+ (+ n 1) 1)) (list 1 2 3)
(if (and (> a3 (* a1 b2)) (< a3 (+ (* a1 b2) b1))) (ver_h a3 b3 1 b2 l)
(ver a3 b3 a2 (+ b2 1) l))))
(define (ver_h a3 b3 a2 b2 l)
(if (= a2 (+ m 1)) (list 1 2 3)
(if (and (> b3 (+ (* a1 a2) b1)) (< b3 (* a1 (+ a2 1)))) (append l (list a2 b2))
(ver_h a3 b3 (+ a2 1) b2 l))))
(define (hor a3 b3 a2 b2 l)
(if (= a2 (+ (+ m 1) 1)) (list 1 2 3)
(if (and (> b3 (* a1 a2)) (< b3 (+ (* a1 a2) b1))) (hor_h a3 b3 a2 1 l)
(hor a3 b3 (+ a2 1) b2 l))))
(define (hor_h a3 b3 a2 b2 l)
(if (= b2 (+ n 1)) (list 1 2 3)
(if (and (> a3 (+ (* a1 b2) b1)) (< a3 (* a1 (+ b2 1)))) (append l (list a2 b2))
(hor_h a3 b3 a2 (+ b2 1) l))))
(ver-or-hor a3 b3))
;this function changes color if mouse is in motion
(define (combined-motion a3 b3)
(define a2 (car (cdr (convert a3 b3))))
(define b2 (car (cdr (cdr (convert a3 b3)))))
(cond ((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "H"))
(begin (set-white 1)
(color-p player)
(send dc draw-rectangle (+ (* a1 b2) b1) (* a1 a2) (- a1 b1) b1)))
((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "V"))
(begin (set-white 1)
(color-p player)
(send dc draw-rectangle (* a1 b2) (+ (* a1 a2) b1) b1 (- a1 b1))))))
;this fun will change color if mouse is clicked
(define (combined-click a3 b3)
(define a2 (car (cdr (convert a3 b3))))
(define b2 (car (cdr (cdr (convert a3 b3)))))
(cond ((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "H"))
(begin (setting-horizontal a2 b2)
(set-white 1)
(send dc set-brush black-brush)
(send dc draw-rectangle (+ (* a1 b2) b1) (* a1 a2) (- a1 b1) b1)))
((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "V"))
(begin (setting-vertical a2 b2)
(set-white 1)
(send dc set-brush black-brush)
(send dc draw-rectangle (* a1 b2) (+ (* a1 a2) b1) b1 (- a1 b1))))))
;this fun will call set white fun
(define (wrt-mouse a2 b2)
(if (or (equal? (car (convert a2 b2)) "H") (equal? (car (convert a2 b2)) "V")) '()
(set-white 1)))
;this will call corresponding fun when mouse event is happened
(define (mo-click a2 b2)
(cond
((equal? (symbol->string (send event get-event-type)) "motion")
(combined-motion a2 b2))
((equal? (symbol->string (send event get-event-type)) "left-up")
(begin ;(combined-click a2 b2)
(cond ((equal? (car (convert a2 b2)) "H")
(user-mh (- (car (cdr (convert a2 b2))) 1) (- (car (cdr (cdr (convert a2 b2)))) 1)))
((equal? (car (convert a2 b2)) "V")
(user-mv (- (car (cdr (convert a2 b2))) 1) (- (car (cdr (cdr (convert a2 b2)))) 1))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;This function is called when a user clicks over a horizontal line connecting the dots...
(define (user-mh i j)
(cond ((< (2d-vector-ref horizontal i j) 1)
(begin
(put-horizontal i j)
(cond ((= (+ (vector-ref score 0) (vector-ref score 1)) (* m n))
(cond ((> (vector-ref score 1) (vector-ref score 0)) (end-frame21))
((< (vector-ref score 1) (vector-ref score 0)) (end-frame22))
(else (end-frame0)))))
))))
;This function is called when a user clicks over a vertical line connecting the dots..
(define (user-mv i j)
(cond ((< (2d-vector-ref vertical i j) 1)
(begin
(put-vertical i j)
(cond ((= (+ (vector-ref score 0) (vector-ref score 1)) (* m n))
(cond ((> (vector-ref score 1) (vector-ref score 0)) (end-frame21))
((< (vector-ref score 1) (vector-ref score 0)) (end-frame22))
(else (end-frame0)))))
))))
;This function updates the "horizontal" vector...
(define (put-horizontal x y)
(begin
(2d-vector-set! horizontal x y 1)
(cond((> x 0)
(2d-vector-set! boxs (- x 1) y (+ (2d-vector-ref boxs (- x 1) y) 1))))
(cond((< x m)
(2d-vector-set! boxs x y (+ (2d-vector-ref boxs x y) 1))))
(hline-box? x y)
(set! player (- 1 player))))
;This function updates the "vertical" vector..
(define (put-vertical x y)
(begin
(2d-vector-set! vertical x y 1)
(cond((> y 0)
(2d-vector-set! boxs x (- y 1) (+ (2d-vector-ref boxs x (- y 1)) 1))))
(cond((< y n)
(2d-vector-set! boxs x y (+ (2d-vector-ref boxs x y) 1))))
(vline-box? x y)
(set! player (- 1 player))))
;This function checks if a box is formed when a horizontal line is put...
(define (hline-box? x y)
(define hit 0)
(define uu 0)
(begin
(cond ((> x 0)
(cond ((= (2d-vector-ref boxs (- x 1) y) 4)
(begin
(set! uu (- x 1))
(color-p player)
(send dc draw-rectangle (+ (* (+ y 1) a1) b1) (+ (* x a1) b1) (- a1 b1) (- a1 b1))
(vector-set! score player (+ (vector-ref score player) 1))
(if (= player 0)
(send scores set-value (number->string (vector-ref score player)))
(send scores1 set-value (number->string (vector-ref score player))))
(send scores2 set-value (number->string (- (* m n) (+ (vector-ref score 0) (vector-ref score 1)))))
(set! hit 1))))))
(cond ((< x m) (cond ((= (2d-vector-ref boxs x y) 4)
(begin
(color-p player)
(send dc draw-rectangle (+ (* (+ y 1) a1) b1) (+ (* (+ x 1) a1) b1) (- a1 b1) (- a1 b1))
(vector-set! score player (+ (vector-ref score player) 1))
(if (= player 0)
(send scores set-value (number->string (vector-ref score player)))
(send scores1 set-value (number->string (vector-ref score player))))
(send scores2 set-value (number->string (- (* m n) (+ (vector-ref score 0) (vector-ref score 1)))))
(set! hit 1))))))
(cond ((> hit 0) (set! player (- 1 player))))))
;This function checks if a box is formed when a horizontal line is put...
(define (vline-box? x y)
(define hit 0)
(define vv 0)
(begin
(cond ((> y 0) (cond ((= (2d-vector-ref boxs x (- y 1)) 4)
(begin
(set! vv (- y 1))
(color-p player)
(send dc draw-rectangle (+ (* y a1) b1) (+ (* (+ x 1) a1) b1) (- a1 b1) (- a1 b1))
(vector-set! score player (+ (vector-ref score player) 1))
(if (= player 0)
(send scores set-value (number->string (vector-ref score player)))
(send scores1 set-value (number->string (vector-ref score player))))
(send scores2 set-value (number->string (- (* m n) (+ (vector-ref score 0) (vector-ref score 1)))))
(set! hit 1))))))
(cond ((< y n)
(cond ((= (2d-vector-ref boxs x y) 4)
(begin
(color-p player)
(send dc draw-rectangle (+ (* (+ y 1) a1) b1) (+ (* (+ x 1) a1) b1) (- a1 b1) (- a1 b1))
(vector-set! score player (+ (vector-ref score player) 1))
(if (= player 0)
(send scores set-value (number->string (vector-ref score player)))
(send scores1 set-value (number->string (vector-ref score player))))
(send scores2 set-value (number->string (- (* m n) (+ (vector-ref score 0) (vector-ref score 1)))))
(set! hit 1))))))
(cond ((> hit 0) (set! player (- 1 player))))))
;fun calling in my-canvas....
(mo-click (send event get-x) (send event get-y))
(wrt-mouse (send event get-x) (send event get-y)))
(super-new)))
;defining canvas....
(define canvas (new my-canvas% [parent frame1]
[paint-callback
(lambda (canvas dc) (my-paint a1 a1))]
[stretchable-width #t]
[stretchable-height #t]))
;defining dc
(define dc (send canvas get-dc))
(send dc set-brush black-brush)
;this will put dots on game
(define (my-paint a2 b2)
(cond ((and (<= a2 (* a1 (+ n 1))) (<= b2 (* a1 (+ m 1)))) (begin (send dc draw-rectangle a2 b2 b1 b1)
(my-paint (+ a2 a1) b2)))
((and (> a2 (* a1 (+ n 1))) (<= b2 (* a1 (+ m 1)))) (my-paint a1 (+ b2 a1)))))
(send frame1 show #t))
;End of code for two-player game...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This function is called when one-player game is initialized...
(define (dots1 m n a1 b1)
(define frame1 (new frame% [label "dots and boxes"]
[min-width (+ (* (+ (+ n 1) 2) a1) b1)]
[min-height (+ (* (+ (+ m 1) 2) a1) b1)]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
;text field for displaying scores....
(define scores (new text-field%
[label "Computer"]
[parent frame1]
[init-value "0"]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores1 (new text-field%
[label "Human"]
[parent frame1]
[init-value "0"]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores2 (new text-field%
[label "Remaining"]
[parent frame1]
[init-value (number->string (* m n))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
;defining brushes.........
(define red-brush (make-object brush% "RED" 'solid))
(define white-brush (make-object brush% "WHITE" 'solid))
(define black-brush (make-object brush% "BLACK" 'solid))
(define green-brush (make-object brush% "GREEN" 'solid))
;defining a new class.....
(define my-canvas%
(class canvas%
(define/override (on-event event)
;this will create a final frames if game is tie
(define (end-frame0)
(define frame (new frame% [label "End of game"]
[min-width 100]
[min-height 100]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
(define msg (new message% [parent frame]
[label "It's a tie!"]))
(define scores1 (new text-field%
[label "Player1"]
[parent frame]
[init-value (number->string (vector-ref score 1))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores (new text-field%
[label "Player2"]
[parent frame]
[init-value (number->string (vector-ref score 0))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define button (new button% [parent frame]
[label "OK"]
[callback (lambda (button event) (send frame show #f)
(send frame1 show #f))]))
(send frame show #t))
;this will create a final frames if computer wins..........
(define (end-frame11)
(define frame (new frame% [label "End of game"]
[min-width 100]
[min-height 100]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
(define msg (new message% [parent frame]
[label "Computer wins! "]))
(define scores1 (new text-field%
[label "Computer"]
[parent frame]
[init-value (number->string (vector-ref score 0))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores (new text-field%
[label "Human"]
[parent frame]
[init-value (number->string (vector-ref score 1))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define button (new button% [parent frame]
[label "OK"]
[callback (lambda (button event) (send frame show #f)
(send frame1 show #f))]))
(send frame show #t))
;this will create a final frames if human wins
(define (end-frame12)
(define frame (new frame% [label "End of game"]
[min-width 100]
[min-height 100]
[stretchable-width #f]
[stretchable-height #f]
[enabled #t]
[border 5]))
(define msg (new message% [parent frame]
[label "Human wins!"]))
(define scores1 (new text-field%
[label "Computer"]
[parent frame]
[init-value (number->string (vector-ref score 0))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define scores (new text-field%
[label "Human"]
[parent frame]
[init-value (number->string (vector-ref score 1))]
[enabled #f]
[min-width 200]
[stretchable-width #f]
[stretchable-height #f]))
(define button (new button% [parent frame]
[label "OK"]
[callback (lambda (button event) (send frame show #f)
(send frame1 show #f))]))
(send frame show #t))
;this will take brush depending upon the value of z.......
(define (color-p z)
(if (= z 1) (send dc set-brush green-brush)
(send dc set-brush red-brush)))
;this fun will white board except the clicked position.........
(define (set-white k)
(define (set-h a2 b2)
(if (= a2 (+ (+ m 1) 1)) '()
(set-h1 a2 1)))
(define (set-h1 a2 b2)
(if (= b2 (+ n 1)) (set-h (+ a2 1) 1)
(if (= (2d-vector-ref horizontal (- a2 1) (- b2 1)) 1) (set-h1 a2 (+ b2 1))
(begin
(send dc set-brush white-brush)
(send dc draw-rectangle (+ (* a1 b2) b1) (* a1 a2) (- a1 b1) b1)
(set-h1 a2 (+ b2 1))))))
(define (set-v a2 b2)
(if (= a2 (+ m 1)) '()
(set-v1 a2 1)))
(define (set-v1 a2 b2)
(if (= b2 (+ (+ n 1) 1)) (set-v (+ a2 1) 1)
(if (= (2d-vector-ref vertical (- a2 1) (- b2 1)) 1) (set-v1 a2 (+ b2 1))
(begin
(send dc set-brush white-brush)
(send dc draw-rectangle (* a1 b2) (+ (* a1 a2) b1) b1 (- a1 b1))
(set-v1 a2 (+ b2 1))))))
(begin
(set-h 1 1)
(set-v 1 1)))
;this fun will check if we already clicked at that position and he again clicks on it it will return yes else no...
(define (check l)
(cond ((equal? (car l) "H")
(if (= (2d-vector-ref horizontal (- (car (cdr l)) 1) (- (car (cdr (cdr l))) 1)) 1) "YES"
"NO"))
((equal? (car l) "V")
(if (= (2d-vector-ref vertical (- (car (cdr l)) 1) (- (car (cdr (cdr l))) 1)) 1) "YES"
"NO"))))
;convert fun will converts x,y to list of horizontal or vertical and corresponding position of rectangle
(define (convert a3 b3)
(define (ver-or-hor a3 b3)
(define (vertical a3 b3 a2)
(if (= a2 (+ m 1)) #f
(if (and (> b3 (+ (* a1 a2) b1)) (< b3 (* a1 (+ a2 1)))) #t
(vertical a3 b3 (+ a2 1)))))
(define (horizontal a3 b3 b2)
(if (= b2 (+ n 1)) #f
(if (and (> a3 (+ (* a1 b2) b1)) (< a3 (* a1 (+ b2 1)))) #t
(horizontal a3 b3 (+ b2 1)))))
(cond ((eq? (vertical a3 b3 1) #t) (ver a3 b3 1 1 (list "V")))
((eq? (horizontal a3 b3 1) #t) (hor a3 b3 1 1 (list "H")))
(else (list 1 2 3))))
(define (ver a3 b3 a2 b2 l)
(if (= b2 (+ (+ n 1) 1)) (list 1 2 3)
(if (and (> a3 (* a1 b2)) (< a3 (+ (* a1 b2) b1))) (ver_h a3 b3 1 b2 l)
(ver a3 b3 a2 (+ b2 1) l))))
(define (ver_h a3 b3 a2 b2 l)
(if (= a2 (+ m 1)) (list 1 2 3)
(if (and (> b3 (+ (* a1 a2) b1)) (< b3 (* a1 (+ a2 1)))) (append l (list a2 b2))
(ver_h a3 b3 (+ a2 1) b2 l))))
(define (hor a3 b3 a2 b2 l)
(if (= a2 (+ (+ m 1) 1)) (list 1 2 3)
(if (and (> b3 (* a1 a2)) (< b3 (+ (* a1 a2) b1))) (hor_h a3 b3 a2 1 l)
(hor a3 b3 (+ a2 1) b2 l))))
(define (hor_h a3 b3 a2 b2 l)
(if (= b2 (+ n 1)) (list 1 2 3)
(if (and (> a3 (+ (* a1 b2) b1)) (< a3 (* a1 (+ b2 1)))) (append l (list a2 b2))
(hor_h a3 b3 a2 (+ b2 1) l))))
(ver-or-hor a3 b3))
;if the mouse is in motion this fun will work.....
(define (combined-motion a3 b3)
(define a2 (car (cdr (convert a3 b3))))
(define b2 (car (cdr (cdr (convert a3 b3)))))
(cond ((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "H"))
(begin (set-white 1)
(send dc set-brush green-brush)
(send dc draw-rectangle (+ (* a1 b2) b1) (* a1 a2) (- a1 b1) b1)))
((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "V"))
(begin (set-white 1)
(send dc set-brush green-brush)
(send dc draw-rectangle (* a1 b2) (+ (* a1 a2) b1) b1 (- a1 b1))))))
;if the mouse is clicked this fun will work.....
(define (combined-click a3 b3)
(define a2 (car (cdr (convert a3 b3))))
(define b2 (car (cdr (cdr (convert a3 b3)))))
(cond ((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "H"))
(begin (setting-horizontal a2 b2)
(set-white 1)
(send dc set-brush black-brush)
(send dc draw-rectangle (+ (* a1 b2) b1) (* a1 a2) (- a1 b1) b1)))
((and (equal? (check (convert a3 b3)) "NO") (equal? (car (convert a3 b3)) "V"))
(begin (setting-vertical a2 b2)
(set-white 1)
(send dc set-brush black-brush)
(send dc draw-rectangle (* a1 b2) (+ (* a1 a2) b1) b1 (- a1 b1))))))
;this will call set-whitw fun...............
(define (wrt-mouse a2 b2)
(if (or (equal? (car (convert a2 b2)) "H") (equal? (car (convert a2 b2)) "V")) '()
(set-white 1)))
;this fun will call gui and algorithm part......
(define (mo-click a2 b2)
(cond ((equal? (symbol->string (send event get-event-type)) "motion") (combined-motion a2 b2))
((equal? (symbol->string (send event get-event-type)) "left-up")
(begin
(cond ((equal? (car (convert a2 b2)) "H")
(user-mh (- (car (cdr (convert a2 b2))) 1) (- (car (cdr (cdr (convert a2 b2)))) 1)))
((equal? (car (convert a2 b2)) "V")
(user-mv (- (car (cdr (convert a2 b2))) 1) (- (car (cdr (cdr (convert a2 b2)))) 1))))))))
; This function is called when the user clicks over a horizontal line...
(define (user-mh i j)
(cond ((< (2d-vector-ref horizontal i j) 1)
(begin
(put-horizontal i j)
(cond ((= (+ (vector-ref score 0) (vector-ref score 1)) (* m n))
(cond ((> (vector-ref score 1) (vector-ref score 0)) (end-frame12))
((> (vector-ref score 0) (vector-ref score 1)) (end-frame11))
(else (end-frame0))))
((= player 0) (comp-step)))))))
; This function is called when the user clicks over a horizontal line...
(define (user-mv i j)
(cond ((< (2d-vector-ref vertical i j) 1)
(begin
(put-vertical i j)
(cond ((= (+ (vector-ref score 0) (vector-ref score 1)) (* m n))
(cond ((> (vector-ref score 1) (vector-ref score 0)) (end-frame12))
((> (vector-ref score 0) (vector-ref score 1)) (end-frame11))
(else (end-frame0))))
((= player 0) (comp-step)))))))
; This function updates the "horizontal" vector...
(define (put-horizontal x y)
(begin
(2d-vector-set! horizontal x y 1)
(cond((> x 0) (2d-vector-set! boxs (- x 1) y (+ (2d-vector-ref boxs (- x 1) y) 1))))
(cond((< x m) (2d-vector-set! boxs x y (+ (2d-vector-ref boxs x y) 1))))
(color-p player)
(send dc draw-rectangle (+ (* a1 (+ y 1)) b1) (* a1 (+ x 1)) (- a1 b1) b1)
(hline-box? x y)
(set! player (- 1 player))))
;This function updates the "vertical" vector...
(define (put-vertical x y)
(begin
(2d-vector-set! vertical x y 1)
(cond((> y 0) (2d-vector-set! boxs x (- y 1) (+ (2d-vector-ref boxs x (- y 1)) 1))))
(cond((< y n) (2d-vector-set! boxs x y (+ (2d-vector-ref boxs x y) 1))))
(color-p player)
(send dc draw-rectangle (* a1 (+ y 1)) (+ (* a1 (+ x 1)) b1) b1 (- a1 b1))
(vline-box? x y)
(set! player (- 1 player))))
(define (put-line zz x y)
(if (> zz 1) (put-vertical x y)
(put-horizontal x y)))
;This is the function which is called when the turn is of computer...
(define (comp-step)
(begin
(safe-boxes)
(cond ((= (+ (vector-ref score 0) (vector-ref score 1)) (* m n))
(cond ((> (vector-ref score 1) (vector-ref score 0)) (end-frame12))
((> (vector-ref score 0) (vector-ref score 1)) (end-frame11))
(else (end-frame0)))))
(cond ((check-if-box) (begin
(if (check-if-sline)
(begin
(allsafeboxes)
(put-line zz x y))
(give-2 u v))
(cond ((= (+ (vector-ref score 0) (vector-ref score 1)) (* m n))
(cond ((> (vector-ref score 1) (vector-ref score 0)) (end-frame12))
((> (vector-ref score 0) (vector-ref score 1)) (end-frame11))
(else (end-frame0)))))))
((check-if-sline) (put-line zz x y))
((check-1give) (put-line zz x y))
((check-2give) (put-line zz x y))
(else (random-step)))))
; This function fills all those boxes which can be filled without increasing the no of sides in adjacent boxes to > 2
(define (safe-boxes)
(define i 0)
(define j 0)
(for (set! i 0)
: (< i m)
: (set! i (+ i 1))
: (for (set! j 0)
: (< j n)
: (set! j (+ j 1))
: (cond ((= (2d-vector-ref boxs i j) 3)
(cond ((< (2d-vector-ref vertical i j) 1)
(cond ((or (= j 0) (not (= (2d-vector-ref boxs i (- j 1)) 2))) (put-vertical i j))))
((< (2d-vector-ref horizontal i j) 1)
(cond ((or (= i 0) (not (= (2d-vector-ref boxs (- i 1) j) 2))) (put-horizontal i j))))
((< (2d-vector-ref vertical i (+ j 1)) 1)
(cond ((or (= j (- n 1)) (not (= (2d-vector-ref boxs i (+ j 1)) 2))) (put-vertical i (+ j 1)))))
(else (cond ((or (= i (- m 1)) (not (= (2d-vector-ref boxs (+ i 1) j) 2))) (put-horizontal (+ i 1) j))))))))))
;This function checks if there is a box with 3 of its sides filled... and returns #t or #f
(define (check-if-box)