From 13f389688100ade15032f30b0a03d4c1cbbfb1ff Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Mon, 3 Apr 2023 01:29:35 +0200 Subject: [PATCH] rotation droite et gauche --- .../but/androidstudio/tetris/GameFragment.kt | 14 +++- Tetris/app/src/main/java/modele/DashBoard.kt | 38 +++++++++++ Tetris/app/src/main/java/modele/Game.kt | 24 ++++--- Tetris/app/src/main/java/modele/Shape.kt | 68 ++++++++++++++++--- 4 files changed, 121 insertions(+), 23 deletions(-) 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 31d904e..6eeebb9 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt @@ -1,21 +1,27 @@ package but.androidstudio.tetris +import android.content.Context +import android.hardware.Sensor +import android.hardware.SensorManager import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button +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) } @@ -49,9 +55,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 74a6410..e6aaf97 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -164,6 +164,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) { @@ -175,6 +211,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) diff --git a/Tetris/app/src/main/java/modele/Game.kt b/Tetris/app/src/main/java/modele/Game.kt index 81c63c3..f1ded19 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -14,25 +14,27 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V // The start game function fun startGame(){ - //this.setCurrentShape(TypeShape.SquareShape) + + 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.SquareShape),Position(2,6)) + //dashBoard.addShape(currentShape) - currentShape = Shape(TypeShape(EnumTypeShape.TShape),Position(2,8)) - 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.ZShape),Position(2,10)) + //dashBoard.addShape(currentShape) - currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1)) - 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[1][0] = 2 - dashBoard.gridOfGame[1][6] = 3 + //dashBoard.gridOfGame[2][0] = 5 //println(currentShape.position.getX()) //println(currentShape.position.getY()) diff --git a/Tetris/app/src/main/java/modele/Shape.kt b/Tetris/app/src/main/java/modele/Shape.kt index 94e3b79..3f8b7d5 100644 --- a/Tetris/app/src/main/java/modele/Shape.kt +++ b/Tetris/app/src/main/java/modele/Shape.kt @@ -61,17 +61,65 @@ class Shape(val typeShape: TypeShape,var position: Position) { return sharePosition } - fun rotateLeft(){ - //val tmp = this.position.getX() - //this.position.setX(this.position.getY() * -1) - //this.position.setY(tmp) - //update + + 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 rotateRight(){ - //val tmp = this.position.getX() - //this.position.setX(this.position.getY()) - //this.position.setY(tmp * -1) - //update + 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 } } \ No newline at end of file