import GUI
setscreen ("graphics:900;600,nobuttonbar,cursor")
%App project group #04 : Anthony, Hussein, Ibrahim, Noor
%Declaration Section

const ARRAY_LIMIT := 9
const WIDGET_BASE := 2000

var button : array 1 .. ARRAY_LIMIT of int
var column_1 : int
var column_2 : int
var column_3 : int
var counter : int
var diagonal_1 : int
var diagonal_2 : int
var halfCol : int
var halfRow : int
var player : int := 1
var playerChoice : string
var player1score : int
var player2score : int
var row_1 : int
var row_2 : int
var row_3 : int
var score : array - 9 .. ARRAY_LIMIT of int
var sentence : string
var squareCounter : int

%Initialization Section

forward procedure Quit
forward procedure restartGame
forward procedure handleClick
halfRow := maxrow div 2
halfCol := maxcol div 2
counter := 1
procedure intro
    sentence := "Welcome to Tic-Tac-Toe"
    colorback (white)
    cls
    color (blue)

    locate (maxrow div 2, maxcol div 2 - (length (sentence) div 2))
    put sentence
    halfRow := halfRow + 1

    sentence := "This game has been brought to you by ICS-201 App Project group #04:"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 1
    put sentence

    sentence := "Anthony Bonito"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 1
    put sentence

    sentence := "Ibrahim Jaafar"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 1
    put sentence

    sentence := "Hussein Mouzannar"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 1
    put sentence

    sentence := "Noor Abdou"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 2
    put sentence

    sentence := "Enjoy!"
    locate (halfRow, halfCol - (length (sentence) div 2))
    put sentence
    delay (5000)
    cls
    colorback (white)
end intro
halfRow := maxrow div 2
halfCol := maxcol div 2
counter := 1
procedure extro

    var sentence := "Thank you for playing Tic Tac Toe"
    colorback (black)
    cls
    color (white)

    locate (maxrow div 2, maxcol div 2 - (length (sentence) div 2))
    put sentence
    halfRow := halfRow + 1

    sentence := "Remember this has been brought to you by:"
    locate (halfRow, halfCol - (length (sentence) div 2))
    put sentence
    halfRow := halfRow + 2

    sentence := "Anthony Bonito"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 1
    put sentence

    sentence := "Ibrahim Jaafar"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 1
    put sentence

    sentence := "Hussein Mouzannar"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 1
    put sentence

    sentence := "Noor Abdou"
    locate (halfRow, halfCol - (length (sentence) div 2))
    halfRow := halfRow + 2
    put sentence

    sentence := "Have a great day"
    locate (halfRow, halfCol - (length (sentence) div 2))
    put sentence

end extro

procedure game_initialization

    column_1 := 0
    column_2 := 0
    column_3 := 0
    diagonal_1 := 0
    diagonal_2 := 0
    player1score := 0
    player2score := 0
    row_1 := 0
    row_2 := 0
    row_3 := 0

    for i : 1 .. ARRAY_LIMIT
	button (i) := 0
	row_1 := 0
	row_2 := 0
	row_3 := 0
	button (i) := 0
	squareCounter := 0
	score (i) := 0
    end for

end game_initialization
intro
colorback (white)
% Bottom Left Square
var button1 : int := GUI.CreateButton (100, 0, 0, "Click me!", handleClick)

% Bottom Centre Square
var button2 : int := GUI.CreateButton (410, 0, 0, "Click me!", handleClick)

% Bottom Right Square
var button3 : int := GUI.CreateButton (715, 0, 0, "Click me!", handleClick)

% Middle Left Square
var button4 : int := GUI.CreateButton (100, 205, 0, "Click me!", handleClick)

% Middle Centre Square
var button5 : int := GUI.CreateButton (410, 205, 0, "Click me!", handleClick)

% Middle Right Square
var button6 : int := GUI.CreateButton (715, 205, 0, "Click me!", handleClick)

% Top Left Square
var button7 : int := GUI.CreateButton (100, 405, 0, "Click me!", handleClick)

% Top Middle Square
var button8 : int := GUI.CreateButton (410, 405, 0, "Click me!", handleClick)

% Top Right Square
var button9 : int := GUI.CreateButton (715, 405, 0, "Click me!", handleClick)
% left vertical line
Draw.ThickLine (maxx div 3, maxy, maxx div 3, 0, 5, brightred)

% bottom horizontal line
Draw.ThickLine (maxx, maxy div 3, 0, maxy div 3, 5, brightred)

% right vertical line
Draw.ThickLine (maxx div 3 * 2, maxy, maxx div 3 * 2, 0, 5, brightred)

% Top horizontal line
Draw.ThickLine (maxx, maxy div 3 * 2, 0, maxy div 3 * 2, 5, brightred)

