diff --git a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt index 5cd7482..2be892e 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt @@ -1,5 +1,8 @@ package but.androidstudio.tetris +import android.content.Context +import android.hardware.Sensor +import android.hardware.SensorManager import android.os.Bundle import android.util.Log import androidx.fragment.app.Fragment @@ -10,16 +13,19 @@ import android.widget.Button import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import androidx.core.content.ContextCompat.getSystemService import modele.Game import views.ViewsGame -class GameFragment : Fragment() { +class GameFragment : Fragment(){ private val heightGame:Int = 15 // Line number private val withGame:Int = 7 // Column number private lateinit var viewGame:ViewsGame private lateinit var modeleGame:Game + private lateinit var sensorManager: SensorManager + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } @@ -53,9 +59,13 @@ class GameFragment : Fragment() { modeleGame.dashBoard.moveLeft(modeleGame.currentShape) } buttonRotateRight.setOnClickListener { - modeleGame.dashBoard.moveDown(modeleGame.currentShape) + modeleGame.dashBoard.rotateShapeRight(modeleGame.currentShape) + } + buttonRotateLeft.setOnClickListener { + modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape) } + modeleGame.startGame() } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/DashBoard.kt b/Tetris/app/src/main/java/modele/DashBoard.kt index 55e067b..e376a92 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -166,6 +166,42 @@ class DashBoard(private val width: Int,private val height: Int,private val view: return true } + private fun rotationShapePossible(shape: Shape,rotation:Int):Boolean{ + val matrix:Array = if ( rotation == 0){ + shape.sharePositionRotationRight(shape) + } else { + shape.sharePositionRotationLeft(shape) + } + + for ( line in 0..3){ + for ( column in 0..3){ + if ( (matrix[line][column] == 1) and (shape.typeShape.showShape[line][column] == 0 )){ + if ( gridOfGame[shape.position.y+column][shape.position.x+line] != 0 ) { + return false + } + } + } + } + shape.typeShape.showShape = matrix + return true + } + + + fun rotateShapeRight(shape: Shape){ + println("Shape action -> Rotation right ! ") + deleteShape(shape) + rotationShapePossible(shape,0) + writeShape(shape) + } + + + fun rotateShapeLeft(shape: Shape){ + println("Shape action -> Rotation left ! ") + deleteShape(shape) + rotationShapePossible(shape,1) + writeShape(shape) + } + // Delete a shape in gridOfGame private fun deleteShape(shape: Shape){ for (line in 0..3) { @@ -177,6 +213,8 @@ class DashBoard(private val width: Int,private val height: Int,private val view: } } + + // Write the shape in gridOfGame private fun writeShape(shape: Shape){ println("Final X : "+shape.position.x) @@ -203,8 +241,4 @@ class DashBoard(private val width: Int,private val height: Int,private val view: delay(800) moveDown(shape) } - - fun rotateRight(shape: 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 9bc397c..32f566e 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -22,8 +22,25 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V dashBoard.addShape(currentShape) - dashBoard.gridOfGame[1][0] = 2 - dashBoard.gridOfGame[1][6] = 3 + currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5)) + dashBoard.addShape(currentShape) + + //currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6)) + //dashBoard.addShape(currentShape) + + //currentShape = Shape(TypeShape(EnumTypeShape.TShape),Position(2,8)) + //dashBoard.addShape(currentShape) + + //currentShape = Shape(TypeShape(EnumTypeShape.ZShape),Position(2,10)) + //dashBoard.addShape(currentShape) + + //currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1)) + //dashBoard.addShape(currentShape) + + //currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1)) + //dashBoard.addShape(currentShape) + + //dashBoard.gridOfGame[2][0] = 5 println("RUN !!") dashBoard.updateViewGame() diff --git a/Tetris/app/src/main/java/modele/Shape.kt b/Tetris/app/src/main/java/modele/Shape.kt index 0d20b0a..464f509 100644 --- a/Tetris/app/src/main/java/modele/Shape.kt +++ b/Tetris/app/src/main/java/modele/Shape.kt @@ -54,6 +54,7 @@ class Shape(val typeShape: TypeShape,var position: Position) { return sharePosition } +<<<<<<< HEAD fun sharePositionUp(shape: Shape): MutableList{ val sharePosition = mutableListOf() @@ -71,5 +72,67 @@ class Shape(val typeShape: TypeShape,var position: Position) { } return sharePosition +======= + + fun sharePositionRotationRight(shape: Shape):Array{ + var leftmostCol = 4 + val rotatedMatrix = Array(4) { row -> + IntArray(4) { col -> + shape.typeShape.showShape[4 - col - 1][row] + } + } + // Trouver l'index de la colonne la plus à gauche + for (row in 0 until 4) { + for (col in 0 until 4) { + if (rotatedMatrix[row][col] != 0 && col < leftmostCol) { + leftmostCol = col + } + } + } + // Décaler chaque ligne de la matrice vers la gauche + for (row in 0 until 4) { + for (col in 0 until leftmostCol) { + rotatedMatrix[row][col] = 0 + } + for (col in leftmostCol until 4) { + rotatedMatrix[row][col - leftmostCol] = rotatedMatrix[row][col] + if ( col - leftmostCol != col){ + rotatedMatrix[row][col] = 0 + } + } + } + return rotatedMatrix + } + + fun sharePositionRotationLeft(shape: Shape):Array{ + var leftmostCol = 4 + // Pivoter la matrice + val rotatedMatrix = Array(4) { row -> + IntArray(4) { col -> + shape.typeShape.showShape[col][4 - row - 1] + } + } + // Trouver l'index de la colonne la plus à gauche + for (row in 0 until 4) { + for (col in 0 until 4) { + if (rotatedMatrix[row][col] != 0 && col < leftmostCol) { + leftmostCol = col + } + } + } + // Décaler chaque ligne de la matrice vers la gauche + for (row in 0 until 4) { + for (col in 0 until leftmostCol) { + rotatedMatrix[row][col] = 0 + } + for (col in leftmostCol until 4) { + rotatedMatrix[row][col - leftmostCol] = rotatedMatrix[row][col] + if ( col - leftmostCol != col){ + rotatedMatrix[row][col] = 0 + } + } + } + return rotatedMatrix +>>>>>>> Enzo } } \ No newline at end of file