Skip to content

Commit cd4d0f3

Browse files
committed
Add ID of world and number of iteration when displaying in terminal. Now
we can choose the char for alive and dead blocks. Also, to run the Test() method, if the ID of the world doesn't exist, it creates a new world.
1 parent b85fb41 commit cd4d0f3

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

OPNLib/Game/ConwayLifeGame.cls

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ Property Iterations As %Integer [ InitialExpression = 0 ];
3434
/// By default, original rules: 2 or 3 to keep alive, 3 for a new born
3535
Property RulesOfLife As %String [ InitialExpression = "2.3/3" ];
3636

37+
/// Char that represents an alive block when displayed in terminal
38+
Property AliveChar as %String [Transient, InitialExpression = {$char(14)} ];
39+
40+
/// Char that represents a dead block when displayed in terminal
41+
Property DeadChar as %String [Transient, InitialExpression = "-"];
42+
43+
3744
/// documentation
3845
Index idxIDKEY On World [ IdKey ];
3946

@@ -414,12 +421,13 @@ Method Display(pIteration As %Integer = 0, pFromVector As %String = "", pToVecto
414421
set tRowIni = $p(pFromVector,",",2)
415422
set tRowFin = $p(pToVector,",",2)
416423

424+
write ?(((tColFin - tColIni)\2)-5),"World:[",..World,"] >",pIteration,"<"
417425
for i = tRowIni:1:tRowFin
418426
{
419427
write !," "
420428
for j = tColIni:1:tColFin
421429
{
422-
write $case($bit(^OPNLib.Game.CWLF(..World,pIteration,i),j),"1":$c(164),:"-")
430+
write $case($bit(^OPNLib.Game.CWLF(..World,pIteration,i),j),"1":..AliveChar,:..DeadChar)
423431
}
424432
}
425433
}
@@ -483,25 +491,37 @@ ClassMethod Test(pTest As %DynamicObject = "") as %Status
483491
{
484492
#dim e as %Exception.General
485493
#dim tSC as %Status = $$$OK
494+
#dim g as OPNLib.Game.ConwayLifeGame
486495

487496
try
488497
{
489498
if '($IsObject(pTest)&&(pTest.%ClassName()="%DynamicObject"))
490499
{
491-
set pTest = {"ID":1,"From":0,"To":200,"Iterations":200,"InitialConfig":5,"Rows":80,"Columns":150,"Vector0":"1,1","VectorN":"100,40"}
500+
set pTest = {"ID":1,"From":0,"To":200,"Iterations":200,"InitialConfig":5,"Rows":50,"Columns":130,"Vector0":"1,1","VectorN":"100,40","AliveChar":($c(164)),"DeadChar":"-"}
492501
}
493502

494503
write !,"These are the settings for the test:",!
495504
do pTest.%ToJSON()
496505
read !,"Press any key to continue... (Ctrl-C to stop)",x
497506

498-
set g = ##class(OPNLib.Game.ConwayLifeGame).%OpenId(pTest.ID)
499-
507+
If ..%ExistsId(pTest.ID)
508+
{
509+
set g = ##class(OPNLib.Game.ConwayLifeGame).%OpenId(pTest.ID)
510+
}
511+
else
512+
{
513+
write !,"World ID introduced doesn't exist. Creating a new world for you...."
514+
set g = ##class(OPNLib.Game.ConwayLifeGame).%New()
515+
}
516+
500517
$$$TOE(tSC,$IsObject(g))
501518

502519
set g.Rows = pTest.Rows
503520
set g.Columns = pTest.Columns
504521

522+
set:(pTest.AliveChar'="") g.AliveChar = pTest.AliveChar
523+
set:(pTest.DeadChar'="") g.DeadChar = pTest.DeadChar
524+
505525
do g.Initialize(pTest.InitialConfig)
506526
do g.Iterate(pTest.Iterations)
507527

0 commit comments

Comments
 (0)