     ___________________________________________________________________________________________________
    /             /    /           /             /    \           \             \             \         \
   /____     ____/____/   ____    /____     ____/      \    ____   \____     ____\     ____    \     ____\
       /    /   _____/   /   /___/    /    /   /        \   \   \___\   \    \    \    \   \    \    \___
      /    /   /    /   /            /    /   /    /\    \   \           \    \    \    \   \    \       \
     /    /   /    /   /   ____     /    /   /    /  \    \   \    ____   \    \    \    \   \    \     __\
    /    /   /    /   /___/   /    /    /   /    ______    \   \___\   \   \    \    \    \___\    \    \_____
   /    /   /    /           /    /    /   /    /      \    \           \   \    \    \             \         \
  /____/   /____/___________/    /____/   /____/        \____\___________\   \____\    \_____________\_________\

						   BY
	        	____         ______ ____ _________ _________ _________
			\   \       /     //   //        //        //        /
		         \   \     /   __//___//____    //________//____    /
		          \   \   /   /  ____ _____/   /_________ _____/   /
		           \   \ /   /  /   //        //        //        /
		            \  //   /  /   //   _____//   _____//   _____/
		             \//   /  /   //   /     /   /______\   \
		              /   /  /   //   /     /           \\   \
		              \  /  /___//___/     /_____________\\   \
		               \/                                  \___\

Name: TicTacToe
Version: 1.0
Machine: TI-83
Type of program: Game
Type of game: 3 in a row
Aurora compatible: yes
Programming language: Basic
Size: 6226 bytes
Author: Daniel van den Ouden A.K.A. Viper
E-Mail: Daniel@Ouden.net

Contents of this readme file.
1. Introduction
2. Contents of the group file
3. Gameplay
4. About the artificial Intelligence
5. About me
6. Thanks

1.Introduction

TTT is a small 3-in-a-row program with a (good) AI (Artificial Intelligence)
There are two play modes:
-vs. CPU
-vs. Human (link cable)

It also contains a CPU vs. CPU program (fun, you can see the intelligence of the AI)

I've tried to make the CPU as smart as possible, and to maintain speed and size aswell.
In about 50% of the games, nobody will win. And in about 5 % of the games, the CPU will win.

There's also a Multiplayer option but I haven't had the time to test it.

2.Contents of the group file

The file ttt.83g contains the following files:

prog3iar.83p		This file is a shortcut for Bill Nagel's Aurora
icon3iar.83p		This file is a Icon for Bill Nagel's Aurora
ttt.83p			This is the main program
tttai.83p		This is the artificial intelligence program
cpuvscpu.83p		This is the CPU vs. CPU program
tttcpuai.83p		This is the extra AI program for CPU vs. CPU

If you want to save space, you can delete cpuvscpu.83p and tttcpuai.83p, and if you don't use Aurora, you can delete prog3iar.83p and icon3iar.83p


3.Gameplay

When you start TTT, you get a intro and after that, a main menu. Here you can select vs. CPU or vs. Human (via Link Cable).
If you choose vs. CPU, The game will immediately start.
A random decides wich player may begin. You can select a place by pressing one of the keys. Here's an explanation:

 7 I 8 I 9
---+---+---
 4 I 5 I 6 
---+---+---
 1 I 2 I 3 

You can press clear to exit.

The multiplayer option isn't perfect yet so no explanation for now.


4. ABOUT THE ARTIFICIAL INTELLIGENCE
Some people might want to create their own AI. That's possible!!!
But first, I will explain how the original AI works.
I began with thinking of some general rules wich the AI had to follow. Here they are:
-If I can win, do it.
-If my opponent can win, stop him.
-If I posess 1 place in a row/collumn, and the other two are empty, choose one.
-If I may begin, pick a nice place.
-If my opponent begins, pick a good place.
-If none of the obove is appropriate, pick a random place.

That's all!!!

If you want to have a look at the source code, look at the bottom of this page.

Here are some rules for creating your own AI.
TTT uses matrix a (  [A]  ), it's 3x3.
-Your program must chose a place and store 2 in it. (since the human player is nr. 1)
ex:
:2->[A](2,1)

0 means an empty place, 1 means a place possesed by human and 2 means a place possesed by the CPU.

-Your program can only choose a place that is free (value is 0).
-The following variables cannot be used, P and T
-It may not clear the screen
-It may only place text on the 2 lower lines ( 7 & 8 )

That's it,

