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
https://stackoverflow.com/questions/21002224/add-a-background-image-to-shape-in-xml-android
## Autheur :
- Enzo Jolys
- Alexis Carreau
## Pièces :
<img src="./Documentations/tetris-pieces.jpg">

@ -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() {

@ -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<ViewsGame>(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<ViewsGame>(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()
}
}

@ -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()
}
}

@ -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<Array<Int>> = 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()
}
}

@ -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
}
}

@ -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<Position> = emptyArray()

@ -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<Array<Array<Int>>>){
class TypeShape(val type: EnumTypeShape/*,private val showShape: Array<Array<Array<Int>>>*/){
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>{
val positions = ArrayList<Position>()
val typeShape = getCurrentType()
@ -26,6 +44,7 @@ class TypeShape(private val type: EnumTypeShape,private val showShape: Array<Arr
return this
}
//To get the next shape
fun getNextShape(){
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
private fun getCurrentType(): Array<Array<Int>> {
return showShape[currentIndex]
}
}*/
}

@ -9,22 +9,19 @@ import android.view.View
class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) {
val myPaint:Paint = Paint()
var tableau = arrayOf<Array<Int>>()
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<Array<Int>>){
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<Int>()
@ -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)

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

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

@ -3,4 +3,8 @@
<string name="TextScore">Score : ??</string>
<!-- TODO: Remove or change this placeholder text -->
<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>
Loading…
Cancel
Save