home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
komix
/
DATA.Z
/
TicTacToe_Game.sru
< prev
next >
Wrap
Text File
|
1997-09-30
|
6KB
|
259 lines
$PBExportHeader$tictactoe_game.sru
$PBExportComments$Generated by ObjectTeam
forward
global type tictactoe_game from nonvisualobject
end type
end forward
shared variables
// User defined attributes
// Non modeled user defined attributes
end variables
global type tictactoe_game from nonvisualobject
end type
global tictactoe_game tictactoe_game
type variables
// User defined attributes
integer turnNumber
string players = 'XO'
string drawText = 'It~'s a draw!!!'
string winText = ' is the winner!!!'
// Association attributes
public tictactoe_gui gui
private classdict boardDict
// Non modeled user defined attributes
// Control to class mappings
// tictactoe_game --> TicTacToe_Game
end variables
forward prototypes
public subroutine createCells ()
public subroutine connectCells ()
public subroutine startGame ()
public subroutine clearBoard ()
public subroutine checkResult (integer number)
public subroutine selectCell (integer number)
public function string getActivePlayer ()
public subroutine removeGui ()
public subroutine setGui (tictactoe_gui newGui)
public function tictactoe_gui getGui ()
public function tictactoe_cell getBoard (integer Index)
public subroutine setBoard (integer Index, tictactoe_cell newBoard)
public subroutine removeBoard (integer Index)
end prototypes
public subroutine createCells ();//
tictactoe_cell aCell
integer i
FOR i = 1 TO 9
aCell = create tictactoe_cell
setBoard(i,aCell)
NEXT
end subroutine
public subroutine connectCells ();//
integer N, S, E, W, MN, MS
integer i
tictactoe_cell cell
string dirs
N = -3
S = -N
E = 1
W = -E
MN = Abs(N)
MS = MN^2 - MN
FOR i = 1 TO 9
dirs = ""
cell = getboard(i)
cell.setneighbour('N', getBoard(i + N))
IF (Mod(i, MN) <> 0) THEN
cell.setNeighbour('NE', getBoard(i + N + E))
cell.setNeighbour('E', getBoard(i + E))
cell.setNeighbour('SE', getBoard(i + S + E))
END IF
cell.setNeighbour('S', getBoard(i + S))
IF (Mod(i, MN) <> 1) THEN
cell.setNeighbour('NW', getBoard(i + N + W))
cell.setNeighbour('W', getBoard(i + W))
cell.setNeighbour('SW', getBoard(i + S + W))
END IF
NEXT
/*
FOR i = 1 TO 9
dirs = ""
cell = getboard(i)
IF (i > MN) THEN
// not most north row
cell.setneighbour('N', getBoard(i + N))
dirs = dirs + ' N' + string(i + N)
END IF
IF (Mod(i, MN) <> 0) THEN
// not most east column
IF (i > MN) THEN
cell.setNeighbour('NE', getBoard(i + N + E))
dirs = dirs + ' NE' + string(i + N + E)
END IF
cell.setNeighbour('E', getBoard(i + E))
dirs = dirs + ' E' + string(i + E)
IF (i <= MS) THEN
cell.setNeighbour('SE', getBoard(i + S + E))
dirs = dirs + ' SE' + string(i + S + E)
END if
END IF
IF (i <= MS) THEN
// not most south row
cell.setNeighbour('S', getBoard(i + S))
dirs = dirs + ' S' + string(i + S)
END IF
IF (Mod(i, MN) <> 1) THEN
// not most west row
IF (i > MN) THEN
cell.setNeighbour('NW', getBoard(i + N + W))
dirs = dirs + ' NW' + string(i + N + W)
END IF
cell.setNeighbour('W', getBoard(i + W))
dirs = dirs + ' W' + string(i + W)
IF (i <= MS) THEN
cell.setNeighbour('SW', getBoard(i + S + W))
dirs = dirs + ' SW' + string(i + S + W)
END IF
END IF
messagebox("ttt_c:connectcells", "i:"+string(i)+"dirs:"+dirs+"~nmod:"+string(Mod(i, MN)))
NEXT
*/
end subroutine
public subroutine startGame ();//
turnnumber = 0
gui.clearboard()
this.clearboard()
gui.enableboard()
gui.displayResult('')
end subroutine
public subroutine clearBoard ();//
tictactoe_cell cell
integer i
FOR i = 1 TO (boardDict.size())
cell = getboard(i)
cell.contents = ' '
NEXT
end subroutine
public subroutine checkResult (integer number);//
tictactoe_cell cell
cell = getBoard(number)
IF (cell.getMaxLine() = 3) THEN
gui.disableBoard()
gui.displayResult(getActivePlayer() + wintext)
ELSEIF (turnnumber = (9 - 1)) THEN
gui.disableBoard()
gui.displayResult(drawtext)
END IF
turnnumber = turnnumber + 1
end subroutine
public subroutine selectCell (integer number);//
tictactoe_cell cell
cell = getboard(number)
cell.contents = getActivePlayer()
checkResult(number)
end subroutine
public function string getActivePlayer ();//
// messagebox("ttt_g", "players:"+ players +"~nturnnumber:"+string(turnnumber) +"~nLen:"+string(Len(players)) +"~nMod:"+string(Mod(turnnumber, Len(players))) +"~nMid:"+Mid(players, Mod(turnnumber, Len(players)) + 1, 1) )
return Mid(players, Mod(turnnumber, Len(players)) + 1, 1)
end function
public subroutine removeGui ();// Association accessor method
if not isNull(gui) then
setNull(gui.game)
end if
setNull(gui)
end subroutine
public subroutine setGui (tictactoe_gui newGui);// Association accessor method
if not isNull(gui) then
setNull(gui.game)
end if
if not isNull(newGui) then
TicTacToe_Game nullRef
setNull(nullRef)
newGui.setGame(nullRef)
newGui.game = this
end if
gui = newGui
end subroutine
public function tictactoe_gui getGui ();// Association accessor method
return gui
end function
public function tictactoe_cell getBoard (integer Index);// Association accessor method
return boardDict.get(Index)
end function
public subroutine setBoard (integer Index, tictactoe_cell newBoard);// Association accessor method
if isNull(newBoard) then
return
end if
boardDict.set(Index, newBoard)
end subroutine
public subroutine removeBoard (integer Index);// Association accessor method
powerobject object
object = boardDict.get(Index)
if isNull(object) then
return
end if
boardDict.remove(Index)
end subroutine
on tictactoe_game.create
TriggerEvent( this, "constructor" )
end on
on tictactoe_game.destroy
TriggerEvent( this, "destructor" )
end on
event constructor;setNull(gui)
boardDict = create classdict
// Start user code section
this.createcells()
this.connectcells()
// End user code section
end event
event destructor;// Start user code section
// End user code section
if not isNull(gui) then
setNull(gui.game)
end if
setNull(gui)
destroy(boardDict)
setNull(boardDict)
end event