九宫格游戏
一个3×3图片矩阵,两个玩家轮流点击未被翻面的卡片,当一方玩家完全点开一行或者一列时获得胜利。利用九个ImageButton,点开某个Button时,将这个ImageButton的图片源替换,同时设置它为不可点击,在每次点击之后对新点击的位置检查是否满足胜利条件。
关键点
ImageButton图片源居中,如果直接设置src会使显示部分截取图片的一部分,如果直接利用background又会覆盖整个按钮而且没有点击效果 最后是src + fitCenter
android:scaleType="fitCenter"
android:src="@drawable/back"
Any数组怎么写,而且存Image资源时应该利用Int数组存储其对应的R.drawable.name,而不是Image本身
var whoChoice = arrayOfNulls<Int>(9)
var whiteImg = arrayOfNulls<Int>(4)
var blackImg = arrayOfNulls<Int>(4)
Code
package com.example.nineblocks
import android.annotation.SuppressLint
import android.media.Image
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.Toast
import java.lang.Exception
import java.util.*
import kotlin.properties.Delegates
class MainActivity : AppCompatActivity() {
var counter = 0 //翻开的格子数
var blackCounter = 0 //黑白照片序列id
var whiteCounter = 0
var exitFlag = false //游戏结束标志
var arrBtns = arrayOfNulls<ImageButton>(9)
var whoChoice = arrayOfNulls<Int>(9) //该位置的格子是谁翻开
var whiteImg = arrayOfNulls<Int>(4) //黑色白色图片序列
var blackImg = arrayOfNulls<Int>(4)
@SuppressLint("ResourceType")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
arrBtns[0] = findViewById(R.id.button1)
arrBtns[1] = findViewById(R.id.button2)
arrBtns[2] = findViewById(R.id.button3)
arrBtns[3] = findViewById(R.id.button4)
arrBtns[4] = findViewById(R.id.button5)
arrBtns[5] = findViewById(R.id.button6)
arrBtns[6] = findViewById(R.id.button7)
arrBtns[7] = findViewById(R.id.button8)
arrBtns[8] = findViewById(R.id.button9)
whiteImg[0] = R.drawable.white
whiteImg[1] = R.drawable.white2
whiteImg[2] = R.drawable.white3
whiteImg[3] = R.drawable.white4
blackImg[0] = R.drawable.black
blackImg[1] = R.drawable.black2
blackImg[2] = R.drawable.black3
blackImg[3] = R.drawable.black4
//init 重启按钮
val restartBtn = findViewById<Button>(R.id.restartBtn)
restartBtn.setOnClickListener {
restartGame()
}
restartGame()
for (i in (0..8))
arrBtns[i]?.setOnClickListener {
onPlayerClick(i + 1)
}
}
fun restartGame() {
for(i in (0..8)) {
whoChoice[i] = 2
arrBtns[i]?.setImageResource(R.drawable.back)
arrBtns[i]?.isEnabled = true
}
exitFlag = false
counter = 0
}
fun onPlayerClick(playerClickpos : Int) {
if(exitFlag) return //判断上一次白色的翻牌是否结束游戏 防崩溃
revCard(playerClickpos,0) //玩家点击翻牌 黑子
if(exitFlag) return
//电脑random翻牌 白子
var computer : Int = (1..9).random()
if(counter >= 9) return
while(counter < 9) {
if(arrBtns[computer - 1]?.isEnabled!!) break
computer = (1..9).random()
}
revCard(computer,1)
}
fun getListId(row : Int,col : Int) : Int{
return row * 3 + col
}
fun forbiddenAll() {
exitFlag = true
for(i in (0..8))
arrBtns[i]?.isEnabled = false
}
fun checkState(playerClickpos : Int,type: Int) {
var row = (playerClickpos - 1) / 3
var col = (playerClickpos - 1) % 3
//列相同 (i,j) <-> (i + 1,j) <-> (i + 2,j) [mod 3]
if(whoChoice[getListId((row + 1) % 3,col)] == whoChoice[playerClickpos - 1] && whoChoice[getListId((row + 2) % 3,col)] == whoChoice[playerClickpos - 1]) {
if(type == 0)
Toast.makeText(this,"你赢了",Toast.LENGTH_SHORT).show()
else
Toast.makeText(this,"你输了",Toast.LENGTH_SHORT).show()
forbiddenAll()
}
//行相同 (i,j) <-> (i,j+1) <-> (i,j+2) [mod 3]
if(whoChoice[getListId(row,(col + 1)%3)] == whoChoice[playerClickpos - 1] && whoChoice[getListId(row,(col + 2) % 3)] == whoChoice[playerClickpos - 1]) {
if(type == 0)
Toast.makeText(this,"你赢了",Toast.LENGTH_SHORT).show()
else
Toast.makeText(this,"你输了",Toast.LENGTH_SHORT).show()
forbiddenAll()
}
if(counter == 9) { //draw
Toast.makeText(this,"平局",Toast.LENGTH_SHORT).show()
return
}
}
fun revCard(pos : Int,type : Int) {
try {
counter++
var myImage = 0
when(type) {
0 -> {
myImage = blackImg[blackCounter]!!
blackCounter++
blackCounter %= 4
}
1 -> {
myImage = whiteImg[whiteCounter]!!
whiteCounter++
whiteCounter %= 4
}
}
whoChoice[pos - 1] = type
arrBtns[pos - 1]?.isEnabled = false
arrBtns[pos - 1]?.setImageResource(myImage)
checkState(pos,type)
} catch (e : Exception) {
Toast.makeText(this,e.message,Toast.LENGTH_SHORT).show()
}
}
}