Merge Enzo and Alexis into master

pull/10/head
Alexis1663 2 years ago
commit adc70edf03

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

@ -1,9 +1,12 @@
# Tetris # Tetris
https://stackoverflow.com/questions/21002224/add-a-background-image-to-shape-in-xml-android
## Autheur : ## Autheur :
- Enzo Jolys - Enzo Jolys
- Alexis Carreau - Alexis Carreau
## Pièces :
<img src="./Documentations/tetris-pieces.jpg">

@ -10,14 +10,15 @@ import but.androidstudio.tetris.R
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity) setContentView(R.layout.main_activity)
if(savedInstanceState == null){ if(savedInstanceState == null){
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction()
.setReorderingAllowed(true) .setReorderingAllowed(true)
.add(R.id.homeLayout,MainFragment()) .add(R.id.homeLayout,MainFragment())
.commit() .commit()
} }
} }
override fun onResume() { override fun onResume() {

@ -5,12 +5,17 @@ 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 kotlinx.coroutines.GlobalScope import android.widget.Button
import kotlinx.coroutines.launch
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 withGame:Int = 7 // Column number
private lateinit var viewGame:ViewsGame
private lateinit var modeleGame:Game
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
@ -26,8 +31,18 @@ class GameFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
val view = view.findViewById<ViewsGame>(R.id.tabGame) // La vue se refresh quand on fait quelque chose dessus (changer un parametre )
val game: Game = Game(10,20,view) viewGame = view.findViewById<ViewsGame>(R.id.tabGame)
game.startGame() 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()
} }
} }

@ -1,7 +1,9 @@
package modele package modele
class DashBoard(private val width: Int,private val height: Int) { import views.ViewsGame
private val gridOfGame = Array(this.height){IntArray(this.width)}
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 // Our getter
fun getWidth(): Int = this.width fun getWidth(): Int = this.width
@ -9,52 +11,170 @@ class DashBoard(private val width: Int,private val height: Int) {
// To set something to occupied // To set something to occupied
fun toOccupied(col: Int,row: Int){ fun toOccupied(col: Int, row: Int, value: Int) {
gridOfGame[row][col] = 1 gridOfGame[row][col] = value
} }
// To check if an position is occupied // 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{ fun isLineFull(row: Int): Boolean {
for (col in 0 until this.width){ for (col in 0 until this.width) {
if(gridOfGame[row][col] == 0){ if (gridOfGame[row][col] == 0) {
return false return false
} }
} }
return true return true
} }
fun clearLine(row: Int){ fun clearLine(row: Int) {
for (col in 0 until this.width){ for (col in 0 until this.width) {
gridOfGame[row][col] = 0 gridOfGame[row][col] = 0
} }
} }
fun shiftDown(rowToBegin: Int){ fun shiftDown(rowToBegin: Int) {
for (row in (height - 1)..rowToBegin){ for (row in (height - 1)..rowToBegin) {
for (col in 0 until this.width){ for (col in 0 until this.width) {
gridOfGame[row][col] = gridOfGame[row - rowToBegin][col] gridOfGame[row][col] = gridOfGame[row - rowToBegin][col]
} }
} }
for (row in 0 until rowToBegin){ for (row in 0 until rowToBegin) {
for (col in 0 until this.width){ for (col in 0 until this.width) {
gridOfGame[row][col] = 0 gridOfGame[row][col] = 0
} }
} }
} }
//To check each grid line and remove if a line is full. Uses clearLine(), isLineFull() and shiftDown() //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 var nbRowCleared: Int = 0
for (row in 0 until this.height){ for (row in 0 until this.height) {
if(isLineFull(row)){ if (isLineFull(row)) {
clearLine(row) clearLine(row)
++nbRowCleared ++nbRowCleared
} }
} }
if(nbRowCleared != 0){ if (nbRowCleared != 0) {
shiftDown(nbRowCleared) 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()
}
} }

@ -1,35 +1,39 @@
package modele package modele
import android.util.Log import android.view.InputQueue
import but.androidstudio.tetris.R
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import views.ViewsGame import views.ViewsGame
class Game(private val width: Int,private val height: Int,private val viewsGame: ViewsGame) { class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame) {
private val dashBoard: DashBoard = DashBoard(width,height) private val dashBoard: DashBoard = DashBoard(width,height,viewGame)
private var currentShape: Shape? = null lateinit var currentShape: Shape
private var table: Array<Array<Int>> = arrayOf(arrayOf())
private var difficulty: Difficulty = Difficulty.EASY private var difficulty: Difficulty = Difficulty.EASY
// To get the current difficulty // To get the current difficulty
fun getDifficulty(): Difficulty = this.difficulty fun getDifficulty(): Difficulty = this.difficulty
// To get the current shape on the game fun getShape():Shape = this.currentShape
fun getCurrentShape(): Shape? = this.currentShape
// To set the current shape // To set the current shape
fun setCurrentShape(newCurrentShape: Shape){ fun setShape(newCurrentShape:Shape){
this.currentShape = newCurrentShape this.currentShape = newCurrentShape
} }
fun getDashbord():DashBoard = this.dashBoard
// The start game function // The start game function
fun startGame(){ 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()
} }
} }

