# TicTacToe # Adam Forness & Frederick Doe # Math-253 Honors Spring 2009 restart: with(Maplets[Tools]): with(Maplets[Elements]): with(plots): StartEngine(); randomize(): rand9:=rand(0..9): redo:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, Xwin, Owin, IsAVictory; A1:=0; A2:=0; A3:=0; B1:=0; B2:=0; B3:=0; C1:=0; C2:=0; C3:=0; Turn:=1; Xwin:=false; Owin:=true; IsAVictory:=false; Set(replymain="The game has been reset"); Set(UpperLeftButton(caption)=" ",UpperLeftButton(background)=blue): Set(UpperButton(caption)=" ",UpperButton(background)=blue): Set(UpperRightButton(caption)=" ",UpperRightButton(background)=blue): Set(LeftButton(caption)=" ",LeftButton(background)=blue): Set(CenterButton(caption)=" ",CenterButton(background)=blue): Set(RightButton(caption)=" ",RightButton(background)=blue): Set(LowerLeftButton(caption)=" ",LowerLeftButton(background)=blue): Set(LowerButton(caption)=" ",LowerButton(background)=blue): Set(LowerRightButton(caption)=" ",LowerRightButton(background)=blue): end proc: placeupperleft:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if A1=1 then Set(replymain="Sorry, that square has already been taken"); elif A1=2 then Set(replymain="Sorry, that square as already been taken"); elif A1=0 then if Turn=1 then Set(UpperLeftButton(caption)="X",UpperLeftButton(background)=red,UpperLeftButton(foreground)=white); A1:=1; if Get(Two) then Turn:=2; Set(replymain="It is now player two's turn"); end if; elif Turn=2 then Set(UpperLeftButton(caption)="O",UpperLeftButton(background)=yellow,UpperLeftButton(foreground)=black); A1:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placeupper:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if A2=1 then Set(replymain="Sorry, that square has already been taken"); elif A2=2 then Set(replymain="Sorry, that square as already been taken"); elif A2=0 then if Turn=1 then Set(UpperButton(caption)="X",UpperButton(background)=red,UpperButton(foreground)=white); A2:=1; if Get(Two) then Turn:=2; Set(replymain="It is now player two's turn"); end if; elif Turn=2 then Set(UpperButton(caption)="O",UpperButton(background)=yellow,UpperButton(foreground)=black); A2:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placeupperright:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if A3=1 then Set(replymain="Sorry, that square has already been taken"); elif A3=2 then Set(replymain="Sorry, that square as already been taken"); elif A3=0 then if Turn=1 then Set(UpperRightButton(caption)="X",UpperRightButton(background)=red,UpperRightButton(foreground)=white); A3:=1; if Get(Two) then Turn:=2; Set(replymain="It is now player two's turn"); end if; elif Turn=2 then Set(UpperRightButton(caption)="O",UpperRightButton(background)=yellow,UpperRightButton(foreground)=black); A3:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placeleft:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if B1=1 then Set(replymain="Sorry, that square has already been taken"); elif B1=2 then Set(replymain="Sorry, that square as already been taken"); elif B1=0 then if Turn=1 then Set(LeftButton(caption)="X",LeftButton(background)=red,LeftButton(foreground)=white); B1:=1; if Get(Two) then Turn:=2; Set(replymain="It is now player two's turn"); end if; elif Turn=2 then Set(LeftButton(caption)="O",LeftButton(background)=yellow,LeftButton(foreground)=black); B1:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placeright:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if B3=1 then Set(replymain="Sorry, that square has already been taken"); elif B3=2 then Set(replymain="Sorry, that square as already been taken"); elif B3=0 then if Turn=1 then Set(RightButton(caption)="X",RightButton(background)=red,RightButton(foreground)=white); B3:=1; if Get(Two) then Turn:=2; Set(replymain="It is now player two's turn"); end if; elif Turn=2 then Set(RightButton(caption)="O",RightButton(background)=yellow,RightButton(foreground)=black); B3:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placecenter:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if B2=1 then Set(replymain="Sorry, that square has already been taken"); elif B2=2 then Set(replymain="Sorry, that square as already been taken"); elif B2=0 then if Turn=1 then Set(CenterButton(caption)="X",CenterButton(background)=red,CenterButton(foreground)=white); B2:=1; if Get(Two) then Turn:=2; Set(replymain="It is now player two's turn"); end if; elif Turn=2 then Set(CenterButton(caption)="O",CenterButton(background)=yellow,CenterButton(foreground)=black); B2:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placelowerleft:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if C1=1 then Set(replymain="Sorry, that square has already been taken"); elif C1=2 then Set(replymain="Sorry, that square as already been taken"); elif C1=0 then if Turn=1 then Set(LowerLeftButton(caption)="X",LowerLeftButton(background)=red,LowerLeftButton(foreground)=white); C1:=1; if Get(Two) then Turn:=2; Set(replymain="It is now player two's turn"); end if; elif Turn=2 then Set(LowerLeftButton(caption)="O",LowerLeftButton(background)=yellow,LowerLeftButton(foreground)=black); C1:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placelower:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if C2=1 then Set(replymain="Sorry, that square has already been taken"); elif C2=2 then Set(replymain="Sorry, that square as already been taken"); elif C2=0 then if Turn=1 then Set(LowerButton(caption)="X",LowerButton(background)=red,LowerButton(foreground)=white); C2:=1; if Get(Two) then Turn:=2; Set(replymain="If is now player two's turn"); end if; elif Turn=2 then Set(LowerButton(caption)="O",LowerButton(background)=yellow,LowerButton(foreground)=black); C2:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: placelowerright:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn, IsAVictory; local temporary; if IsAVictory then return end if; if C3=1 then Set(replymain="Sorry, that square has already been taken"); elif C3=2 then Set(replymain="Sorry, that square as already been taken"); elif C3=0 then if Turn=1 then Set(LowerRightButton(caption)="X",LowerRightButton(background)=red,LowerRightButton(foreground)=white); C3:=1; if Get(Two) then Turn:=2; Set(replymain="If is now player two's turn"); end if; elif Turn=2 then Set(LowerRightButton(caption)="O",LowerRightButton(background)=yellow,LowerRightButton(foreground)=black); C3:=2; Turn:=1; Set(replymain="It is now player one's turn"); end if; temporary:=checkO(); temporary:=checkX(); ComputerGo(); end if; temporary:=checkO(); temporary:=checkX(); end proc: playertwogo:=proc() global Turn; Turn:=2; end proc: test:=proc() Set(replymain="The test worked"); end proc: checkX:=proc() global IsAVictory; local IsACatsGame; IsACatsGame:=0; if A1=0 or A2=0 or A3=0 or B1=0 or B2=0 or B3=0 or C1=0 or C2=0 or C3=0 then IsACatsGame:=0; else IsACatsGame:=1; end if; if A1=1 and A2=1 and A3=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if B1=1 and B2=1 and B3=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if C1=1 and C2=1 and C3=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if A1=1 and B1=1 and C=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if A2=1 and B2=1 and C2=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if A3=1 and B3=1 and C3=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if A1=1 and B2=1 and C3=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if A3=1 and B2=1 and C1=1 then Set(replymain="X team wins!"); IsAVictory:=true; end if; if not IsAVictory and IsACatsGame=1 then Set(replymain="It's a cats game!"); end if; end proc: checkO:=proc() global IsAVictory; IsAVictory:=false; if A1=2 and A2=2 and A3=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; if B1=2 and B2=2 and B3=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; if C1=2 and C2=2 and C3=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; if A1=2 and B1=2 and C1=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; if A2=2 and B2=2 and C2=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; if A3=2 and B3=2 and C3=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; if A1=2 and B2=2 and C3=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; if A3=2 and B2=2 and C1=2 then Set(replymain="O team wins!"); IsAVictory:=true; end if; end proc: A1:=0: A2:=0: A3:=0: B1:=0: B2:=0: B3:=0: C1:=0: C2:=0: C3:=0: Turn:=1: IsAVictory:=false: ComputerGo:=proc() global A1, A2, A3, B1, B2, B3, C1, C2, C3, Turn; local looping, TempNumber; if not IsAVictory then if Get(One) then looping:=1; if A1=0 or A2=0 or A3=0 or B1=0 or B2=0 or B3=0 or C1=0 or C2=0 or C3=0 then while (looping=1) do TempNumber:=rand9(); if TempNumber=1 then if A1=0 then Set(UpperLeftButton(caption)="O",UpperLeftButton(background)=yellow,UpperLeftButton(foreground)=black); A1:=2; looping:=0; end if; elif TempNumber=2 then if A2=0 then Set(UpperButton(caption)="O",UpperButton(background)=yellow,UpperButton(foreground)=black); A2:=2; looping:=0; end if; elif TempNumber=3 then if A3=0 then Set(UpperRightButton(caption)="O",UpperRightButton(background)=yellow,UpperRightButton(foreground)=black); A3:=2; looping:=0; end if; elif TempNumber=4 then if B1=0 then Set(LeftButton(caption)="O",LeftButton(background)=yellow,LeftButton(foreground)=black); B1:=2; looping:=0; end if; elif TempNumber=5 then if B2=0 then Set(CenterButton(caption)="O",CenterButton(background)=yellow,CenterButton(foreground)=black); B2:=2; looping:=0; end if; elif TempNumber=6 then if B3=0 then Set(RightButton(caption)="O",RightButton(background)=yellow,RightButton(foreground)=black); B3:=2; looping:=0; end if; elif TempNumber=7 then if C1=0 then Set(LowerLeftButton(caption)="O",LowerLeftButton(background)=yellow,LowerLeftButton(foreground)=black); C1:=2; looping:=0; end if; elif TempNumber=8 then if C2=0 then Set(LowerButton(caption)="O",LowerButton(background)=yellow,LowerButton(foreground)=black); C2:=2; looping:=0; end if; elif TempNumber=9 then if C3=0 then Set(LowerRightButton(caption)="O",LowerRightButton(background)=yellow,LowerRightButton(foreground)=black); C3:=2; looping:=0; end if; end if; end; end if; end if; end if; end proc: TicTacToe:= Maplet(onstartup = RunWindow(MAIN), Font[helv10]("helvetica",10), Window[MAIN](title="Tic Tac Toe", [ [ RadioButton[One]("One player",group=OorT,true), RadioButton[Two]("Two players",group=OorT) ], [ Button("Reset game",Evaluate(function = "redo")) ], [ "Please select where you would like to go:" ], [ Button[UpperLeftButton](" ",background=blue, Evaluate(function = "placeupperleft")) , Button[UpperButton](" ",background=blue,Evaluate(function = "placeupper")), Button[UpperRightButton](" ",background=blue, Evaluate(function = "placeupperright")) ], [ Button[LeftButton](" ",background=blue, Evaluate(function = "placeleft")), Button[CenterButton](" ",background=blue, Evaluate(function = "placecenter")), Button[RightButton](" ",background=blue, Evaluate(function = "placeright")) ], [ Button[LowerLeftButton](" ",background=blue, Evaluate(function = "placelowerleft")) , Button[LowerButton](" ",background=blue,Evaluate(function = "placelower")), Button[LowerRightButton](" ",background=blue, Evaluate(function = "placelowerright")) ], [ HorizontalGlue(), TextField[replymain](width=40, editable=false), HorizontalGlue() ], [ Button("Good Bye", Shutdown()) ], [ background="#DDFFFF", inset=0, spacing=0, Label("Programmers: Adam Forness & Frederick Doe ", font=helv10), HorizontalGlue(), Label("Copyright P. Yasskin 2009", font=helv10) ] ] ), ButtonGroup[OorT](onchange=Evaluate(function = "redo")) ): Maplets[Display](TicTacToe);