rotation droite et gauche

pull/8/head
Jolys Enzo 2 years ago
parent 9993f3ac09
commit 13f3896881

@ -1,21 +1,27 @@
package but.androidstudio.tetris package but.androidstudio.tetris
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorManager
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import androidx.core.content.ContextCompat.getSystemService
import modele.Game import modele.Game
import views.ViewsGame import views.ViewsGame
class GameFragment : Fragment() { class GameFragment : Fragment(){
private val heightGame:Int = 15 // Line number private val heightGame:Int = 15 // Line number
private val withGame:Int = 7 // Column number private val withGame:Int = 7 // Column number
private lateinit var viewGame:ViewsGame private lateinit var viewGame:ViewsGame
private lateinit var modeleGame:Game private lateinit var modeleGame:Game
private lateinit var sensorManager: SensorManager
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
@ -49,9 +55,13 @@ class GameFragment : Fragment() {
modeleGame.dashBoard.moveLeft(modeleGame.currentShape) modeleGame.dashBoard.moveLeft(modeleGame.currentShape)
} }
buttonRotateRight.setOnClickListener { buttonRotateRight.setOnClickListener {
modeleGame.dashBoard.moveDown(modeleGame.currentShape) modeleGame.dashBoard.rotateShapeRight(modeleGame.currentShape)
}
buttonRotateLeft.setOnClickListener {
modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape)
} }
modeleGame.startGame() modeleGame.startGame()
} }
} }

@ -164,6 +164,42 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
return true return true
} }
private fun rotationShapePossible(shape: Shape,rotation:Int):Boolean{
val matrix:Array<IntArray> = 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 // Delete a shape in gridOfGame
private fun deleteShape(shape: Shape){ private fun deleteShape(shape: Shape){
for (line in 0..3) { 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 // Write the shape in gridOfGame
private fun writeShape(shape: Shape){ private fun writeShape(shape: Shape){
println("Final X : "+shape.position.x) println("Final X : "+shape.position.x)

@ -14,25 +14,27 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
// The start game function // The start game function
fun startGame(){ fun startGame(){
//this.setCurrentShape(TypeShape.SquareShape)
currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5)) currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5))
dashBoard.addShape(currentShape) dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6)) //currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6))
dashBoard.addShape(currentShape) //dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.TShape),Position(2,8)) //currentShape = Shape(TypeShape(EnumTypeShape.TShape),Position(2,8))
dashBoard.addShape(currentShape) //dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.ZShape),Position(2,10)) //currentShape = Shape(TypeShape(EnumTypeShape.ZShape),Position(2,10))
dashBoard.addShape(currentShape) //dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1)) //currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
dashBoard.addShape(currentShape) //dashBoard.addShape(currentShape)
//currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1))
//dashBoard.addShape(currentShape)
dashBoard.gridOfGame[1][0] = 2 //dashBoard.gridOfGame[2][0] = 5
dashBoard.gridOfGame[1][6] = 3
//println(currentShape.position.getX()) //println(currentShape.position.getX())
//println(currentShape.position.getY()) //println(currentShape.position.getY())

@ -61,17 +61,65 @@ class Shape(val typeShape: TypeShape,var position: Position) {
return sharePosition return sharePosition
} }
fun rotateLeft(){
//val tmp = this.position.getX() fun sharePositionRotationRight(shape: Shape):Array<IntArray>{
//this.position.setX(this.position.getY() * -1) var leftmostCol = 4
//this.position.setY(tmp) val rotatedMatrix = Array(4) { row ->
//update 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(){ fun sharePositionRotationLeft(shape: Shape):Array<IntArray>{
//val tmp = this.position.getX() var leftmostCol = 4
//this.position.setX(this.position.getY()) // Pivoter la matrice
//this.position.setY(tmp * -1) val rotatedMatrix = Array(4) { row ->
//update 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
} }
} }
Loading…
Cancel
Save