[CS] TicTacToe

Bibbers bibbers at onetel.net.uk
Mon Apr 28 11:17:07 UTC 2003

On Sunday, April 27, 2003, at 06:11 PM, ingo at 2b1.de wrote:

> from preamble:
> "Change Set:		TicTacToe
> Date:			27 April 2003
> Author:			Ingo Hohmann
> Just a little Morphic project - to learn Squeak, Morphic and so ...
> It implements a two player TicTacToe game.
> And of course, I hope to get this thing right.
> "!


I have tried your game and for anybody else that's also still getting to 
grips with Morphic I've posted my comments here...they are related to your 
comments in the Transcript window after installing the code.

DRAWING A CROSS: I've modified your drawing code to draw the red mark as a 
cross instead of a circle...

drawOn: canvas
	| markBounds |
	super drawOn: canvas.
		ifNotNil: [markBounds _ self bounds scaleBy: 0.75.
			markBounds _ markBounds translateBy: self center - markBounds center.
			"In my working Version of Squeak (3.5+something) there is a
			Rectangle center:
			but in the clean 3.5 it seems to be missing, why?"
			mark = 0
				ifTrue: ["canvas fillOval: markBounds color: Color red"
						line: markBounds topLeft
						to: markBounds bottomRight
						width: 5
						color: Color red.
						line: markBounds topRight
						to: markBounds bottomLeft
						width: 5
						color: Color red]
				ifFalse: [canvas fillOval: markBounds color: Color green]]

MAKING IT RESIZABLE: You are close! The way you draw the TicTacToe field 
morph means it will scale. You can try this by using the resize halo to 
stretch it and you will see what I mean. That's good. However, to make it 
truly resizable you need to change the TicTacToe class to use a table 
layout. I won't give that away here, I'll leave it as an exercise to the 
reader. It ain't that hard! Study the ChessMorph>>addButtonRow, or look 
for 'references' to AlignmentMorph (195 in my image, plenty to choose from!
) and you will soon get the hang of it! The sheer beauty of Squeak/Morphic 
is you can twiddle all of it on live objects to see what happens. If you 
still can't figure it out, mail me off-list.

WINNING CONDITION CHECKING: Welcome to game programming. Messy sometimes. 
Given the way that you have implemented the game, this is as good a way as 
any for testing for a completed line. Me being a bit-basher would have 
probably have maintained two 16-bit numbers, one for each player with bit0 
for submorph at:1 etc and then used bit-masks to test three bits at once, 
the bit patterns having a 1 set for the presence of a mark at that 
position. Assumption: only one mark occupies the same square! Any 
hard-core AI types care to offer a solution?

DISPLAYING A WINNING MESSAGE: A cheap and cheery solution would be to 
replace the bottom of TicTacToe>>setMark with this:

ifTrue: [winner _ (turn \\ 2) asString.
			Transcript show: 'Player ' , winner , ' wins';
			self showBalloon: 'A WINNER
Player' , winner , ' has won' hand: nil]

It get's the message over and it takes care of removing itself as well!

Sean Charles.

More information about the Squeak-dev mailing list