move right,move left,move down et les pieces

pull/8/head
Jolys Enzo 2 years ago
parent c706736757
commit a985dce119

@ -39,8 +39,17 @@ class GameFragment : Fragment() {
modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame) modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame)
val buttonRight:Button = view.findViewById(R.id.Button_Right) val buttonRight:Button = view.findViewById(R.id.Button_Right)
val buttonLeft:Button = view.findViewById(R.id.Button_Left)
val buttonRotateRight:Button = view.findViewById(R.id.Button_Right_Rotation)
val buttonRotateLeft:Button = view.findViewById(R.id.Button_Left_Rotation)
buttonRight.setOnClickListener { buttonRight.setOnClickListener {
modeleGame.getDashbord().moveRight(modeleGame.currentShape) modeleGame.dashBoard.moveRight(modeleGame.currentShape)
}
buttonLeft.setOnClickListener {
modeleGame.dashBoard.moveLeft(modeleGame.currentShape)
}
buttonRotateRight.setOnClickListener {
modeleGame.dashBoard.moveDown(modeleGame.currentShape)
} }
modeleGame.startGame() modeleGame.startGame()

@ -3,7 +3,7 @@ package modele
import views.ViewsGame import views.ViewsGame
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) {
private val gridOfGame = Array(this.height) { IntArray(this.width) } val gridOfGame = Array(this.height) { IntArray(this.width) }
// Our getter // Our getter
fun getWidth(): Int = this.width fun getWidth(): Int = this.width
@ -61,91 +61,110 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
} }
fun addShape(shape: Shape): Boolean { fun addShape(shape: Shape): Boolean {
when (shape.typeShape.type) {
EnumTypeShape.IShape -> { // Shape I (la ligne) for (line in 0..3) { // On vérifie que l'espace est disponible
// Mettre ICI des verification ou un try pour les limite du tableau for (column in 0..3) {
if ((isOccupied(
for (line in 0..3) { // On vérifie que l'espace est disponible shape.position.x + column,
for (column in 0..3) { shape.position.y + line
if ((isOccupied( )) and (shape.typeShape.showShape[line][column] == 1)
shape.position.getX() + column, ) {
shape.position.getY() + line return false
)) and (shape.typeShape.showShape[line][column] == 1)
) {
return false
}
}
} }
writeShape(shape)
}
else -> {
println("Type de piece incomprehensible !!")
return false
} }
} }
return false writeShape(shape)
return true
} }
// Test si c'est possible de l'intégré dans gridOfGame
private fun moveRightPossible(shape: Shape):Boolean{
// ATTENTION ELLE BUG PAS DE COLLISION val pos:MutableList<Position> = shape.sharePositionRight()
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 ){ for ( position in pos){
limite = 4 if ( position.x+shape.position.x >= width){
}else { return false
limite = limiteInitial }
if( gridOfGame[position.y+shape.position.y][position.x+shape.position.x] != 0) {
return false
}
} }
return true
}
for ( line in 0..limite){ // Check si le décalage est possible fun moveRight(shape: Shape):Boolean{
for ( column in 0..limite){ println("Shape action -> Move Right ! ")
if ( column != limite) { shape.position.addX()
if ((shape.typeShape.showShape[line][column] == 1) if (!moveRightPossible(shape)){
and (shape.typeShape.showShape[line][column + 1] == 0) shape.position.decrementeX()
and (gridOfGame[shape.position.getX() + line][shape.position.getY() + column + 1] == 1) return false
) {
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
}
}
}
} }
shape.position.decrementeX()
deleteShape(shape) deleteShape(shape)
shape.position.addX() shape.position.addX()
writeShape(shape) writeShape(shape)
return shape.position return true
} }
fun moveLeft(shape:Shape):Position{ // Test si c'est possible de l'intégré dans gridOfGame
private fun moveLeftPossible(shape: Shape):Boolean{
val pos:MutableList<Position> = shape.sharePositionLeft()
// Check if X == 0 for ( position in pos){
if ( shape.position.getX() == 0 ){ println("X -> : "+position.x)
return shape.position println("X -> : "+position.y)
}
for( line in 0..3){ if ( shape.position.x < 0 ){
for( column in 0..3 ){ return false
if ( column == 0 ){ }
if ( (shape.typeShape.showShape[line][column] == 1) if( gridOfGame[position.y+shape.position.y][position.x+shape.position.x] != 0) {
and (gridOfGame[shape.position.getX()+line][shape.position.getY()+column-1] == 1) ){ return false
return shape.position
}
}
} }
} }
return true
}
fun moveLeft(shape:Shape):Boolean{
println("Shape action -> Move Left ! ")
shape.position.decrementeX()
if (!moveLeftPossible(shape)){
shape.position.addX()
return false
}
shape.position.addX()
deleteShape(shape)
shape.position.decrementeX()
writeShape(shape)
return true
}
private fun moveDownPossible(shape: Shape):Boolean{
val pos:MutableList<Position> = shape.sharePositionRight()
return shape.position for ( position in pos){
if ( position.y+shape.position.y >= height){
return false
}
if( gridOfGame[position.y+shape.position.y][position.x+shape.position.x] != 0) {
return false
}
}
return true
}
fun moveDown(shape: Shape):Boolean{
println("Shape action -> Move down ! ")
shape.position.addY()
if (!moveDownPossible(shape)){
shape.position.decrementeY()
return false
}
shape.position.decrementeY()
deleteShape(shape)
shape.position.addY()
writeShape(shape)
return true
} }
// Delete a shape in gridOfGame // Delete a shape in gridOfGame
@ -153,7 +172,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
for (line in 0..3) { for (line in 0..3) {
for (column in 0..3) { for (column in 0..3) {
if (shape.typeShape.showShape[line][column] == 1) { if (shape.typeShape.showShape[line][column] == 1) {
gridOfGame[shape.position.getX() + line][shape.position.getY() + column] = 0 gridOfGame[shape.position.y + line][shape.position.x + column] = 0
} }
} }
} }
@ -161,10 +180,13 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
// Write the shape in gridOfGame // Write the shape in gridOfGame
private fun writeShape(shape: Shape){ private fun writeShape(shape: Shape){
println("Final X : "+shape.position.x)
println("Final Y : "+shape.position.y)
for (line in 0..3) { for (line in 0..3) {
for (column in 0..3) { for (column in 0..3) {
if (shape.typeShape.showShape[line][column] == 1) { if (shape.typeShape.showShape[line][column] == 1) {
gridOfGame[shape.position.getY() + line][shape.position.getX() + column] = gridOfGame[shape.position.y+ line][shape.position.x + column] =
shape.typeShape.couleur shape.typeShape.couleur
} }
} }

@ -4,35 +4,39 @@ import android.view.InputQueue
import views.ViewsGame import views.ViewsGame
class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame) { class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame) {
private val dashBoard: DashBoard = DashBoard(width,height,viewGame) val dashBoard: DashBoard = DashBoard(width,height,viewGame)
lateinit var currentShape: Shape lateinit var currentShape: Shape
private var difficulty: Difficulty = Difficulty.EASY var difficulty: Difficulty = Difficulty.EASY
// To get the current difficulty
fun getDifficulty(): Difficulty = this.difficulty
fun getShape():Shape = this.currentShape
// To set the current shape
fun setShape(newCurrentShape:Shape){
this.currentShape = newCurrentShape
}
fun getDashbord():DashBoard = this.dashBoard
// The start game function // The start game function
fun startGame(){ fun startGame(){
//this.setCurrentShape(TypeShape.SquareShape) //this.setCurrentShape(TypeShape.SquareShape)
currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(1,1)) currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5))
dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6))
dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.TShape),Position(2,8))
dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.ZShape),Position(2,10))
dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
dashBoard.addShape(currentShape) dashBoard.addShape(currentShape)
currentShape.position = dashBoard.moveRight(currentShape)
println(currentShape.position.getX()) dashBoard.gridOfGame[1][0] = 2
println(currentShape.position.getY()) dashBoard.gridOfGame[1][6] = 3
//println(currentShape.position.getX())
//println(currentShape.position.getY())
println("RUN !!")
dashBoard.updateViewGame() dashBoard.updateViewGame()
} }

