Created
November 14, 2014 20:36
-
-
Save MatMoore/a11d2820c87a59407d5f to your computer and use it in GitHub Desktop.
West london hack night november 2014
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tetris | |
/** | |
* Created by mat on 12/11/2014. | |
*/ | |
abstract class shape { | |
var orientation: Int = 0 | |
val orientations: Array[Array[Point]] | |
def rotate(direction: Int): Array[Point] = { | |
orientation = orientation + direction | |
if (orientation < 0) orientation = orientation + 4 | |
else if (orientation > 3) orientation = orientation - 4 | |
orientations(orientation) | |
} | |
} | |
case class Point(val x: Int, val y: Int) | |
class shape_i extends shape { | |
override val orientations = Seq( | |
Seq(Point(0, 0), Point(0, 1), Point(0, 2), Point(0, 3)).toArray[Point], | |
Seq(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 0)).toArray[Point], | |
Seq(Point(0, 0), Point(0, 1), Point(0, 2), Point(0, 3)).toArray[Point], | |
Seq(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 0)).toArray[Point] | |
).toArray | |
} | |
object Tetris extends App { | |
val shapeGrid = for (y <- 0 to 3; x <- 0 to 3) yield (x, y) | |
def renderShape(shapePoints: Seq[Point]) = { | |
val pointSet = shapePoints.toSet | |
shapeGrid map( point => if(pointSet.contains(Point(point._1, point._2))) "X" else " ") | |
} | |
val my_i_render = renderShape((new shape_i).rotate(0)) | |
val lines = my_i_render.grouped(4) | |
lines.foreach (line => { | |
line.foreach(print(_)) | |
println() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment