forums

TNT Basic Forums > Games In Progress
Tic-tac-toe in TNT Basic
< Last Thread     Next Thread >
Author
Thread        Post A Reply

01-28-2007 01:43

Posted by:
JHVipond

Location:
South Dakota

Find more posts by JHVipond

I've decided that if I'm going to write game show programs in TNT Basic, I should master the familiar games first. Here's my preliminary plan for a tic-tac-toe game between one player and the computer, which I hope later to expand into either Tic Tac Dough or The Hollywood Squares:

Variables:
int cell[9] ' Array for game board cells
int n ' Counter in FOR loop
int winner ' Who won? (1=player, 2=computer)
string name ' Player's name

Only three images would be needed: the game board, an "X" sprite and an "O" sprite. The board cells are numbered 0,1,2 on the top row; 3,4,5 on the middle row, and 6,7,8 on the bottom row.

Instructions:
1. Display an empty board with nine cells. Set all cell values to 0. Set winner to 0.
2. Ask the player to choose "X" or "O". The computer takes the other letter. "X" always moves first.
3. The player, on his/her turn, clicks on a desired cell with the mouse. Set that cell's value to 1 and display the player's letter in that cell.
4. The computer, on its turn, selects an empty cell at random, unless it must block. (For example, if the player has taken cells 0 and 4, the computer must take cell 8.) Set that cell's value to 2 and display the computer's letter in that cell.
5. Check the following lines for three of the same letter in a row: 0-1-2, 0-3-6, 0-4-8, 1-4-7, 2-4-6, 2-5-8, 3-4-5, 6-7-8.
6. If the three cells in any line all equal 1, then set winner to 1 (the player won).
7. If the three cells in any line all equal 2, then set winner to 2 (the computer won).
8. Offer to play another game.

The most difficult parts to program, as I see it, are the computer's blocking strategy and checking for a winner.

01-28-2007 23:02

Posted by:
Tim

Location:
New Zealand

Find more posts by Tim

Yeah, AI or rules are not something to be dismissed easily, but they can be the make or break of any game. I think my old STOS Game Makers basic book has something about AI programming in it. As you might know, TNT is based on STOS. A lot of the commands are identical.

01-29-2007 00:05

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

"Game Makers Manual" by Stephen Hill?

Damn fine book... Should be compulsory reading for all TNT Basic users!

Danny (nods)

01-29-2007 02:31

Posted by:
Tim

Location:
New Zealand

Find more posts by Tim

Yep that's the one, brilliant, has an awful lot in it.

01-29-2007 19:59

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

the best way to do an Ai for tic-tac-toe is to set priorities for each sqaure.

01-29-2007 21:33

Posted by:
Tim

Location:
New Zealand

Find more posts by Tim

Has anyone seen examples of AI done in basic any where? I checked Mr Hills Book last night, and nothing there. Although he does dive into the logic and how to for RPGs which may have interested appleid...complete with code.

01-29-2007 22:12

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

snif snif - I am starting to miss my old Amiga 2000 =)

01-29-2007 23:31

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Had a couple of Atari STs myself... Halcyon days!

But nostalgia aside, I don't think the decision to put a nought or a cross in one of nine boxes is going to require too much in the way of artificial intelligence... I've seen "War Games" and it would only end in tears anyway ;)

There are only really two decisions here, "Can I put a piece somewhere that will win me the game?" or "Can I stop the other guy doing it?". If both of these return a false result then place a random piece. These can all be implemented by pattern matching a very small 2D array, which will need to be done anyway to check for a victory condition.
I'd pick an empty square and check to see if this one will win or potentially lose the game, this needs four checks for the centre square, three for the corners and two for the others. If you get a positive result then make the move, otherwise add the square to a list. If they're all negative just pick a random array index from the list and go there. If there are no squares left it's a draw.

Well have fun, I'm off to boot up an emulator... See you all back in the eighties!

Danny (nods)

01-30-2007 18:23

Posted by:
Tim

Location:
New Zealand

Find more posts by Tim

Array Checks?

may be some array checks like


1 1 1
0 0 0
0 0 0

0 0 1
0 1 0
1 0 0

02-09-2007 19:29

Posted by:
Jason Anderson

Location:
Doylestown, PA

Click Here to Email Jason Anderson   Find more posts by Jason Anderson

I wrote a Tic Tac Toe game in VB years ago. I just used array checking. I actually made it have different skill levels too. I wish I still had that code. It was my first real working program. I miss it.

11-01-2009 18:07

Posted by:
JHVipond

Location:
South Dakota

Find more posts by JHVipond

Source code is online

I've uploaded the current source code for a TNT Basic port of tic-tac-toe, based on the Atari BASIC program in Fred D'Ignazio's book:
http://wat.midco.net/jvipond/assets/TicTacToe.tbproj.sitx

In this version of the game, the human player places X's by clicking on empty cells on the game board, and the computer places O's where it can. At present, I'm having difficulties with the procedures where the computer looks for a win or a block. Does the following table of priority moves make sense (0=best move, 8=worst move)? Any help would be appreciated!

1 | 5 | 2
--+--+--
6 | 0 | 7
--+--+--
3 | 8 | 4

11-01-2009 22:47

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Er... JH, it don't work mate...

With the unmodified source code you posted absolutely nothing happens. You click twice (anywhere you like, it doesn't matter) and you get told you've lost... No sprites, no nothing, just 9 squares and some text.

I changed your display code so that it actually shows the updates (I just added the DrawBoard() call to both of the Place() calls, just before the 'end proc', DrawBoard() calls 'draw frame' so this works and we now get sprites). This, I'm afraid, didn't really help matters: Same problem only with graphics...

There is a serious issue with your code logic:

With PlaceX() - if the player clicks on a square they don't own then they get it, OK, no problems with that. But what happens (and I want you to check your code for this!) if they click on a square that they already own, or is owned by the AI? Same goes for PlaceO(). This (I believe) is the main cause of your problems... What would happen if the user clicked 5 times on the same square? See my point?

The second issue I would raise is the way you check for a click. This isn't a biggy, but there are far 'nicer' ways of checking the location of a user's click than using sprite collisions. I know the code you cribbed from wasn't TNT, but Atari basic didn't have the range of features that TNT does (though it did have good old fashioned maths). I would look at other ways of detecting the squares.

I'm sorry if this sounds like I'm having a go, or being patronising, this isn't my intention at all. I can see the problems with the code and I know how to fix them (I've been doing this sort of thing for a long time now), I'm just pointing them out so you know where to look and can hopefully fix them yourself. If you need more help then please post here again or PM me (address is cunningly hidden to the left!).

Good luck with the debug...

Danny (nod the mod)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.