@ -1,16 +1,6 @@
package modele package modele
class Position(private var x: Int,private var y: Int) { class Position(var x: Int,var y: Int) {
fun getX() = this.x
fun getY() = this.y
fun setX(newX: Int){
this.x = newX
}
fun setY(newY: Int){
this.y = newY
}
// Incrémente X // Incrémente X
fun addX(){ fun addX(){
@ -21,4 +11,12 @@ class Position(private var x: Int,private var y: Int) {
fun addY(){ fun addY(){
this.y+=1 this.y+=1
} }
fun decrementeX(){
this.x-=1
}
fun decrementeY(){
this.y-=1
}
} }

@ -4,38 +4,74 @@ import java.lang.reflect.Type
class Shape(val typeShape: TypeShape,var position: Position) { class Shape(val typeShape: TypeShape,var position: Position) {
private var shapePosition : Array<Position> = emptyArray() //private var sharePosition = mutableListOf<Position>()
/* init { /* init {
val val
}*/ }*/
fun moveRight(){ fun sharePositionRight():MutableList<Position>{
this.position.setX(this.position.getX()+1) val sharePosition = mutableListOf<Position>()
//update for( line in 0..3){
for (column in 0..3) {
if ( column != 3 ){
if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line][column+1] == 0)){
sharePosition.add(Position(column,line))
}
}
if ( (column == 3) and (typeShape.showShape[line][column] == 1) ){
sharePosition.add(Position(column,line))
}
}
}
return sharePosition
} }
fun moveLeft(){ fun sharePositionLeft():MutableList<Position>{
this.position.setX(this.position.getX()-1) val sharePosition = mutableListOf<Position>()
//update for( line in 0..3){
for (column in 0..3) {
if ( column != 0 ){
if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line][column-1] == 0)){
sharePosition.add(Position(column,line))
}
}
if ( (column == 0) and (typeShape.showShape[line][column] == 1) ){
sharePosition.add(Position(column,line))
}
}
}
return sharePosition
} }
fun moveDown(){ fun sharePositionDown(shape: Shape):MutableList<Position>{
this.position.setY(this.position.getY()-1) val sharePosition = mutableListOf<Position>()
//update for( line in 0..3){
} for (column in 0..3) {
if ( line != 3 ){
if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line+1][column] == 0)){
sharePosition.add(Position(column,line))
}
}
if ( (line == 3) and (typeShape.showShape[line][column] == 1) ){
sharePosition.add(Position(column,line))
}
}
}
return sharePosition
}
fun rotateLeft(){ fun rotateLeft(){
val tmp = this.position.getX() //val tmp = this.position.getX()
this.position.setX(this.position.getY() * -1) //this.position.setX(this.position.getY() * -1)
this.position.setY(tmp) //this.position.setY(tmp)
//update //update
} }
fun rotateRight(){ fun rotateRight(){
val tmp = this.position.getX() //val tmp = this.position.getX()
this.position.setX(this.position.getY()) //this.position.setX(this.position.getY())
this.position.setY(tmp * -1) //this.position.setY(tmp * -1)
//update //update
} }
} }

