Connect Four

Using the programming features given on Texas Instruments’ graphing calculator TI-84 Plus, I created this text-base connect four game.
The code is written in BASIC and can be seen below.
To transfer code to and from the graphing calculator, I used Texas Instruments’ TI Connect™ CE Software.

Controls

  • Numbers 1 to 7 put your piece accordingly
  • ‘clear’ pauses the game (for taking screenshots)

X won! O won! That game ended in a draw...


"// TI-84 Plus BASIC Code"
"// Jonathan Frech  8th of May, 2016"
"//         edited 13th of May, 2016"

"// clear"
ClrHome

"// init"
[[0,0,0,0,0,0,0][0,0,0,0,0,0,0][0,0,0,0,0,0,0][0,0,0,0,0,0,0][0,0,0,0,0,0,0][0,0,0,0,0,0,0]]→[J]
{0,0,0,0}→⌊WINY
{0,0,0,0}→⌊WINX
{0,5}→⌊OFF
1→P
1→R

"// draw mask"
Output(⌊OFF(1)+7,⌊OFF(2),"+")
Output(⌊OFF(1)+7,⌊OFF(2)+8,"+")
For(Y,1,6)
Output(Y+⌊OFF(1),⌊OFF(2),"|")
Output(Y+⌊OFF(1),⌊OFF(2)+8,"|")
End
For(X,1,7)
Output(⌊OFF(1)+7,⌊OFF(2)+X,"-")
Output(⌊OFF(1)+8,⌊OFF(2)+X,X)
End

"// while loop"
While R

"// draw player"
Output(2,1,"Turn")
If P=1
Then
Output(3,1," X")
Else
Output(3,1," O")
End

"// get key"
0→K
While K=0
getKey→K
End

"// evaluate keys (92,93,94,82,83,84,72)"
0→X
If K=92
Then
1→X
End
If K=93
Then
2→X
End
If K=94
Then
3→X
End
If K=82
Then
4→X
End
If K=83
Then
5→X
End
If K=84
Then
6→X
End
If K=72
Then
7→X
End

"// the clear key is used to pause (for taking screenshots)"
If K=45
Then
Pause 
End

"// put piece"
If X>0
Then
If [J](1,X)=0
Then

0→Y
[J](Y+1,X)=0→θ
While θ
Y+1→Y
0→θ
If Y<6
Then
If [J](Y+1,X)=0
Then
1→θ
End
End
End

"// draw put piece"
If P=1
Then
Output(⌊OFF(1)+Y,⌊OFF(2)+X,"X")
Else
Output(⌊OFF(1)+Y,⌊OFF(2)+X,"O")
End

"// save piece to matrix"
P→[J](Y,X)

"// check win"
"// 0"
If Y≤3
Then
If [J](Y+1,X)=P and [J](Y+2,X)=P and [J](Y+3,X)=P
Then
{Y,Y+1,Y+2,Y+3}→⌊WINY
{X,X,X,X}→⌊WINX
0→R
End
End

"// 1"
If X≥1 and X≤4 and Y≥1 and Y≤3
Then
If [J](Y+1,X+1)=P and [J](Y+2,X+2)=P and [J](Y+3,X+3)=P
Then
{Y,Y+1,Y+2,Y+3}→⌊WINY
{X,X+1,X+2,X+3}→⌊WINX
0→R
End
End

"// 2"
If X≥2 and X≤5 and Y≥2 and Y≤4
Then
If [J](Y-1,X-1)=P and [J](Y+1,X+1)=P and [J](Y+2,X+2)=P
Then
{Y,Y-1,Y+1,Y+2}→⌊WINY
{X,X-1,X+1,X+2}→⌊WINX
0→R
End
End

"// 3"
If X≥3 and X≤6 and Y≥3 and Y≤5
Then
If [J](Y-2,X-2)=P and [J](Y-1,X-1)=P and [J](Y+1,X+1)=P
Then
{Y,Y-2,Y-1,Y+1}→⌊WINY
{X,X-2,X-1,X+1}→⌊WINX
0→R
End
End

