Facebook
From Unique Terrapin, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 62
  1. package com.example.memory
  2.  
  3.  data class Card(val imageId: Int, val value: Int) {
  4.      var isFaceUp = false
  5. }
  6. package com.example.memory
  7.  
  8. class Game(private val size: Int) {
  9.     private val images = mutableListOf(
  10.         R.drawable.card_back1,
  11.         R.drawable.card_back2,
  12.         R.drawable.card_back3,
  13.         R.drawable.card_back4,
  14.         R.drawable.card_back5,
  15.         R.drawable.card_back6,
  16.         R.drawable.card_back7,
  17.         R.drawable.card_back8
  18.     )
  19.     val cards = MutableList(size) { Card(images[it / 2], it / 2) }
  20.  
  21.     fun shuffleCards() {
  22.         cards.shuffle()
  23.     }
  24.  
  25.     fun checkCards(firstIndex: Int, secondIndex: Int): Boolean {
  26.         return cards[firstIndex].imageId == cards[secondIndex].imageId
  27.     }
  28. }
  29. package com.example.memory
  30.  
  31. import android.graphics.Color
  32. import android.graphics.drawable.ColorDrawable
  33. import android.graphics.drawable.LayerDrawable
  34. import androidx.appcompat.app.AppCompatActivity
  35. import android.os.Bundle
  36. import android.widget.GridLayout
  37. import android.widget.ImageButton
  38. import androidx.constraintlayout.helper.widget.Layer
  39. import androidx.core.content.ContextCompat
  40.  
  41. class Memory : AppCompatActivity() {
  42.     private lateinit var game: Game
  43.     private lateinit var gridLayout: GridLayout
  44.     private var selectedCardIndex: Int? = null
  45.  
  46.  
  47.     override fun onCreate(savedInstanceState: Bundle?) {
  48.         super.onCreate(savedInstanceState)
  49.         setContentView(R.layout.activity_memory)
  50.  
  51.         gridLayout = findViewById(R.id.gridLayout)
  52.  
  53.         game = Game(16)
  54.         game.shuffleCards()
  55.  
  56.  
  57.         for (i in 0 until gridLayout.childCount) {
  58.             val cardButton = gridLayout.getChildAt(i) as ImageButton
  59.             val card = game.cards[i]
  60.  
  61.             updateCard(cardButton, card)
  62.  
  63.  
  64.             cardButton.setOnClickListener {
  65.                 card.isFaceUp = !card.isFaceUp
  66.                 updateCard(cardButton, card)
  67.  
  68.                 if (selectedCardIndex != null) {
  69.                     if (game.checkCards(selectedCardIndex!!, i)) {
  70.                         hideCards(selectedCardIndex!!, i)
  71.                     }else {
  72.                         val prevCardButton = gridLayout.getChildAt(selectedCardIndex!!) as ImageButton
  73.                         val prevCard = game.cards[selectedCardIndex!!]
  74.                         prevCard.isFaceUp = false
  75.                         updateCard(prevCardButton, prevCard)
  76.  
  77.                         selectedCardIndex = null
  78.                     }
  79.                 }else{
  80.                     selectedCardIndex = i
  81.                 }
  82.             }
  83.  
  84.         }
  85.  
  86.     }
  87.     private fun updateCard(cardButton: ImageButton, card: Card) {
  88.         val resourceId = if (card.isFaceUp) {
  89.             R.color.white // Use a color resource instead of ColorDrawable
  90.         } else {
  91.             resources.getIdentifier("card_back_${card.value}", "drawable", packageName)
  92.         }
  93.         cardButton.setImageDrawable(
  94.             if (resourceId == 0) {
  95.                 // The resource was not found, so use a placeholder drawable
  96.                 ContextCompat.getDrawable(this, R.drawable.placeholder)!!
  97.             } else {
  98.                 ContextCompat.getDrawable(this, resourceId)!!
  99.             }
  100.         )
  101.     }
  102.     private fun hideCards(index1: Int, index2: Int) {
  103.         val cardButton1 = gridLayout.getChildAt(index1) as ImageButton
  104.         val cardButton2 = gridLayout.getChildAt(index2) as ImageButton
  105.         cardButton1.isClickable = false
  106.         cardButton2.isClickable = false
  107.     }
  108.  
  109.  
  110.  
  111. }