From a985dce119b4d7e10adcf96cb4153a2c596f1a6d Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Sat, 1 Apr 2023 00:46:21 +0200 Subject: [PATCH] move right,move left,move down et les pieces --- .../but/androidstudio/tetris/GameFragment.kt | 11 +- Tetris/app/src/main/java/modele/DashBoard.kt | 156 ++++++++++-------- Tetris/app/src/main/java/modele/Game.kt | 34 ++-- Tetris/app/src/main/java/modele/Position.kt | 20 +-- Tetris/app/src/main/java/modele/Shape.kt | 70 ++++++-- Tetris/app/src/main/java/modele/TypeShape.kt | 47 +++++- 6 files changed, 224 insertions(+), 114 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 53b9df7..31d904e 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt @@ -39,8 +39,17 @@ class GameFragment : Fragment() { modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame) val buttonRight:Button = view.findViewById(R.id.Button_Right) + val buttonLeft:Button = view.findViewById(R.id.Button_Left) + val buttonRotateRight:Button = view.findViewById(R.id.Button_Right_Rotation) + val buttonRotateLeft:Button = view.findViewById(R.id.Button_Left_Rotation) buttonRight.setOnClickListener { - modeleGame.getDashbord().moveRight(modeleGame.currentShape) + modeleGame.dashBoard.moveRight(modeleGame.currentShape) + } + buttonLeft.setOnClickListener { + modeleGame.dashBoard.moveLeft(modeleGame.currentShape) + } + buttonRotateRight.setOnClickListener { + modeleGame.dashBoard.moveDown(modeleGame.currentShape) } modeleGame.startGame() diff --git a/Tetris/app/src/main/java/modele/DashBoard.kt b/Tetris/app/src/main/java/modele/DashBoard.kt index 3a8f424..f1d12ef 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -3,7 +3,7 @@ package modele import views.ViewsGame class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) { - private val gridOfGame = Array(this.height) { IntArray(this.width) } + val gridOfGame = Array(this.height) { IntArray(this.width) } // Our getter fun getWidth(): Int = this.width @@ -61,91 +61,110 @@ class DashBoard(private val width: Int,private val height: Int,private val view: } fun addShape(shape: Shape): Boolean { - when (shape.typeShape.type) { - EnumTypeShape.IShape -> { // Shape I (la ligne) - // Mettre ICI des verification ou un try pour les limite du tableau - - for (line in 0..3) { // On vérifie que l'espace est disponible - for (column in 0..3) { - if ((isOccupied( - shape.position.getX() + column, - shape.position.getY() + line - )) and (shape.typeShape.showShape[line][column] == 1) - ) { - return false - } - } + + 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) + ) { + return false } - writeShape(shape) - } - else -> { - println("Type de piece incomprehensible !!") - return false } } - return false + writeShape(shape) + return true } + // Test si c'est possible de l'intégré dans gridOfGame + private fun moveRightPossible(shape: Shape):Boolean{ - // ATTENTION ELLE BUG PAS DE COLLISION - fun moveRight(shape:Shape):Position{ - //Check les limite du tableau à droite !!!! A vérifier que ca marche !!! - var limiteInitial = width-shape.position.getX() - var limite:Int = 0 + val pos:MutableList = shape.sharePositionRight() - if (limiteInitial >= 4 ){ - limite = 4 - }else { - limite = limiteInitial + for ( position in pos){ + if ( position.x+shape.position.x >= width){ + return false + } + if( gridOfGame[position.y+shape.position.y][position.x+shape.position.x] != 0) { + return false + } } + return true + } - for ( line in 0..limite){ // Check si le décalage est possible - for ( column in 0..limite){ - if ( column != limite) { - if ((shape.typeShape.showShape[line][column] == 1) - and (shape.typeShape.showShape[line][column + 1] == 0) - and (gridOfGame[shape.position.getX() + line][shape.position.getY() + column + 1] == 1) - ) { - return shape.position - } - }else{ - if ( limite == limiteInitial and shape.typeShape.showShape[line][column] == 1){ - return shape.position - } - if ((shape.typeShape.showShape[line][column] == 1) - and (gridOfGame[shape.position.getX() + line][shape.position.getY() + column + 1] == 1) - ) { - return shape.position - } - } - } + fun moveRight(shape: Shape):Boolean{ + println("Shape action -> Move Right ! ") + shape.position.addX() + if (!moveRightPossible(shape)){ + shape.position.decrementeX() + return false } + shape.position.decrementeX() deleteShape(shape) shape.position.addX() writeShape(shape) - return shape.position + return true } - fun moveLeft(shape:Shape):Position{ + // Test si c'est possible de l'intégré dans gridOfGame + private fun moveLeftPossible(shape: Shape):Boolean{ + val pos:MutableList = shape.sharePositionLeft() - // Check if X == 0 - if ( shape.position.getX() == 0 ){ - return shape.position - } + for ( position in pos){ + println("X -> : "+position.x) + println("X -> : "+position.y) - for( line in 0..3){ - for( column in 0..3 ){ - if ( column == 0 ){ - if ( (shape.typeShape.showShape[line][column] == 1) - and (gridOfGame[shape.position.getX()+line][shape.position.getY()+column-1] == 1) ){ - return shape.position - } - } + if ( shape.position.x < 0 ){ + return false + } + if( gridOfGame[position.y+shape.position.y][position.x+shape.position.x] != 0) { + return false } } + return true + } + fun moveLeft(shape:Shape):Boolean{ + println("Shape action -> Move Left ! ") + shape.position.decrementeX() + if (!moveLeftPossible(shape)){ + shape.position.addX() + return false + } + shape.position.addX() + deleteShape(shape) + shape.position.decrementeX() + writeShape(shape) + return true + } + private fun moveDownPossible(shape: Shape):Boolean{ + + val pos:MutableList = shape.sharePositionRight() - return shape.position + for ( position in pos){ + if ( position.y+shape.position.y >= height){ + return false + } + if( gridOfGame[position.y+shape.position.y][position.x+shape.position.x] != 0) { + return false + } + } + return true + } + + fun moveDown(shape: Shape):Boolean{ + println("Shape action -> Move down ! ") + shape.position.addY() + if (!moveDownPossible(shape)){ + shape.position.decrementeY() + return false + } + shape.position.decrementeY() + deleteShape(shape) + shape.position.addY() + writeShape(shape) + return true } // Delete a shape in gridOfGame @@ -153,7 +172,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: for (line in 0..3) { for (column in 0..3) { if (shape.typeShape.showShape[line][column] == 1) { - gridOfGame[shape.position.getX() + line][shape.position.getY() + column] = 0 + gridOfGame[shape.position.y + line][shape.position.x + column] = 0 } } } @@ -161,10 +180,13 @@ 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) + println("Final Y : "+shape.position.y) + for (line in 0..3) { for (column in 0..3) { if (shape.typeShape.showShape[line][column] == 1) { - gridOfGame[shape.position.getY() + line][shape.position.getX() + column] = + gridOfGame[shape.position.y+ line][shape.position.x + column] = shape.typeShape.couleur } } diff --git a/Tetris/app/src/main/java/modele/Game.kt b/Tetris/app/src/main/java/modele/Game.kt index 278f6b6..81c63c3 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -4,35 +4,39 @@ import android.view.InputQueue import views.ViewsGame class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame) { - private val dashBoard: DashBoard = DashBoard(width,height,viewGame) + val dashBoard: DashBoard = DashBoard(width,height,viewGame) lateinit var currentShape: Shape - private var difficulty: Difficulty = Difficulty.EASY + var difficulty: Difficulty = Difficulty.EASY - // To get the current difficulty - fun getDifficulty(): Difficulty = this.difficulty - fun getShape():Shape = this.currentShape - // To set the current shape - fun setShape(newCurrentShape:Shape){ - this.currentShape = newCurrentShape - } - - fun getDashbord():DashBoard = this.dashBoard // The start game function fun startGame(){ //this.setCurrentShape(TypeShape.SquareShape) - currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(1,1)) + 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.position = dashBoard.moveRight(currentShape) - println(currentShape.position.getX()) - println(currentShape.position.getY()) + dashBoard.gridOfGame[1][0] = 2 + dashBoard.gridOfGame[1][6] = 3 + //println(currentShape.position.getX()) + //println(currentShape.position.getY()) + println("RUN !!") dashBoard.updateViewGame() } diff --git a/Tetris/app/src/main/java/modele/Position.kt b/Tetris/app/src/main/java/modele/Position.kt index eec3ff0..695f67c 100644 --- a/Tetris/app/src/main/java/modele/Position.kt +++ b/Tetris/app/src/main/java/modele/Position.kt @@ -1,16 +1,6 @@ package modele -class Position(private var x: Int,private var y: Int) { - fun getX() = this.x - fun getY() = this.y - - fun setX(newX: Int){ - this.x = newX - } - - fun setY(newY: Int){ - this.y = newY - } +class Position(var x: Int,var y: Int) { // Incrémente X fun addX(){ @@ -21,4 +11,12 @@ class Position(private var x: Int,private var y: Int) { fun addY(){ this.y+=1 } + + fun decrementeX(){ + this.x-=1 + } + + fun decrementeY(){ + this.y-=1 + } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/Shape.kt b/Tetris/app/src/main/java/modele/Shape.kt index 46364de..94e3b79 100644 --- a/Tetris/app/src/main/java/modele/Shape.kt +++ b/Tetris/app/src/main/java/modele/Shape.kt @@ -4,38 +4,74 @@ import java.lang.reflect.Type class Shape(val typeShape: TypeShape,var position: Position) { - private var shapePosition : Array = emptyArray() + //private var sharePosition = mutableListOf() /* init { val }*/ - fun moveRight(){ - this.position.setX(this.position.getX()+1) - //update + fun sharePositionRight():MutableList{ + val sharePosition = mutableListOf() + for( line in 0..3){ + for (column in 0..3) { + if ( column != 3 ){ + if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line][column+1] == 0)){ + sharePosition.add(Position(column,line)) + } + } + if ( (column == 3) and (typeShape.showShape[line][column] == 1) ){ + sharePosition.add(Position(column,line)) + } + } + } + return sharePosition } - fun moveLeft(){ - this.position.setX(this.position.getX()-1) - //update + fun sharePositionLeft():MutableList{ + val sharePosition = mutableListOf() + for( line in 0..3){ + for (column in 0..3) { + if ( column != 0 ){ + if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line][column-1] == 0)){ + sharePosition.add(Position(column,line)) + } + } + if ( (column == 0) and (typeShape.showShape[line][column] == 1) ){ + sharePosition.add(Position(column,line)) + } + } + } + return sharePosition } - fun moveDown(){ - this.position.setY(this.position.getY()-1) - //update - } + fun sharePositionDown(shape: Shape):MutableList{ + val sharePosition = mutableListOf() + for( line in 0..3){ + for (column in 0..3) { + if ( line != 3 ){ + if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line+1][column] == 0)){ + sharePosition.add(Position(column,line)) + } + } + if ( (line == 3) and (typeShape.showShape[line][column] == 1) ){ + sharePosition.add(Position(column,line)) + } + } + } + return sharePosition + } fun rotateLeft(){ - val tmp = this.position.getX() - this.position.setX(this.position.getY() * -1) - this.position.setY(tmp) + //val tmp = this.position.getX() + //this.position.setX(this.position.getY() * -1) + //this.position.setY(tmp) //update } fun rotateRight(){ - val tmp = this.position.getX() - this.position.setX(this.position.getY()) - this.position.setY(tmp * -1) + //val tmp = this.position.getX() + //this.position.setX(this.position.getY()) + //this.position.setY(tmp * -1) //update } } \ 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 db597d4..ceeb7db 100644 --- a/Tetris/app/src/main/java/modele/TypeShape.kt +++ b/Tetris/app/src/main/java/modele/TypeShape.kt @@ -3,21 +3,62 @@ package modele import java.lang.reflect.Type import kotlin.random.Random -class TypeShape(val type: EnumTypeShape/*,private val showShape: Array>>*/){ +class TypeShape(val type: EnumTypeShape){ private var currentIndex = 0 - lateinit var showShape:Array + var showShape:Array = Array(4){ IntArray(4 ) { 0 } } var couleur:Int = 0 init { when(type){ EnumTypeShape.IShape -> { couleur = 1 // Set the color of the shape - showShape = Array(4){ IntArray(4 ) { 0 } } // tableau a 2 dimensions a 0 for( i in 0..3){ showShape[0][i] = 1 // Create shape } } + EnumTypeShape.SquareShape -> { + couleur = 6 + showShape[0][0] = 1 + showShape[0][1] = 1 + showShape[1][0] = 1 + showShape[1][1] = 1 + } + EnumTypeShape.TShape -> { + couleur = 7 + showShape[0][1] = 1 + showShape[1][0] = 1 + showShape[1][1] = 1 + showShape[1][2] = 1 + } + EnumTypeShape.LShape -> { + couleur = 5 + showShape[0][2] = 1 + showShape[1][0] = 1 + showShape[1][1] = 1 + showShape[1][2] = 1 + } + EnumTypeShape.JShape -> { + couleur = 4 + showShape[0][0] = 1 + showShape[1][0] = 1 + showShape[1][1] = 1 + showShape[1][2] = 1 + } + EnumTypeShape.SShape -> { + couleur = 3 + showShape[0][1] = 1 + showShape[0][2] = 1 + showShape[1][0] = 1 + showShape[1][1] = 1 + } + EnumTypeShape.ZShape -> { + couleur = 2 + showShape[0][0] = 1 + showShape[0][1] = 1 + showShape[1][1] = 1 + showShape[1][2] = 1 + } else -> { println("Type inconnu !") }