"// 4"
If X≥4 and X≤7 and Y≥4 and Y≤6
Then
If [J](Y-3,X-3)=P and [J](Y-2,X-2)=P and [J](Y-1,X-1)=P
Then
{Y,Y-3,Y-2,Y-1}→⌊WINY
{X,X-3,X-2,X-1}→⌊WINX
0→R
End
End

"// 5"
If X≥1 and X≤4 and Y≥4 and Y≤6
Then
If [J](Y-1,X+1)=P and [J](Y-2,X+2)=P and [J](Y-3,X+3)=P
Then
{Y,Y-1,Y-2,Y-3}→⌊WINY
{X,X+1,X+2,X+3}→⌊WINX
0→R
End
End

"// 6"
If X≥2 and X≤5 and Y≥3 and Y≤5
Then
If [J](Y+1,X-1)=P and [J](Y-1,X+1)=P and [J](Y-2,X+2)=P
Then
{Y,Y+1,Y-1,Y-2}→⌊WINY
{X,X-1,X+1,X+2}→⌊WINX
0→R
End
End

"// 7"
If X≥3 and X≤6 and Y≥2 and Y≤4
Then
If [J](Y+2,X-2)=P and [J](Y+1,X-1)=P and [J](Y-1,X+1)=P
Then
{Y,Y+2,Y+1,Y-1}→⌊WINY
{X,X-2,X-1,X+1}→⌊WINX
0→R
End
End

"// 8"
If X≥4 and X≤7 and Y≥1 and Y≤3
Then
If [J](Y+3,X-3)=P and [J](Y+2,X-2)=P and [J](Y+1,X-1)=P
Then
{Y,Y+3,Y+2,Y+1}→⌊WINY
{X,X-3,X-2,X-1}→⌊WINX
0→R
End
End

"// 9"
If X≥1 and X≤4
Then
If [J](Y,X+1)=P and [J](Y,X+2)=P and [J](Y,X+3)=P
Then
{Y,Y,Y,Y}→⌊WINY
{X,X+1,X+2,X+3}→⌊WINX
0→R
End
End

"// 10"
If X≥2 and X≤5
Then
If [J](Y,X-1)=P and [J](Y,X+1)=P and [J](Y,X+2)=P
Then
{Y,Y,Y,Y}→⌊WINY
{X,X-1,X+1,X+2}→⌊WINX
0→R
End
End

"// 11"
If X≥3 and X≤6
Then
If [J](Y,X-2)=P and [J](Y,X-1)=P and [J](Y,X+1)=P
Then
{Y,Y,Y,Y}→⌊WINY
{X,X-2,X-1,X+1}→⌊WINX
0→R
End
End

"// 12"
If X≥4 and X≤7
Then
If [J](Y,X-3)=P and [J](Y,X-2)=P and [J](Y,X-1)=P
Then
{Y,Y,Y,Y}→⌊WINY
{X,X-3,X-2,X-1}→⌊WINX
0→R
End
End

"// game ended in a draw"
If [J](1,1)≠0 and [J](1,2)≠0 and [J](1,3)≠0 and [J](1,4)≠0 and [J](1,5)≠0 and [J](1,6)≠0 and [J](1,7)≠0
Then
3→P
0→R
End

"// swap player"
If R=1
Then
If P=1
Then
2→P
Else
1→P
End
End

End
End
End
"// while loop ended"

"// draw a draw"
If P=3
Then
Output(2,14,"Drw")
Pause 
Else

"// draw a win"
Output(2,14,"Win")
If P=1
Then
Output(3,14," X")
End
If P=2
Then
Output(3,14," O")
End

"// blink winning pieces; key 105 is enter"
0→K
While K≠105
getKey→K

"// pause"
If K=45
Then
Pause 
End

For(I,1,4)
Output(⌊WINY(I)+⌊OFF(1),⌊WINX(I)+⌊OFF(2)," ")
End

For(I,1,4)
If P=1
Then
Output(⌊WINY(I)+⌊OFF(1),⌊WINX(I)+⌊OFF(2),"X")
Else
Output(⌊WINY(I)+⌊OFF(1),⌊WINX(I)+⌊OFF(2),"O")
End
End
End

End

"// clear at end"
ClrHome
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s