Last active
February 21, 2018 02:14
-
-
Save wightwulf1944/33c97f91b0cd5bc06b67705832f5e200 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.pc.tictactoy | |
import android.graphics.Color | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.view.View | |
import android.widget.ArrayAdapter | |
import android.widget.Button | |
import android.widget.Toast | |
import x.android.synthetic.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
} | |
protected fun buClick(view: View) { | |
val buSelected: Button = view as Button | |
var cellID = 0 | |
when (buSelected.id) { | |
R.id.bu1 -> cellID = 1 | |
R.id.bu2 -> cellID = 2 | |
R.id.bu3 -> cellID = 3 | |
R.id.bu4 -> cellID = 4 | |
R.id.bu5 -> cellID = 5 | |
R.id.bu6 -> cellID = 6 | |
R.id.bu7 -> cellID = 7 | |
R.id.bu8 -> cellID = 8 | |
R.id.bu9 -> cellID = 9 | |
} | |
//Toast.makeText(this,"ID:"+ cellID, Toast.LENGTH_LONG).show() | |
PlayGame(cellID, buSelected) | |
} | |
var player1 = ArrayList<Int>() | |
var player2 = ArrayList<Int>() | |
var activePlayer = 1 | |
fun PlayGame(cellID: Int, buSelected: Button) { | |
if (activePlayer == 1) { | |
buSelected.text = "X" | |
buSelected.setBackgroundResource(R.color.blue) | |
player1.add(cellID) | |
activePlayer = 2 | |
} else { | |
buSelected.text = "O" | |
buSelected.setBackgroundResource(R.color.green) | |
player2.add(cellID) | |
activePlayer = 1 | |
} | |
buSelected.isEnabled = false | |
CheckWiner() | |
} | |
fun CheckWiner() { | |
var winer = -1 | |
//row 1 | |
if (player1.contains(1) && player1.contains(2) && player1.contains(3)) { | |
winer = 1 | |
} | |
if (player2.contains(1) && player2.contains(2) && player2.contains(3)) { | |
winer = 2 | |
} | |
//row 2 | |
if (player1.contains(4) && player1.contains(5) && player1.contains(6)) { | |
winer = 1 | |
} | |
if (player2.contains(4) && player2.contains(5) && player2.contains(6)) { | |
winer = 2 | |
} | |
//row 3 | |
if (player1.contains(7) && player1.contains(8) && player1.contains(9)) { | |
winer = 1 | |
} | |
if (player2.contains(7) && player2.contains(8) && player2.contains(9)) { | |
winer = 2 | |
} | |
//kolumna 1 | |
if (player1.contains(1) && player1.contains(4) && player1.contains(7)) { | |
winer = 1 | |
} | |
if (player2.contains(1) && player2.contains(4) && player2.contains(7)) { | |
winer = 2 | |
} | |
//kolumna 2 | |
if (player1.contains(2) && player1.contains(5) && player1.contains(8)) { | |
winer = 1 | |
} | |
if (player2.contains(2) && player2.contains(5) && player2.contains(8)) { | |
winer = 2 | |
} | |
//kolumna 3 | |
if (player1.contains(3) && player1.contains(6) && player1.contains(9)) { | |
winer = 1 | |
} | |
if (player2.contains(3) && player2.contains(6) && player2.contains(9)) { | |
winer = 2 | |
} | |
//ukos 1 | |
if (player1.contains(1) && player1.contains(5) && player1.contains(9)) { | |
winer = 1 | |
} | |
if (player2.contains(1) && player2.contains(5) && player2.contains(9)) { | |
winer = 2 | |
} | |
//ukos 2 | |
if (player1.contains(3) && player1.contains(5) && player1.contains(7)) { | |
winer = 1 | |
} | |
if (player2.contains(3) && player2.contains(5) && player2.contains(7)) { | |
winer = 2 | |
} | |
if (winer != -1) { | |
if (winer == 1) { | |
Toast.makeText(this, " Player 1 win the game", Toast.LENGTH_LONG).show() | |
} else { | |
Toast.makeText(this, " Player 2 win the game", Toast.LENGTH_LONG).show() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment