Boucle de jeu fini avec une fin, difficulté avec les delay,

pull/9/head
Jolys Enzo 2 years ago
parent ba3b2aaf60
commit e8544b419a

@ -1,8 +1,8 @@
package modele package modele
import android.util.Log
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import views.ViewsGame import views.ViewsGame
import kotlin.io.path.fileVisitor
class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) { class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) {
val gridOfGame = Array(this.height) { IntArray(this.width) } val gridOfGame = Array(this.height) { IntArray(this.width) }
@ -32,30 +32,33 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
} }
} }
fun shiftDown(rowToBegin: Int) { fun shiftDown(listeLine:MutableList<Int>) {
for (row in (height - 1)..rowToBegin) {
for (col in 0 until this.width) { println("Shift Down")
gridOfGame[row][col] = gridOfGame[row - rowToBegin][col] for( index in listeLine){
} println(index)
for ( line in index downTo 1 ){
for ( column in 0 until width){
gridOfGame[line][column] = gridOfGame[line-1][column]
} }
for (row in 0 until rowToBegin) {
for (col in 0 until this.width) {
gridOfGame[row][col] = 0
} }
} }
updateViewGame()
} }
//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():Int{ fun clearLines():Int{
val listeLine = mutableListOf<Int>()
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
listeLine.add(row)
} }
} }
if (nbRowCleared != 0) { if (nbRowCleared != 0) {
shiftDown(nbRowCleared) shiftDown(listeLine)
} }
return nbRowCleared return nbRowCleared
} }
@ -64,11 +67,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
for (line in 0..3) { // On vérifie que l'espace est disponible for (line in 0..3) { // On vérifie que l'espace est disponible
for (column in 0..3) { for (column in 0..3) {
if ((isOccupied( if ( (shape.typeShape.showShape[line][column] == 1) and (gridOfGame[shape.position.y+line][shape.position.x+column] != 0)){
shape.position.x + column,
shape.position.y + line
)) and (shape.typeShape.showShape[line][column] == 1)
) {
return false return false
} }
} }
@ -112,9 +111,6 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
val pos:MutableList<Position> = shape.sharePositionLeft() val pos:MutableList<Position> = shape.sharePositionLeft()
for ( position in pos){ for ( position in pos){
println("X -> : "+position.x)
println("X -> : "+position.y)
if ( shape.position.x < 0 ){ if ( shape.position.x < 0 ){
return false return false
} }
@ -236,8 +232,12 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
view.invalidate() view.invalidate()
} }
suspend fun fallingShape(shape: Shape):Boolean{ suspend fun fallingShape(shape: Shape, difficulty: Difficulty):Boolean{
delay(800) when(difficulty){
Difficulty.EASY -> delay(800)
Difficulty.MEDIUM -> delay(600)
Difficulty.HARD -> delay(400)
}
return moveDown(shape) return moveDown(shape)
} }
} }

@ -1,26 +1,35 @@
package modele package modele
import android.annotation.SuppressLint
import android.util.Log
import android.widget.TextView import android.widget.TextView
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import views.ViewsGame 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) { 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) val dashBoard: DashBoard = DashBoard(width,height,viewGame)
lateinit var currentShape: Shape 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 // The start game function
fun startGame(){ 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)) //currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
//dashBoard.addShape(currentShape) //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)) //currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1))
//dashBoard.addShape(currentShape) //dashBoard.addShape(currentShape)
/*
dashBoard.gridOfGame[0][0] = 5 currentShape = Shape(getNextShape(),Position(width/2,0))
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++
dashBoard.addShape(currentShape) dashBoard.addShape(currentShape)
println("RUN !!") println("RUN !!")
@ -60,11 +60,12 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
GlobalScope.launch { GlobalScope.launch {
while(true){ while(true){
if(! dashBoard.fallingShape(currentShape)){ if(dashBoard.fallingShape(currentShape,difficulty)){
if ( indexListeTypeShape == 7 ){
indexListeTypeShape = 0 // Clear line
}
val nbLine = dashBoard.clearLines() val nbLine = dashBoard.clearLines()
// Score
if ( (nbLine > 0) and (nbLine<4)){ if ( (nbLine > 0) and (nbLine<4)){
val tmpPoints:String = points.text as String val tmpPoints:String = points.text as String
points.text = (tmpPoints.toInt()+(nbLine*100)).toString() 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 val tmpPoints:String = points.text as String
points.text = (tmpPoints.toInt()+1200).toString() points.text = (tmpPoints.toInt()+1200).toString()
} }
}
currentShape = Shape(TypeShape(listeTypeShape[indexListeTypeShape]),Position(width/2,0)) else {
dashBoard.addShape(currentShape) // New shape
indexListeTypeShape++ currentShape = Shape(getNextShape(),Position(width/2,0))
if ( !dashBoard.addShape(currentShape)){
break
}
} }
} }
println("Game end !!")
} }
} }
} }

@ -84,25 +84,6 @@ class TypeShape(val type: EnumTypeShape){
currentIndex++ currentIndex++
return this return this
} }
*/
//To get the next shape
fun getNextShape(){
val random: Int = Random.nextInt(1,7)
when(random){
1 -> TypeShape(EnumTypeShape.IShape, arrayOf(arrayOf(arrayOf())))
2 -> TypeShape(EnumTypeShape.SquareShape, arrayOf(arrayOf(arrayOf())))
3 -> TypeShape(EnumTypeShape.JShape, arrayOf(arrayOf(arrayOf())))
4 -> TypeShape(EnumTypeShape.LShape, arrayOf(arrayOf(arrayOf())))
5 -> TypeShape(EnumTypeShape.SShape, arrayOf(arrayOf(arrayOf())))
6 -> TypeShape(EnumTypeShape.TShape, arrayOf(arrayOf(arrayOf())))
7 -> TypeShape(EnumTypeShape.ZShape, arrayOf(arrayOf(arrayOf())))
else -> throw Exception("Problème de random getNextShape()")
}
}
// To get the table of position of the current shape type
private fun getCurrentType(): Array<Array<Int>> {
return showShape[currentIndex]
}*/
} }
Loading…
Cancel
Save