@ -3,21 +3,62 @@ package modele
import java.lang.reflect.Type import java.lang.reflect.Type
import kotlin.random.Random import kotlin.random.Random
class TypeShape(val type: EnumTypeShape/*,private val showShape: Array<Array<Array<Int>>>*/){ class TypeShape(val type: EnumTypeShape){
private var currentIndex = 0 private var currentIndex = 0
lateinit var showShape:Array<IntArray> var showShape:Array<IntArray> = Array(4){ IntArray(4 ) { 0 } }
var couleur:Int = 0 var couleur:Int = 0
init { init {
when(type){ when(type){
EnumTypeShape.IShape -> { EnumTypeShape.IShape -> {
couleur = 1 // Set the color of the shape 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){ for( i in 0..3){
showShape[0][i] = 1 // Create shape showShape[0][i] = 1 // Create shape
} }
} }
EnumTypeShape.SquareShape -> {
couleur = 6
showShape[0][0] = 1
showShape[0][1] = 1
showShape[1][0] = 1
showShape[1][1] = 1
}
EnumTypeShape.TShape -> {
couleur = 7
showShape[0][1] = 1
showShape[1][0] = 1
showShape[1][1] = 1
showShape[1][2] = 1
}
EnumTypeShape.LShape -> {
couleur = 5
showShape[0][2] = 1
showShape[1][0] = 1
showShape[1][1] = 1
showShape[1][2] = 1
}
EnumTypeShape.JShape -> {
couleur = 4
showShape[0][0] = 1
showShape[1][0] = 1
showShape[1][1] = 1
showShape[1][2] = 1
}
EnumTypeShape.SShape -> {
couleur = 3
showShape[0][1] = 1
showShape[0][2] = 1
showShape[1][0] = 1
showShape[1][1] = 1
}
EnumTypeShape.ZShape -> {
couleur = 2
showShape[0][0] = 1
showShape[0][1] = 1
showShape[1][1] = 1
showShape[1][2] = 1
}
else -> { else -> {
println("Type inconnu !") println("Type inconnu !")
} }

Loading…
Cancel
Save