game_initialization
body procedure handleClick

    % left vertical line
    Draw.ThickLine (maxx div 3, maxy, maxx div 3, 0, 5, brightred)

    % bottom horizontal line
    Draw.ThickLine (maxx, maxy div 3, 0, maxy div 3, 5, brightred)

    % right vertical line
    Draw.ThickLine (maxx div 3 * 2, maxy, maxx div 3 * 2, 0, 5, brightred)

    % Top horizontal line
    Draw.ThickLine (maxx, maxy div 3 * 2, 0, maxy div 3 * 2, 5, brightred)

    var theButton := GUI.GetEventWidgetID - WIDGET_BASE
    button (theButton) := button (theButton) + 1

    if player = 1 then
	squareCounter := squareCounter + 1
	score (theButton) := 1
    elsif player = 2 then
	squareCounter := squareCounter + 1
	score (theButton) := -1
    end if

    if player = 1 then
	case theButton of
	    label 1 :
		Draw.FillMapleLeaf (80, 50, 210, 175, brightred)
		GUI.Disable (button1)
	    label 2 :
		Draw.FillMapleLeaf (390, 50, 520, 175, brightred)
		GUI.Disable (button2)
	    label 3 :
		Draw.FillMapleLeaf (700, 50, 830, 175, brightred)
		GUI.Disable (button3)
	    label 4 :
		Draw.FillMapleLeaf (80, 250, 210, 375, brightred)
		GUI.Disable (button4)
	    label 5 :
		Draw.FillMapleLeaf (390, 250, 520, 375, brightred)
		GUI.Disable (button5)
	    label 6 :
		Draw.FillMapleLeaf (700, 250, 830, 375, brightred)
		GUI.Disable (button6)
	    label 7 :
		Draw.FillMapleLeaf (80, 450, 210, 575, brightred)
		GUI.Disable (button7)
	    label 8 :
		Draw.FillMapleLeaf (390, 450, 520, 575, brightred)
		GUI.Disable (button8)
	    label 9 :
		Draw.FillMapleLeaf (700, 450, 830, 575, brightred)
		GUI.Disable (button9)
	end case
    end if
    if player = 2 then
	case theButton of
	    label 1 :
		Draw.FillStar (80, 50, 210, 175, brightblue)
		GUI.Disable (button1)
	    label 2 :
		Draw.FillStar (390, 50, 520, 175, brightblue)
		GUI.Disable (button2)
	    label 3 :
		Draw.FillStar (700, 50, 830, 175, brightblue)
		GUI.Disable (button3)
	    label 4 :
		Draw.FillStar (80, 250, 210, 375, brightblue)
		GUI.Disable (button4)
	    label 5 :
		Draw.FillStar (390, 250, 520, 375, brightblue)
		GUI.Disable (button5)
	    label 6 :
		Draw.FillStar (700, 250, 830, 375, brightblue)
		GUI.Disable (button6)
	    label 7 :
		Draw.FillStar (80, 450, 210, 575, brightblue)
		GUI.Disable (button7)
	    label 8 :
		Draw.FillStar (390, 450, 520, 575, brightblue)
		GUI.Disable (button8)
	    label 9 :
		Draw.FillStar (700, 450, 830, 575, brightblue)
		GUI.Disable (button9)
	end case
    end if

    %Turns
    if player = 1 then
	player := 2
    elsif player = 2 then
	player := 1
    end if
    %How to win
    row_1 := score (1) + score (2) + score (3)
    row_2 := score (4) + score (5) + score (6)
    row_3 := score (7) + score (8) + score (9)
    column_1 := score (1) + score (4) + score (7)
    column_2 := score (2) + score (5) + score (8)
    column_3 := score (3) + score (6) + score (9)
    diagonal_1 := score (1) + score (5) + score (9)
    diagonal_2 := score (3) + score (5) + score (7)
    if row_1 = 3 or row_2 = 3 or row_3 = 3 or column_1 = 3 or column_2 = 3 or column_3 = 3 or diagonal_1 = 3 or diagonal_2 = 3 then
	cls
	put "Player 1 wins!"
	delay (500)
	cls
	restartGame
    elsif row_1 = -3 or row_2 = -3 or row_3 = -3 or column_1 = -3 or column_2 = -3 or column_3 = -3 or diagonal_1 = -3 or diagonal_2 = -3 then
	cls
	put "Player 2 wins!"
	delay (500)
	cls
	restartGame
    elsif squareCounter = 9 then
	cls
	restartGame
    end if
end handleClick
body procedure restartGame
    handleClick
    cls
    % left vertical line
    Draw.ThickLine (maxx div 3, maxy, maxx div 3, 0, 5, brightred)

    % bottom horizontal line
    Draw.ThickLine (maxx, maxy div 3, 0, maxy div 3, 5, brightred)

    % right vertical line
    Draw.ThickLine (maxx div 3 * 2, maxy, maxx div 3 * 2, 0, 5, brightred)

    % Top horizontal line
    Draw.ThickLine (maxx, maxy div 3 * 2, 0, maxy div 3 * 2, 5, brightred)
    game_initialization

    for i : 1 .. ARRAY_LIMIT
	button (i) := 0
    end for

    GUI.Enable (button1)
    GUI.Enable (button2)
    GUI.Enable (button3)
    GUI.Enable (button4)
    GUI.Enable (button5)
    GUI.Enable (button6)
    GUI.Enable (button7)
    GUI.Enable (button8)
    GUI.Enable (button9)

    var button10 : int := GUI.CreateButton (100, 570, 0, "Quit", Quit)

end restartGame

body procedure Quit
    extro
    delay (3000)
    GUI.Quit
end Quit

loop
    exit when GUI.ProcessEvent
end loop


