Four_in_a RowErik Katzen & Mitch Pace253 Honors Spring 2009restart:with(Maplets[Tools]): with(Maplets[Elements]): with(plots):with(plottools):
StartEngine();vlines:=plot([[[0,0],[0,6]], [[1,0],[1,6]], [[2,0],[2,6]], [[3,0],[3,6]], [[4,0],[4,6]], [[5,0],[5,6]], [[6,0],[6,6]], [[7,0],[7,6]],[[0,0],[7,0]]], color=black, thickness=3, axes=none):hlines:=plot([[[0,1],[7,1]],[[0,2],[7,2]],[[0,3],[7,3]],[[0,4],[7,4]],[[0,5],[7,5]]],color=black, thickness=1, axes=none): #, linestyle=dashPcolor[1]:=tan:Pcolor[2]:=cyan:ColorList:=["Maroon", "White", "Black", "Blue", "Red", "Green", "Orange", "Yellow"]:ColorTable:=table({"Maroon"=maroon, "White"=white, "Black"=black, "Blue"=blue, "Red"=red, "Green"=green, "Orange"=orange, "Yellow"=yellow}):player[1]:="Player 1":
player[2]:="Player 2":
playercolor[1]:=ColorTable["Maroon"]:
playercolor[2]:=ColorTable["White"]:
chip[1]:=disk([-.5,-.5], .4, color=playercolor[1]):
chip[2]:=disk([-.5,-.5], .4, color=playercolor[2]):bkgd:=rectangle([0,0],[7,6],color=gray):enabledcols:={1,2,3,4,5,6,7}:gamegrid:=vlines, hlines, bkgd:gameplot:=display([gamegrid], scaling=constrained):Balls:=Array(1..6,1..7):TitlePic:="Four_in_a_Row_Title.gif":GamePic:="Four_in_a_Row_Game.gif":Color1:=proc()
global color, chip, playercolor;
playercolor[1]:=ColorTable[Get(P1color)];
chip[1]:=disk([-.5,-.5], .4, color=playercolor[1]):
Set(P2color(itemlist)=remove(has,ColorList,playercolor[1])):
end proc:Color2:=proc()
global color, chip, playercolor;
playercolor[2]:=ColorTable[Get(P2color)];
chip[2]:=disk([-.5,-.5], .4, color=playercolor[2]):
end proc:ColorButtons:=proc(who, whichcols)
local i;
for i in whichcols do
Set(cat("B",i)(background)=Pcolor[who]);
end do;
end proc:Turn1:=proc()
global player, currplayer, enabledcols;
local Num1;
if Get(P1Init)<>"" then
player[1]:=Get(P1Init):
end if;
if Get(P2Init)<>"" then
player[2]:=Get(P2Init):
end if;
currplayer:=1;
ColorButtons(currplayer, enabledcols);
Set(TFturn=cat(player[currplayer], "'s Turn"))
end proc:Turn:=proc(col)
global player, currplayer, Balls, gamegrid, gameplot, enabledcols;
local i;
for i from 1 to 6 do
if Balls[i,col]=0
then Balls[i,col]:=currplayer;
gamegrid:=translate(chip[currplayer],col,i),gamegrid;
gameplot:=display([gamegrid], scaling=constrained):
break;
end if;
end do;
Winner(i);
if i=6 then
Set(cat("B",col)(enabled)=false, cat("B",col)(background)=gray);
enabledcols:=enabledcols minus {col};
end if;
if currplayer=1 then currplayer:=2 else currplayer:=1 end if;
ColorButtons(currplayer, enabledcols);
Set(TFturn=cat(player[currplayer], "'s Turn"));
Set(Board=gameplot);
end proc:Winner:=proc(i)
global cols, currplayer, row;
for cols from 1 to 4 do
for row from 1 to 6 do
if Balls[row,cols]=currplayer then
if Balls[row,cols+1]=currplayer then
if Balls[row,cols+2]=currplayer then
if Balls[row,cols+3]=currplayer then
Set(WhoWins=cat(" Congratulations!\134n ", player[currplayer],"\134n Wins Horizontally!"));
Set(Win(run)=true);
end if;
end if;
end if;
end if;
end do;
end do;
for cols from 1 to 7 do
for row from 1 to 3 do
if Balls[row,cols]=currplayer then
if Balls[row+1,cols]=currplayer then
if Balls[row+2,cols]=currplayer then
if Balls[row+3,cols]=currplayer then
Set(WhoWins=cat(" Congratulations!\134n ", player[currplayer],"\134n Wins Vertically!"));
Set(Win(run)=true);
end if;
end if;
end if;
end if;
end do;
end do;
for cols from 1 to 4 do
for row from 1 to 3 do
if Balls[row,cols]=currplayer then
if Balls[row+1,cols+1]=currplayer then
if Balls[row+2,cols+2]=currplayer then
if Balls[row+3,cols+3]=currplayer then
Set(WhoWins=cat(" Congratulations!\134n ", player[currplayer],"\134n Wins Diagonally!"));
Set(Win(run)=true);
end if;
end if;
end if;
end if;
end do;
end do;
for cols from 1 to 4 do
for row from 4 to 6 do
if Balls[row,cols]=currplayer then
if Balls[row-1,cols+1]=currplayer then
if Balls[row-2,cols+2]=currplayer then
if Balls[row-3,cols+3]=currplayer then
Set(WhoWins=cat(" Congratulations!\134n ", player[currplayer],"\134n Wins Diagonally!"));
Set(Win(run)=true);
end if;
end if;
end if;
end if;
end do;
end do;
end proc:FourRow:=Maplet(onstartup = RunWindow(Info),
Font[helv10]("helvetica",10),
Window[Info](title="Four in a Row", width=450, height=410,
[ [ Label(Image(TitlePic)), halign=center
],
[ "Player 1","Player 2"],
[ "Enter Your Name","Enter Your Name"],
[ [ TextField[P1Init](width=5, background=cyan)
],
[ TextField[P2Init](width=5, background=cyan)
]
],
[ "Choose Your Color"
],
[ DropDownBox[P1color]("Maroon", ColorList, onchange=Acolor1),
DropDownBox[P2color]("White", ColorList, onchange=Acolor2 )
],
[ Button("Game On!", onclick=A1, background=wheat)
],
[ background="#DDFFFF", inset=0, spacing=0,
Label("Programmers: Erik Katzen & Mitch Pace", font=helv10),
HorizontalGlue(),
Label("Copyright P. Yasskin 2009", font=helv10)
]
]
),
Window[Game](title="Four in a Row",
[ [ Label(Image(GamePic)), halign=center
],
[ inset=0, halign=none,
HorizontalGlue(),
HorizontalGlue(),
Button[B1]("V", width=55, height=40, onclick=Acol1, background=grey,
Font(family=Helvetica, bold=true, size=24)),
HorizontalGlue(),
Button[B2]("V", width=55, height=40, onclick=Acol2, background=grey,
Font(family=Helvetica, bold=true, size=24)),
HorizontalGlue(),
Button[B3]("V", width=55, height=40, onclick=Acol3, background=grey,
Font(family=Helvetica, bold=true, size=24)),
HorizontalGlue(),
Button[B4]("V", width=55, height=40, onclick=Acol4, background=grey,
Font(family=Helvetica, bold=true, size=24)),
HorizontalGlue(),
Button[B5]("V", width=55, height=40, onclick=Acol5, background=grey,
Font(family=Helvetica, bold=true, size=24)),
HorizontalGlue(),
Button[B6]("V", width=55, height=40, onclick=Acol6, background=grey,
Font(family=Helvetica, bold=true, size=24)),
HorizontalGlue(),
Button[B7]("V", width=55, height=40, onclick=Acol7, background=grey,
Font(family=Helvetica, bold=true, size=24)),
HorizontalGlue(),
HorizontalGlue()
],
[ Plotter[Board](gameplot , width=525, height=500)
],
[ TextField[TFturn](width=10, editable=false, background=wheat)
],
[ background="#DDFFFF", inset=0, spacing=0,
Label("Programmers: Erik Katzen & Mitch Pace", font=helv10),
HorizontalGlue(),
Label("Copyright P. Yasskin 2009", font=helv10)
]
]
),
Window[EndGame](title="Four in a Row",
[ [ TextBox[WhoWins](width=15, height=3, editable=false, background=white,
font=Font(family=Stencil, bold=false, size=24))
],
[ Button("Quit", onclick=A2, background=wheat)
]
]
),
Action[A1](RunWindow(Game), CloseWindow(Info), Evaluate(function = "Turn1")),
Action[Acolor1](Evaluate(function=Color1)),
Action[Acolor2](Evaluate(function=Color2)),
Action[A2](Shutdown()),
Action[Acol1](Evaluate(function = "Turn(1)")),
Action[Acol2](Evaluate(function = "Turn(2)")),
Action[Acol3](Evaluate(function = "Turn(3)")),
Action[Acol4](Evaluate(function = "Turn(4)")),
Action[Acol5](Evaluate(function = "Turn(5)")),
Action[Acol6](Evaluate(function = "Turn(6)")),
Action[Acol7](Evaluate(function = "Turn(7)")),
Action[Win](RunWindow(EndGame))
):Maplets[Display]( FourRow ):