;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 3 Player Connect Four (with assists)
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ROLE Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(role red)
(role yellow)
(role blue)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; BASE & INPUT Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(<= (base (control ?p)) (role ?p))
(<= (base (cell ?x ?y ?p)) (x ?x) (y ?y) (role ?p))

(<= (input ?p noop) (role ?p))
(<= (input ?p (drop ?x)) (role ?p) (x ?x))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; INIT Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(init (control red))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; LEGAL Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(<= (legal ?player noop)
	(role ?player)
	(not (true (control ?player))))
(<= (legal ?player (drop ?x))
	(true (control ?player))
	(columnOpen ?x))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; NEXT Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(<= (next (cell ?x 1 ?player))
    (does ?player (drop ?x))
	(columnEmpty ?x))
(<= (next (cell ?x ?y2 ?player))
    (does ?player (drop ?x))
    (cellOpen ?x ?y2)
    (plusplus ?y1 ?y2)
    (not (cellOpen ?x ?y1))) 
(<= (next (cell ?x ?y ?player))
    (true (cell ?x ?y ?player)))
    
(<= (next (control red))
    (true (control blue)))
(<= (next (control yellow))
    (true (control red)))
(<= (next (control blue))
    (true (control yellow)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; TERMINAL Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(<= terminal
	(role ?player)
    (line ?player))
    
(<= terminal
    (not boardOpen))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; GOAL Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(<= (goal ?player 0)
	(role ?player)
	(not (line red))
	(not (line yellow))
	(not (line blue)))
	
(<= (goal ?player 100)
	(role ?player)
	(line ?player))
(<= (goal ?assister 50)
	(role ?assister)
	(assist ?assister ?player)
	(line ?player))
(<= (goal ?loser 0)
	(role ?loser)
	(assist ?player ?loser)
	(line ?player))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; View Definitions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(<= (cellOpen ?x ?y)
    (x ?x)
    (y ?y)
    (not (true (cell ?x ?y red)))
    (not (true (cell ?x ?y yellow)))
    (not (true (cell ?x ?y blue))))
(<= (columnOpen ?x)
    (cellOpen ?x 6))
(<= (columnEmpty ?x)
    (cellOpen ?x 1))
(<= boardOpen
    (x ?x)
    (columnOpen ?x))

(<= (line ?player)
    (true (cell ?x1 ?y ?player))
    (plusplus ?x1 ?x2)
    (plusplus ?x2 ?x3)
    (plusplus ?x3 ?x4)
    (true (cell ?x2 ?y ?player))
    (true (cell ?x3 ?y ?player))
    (true (cell ?x4 ?y ?player)))
(<= (line ?player)
    (true (cell ?x ?y1 ?player))
    (plusplus ?y1 ?y2)
    (plusplus ?y2 ?y3)
    (plusplus ?y3 ?y4)
    (true (cell ?x ?y2 ?player))
    (true (cell ?x ?y3 ?player))
    (true (cell ?x ?y4 ?player)))
(<= (line ?player)
    (true (cell ?x1 ?y1 ?player))
    (plusplus ?x1 ?x2)
    (plusplus ?x2 ?x3)
    (plusplus ?x3 ?x4)
    (plusplus ?y1 ?y2)
    (plusplus ?y2 ?y3)
    (plusplus ?y3 ?y4)
    (true (cell ?x2 ?y2 ?player))
    (true (cell ?x3 ?y3 ?player))
    (true (cell ?x4 ?y4 ?player)))
(<= (line ?player)
    (true (cell ?x1 ?y4 ?player))
    (plusplus ?x1 ?x2)
    (plusplus ?x2 ?x3)
    (plusplus ?x3 ?x4)
    (plusplus ?y3 ?y4)
    (plusplus ?y2 ?y3)
    (plusplus ?y1 ?y2)
    (true (cell ?x2 ?y3 ?player))
    (true (cell ?x3 ?y2 ?player))
    (true (cell ?x4 ?y1 ?player)))
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Static Relations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(plusplus 1 2) (plusplus 2 3) (plusplus 3 4) (plusplus 4 5) (plusplus 5 6) (plusplus 6 7) (plusplus 7 8)

(x 1) (x 2) (x 3) (x 4) (x 5) (x 6) (x 7) (x 8)
(y 1) (y 2) (y 3) (y 4) (y 5) (y 6)

(assist red yellow)
(assist yellow blue)
(assist blue red)