From 68fb0767d5c2c48442a26bd3bbd4f5c35756daca Mon Sep 17 00:00:00 2001 From: Enzo JOLYS Date: Tue, 28 Mar 2023 08:40:51 +0200 Subject: [PATCH 01/14] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'README.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 67978a6..4ca2863 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Tetris - https://stackoverflow.com/questions/21002224/add-a-background-image-to-shape-in-xml-android ## Autheur : From bc3b7ea840eae035b2fb97891c6f0a69d716836d Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Tue, 28 Mar 2023 08:48:36 +0200 Subject: [PATCH 02/14] correction manifest --- Tetris/app/src/main/AndroidManifest.xml | 27 ------------------- .../java/tetrisGame/activity/MainActivity.kt | 1 - 2 files changed, 28 deletions(-) diff --git a/Tetris/app/src/main/AndroidManifest.xml b/Tetris/app/src/main/AndroidManifest.xml index 6c7d171..902036d 100644 --- a/Tetris/app/src/main/AndroidManifest.xml +++ b/Tetris/app/src/main/AndroidManifest.xml @@ -11,34 +11,7 @@ android:supportsRtl="true" android:theme="@style/Theme.Tetris" tools:targetApi="31"> - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Tetris/app/src/main/java/tetrisGame/activity/MainActivity.kt b/Tetris/app/src/main/java/tetrisGame/activity/MainActivity.kt index bac5d66..5d4a67e 100644 --- a/Tetris/app/src/main/java/tetrisGame/activity/MainActivity.kt +++ b/Tetris/app/src/main/java/tetrisGame/activity/MainActivity.kt @@ -17,7 +17,6 @@ class MainActivity : AppCompatActivity() { .add(R.id.homeLayout,MainFragment()) .commit() } - } override fun onResume() { From 3a28a28c6b49d760d4b12ccc009250787f338253 Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Tue, 28 Mar 2023 16:18:47 +0200 Subject: [PATCH 03/14] correction manifest --- Tetris/app/src/main/AndroidManifest.xml | 30 ------------------------- 1 file changed, 30 deletions(-) diff --git a/Tetris/app/src/main/AndroidManifest.xml b/Tetris/app/src/main/AndroidManifest.xml index 51d0ed1..d884f48 100644 --- a/Tetris/app/src/main/AndroidManifest.xml +++ b/Tetris/app/src/main/AndroidManifest.xml @@ -11,36 +11,6 @@ android:supportsRtl="true" android:theme="@style/Theme.Tetris" tools:targetApi="31"> - - - - - - - - - - - - - - - - - - - - - - - - - - From 08441acca31b03467d81e64506a054ee79796772 Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Thu, 30 Mar 2023 16:12:07 +0200 Subject: [PATCH 04/14] rebase on Enzo --- Tetris/app/src/main/java/views/ViewsGame.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/Tetris/app/src/main/java/views/ViewsGame.kt b/Tetris/app/src/main/java/views/ViewsGame.kt index c7d2549..7e118ee 100644 --- a/Tetris/app/src/main/java/views/ViewsGame.kt +++ b/Tetris/app/src/main/java/views/ViewsGame.kt @@ -14,6 +14,7 @@ class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) { override fun onDraw(canvas: Canvas) { super.onDraw(canvas) + val tailleGrille:Float = 5F val tailleTableauHauteur:Float = measuredHeight.toFloat() val tailleTableauLargeur:Float = measuredWidth.toFloat() From 5eb619760cdfe53c844c2e678195b416f06de498 Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Thu, 30 Mar 2023 22:40:32 +0200 Subject: [PATCH 05/14] fonction moveRight,deleteShape,AddShape,WriteShape et un peux de modif partout --- .../src/main/java/activity/MainActivity.kt | 2 + .../but/androidstudio/tetris/GameFragment.kt | 23 +++- Tetris/app/src/main/java/modele/DashBoard.kt | 125 +++++++++++++++--- Tetris/app/src/main/java/modele/Game.kt | 26 +++- Tetris/app/src/main/java/modele/Position.kt | 10 ++ Tetris/app/src/main/java/modele/Shape.kt | 2 +- Tetris/app/src/main/java/modele/TypeShape.kt | 23 +++- Tetris/app/src/main/java/views/ViewsGame.kt | 14 +- .../app/src/main/res/layout/fragment_game.xml | 3 +- .../app/src/main/res/layout/fragment_main.xml | 8 +- Tetris/app/src/main/res/values/strings.xml | 4 + 11 files changed, 198 insertions(+), 42 deletions(-) diff --git a/Tetris/app/src/main/java/activity/MainActivity.kt b/Tetris/app/src/main/java/activity/MainActivity.kt index 5d4a67e..5464166 100644 --- a/Tetris/app/src/main/java/activity/MainActivity.kt +++ b/Tetris/app/src/main/java/activity/MainActivity.kt @@ -10,7 +10,9 @@ import but.androidstudio.tetris.R class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + setContentView(R.layout.main_activity) + if(savedInstanceState == null){ supportFragmentManager.beginTransaction() .setReorderingAllowed(true) 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 1a4232c..53b9df7 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt @@ -5,9 +5,17 @@ import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Button +import modele.Game import views.ViewsGame 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 + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } @@ -23,9 +31,18 @@ class GameFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val view = view.findViewById(R.id.tabGame) + // La vue se refresh quand on fait quelque chose dessus (changer un parametre ) + viewGame = view.findViewById(R.id.tabGame) + viewGame.nbCaseHauteur = heightGame + viewGame.nbCaseLargeur = withGame + + modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame) + + val buttonRight:Button = view.findViewById(R.id.Button_Right) + buttonRight.setOnClickListener { + modeleGame.getDashbord().moveRight(modeleGame.currentShape) + } - //To clean the window - view.invalidate() + 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 60ead99..2b3580e 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -1,7 +1,9 @@ package modele -class DashBoard(private val width: Int,private val height: Int) { - private val gridOfGame = Array(this.height){IntArray(this.width)} +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) } // Our getter fun getWidth(): Int = this.width @@ -9,52 +11,137 @@ class DashBoard(private val width: Int,private val height: Int) { // To set something to occupied - fun toOccupied(col: Int,row: Int){ - gridOfGame[row][col] = 1 + fun toOccupied(col: Int, row: Int, value: Int) { + gridOfGame[row][col] = value } // To check if an position is occupied - fun isOccupied(col: Int,row: Int): Boolean = gridOfGame[row][col] != 0 + fun isOccupied(col: Int, row: Int): Boolean = gridOfGame[row][col] != 0 - fun isLineFull(row: Int): Boolean{ - for (col in 0 until this.width){ - if(gridOfGame[row][col] == 0){ + fun isLineFull(row: Int): Boolean { + for (col in 0 until this.width) { + if (gridOfGame[row][col] == 0) { return false } } return true } - fun clearLine(row: Int){ - for (col in 0 until this.width){ + fun clearLine(row: Int) { + for (col in 0 until this.width) { gridOfGame[row][col] = 0 } } - fun shiftDown(rowToBegin: Int){ - for (row in (height - 1)..rowToBegin){ - for (col in 0 until this.width){ + fun shiftDown(rowToBegin: Int) { + for (row in (height - 1)..rowToBegin) { + for (col in 0 until this.width) { gridOfGame[row][col] = gridOfGame[row - rowToBegin][col] } } - for (row in 0 until rowToBegin){ - for (col in 0 until this.width){ + for (row in 0 until rowToBegin) { + for (col in 0 until this.width) { gridOfGame[row][col] = 0 } } } //To check each grid line and remove if a line is full. Uses clearLine(), isLineFull() and shiftDown() - fun clearLines(){ + fun clearLines() { var nbRowCleared: Int = 0 - for (row in 0 until this.height){ - if(isLineFull(row)){ + for (row in 0 until this.height) { + if (isLineFull(row)) { clearLine(row) ++nbRowCleared } } - if(nbRowCleared != 0){ + if (nbRowCleared != 0) { shiftDown(nbRowCleared) } } + + 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 + } + } + } + writeShape(shape) + } + else -> { + println("Type de piece incomprehensible !!") + return false + } + } + return false + } + + + // ATTENTION ELLE BUG PAS DE COLLISION + fun moveRight(shape:Shape):Position{ + //Check les limite du tableau ou mettre un try + for ( line in 0..3){ // Check si le décalage est possible + for ( column in 0..3){ + if ( column != 3) { + 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 ((shape.typeShape.showShape[line][column] == 1) + and (shape.typeShape.showShape[line][column] == 0) + and (gridOfGame[shape.position.getX() + line][shape.position.getY() + column + 1] == 1) + ) { + return shape.position + } + } + } + } + deleteShape(shape) + shape.position.addX() + writeShape(shape) + return shape.position + } + + // Delete a shape in gridOfGame + private fun deleteShape(shape: Shape){ + 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 + } + } + } + } + + // Write the shape in gridOfGame + private fun writeShape(shape: Shape){ + 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] = + shape.typeShape.couleur + } + } + } + updateViewGame() + } + + // Update the view + fun updateViewGame(){ + println("-----------------") + view.tableau = gridOfGame + } } \ 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 d7d0b9d..278f6b6 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -1,25 +1,39 @@ package modele -class Game(private val width: Int,private val height: Int) { - private val dashBoard: DashBoard = DashBoard(width,height) - private var currentShape: Shape? = null + +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) + lateinit var currentShape: Shape private var difficulty: Difficulty = Difficulty.EASY // To get the current difficulty fun getDifficulty(): Difficulty = this.difficulty - // To get the current shape on the game - fun getCurrentShape(): Shape? = this.currentShape + fun getShape():Shape = this.currentShape // To set the current shape - fun setCurrentShape(newCurrentShape: 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)) + + dashBoard.addShape(currentShape) + currentShape.position = dashBoard.moveRight(currentShape) + + + println(currentShape.position.getX()) + println(currentShape.position.getY()) + dashBoard.updateViewGame() } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/Position.kt b/Tetris/app/src/main/java/modele/Position.kt index 4d8cfad..eec3ff0 100644 --- a/Tetris/app/src/main/java/modele/Position.kt +++ b/Tetris/app/src/main/java/modele/Position.kt @@ -11,4 +11,14 @@ class Position(private var x: Int,private var y: Int) { fun setY(newY: Int){ this.y = newY } + + // Incrémente X + fun addX(){ + this.x+=1 + } + + // Incrémente y + fun addY(){ + 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 1624f03..46364de 100644 --- a/Tetris/app/src/main/java/modele/Shape.kt +++ b/Tetris/app/src/main/java/modele/Shape.kt @@ -2,7 +2,7 @@ package modele import java.lang.reflect.Type -class Shape(private val typeShape: TypeShape,private var position: Position) { +class Shape(val typeShape: TypeShape,var position: Position) { private var shapePosition : Array = emptyArray() diff --git a/Tetris/app/src/main/java/modele/TypeShape.kt b/Tetris/app/src/main/java/modele/TypeShape.kt index 819393b..c02cadd 100644 --- a/Tetris/app/src/main/java/modele/TypeShape.kt +++ b/Tetris/app/src/main/java/modele/TypeShape.kt @@ -3,10 +3,28 @@ package modele import java.lang.reflect.Type import kotlin.random.Random -class TypeShape(private val type: EnumTypeShape,private val showShape: Array>>){ +class TypeShape(val type: EnumTypeShape/*,private val showShape: Array>>*/){ private var currentIndex = 0 + lateinit var showShape:Array + var couleur:Int = 0 + init { + when(type){ + EnumTypeShape.IShape -> { + couleur = 1 // Put 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 + } + } + else -> { + println("Type inconnu !") + } + } + } + + /* fun getPoints(): Array{ val positions = ArrayList() val typeShape = getCurrentType() @@ -26,6 +44,7 @@ class TypeShape(private val type: EnumTypeShape,private val showShape: Array> { return showShape[currentIndex] - } + }*/ } \ No newline at end of file diff --git a/Tetris/app/src/main/java/views/ViewsGame.kt b/Tetris/app/src/main/java/views/ViewsGame.kt index 7e118ee..d2ce54f 100644 --- a/Tetris/app/src/main/java/views/ViewsGame.kt +++ b/Tetris/app/src/main/java/views/ViewsGame.kt @@ -9,18 +9,19 @@ import android.view.View class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) { - val myPaint:Paint = Paint() + private val myPaint:Paint = Paint() + private var tailleGrille:Float = 5F + var nbCaseLargeur:Int = 20 // Value default + var nbCaseHauteur:Int = 20 // Value default + var tableau = Array(this.nbCaseHauteur){IntArray(this.nbCaseLargeur)} + override fun onDraw(canvas: Canvas) { super.onDraw(canvas) - - val tailleGrille:Float = 5F val tailleTableauHauteur:Float = measuredHeight.toFloat() val tailleTableauLargeur:Float = measuredWidth.toFloat() - val nbCaseLargeur:Int = 7 - val nbCaseHauteur:Int = 15 - + /* ----------------------------------- TEST VIEW --------------------- // Pour remplir le tableau de zero ( a enlever) var tableau = arrayOf>() for (i in 0 until nbCaseHauteur) { @@ -67,6 +68,7 @@ class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) { tableau[6][4] = 7 tableau[7][4] = 7 tableau[7][5] = 7 + */ myPaint.color = Color.GRAY canvas.drawRect(0F,0F,tailleTableauLargeur,tailleTableauHauteur,myPaint) diff --git a/Tetris/app/src/main/res/layout/fragment_game.xml b/Tetris/app/src/main/res/layout/fragment_game.xml index c33fcb0..cfa7b3d 100644 --- a/Tetris/app/src/main/res/layout/fragment_game.xml +++ b/Tetris/app/src/main/res/layout/fragment_game.xml @@ -20,7 +20,8 @@ + android:layout_height="wrap_content" + /> + android:text="@string/start" />