Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find minimum and maximum of cartesian co-ordinates in Scala
    primarykey
    data
    text
    <p>I am trying to solve GameOfLife in Scala where I have an infinte grid. I am trying to represent the grid as a Set of Cell(x,y). When I read from say String I start at (0,0). But because of the laws of GameOfLife and since I am considering Infinite Grid after having applied rules to my Generation class I want to print current generation.</p> <p>Here I am not sure how to calculate the minimum position (read x,y iterators) from where to start iterating and printing either 'X' for alive cell and '-' for dead cell in GameOfLife for that Generation.I am supplying my naive solution of toString method of Generation class. But I am not at all happy with it. Can somebody suggest be a better succient solution?</p> <pre><code>override def toString:String = { val output:StringBuilder = new StringBuilder(); val minOfRowColumn = for { cell &lt;- aliveCells row = cell.row column = cell.column } yield if( row &lt; column ) row else column val min = minOfRowColumn.min val maxOfRowColumn = for { cell &lt;- aliveCells row = cell.row column = cell.column } yield if( row &gt; column ) row else column val max = maxOfRowColumn.max var row = min; var column = min; while(row &lt;= max) { while(column &lt;= max) { if(aliveCells.contains(Cell(row,column))) { output.append('X') } else output.append('-') column = column + 1 } output.append("\n"); column = min row = row + 1 } //remove the last new line addded. val indexOfNewLine = output.lastIndexOf("\n"); if( -1 != indexOfNewLine ) output.delete(indexOfNewLine,output.length()); return output.toString(); } </code></pre> <p>aliveCells here is a Set[Cell] where Cell is Cell(x,y) a case class.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload