T3 - TicTacToe
T3.BAS

Platform: MSX 1.0
Author: Christian Dijkstra
Language: MSX BASIC 1.0
Category: PUR-120

Description of the game:
The Classic TicTacToe - (Boter Kaas en Eieren) game. The target is to get a three in a row (Horizontal, vertical or diagonal)
After the start, the player is asked who is playing 'X'
Input 0 for player self, 1 selects MSX. In the last case the MSX is playing against itself.
The player for O is defaulted to MSX.

When it is the players turn, a number in the range 1-9 must be selected, if a cell is selected which already contains a 'X' or 'O' a BEEP is given.
This is also the case when it is the turn of the computer. Or S is for stopping the game. This is repeated until there is a winner or all the fields contain a 'X' or 'O'.

How to emulate:
For simplicity the preferred emulator is blueMSX in MSX mode. (http://www.emulator-zone.com/doc.php/msx/bluemsx.html)
Setting MSX mode via [Options->Emulation: MSX Machine: MSX]
Set a folder as a disk [File->Disk Drive A->Insert Directory] where the BAS file is stored

Start the MSX emulation. In the BASIC environment:

For a list of files enter FILES

To load a file
load"a:t3.bas"
and then run

or load"a:t3.bas",r

Also Tested on openMSX using Sony HitBit-75P roms with Sony HBD-50

Breakdown of the program:

Line 1 - 3
1 S1$="T3-TicTacToe":S2$="Select a number 1-9":S3$="or S to stop":S4$="'s turn":S5$=" Won":S6$="Another game? <Y/N>"
2 S7$=" (0=You;1=MSX)":H$="-+-+-":DIMT$(1):T$(0)="X":T$(1)="O":DIMG$(8):DEFFNT3(A,B)=(G$(A)=G$(A+(B-A)/2)ANDG$(A)=G$(B))
3 DIMP(1):P(0)=1:P(1)=1:DEFFNGT=(FNT3(0,2)ORFNT3(3,5)ORFNT3(6,8)ORFNT3(0,8)ORFNT3(0,6)ORFNT3(1,7)ORFNT3(2,8)ORFNT3(2,6))

Initialize the variables and define some helper functions for the tests if there is a three on a row.
The array P in line 3 defines the player, P(0)=1 means that for the 'X' the MSX is the player, this value gets altered in line 5.
By changing the value for P(1)=1 to 0, it possible use this program as a two player game.

Line 4
4 V$="|":X=RND(-TIME):KEYOFF:CLS:GL=0:FORI=0TO8:G$(I)=CHR$(49+I):NEXTI:LOCATE0,0:?S1$,S2$:?,S3$:?:FORGL=0TO9

Initialize the randomizer, fill the playfield, grid (3x) and start the actual gameloop

Line 5 - 6
5 IFGL=0THEN?,"Who X?";S7$;:P(0)=VAL(INPUT$(1)):GOTO 7 ELSEIFP(T)=1THENK=INT((RND(1)*9)+1):K$="M"ELSEK$=INKEY$:K=VAL(K$)
6 IFK$=""ORK<1ORK>9THEN 5 ELSEIFK$="S"ORK$="s"THEN 10 ELSEIFG$(K-1)<="9" THEN G$(K-1)=T$(T) ELSE BEEP:GOTO 5

If the first loop is started (GL=0) then the program asks who is playing X and sets P(0) defined in line 3.
In the case of the computer is playing a random value (no AI) is selected, or in case of the player the input is checked for valid values.

Line 7
7 T=GLMOD2:LOCATE0,5:FORI=0TO2:?,G$(I*3);V$;G$(I*3+1);V$;G$(I*3+2):IFI=2THEN?ELSE?,H$:NEXTI

The current player is set, and the playfield is printed to the screen.

Line 8
8 LOCATE0,12:IFFNGTTHEN?,T$((GL-1)MOD2)+S5$;SPC(3):GL=9:GOTO10:ELSE?,T$(T);S4$;:IFP(T)=0THEN?" (You)"ELSE?" (MSX)"

Test if there is a three in a row after the turn is played and printed. If there is winner he is called, or else the player for the next turn is shown.

Line 9
9 NEXTGL:LOCATE0,12:?,"A Draw";SPC(9) ' Written for the homeputerium's BASIC Tenliner Contest 2016 - Christian Dijkstra

If the GL>9 and there is no winner, then it must be a draw.

Line 10
10 LOCATE0,14:?S6$:K$=INKEY$:IFK$=""THEN 10 ELSEIFK$="y"ORK$="Y"THEN 4 ELSEIFK$="n"ORK$="N"THENCLS:KEYON:ENDELSE 10

Ask the player for another game, if answered Y then the game starts over again