Have fun!

TTT is FREEWARE!!!
Please give it to all your friends!


5. About me

If you have any questions, please let me know!
the adress is:
Daniel@Ouden.net

6.Thanks

I would like to thank:

Roelof Jongeneel, my best friend.

Dave van het Lijntje, good friend and programmer.

Harmen Heuvelman, my second best friend (He wanted to have a super intelligent game, it doesn't even get close to it, but it is a start ;)  )

Kees Vuik, his articles on Artificial Intelligence gave me a lot of inspiration and ideas.

James Matthews, for writing the BEST ASM tutorials EVER!!!







O.K.
Here's the sourcecode for TTTAI.83p


1->D
Output(7,1,"THINKING..."
Lbl CW
If D=1				D is used to create a loop (1 means checking if CPU can win, 2 means checking if human 
				can win)
Output(8,1," I WIN?"
If D=2
Output(8,1,"I LOSE?"
0->Z				0 store in Z, Z is used to check the number
				  of places possesed by CPU
0->				0 store in theta, theta is used to check the number
				  of places possesed by human player
1->A				1 store in A, A is used to store the search mode (1 means horizontal search)
For(X,1,3			Iv'e created a double loop to check every place in
For(Y,1,3			the matrix
If [A](X,Y)=2			If the value of this place in the matrix is 2 (CPU owned)
Z+1->Z				Then add 1 to Z
If [A](X,Y)=1			If the value of this place in the matrix is 1 (Human)
+1->				then add 1 to theta
If Y=3 and Z=2 and =0 and D=1  If CPU can win and this is the first time I'm checking and it's the end of the loop
Goto W1				then goto label W1
If Y=3 and Z=0 and =2 and D=2	If Human can win and it's the second time I'm checking and it's the end of the loop
Goto W1				Then goto label W1
If Y=3				If it's the end of the loop
Then				then
0->Z				Reset Z
0->				Reset theta
End				End of if-then statement
End				End of second loop
Output(8,7+X,"."		Show a dot (progress bar)
End				end of first loop
2->A				Second search mode (vertical)
For(X,1,3)			Here's another double loop
For(Y,1,3)
If [A](Y,X)=2			Means the same as in the previous loop
Z+1->Z						"
If [A](Y,X)=1					"
+1->						"
If Y=3 and Z=2 and =0 and D=1			"
Goto W2						"
If Y=3 and Z=0 and =2 and D=2			"
Goto W2						"
If Y=3						"
Then						"
0->Z						"
0->						"
End						"
End						"
Output(8,10+X,"."				"
End						"
3->A				Third search mode (diagonal)
For(X,1,3			There's just one loop needed here
If [A](X,X)=2			If place possesed by CPU
Z+1->Z				add one to Z
If [A](X,X)=1			If place is possesed by human
+1->				add one to theta
End				end of loop
If Z=2 and =0 and D=1		Can CPU win?
Goto W3				then goto W3
If Z=0 and =2 and D=2		Can Human win?
Goto W3				Then goto W3
Output(8,14,"."			Output a dot for the progress bar
0->Z				Reset Z
0->				Reset theta
4->A				Search mode 4 (mirrored diagonal)
For(X,1,3			This means the same as the previous loop
If [A](X,4-X)=2					"
Z+1->Z						"
If [A](X,4-X)=1					"
+1->						"
End						"
If Z=2 and =0 and D=1				"
Goto W4						"
If Z=0 and =2 and D=2				"
Goto W4						"
Output(8,15,"."					"
If D=2				If this is the second check
Goto CP				Then goto label CP (Choose Place)
2->D				Else, do a second check
Output(8,1,"                "	Clear the progress bar
Goto CW				Goto label CW (Check Win)
Lbl CP				Label CP (Choose Place)
Output(8,1,"CHOOSING PLACE. "	Guess what this means
0->Z				Reset Z
0->				Reset Theta
For(X,1,3)			Create a double loop here to check every place in the matrix
For(Y,1,3)
If [A](X,Y)=1			If place is possesed by human
+1->				Add 1 to theta
If [A](X,Y)=2			If place is possesed by CPU
Z+1->Z				Add 1 to Z 
End				End of second loop
End				End of first loop
If Z=0 and =1			If human player made first move
Goto 1S				Goto label 1S (player 1 Started)
If Z=0 and =0			If CPU may begin
Goto 2S				Goto label 2S (player 2 Started)
0->Z				Reset Z
0->				Reset theta
1->A				first search mode (Vertical)
Lbl ZZ				Label ZZ (means nothin', I just needed a name)
For(X,1,3)			I've created a single loop here to prevent memory polution
If [A](X,A)=1			You know what this means,
+1->				and this,
If [A](X,A)=2			and the rest of it
Z+1->Z
End
If =0 and Z=1			If CPU posseses 1 place in this collumn and Human none
Goto P1				then goto label P1
0->Z				Reset Z
0->				Reset theta
A+1->A				Add 1 to A (search mode)
If A<4				If A is smaller than three
Goto ZZ				then do another search but in the next collumn
Lbl Z				Label Ztheta (has no meaning)
For(X,1,3			create a single loop
If [A](A-3,X)=1			Same ol' shit
+1->
If [A](A-3,X)=2
Z+1->
End
If Z=1 and =0
Goto P2
0->Z
0->
A+1->A
If A<7				If A is smaller than 7 (searched less than three times)
Goto Z				Then do annother search (next row)
For(X,1,3)			And another loop
If [A](X,X)=1			This time to search diagonal
+1->
If [A](X,X)=2
Z+1->Z
End
If Z=1 and =0
Goto P3
A+1->A
0->Z
0->
For(X,1,3			And another loop
If [A](X,4-X)=1			Mirrored diagonal search
+1->
If [A](X,4-X)=2
Z+1->Z
End
If Z=1 and =0
Goto P4
Lbl PR				Label PR (Pick a Random place)
randInt(1,3)->X			Random X coord
randInt(1,3)->Y			Random Y coord
If [A](X,Y) (is not) 0		If this isn't a empty place
Goto PR				then do another random
2->[A](X,Y)			Else, pick the place
Goto E				and leave the program
Lbl 2S				Label 2s (player 2 Started)
randInt(1,8)->A			Random number to pick a starting place
If A>4				50 percent of the times,
2->[A](2,2)			CPU will pick the middle cell
If A=1				And in the rest of the cases
2->[A](1,1)			It will pick a corner
If A=2
2->[A](1,3)
If A=3
2->[A](3,1)
If A=4
2->[A](3,3)
Goto E				Leave the program
Lbl 1S				Label 1S (player 1 Started
For(X,1,3)			Double loop to check the starting place of the human player
For(Y,1,3)
If [A](X,Y)=1			If the place is found
Then
X->A				The coords are stored
Y->B
End
End
End
If not(A+B=4 and A=2)		If the player has chosen a corner
Then				Then CPU will chose the oposite cell
2->[A](4-A,4-B)
randInt(1,4)->C			I forgot why I put this random here.
Else				else, the CPU will chose a corner
If C=1
2->[A](1,1)
If C=2
2->[A](1,3)
If C=3
2->[A](3,1)
If C=4
2->[A](3,3)
End
Goto E				Leave the program
Lbl W1				Label W1 (Win 1)
For(Y,1,3)			A loop
If [A](X,Y)=0			To find the empty cell in the row
2->[A](X,Y)			And fill it up
End				end of loop
Goto E				leave the program
Lbl W2				Same thing as above
For(Y,1,3)
If [A](Y,X)=0
2->[A](Y,X)
End
Goto E
Lbl W3				Diagonal check
For(X,1,3
If [A](X,X)=0
2->[A](X,X)
End
Goto E
Lbl W4				And another one
For(X,1,3
If [A](X,4-X)=0
2->[A](X,4-X)
End
Goto E
Lbl P1				Label P1 (Pick a place 1)
randInt(1,3)->B			Choose a random place in the collumn
If [A](B,A) (is not) 0		Check if it's possesed
Goto P1				If so, Do another random
2->[A](B,A)			Else, select it
Goto E				leave the program
Lbl P2				Same as above
randInt(1,3)->B
If [A](A,B) (is not) 0
Goto P2
2->[A](A,B)
Goto E
Lbl P3				Same as above
randInt(1,3)->B
If [A](B,B) (is not) 0
Goto P3
2->[A](B,B)
Goto E
Lbl P4				Same as above
randInt(1,3)->B
If [A](B,4-B) (is not) 0
Goto P4
2->[A](B,4-B)
Lbl E						Label E (Exit)
Output(7,1,"                                "   Clear the progress bar
						And exit


That's it!!!

This is a really simple program, but it works. I hope you've got an idea of how this program works.
Maybe this code will help you program your own AI.
Good luck.