|
|
|
@ -1,26 +1,35 @@
|
|
|
|
|
package modele
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
|
import android.util.Log
|
|
|
|
|
import android.widget.TextView
|
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
|
import kotlinx.coroutines.cancel
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import views.ViewsGame
|
|
|
|
|
import kotlin.random.Random
|
|
|
|
|
|
|
|
|
|
class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame,private val points:TextView) {
|
|
|
|
|
val dashBoard: DashBoard = DashBoard(width,height,viewGame)
|
|
|
|
|
lateinit var currentShape: Shape
|
|
|
|
|
var difficulty: Difficulty = Difficulty.EASY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var difficulty: Difficulty = Difficulty.EASY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//To get the next shape
|
|
|
|
|
private fun getNextShape(): TypeShape {
|
|
|
|
|
return when(Random.nextInt(1,7)){
|
|
|
|
|
1 -> TypeShape(EnumTypeShape.IShape)
|
|
|
|
|
2 -> TypeShape(EnumTypeShape.SquareShape)
|
|
|
|
|
3 -> TypeShape(EnumTypeShape.JShape)
|
|
|
|
|
4 -> TypeShape(EnumTypeShape.LShape)
|
|
|
|
|
5 -> TypeShape(EnumTypeShape.SShape)
|
|
|
|
|
6 -> TypeShape(EnumTypeShape.TShape)
|
|
|
|
|
7 -> TypeShape(EnumTypeShape.ZShape)
|
|
|
|
|
else -> throw Exception("Problème de random getNextShape()")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The start game function
|
|
|
|
|
|
|
|
|
|
fun startGame(){
|
|
|
|
|
val listeTypeShape: List<EnumTypeShape> = listOf(EnumTypeShape.ZShape,EnumTypeShape.JShape,EnumTypeShape.IShape,EnumTypeShape.LShape,EnumTypeShape.TShape,EnumTypeShape.SquareShape,EnumTypeShape.SShape)
|
|
|
|
|
var indexListeTypeShape:Int = 0
|
|
|
|
|
|
|
|
|
|
//currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
|
|
|
|
|
//dashBoard.addShape(currentShape)
|
|
|
|
@ -42,17 +51,8 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
|
|
|
|
|
|
|
|
|
|
//currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1))
|
|
|
|
|
//dashBoard.addShape(currentShape)
|
|
|
|
|
/*
|
|
|
|
|
dashBoard.gridOfGame[0][0] = 5
|
|
|
|
|
dashBoard.gridOfGame[0][1] = 5
|
|
|
|
|
dashBoard.gridOfGame[0][2] = 5
|
|
|
|
|
dashBoard.gridOfGame[0][3] = 5
|
|
|
|
|
dashBoard.gridOfGame[0][4] = 5
|
|
|
|
|
dashBoard.gridOfGame[0][5] = 5
|
|
|
|
|
dashBoard.gridOfGame[0][6] = 5
|
|
|
|
|
*/
|
|
|
|
|
currentShape = Shape(TypeShape(listeTypeShape[indexListeTypeShape]),Position(width/2,0))
|
|
|
|
|
indexListeTypeShape++
|
|
|
|
|
|
|
|
|
|
currentShape = Shape(getNextShape(),Position(width/2,0))
|
|
|
|
|
dashBoard.addShape(currentShape)
|
|
|
|
|
|
|
|
|
|
println("RUN !!")
|
|
|
|
@ -60,11 +60,12 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
|
|
|
|
|
|
|
|
|
|
GlobalScope.launch {
|
|
|
|
|
while(true){
|
|
|
|
|
if(! dashBoard.fallingShape(currentShape)){
|
|
|
|
|
if ( indexListeTypeShape == 7 ){
|
|
|
|
|
indexListeTypeShape = 0
|
|
|
|
|
}
|
|
|
|
|
if(dashBoard.fallingShape(currentShape,difficulty)){
|
|
|
|
|
|
|
|
|
|
// Clear line
|
|
|
|
|
val nbLine = dashBoard.clearLines()
|
|
|
|
|
|
|
|
|
|
// Score
|
|
|
|
|
if ( (nbLine > 0) and (nbLine<4)){
|
|
|
|
|
val tmpPoints:String = points.text as String
|
|
|
|
|
points.text = (tmpPoints.toInt()+(nbLine*100)).toString()
|
|
|
|
@ -73,12 +74,16 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
|
|
|
|
|
val tmpPoints:String = points.text as String
|
|
|
|
|
points.text = (tmpPoints.toInt()+1200).toString()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentShape = Shape(TypeShape(listeTypeShape[indexListeTypeShape]),Position(width/2,0))
|
|
|
|
|
dashBoard.addShape(currentShape)
|
|
|
|
|
indexListeTypeShape++
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// New shape
|
|
|
|
|
currentShape = Shape(getNextShape(),Position(width/2,0))
|
|
|
|
|
if ( !dashBoard.addShape(currentShape)){
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println("Game end !!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|