如何阻止用户玩游戏

时间:2018-02-19 11:39:31

标签: java android kotlin

我正在尝试做一个简单的游戏(X和O)问题,当其中一个玩家赢了游戏时,另一个玩家仍然可以玩...当他们中的一个赢得游戏时我怎么能结束游戏呢? / p>

 fun buClicked(view: View){


    var buChoise = view as Button

    var cellID =0

    when (buChoise.id){
        R.id.b1-> cellID=1
        R.id.b2-> cellID=2
        R.id.b3-> cellID=3
        R.id.b4-> cellID=4
        R.id.b5-> cellID=5
        R.id.b6-> cellID=6
        R.id.b7-> cellID=7
        R.id.b8-> cellID=8
        R.id.b9-> cellID=9

    }

    PlayGame(cellID,buChoise)

}

var Player1=ArrayList<Int>()
var Player2=ArrayList<Int>()
var activePlayer=1

fun PlayGame(cellID:Int,buChoise:Button){

    if(activePlayer==1){

        buChoise.text="X"
        buChoise.setBackgroundResource(R.color.colorPrimaryDark)
        Player1.add(cellID)
        activePlayer=2

    }else{

        buChoise.text="O"
        buChoise.setBackgroundResource(R.color.colorAccent)
        Player2.add(cellID)
        activePlayer=1

    }


    buChoise.isEnabled=false
    checkWinner()

}
fun checkWinner(){

    var winner= -1

    //row 1
    if(Player1.contains(1) && Player1.contains(2) && Player1.contains(3)){

        Toast.makeText(this,"the winner is player 1",Toast.LENGTH_LONG).show()

    }
    if(Player2.contains(1) && Player2.contains(2) && Player2.contains(3)){

        Toast.makeText(this,"the winner is player 2",Toast.LENGTH_LONG).show()
    }

    //row 2
    if(Player1.contains(4) && Player1.contains(5) && Player1.contains(6)){

        Toast.makeText(this,"the winner is player 1",Toast.LENGTH_LONG).show()
    }
    if(Player2.contains(4) && Player2.contains(5) && Player2.contains(6)){

        Toast.makeText(this,"the winner is player 2",Toast.LENGTH_LONG).show()
    }
    //row 3
    if(Player1.contains(7) && Player1.contains(8) && Player1.contains(9)){

        Toast.makeText(this,"the winner is player 1",Toast.LENGTH_LONG).show()
    }
    if(Player2.contains(7) && Player2.contains(8) && Player2.contains(9)){

        Toast.makeText(this,"the winner is player 2",Toast.LENGTH_LONG).show()
    }

    //col 1
    if(Player1.contains(1) && Player1.contains(4) && Player1.contains(7)){

        Toast.makeText(this,"the winner is player 1",Toast.LENGTH_LONG).show()
    }
    if(Player2.contains(1) && Player2.contains(4) && Player2.contains(7)){

        Toast.makeText(this,"the winner is player 2",Toast.LENGTH_LONG).show()
    }

    //row 2
    if(Player1.contains(2) && Player1.contains(5) && Player1.contains(8)){

        Toast.makeText(this,"the winner is player 1",Toast.LENGTH_LONG).show()
    }
    if(Player2.contains(2) && Player2.contains(5) && Player2.contains(8)){

        Toast.makeText(this,"the winner is player 2",Toast.LENGTH_LONG).show()
    }
    //col 3
    if(Player1.contains(3) && Player1.contains(6) && Player1.contains(9)){

        Toast.makeText(this,"the winner is player 1",Toast.LENGTH_LONG).show()

    }
    if(Player2.contains(3) && Player2.contains(6) && Player2.contains(9)){

        Toast.makeText(this,"the winner is player 2",Toast.LENGTH_LONG).show()
    }
    } 

2 个答案:

答案 0 :(得分:0)

if (Player1.containsAll(listOf(1, 2, 3)) || Player1.containsAll(listOf(4, 5, 6)) || Player1.containsAll(listOf(7, 8, 9)) || Player1.containsAll(listOf(1, 5, 9)) || Player1.containsAll(listOf(3, 5, 7))) {
        Toast.makeText(this, "the winner is player 1", Toast.LENGTH_LONG).show()
    } else if (Player2.containsAll(listOf(1, 2, 3)) || Player2.containsAll(listOf(4, 5, 6)) || Player2.containsAll(listOf(7, 8, 9)) || Player2.containsAll(listOf(1, 5, 9)) || Player2.containsAll(listOf(3, 5, 7))) {
        Toast.makeText(this, "the winner is player 2", Toast.LENGTH_LONG).show()
    }

答案 1 :(得分:0)

当其中一个玩家赢了游戏时,另一个玩家仍然可以玩”的问题。你只需要一个布尔变量。在类的顶部添加var isGameOver。

   fun main(args: Array<String>) {

        var isGameOver = false

        fun checkWinner(){

            var winner= -1

        //row 1
            if(Player1.contains(1) && Player1.contains(2) && Player1.contains(3)){
                 isGameOver = true
            }

            ..........
        }

        fun PlayGame(cellID:Int,buChoise:Button){
            if(!isGameOver){
                if(activePlayer==1){
                    buChoise.text="X"
                    buChoise.setBackgroundResource(R.color.colorPrimaryDark)
                    Player1.add(cellID)
                    activePlayer=2
                }else{
                    buChoise.text="O"
                    buChoise.setBackgroundResource(R.color.colorAccent)
                    Player2.add(cellID)
                    activePlayer=1
                }
                buChoise.isEnabled=false
                checkWinner()
            }else{
            //give replay button
            }
        }
    }

然后在PlayGame功能上给出条件。如果isGameOver为true,则两个玩家都无法再次执行操作并给他们重播按钮以重复此过程。