[CS] TicTacToe

Martin Drautzburg martin.drautzburg at web.de
Sun Apr 27 23:39:23 UTC 2003

ingo at 2b1.de writes:
You asked: How can I make it resizable?

I am a newbie  myself, but this is what I'd do:

You need to draw your TicTacToe fields with respect to the owning Morph
(the TicTacToe). Therefore the TicTacToe field needs to know its row
and column and thus needs 2 more instance variables:

BorderedMorph subclass: #TicTacToeField
	instanceVariableNames: 'mark row col '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'IHO-Learning'

You then initialize them by passing row and column. And you can forget
about all other geometry stuff during initialisation because the
TicTacToeField will take care of that:

	| temp |
	super initialize.
	self extent: 300 @ 300.
	turn _ 0.
	winner _ nil.
		to: 3
		do: [:x | 1
				to: 3
				do: [:y | 
					temp _ TicTacToeField new row: y;
								 col: x.
					self addMorph: temp]]

Next thing is to compute the real geometry of TicTacToe field. Lets
put this into an extra method:

	| height width posx posy |
	height _ (owner height / 3) asInteger.
	width _ (owner width / 3) asInteger.
	self extent: width @ height.
	posx _ self col - 1 * width.
	posy _ self row - 1 * height.
	self position: owner position + (posx @ posy)

This has to be called before we draw the field, so:

drawOn: canvas 
	| markBounds |
	self computeLayout.
	super drawOn: canvas.
		ifNotNil: [markBounds _ self bounds scaleBy: 0.75.


More information about the Squeak-dev mailing list