Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework_4 - Волков Денис #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions homeworks/homework_4/src/main/scala/Cell.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
trait Cell

case class EmptyCell() extends Cell {
override def toString: String = "empty"
}

case class NumberCell(number: Int) extends Cell
{
override def toString: String = number.toString
}

case class StringCell(text: String) extends Cell
{
override def toString: String = text
}

case class ReferenceCell(x: Int, y: Int, table: Table) extends Cell {
override def toString: String = {
table.getCell(x, y) match {
case Some(ref: ReferenceCell) =>
if (ref.table.getCell(ref.x, ref.y).get == this) "cyclic" else ref.toString
case other => table.getCell(x, y).get.toString
}
}
}

19 changes: 19 additions & 0 deletions homeworks/homework_4/src/main/scala/Table.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import scala.collection.mutable

class Table(var width: Int, var height: Int) {
private val table = mutable.HashMap[(Int, Int), Cell]()

def getCell(x: Int, y: Int): Option[Cell] = {
if(inRange(x, y))
table get((x, y)) orElse Some(new EmptyCell)
else
Option.empty[Cell]
}

def setCell(x: Int, y: Int, cell: Cell): Unit = {
table((x, y)) = cell
}

def inRange(x: Int, y: Int): Boolean =
x >= 0 && y >= 0 && x <= width && y <= height
}
2 changes: 1 addition & 1 deletion homeworks/homework_4/src/test/scala/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Test extends TestSuite {
'test_createTable - {
val table = new Table(3, 3)
for (i <- 0 until 9) {
assert(table.getCell(i / 3, i % 3).toString == Some("empty"))
assert(table.getCell(i / 3, i % 3).map(_.toString) == Some("empty"))
}
assert(table.getCell(0, -1).map(_.toString) == None)
assert(table.getCell(-1, 0).map(_.toString) == None)
Expand Down