diff --git a/Tetris.mdj b/Documentations/Tetris.mdj similarity index 100% rename from Tetris.mdj rename to Documentations/Tetris.mdj diff --git a/Tetris_Diagramme.drawio b/Documentations/Tetris_Diagramme.drawio similarity index 100% rename from Tetris_Diagramme.drawio rename to Documentations/Tetris_Diagramme.drawio diff --git a/Documentations/tetris-pieces.jpg b/Documentations/tetris-pieces.jpg new file mode 100644 index 0000000..bebd326 Binary files /dev/null and b/Documentations/tetris-pieces.jpg differ diff --git a/README.md b/README.md index 67978a6..21bdd5a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # Tetris - https://stackoverflow.com/questions/21002224/add-a-background-image-to-shape-in-xml-android ## Autheur : - Enzo Jolys - Alexis Carreau +## Pièces : + + + diff --git a/Tetris/app/src/main/java/activity/MainActivity.kt b/Tetris/app/src/main/java/activity/MainActivity.kt index bac5d66..5464166 100644 --- a/Tetris/app/src/main/java/activity/MainActivity.kt +++ b/Tetris/app/src/main/java/activity/MainActivity.kt @@ -10,14 +10,15 @@ 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) .add(R.id.homeLayout,MainFragment()) .commit() } - } override fun onResume() { 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 8deddf0..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,12 +5,17 @@ import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch +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) } @@ -26,8 +31,18 @@ class GameFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val view = view.findViewById(R.id.tabGame) - val game: Game = Game(10,20,view) - game.startGame() + // 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) + } + + 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..3a8f424 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,170 @@ 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 à droite !!!! A vérifier que ca marche !!! + var limiteInitial = width-shape.position.getX() + var limite:Int = 0 + + if (limiteInitial >= 4 ){ + limite = 4 + }else { + limite = limiteInitial + } + + 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 + } + } + } + } + deleteShape(shape) + shape.position.addX() + writeShape(shape) + return shape.position + } + + fun moveLeft(shape:Shape):Position{ + + // Check if X == 0 + if ( shape.position.getX() == 0 ){ + return shape.position + } + + 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 + } + } + } + } + + + 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(){ + view.tableau = gridOfGame + view.invalidate() + } } \ 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 b550eab..278f6b6 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -1,35 +1,39 @@ package modele -import android.util.Log -import but.androidstudio.tetris.R -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch +import android.view.InputQueue import views.ViewsGame -class Game(private val width: Int,private val height: Int,private val viewsGame: ViewsGame) { - private val dashBoard: DashBoard = DashBoard(width,height) - private var currentShape: Shape? = null - private var table: Array> = arrayOf(arrayOf()) - +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(){ - Log.d("toto","toto") + //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()) - Log.d("toto","toto404") + 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..db597d4 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 // 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 + } + } + 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 7c9519c..4234455 100644 --- a/Tetris/app/src/main/java/views/ViewsGame.kt +++ b/Tetris/app/src/main/java/views/ViewsGame.kt @@ -9,22 +9,19 @@ import android.view.View class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) { - val myPaint:Paint = Paint() - var tableau = arrayOf>() + 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)} - fun setCoordinateTable(table: Array>){ - this.tableau = table - } 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) for (i in 0 until nbCaseHauteur) { var array = arrayOf() @@ -69,7 +66,12 @@ class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) { tableau[6][3] = 7 tableau[6][4] = 7 tableau[7][4] = 7 +<<<<<<< HEAD tableau[7][5] = 7*/ +======= + tableau[7][5] = 7 + */ +>>>>>>> Enzo 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 4940f57..1efa397 100644 --- a/Tetris/app/src/main/res/layout/fragment_game.xml +++ b/Tetris/app/src/main/res/layout/fragment_game.xml @@ -21,7 +21,8 @@ + android:layout_height="wrap_content" + />