rotation droite et gauche

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

@ -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()
}
}

@ -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<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
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)

@ -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())

@ -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<IntArray>{
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<IntArray>{
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
}
}
Loading…
Cancel
Save