@ -11,4 +11,14 @@ class Position(private var x: Int,private var y: Int) {
fun setY(newY: Int){ fun setY(newY: Int){
this.y = newY this.y = newY
} }
// Incrémente X
fun addX(){
this.x+=1
}
// Incrémente y
fun addY(){
this.y+=1
}
} }

@ -2,7 +2,7 @@ package modele
import java.lang.reflect.Type 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<Position> = emptyArray() private var shapePosition : Array<Position> = emptyArray()

@ -3,10 +3,28 @@ package modele
import java.lang.reflect.Type import java.lang.reflect.Type
import kotlin.random.Random import kotlin.random.Random
class TypeShape(private val type: EnumTypeShape,private val showShape: Array<Array<Array<Int>>>){ class TypeShape(val type: EnumTypeShape/*,private val showShape: Array<Array<Array<Int>>>*/){
private var currentIndex = 0 private var currentIndex = 0
lateinit var showShape:Array<IntArray>
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<Position>{ fun getPoints(): Array<Position>{
val positions = ArrayList<Position>() val positions = ArrayList<Position>()
val typeShape = getCurrentType() val typeShape = getCurrentType()
@ -26,6 +44,7 @@ class TypeShape(private val type: EnumTypeShape,private val showShape: Array<Arr
return this return this
} }
//To get the next shape //To get the next shape
fun getNextShape(){ fun getNextShape(){
val random: Int = Random.nextInt(1,7) val random: Int = Random.nextInt(1,7)
@ -44,5 +63,5 @@ class TypeShape(private val type: EnumTypeShape,private val showShape: Array<Arr
// To get the table of position of the current shape type // To get the table of position of the current shape type
private fun getCurrentType(): Array<Array<Int>> { private fun getCurrentType(): Array<Array<Int>> {
return showShape[currentIndex] return showShape[currentIndex]
} }*/
} }

@ -9,22 +9,19 @@ import android.view.View
class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) { class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) {
val myPaint:Paint = Paint() private val myPaint:Paint = Paint()
var tableau = arrayOf<Array<Int>>() 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<Array<Int>>){
this.tableau = table
}
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
val tailleGrille:Float = 5F
val tailleTableauHauteur:Float = measuredHeight.toFloat() val tailleTableauHauteur:Float = measuredHeight.toFloat()
val tailleTableauLargeur:Float = measuredWidth.toFloat() val tailleTableauLargeur:Float = measuredWidth.toFloat()
val nbCaseLargeur:Int = 7 /* ----------------------------------- TEST VIEW ---------------------
val nbCaseHauteur:Int = 15
// Pour remplir le tableau de zero ( a enlever) // Pour remplir le tableau de zero ( a enlever)
for (i in 0 until nbCaseHauteur) { for (i in 0 until nbCaseHauteur) {
var array = arrayOf<Int>() var array = arrayOf<Int>()
@ -69,7 +66,12 @@ class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) {
tableau[6][3] = 7 tableau[6][3] = 7
tableau[6][4] = 7 tableau[6][4] = 7
tableau[7][4] = 7 tableau[7][4] = 7
<<<<<<< HEAD
tableau[7][5] = 7*/ tableau[7][5] = 7*/
=======
tableau[7][5] = 7
*/
>>>>>>> Enzo
myPaint.color = Color.GRAY myPaint.color = Color.GRAY
canvas.drawRect(0F,0F,tailleTableauLargeur,tailleTableauHauteur,myPaint) canvas.drawRect(0F,0F,tailleTableauLargeur,tailleTableauHauteur,myPaint)

@ -21,7 +21,8 @@
<views.ViewsGame <views.ViewsGame
android:id="@+id/tabGame" android:id="@+id/tabGame"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"
/>
</FrameLayout> </FrameLayout>
<LinearLayout <LinearLayout

@ -36,13 +36,13 @@
android:id="@+id/buttonStart" android:id="@+id/buttonStart"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Start" /> android:text="@string/start" />
<Button <Button
android:id="@+id/buttonOption" android:id="@+id/buttonOption"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Options" /> android:text="@string/options" />
</LinearLayout> </LinearLayout>
@ -55,13 +55,13 @@
android:id="@+id/bestScore" android:id="@+id/bestScore"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Best Score :" /> android:text="@string/best_score" />
<TextView <TextView
android:id="@+id/lastScore" android:id="@+id/lastScore"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="X" /> android:text="@string/x" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

@ -3,4 +3,8 @@
<string name="TextScore">Score : ??</string> <string name="TextScore">Score : ??</string>
<!-- TODO: Remove or change this placeholder text --> <!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string> <string name="hello_blank_fragment">Hello blank fragment</string>
<string name="start">Start</string>
<string name="options">Options</string>
<string name="best_score">Best Score :</string>
<string name="x">X</string>
</resources> </resources>
Loading…
Cancel
Save