diff --git a/Tetris/app/src/main/java/modele/DashBoard.kt b/Tetris/app/src/main/java/modele/DashBoard.kt index 9bafb84..88eca50 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -1,8 +1,8 @@ package modele -import android.util.Log import kotlinx.coroutines.delay import views.ViewsGame +import kotlin.io.path.fileVisitor class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) { val gridOfGame = Array(this.height) { IntArray(this.width) } @@ -32,30 +32,33 @@ class DashBoard(private val width: Int,private val height: Int,private val view: } } - fun shiftDown(rowToBegin: Int) { - for (row in (height - 1)..rowToBegin) { - for (col in 0 until this.width) { - gridOfGame[row][col] = gridOfGame[row - rowToBegin][col] - } - } - for (row in 0 until rowToBegin) { - for (col in 0 until this.width) { - gridOfGame[row][col] = 0 + fun shiftDown(listeLine:MutableList) { + + println("Shift Down") + for( index in listeLine){ + println(index) + for ( line in index downTo 1 ){ + for ( column in 0 until width){ + gridOfGame[line][column] = gridOfGame[line-1][column] + } } } + updateViewGame() } //To check each grid line and remove if a line is full. Uses clearLine(), isLineFull() and shiftDown() fun clearLines():Int{ + val listeLine = mutableListOf() var nbRowCleared: Int = 0 for (row in 0 until this.height) { if (isLineFull(row)) { clearLine(row) ++nbRowCleared + listeLine.add(row) } } if (nbRowCleared != 0) { - shiftDown(nbRowCleared) + shiftDown(listeLine) } return nbRowCleared } @@ -64,11 +67,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: for (line in 0..3) { // On vérifie que l'espace est disponible for (column in 0..3) { - if ((isOccupied( - shape.position.x + column, - shape.position.y + line - )) and (shape.typeShape.showShape[line][column] == 1) - ) { + if ( (shape.typeShape.showShape[line][column] == 1) and (gridOfGame[shape.position.y+line][shape.position.x+column] != 0)){ return false } } @@ -112,9 +111,6 @@ class DashBoard(private val width: Int,private val height: Int,private val view: val pos:MutableList = shape.sharePositionLeft() for ( position in pos){ - println("X -> : "+position.x) - println("X -> : "+position.y) - if ( shape.position.x < 0 ){ return false } @@ -236,8 +232,12 @@ class DashBoard(private val width: Int,private val height: Int,private val view: view.invalidate() } - suspend fun fallingShape(shape: Shape):Boolean{ - delay(800) + suspend fun fallingShape(shape: Shape, difficulty: Difficulty):Boolean{ + when(difficulty){ + Difficulty.EASY -> delay(800) + Difficulty.MEDIUM -> delay(600) + Difficulty.HARD -> delay(400) + } return moveDown(shape) } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/Game.kt b/Tetris/app/src/main/java/modele/Game.kt index a98febb..8a9e07c 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -1,26 +1,35 @@ package modele -import android.annotation.SuppressLint -import android.util.Log import android.widget.TextView import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import views.ViewsGame +import kotlin.random.Random class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame,private val points:TextView) { val dashBoard: DashBoard = DashBoard(width,height,viewGame) lateinit var currentShape: Shape - var difficulty: Difficulty = Difficulty.EASY - - + private var difficulty: Difficulty = Difficulty.EASY + + + //To get the next shape + private fun getNextShape(): TypeShape { + return when(Random.nextInt(1,7)){ + 1 -> TypeShape(EnumTypeShape.IShape) + 2 -> TypeShape(EnumTypeShape.SquareShape) + 3 -> TypeShape(EnumTypeShape.JShape) + 4 -> TypeShape(EnumTypeShape.LShape) + 5 -> TypeShape(EnumTypeShape.SShape) + 6 -> TypeShape(EnumTypeShape.TShape) + 7 -> TypeShape(EnumTypeShape.ZShape) + else -> throw Exception("Problème de random getNextShape()") + } + } // The start game function fun startGame(){ - val listeTypeShape: List = listOf(EnumTypeShape.ZShape,EnumTypeShape.JShape,EnumTypeShape.IShape,EnumTypeShape.LShape,EnumTypeShape.TShape,EnumTypeShape.SquareShape,EnumTypeShape.SShape) - var indexListeTypeShape:Int = 0 //currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1)) //dashBoard.addShape(currentShape) @@ -42,17 +51,8 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V //currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1)) //dashBoard.addShape(currentShape) - /* - dashBoard.gridOfGame[0][0] = 5 - dashBoard.gridOfGame[0][1] = 5 - dashBoard.gridOfGame[0][2] = 5 - dashBoard.gridOfGame[0][3] = 5 - dashBoard.gridOfGame[0][4] = 5 - dashBoard.gridOfGame[0][5] = 5 - dashBoard.gridOfGame[0][6] = 5 - */ - currentShape = Shape(TypeShape(listeTypeShape[indexListeTypeShape]),Position(width/2,0)) - indexListeTypeShape++ + + currentShape = Shape(getNextShape(),Position(width/2,0)) dashBoard.addShape(currentShape) println("RUN !!") @@ -60,11 +60,12 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V GlobalScope.launch { while(true){ - if(! dashBoard.fallingShape(currentShape)){ - if ( indexListeTypeShape == 7 ){ - indexListeTypeShape = 0 - } + if(dashBoard.fallingShape(currentShape,difficulty)){ + + // Clear line val nbLine = dashBoard.clearLines() + + // Score if ( (nbLine > 0) and (nbLine<4)){ val tmpPoints:String = points.text as String points.text = (tmpPoints.toInt()+(nbLine*100)).toString() @@ -73,12 +74,16 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V val tmpPoints:String = points.text as String points.text = (tmpPoints.toInt()+1200).toString() } - - currentShape = Shape(TypeShape(listeTypeShape[indexListeTypeShape]),Position(width/2,0)) - dashBoard.addShape(currentShape) - indexListeTypeShape++ + } + else { + // New shape + currentShape = Shape(getNextShape(),Position(width/2,0)) + if ( !dashBoard.addShape(currentShape)){ + break + } } } + println("Game end !!") } } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/TypeShape.kt b/Tetris/app/src/main/java/modele/TypeShape.kt index ceeb7db..77717b5 100644 --- a/Tetris/app/src/main/java/modele/TypeShape.kt +++ b/Tetris/app/src/main/java/modele/TypeShape.kt @@ -84,25 +84,6 @@ class TypeShape(val type: EnumTypeShape){ currentIndex++ return this } + */ - - //To get the next shape - fun getNextShape(){ - val random: Int = Random.nextInt(1,7) - when(random){ - 1 -> TypeShape(EnumTypeShape.IShape, arrayOf(arrayOf(arrayOf()))) - 2 -> TypeShape(EnumTypeShape.SquareShape, arrayOf(arrayOf(arrayOf()))) - 3 -> TypeShape(EnumTypeShape.JShape, arrayOf(arrayOf(arrayOf()))) - 4 -> TypeShape(EnumTypeShape.LShape, arrayOf(arrayOf(arrayOf()))) - 5 -> TypeShape(EnumTypeShape.SShape, arrayOf(arrayOf(arrayOf()))) - 6 -> TypeShape(EnumTypeShape.TShape, arrayOf(arrayOf(arrayOf()))) - 7 -> TypeShape(EnumTypeShape.ZShape, arrayOf(arrayOf(arrayOf()))) - else -> throw Exception("Problème de random getNextShape()") - } - } - - // To get the table of position of the current shape type - private fun getCurrentType(): Array> { - return showShape[currentIndex] - }*/ } \ No newline at end of file