merge buttons on click functions
This commit is contained in:
parent
7843848653
commit
10c6d6a31d
@ -12,9 +12,9 @@ class EndOfMatchDialog: DialogFragment() {
|
|||||||
override fun onCreateDialog(savedInstanceState: Bundle?)=
|
override fun onCreateDialog(savedInstanceState: Bundle?)=
|
||||||
AlertDialog.Builder(activity).apply{
|
AlertDialog.Builder(activity).apply{
|
||||||
val players = (activity as MainActivity).players
|
val players = (activity as MainActivity).players
|
||||||
val names = players.map { it.name }.toTypedArray()
|
val names = players.map { it.name }
|
||||||
val winnerName = players.maxBy { it.score }?.name ?: ""
|
val winnerName = players.maxBy { it.score }?.name ?: ""
|
||||||
val score = players.map { it.score }.sortedDescending().toIntArray()
|
val score = players.map { it.score }.sortedDescending()
|
||||||
|
|
||||||
setTitle(getString(R.string.end_match_dialog_title, winnerName))
|
setTitle(getString(R.string.end_match_dialog_title, winnerName))
|
||||||
setMessage(getString(R.string.score, score[0], score[1]))
|
setMessage(getString(R.string.score, score[0], score[1]))
|
||||||
@ -23,7 +23,7 @@ class EndOfMatchDialog: DialogFragment() {
|
|||||||
DialogInterface.OnClickListener { dialog, id ->
|
DialogInterface.OnClickListener { dialog, id ->
|
||||||
startActivity(
|
startActivity(
|
||||||
Intent(context, MainActivity::class.java).apply {
|
Intent(context, MainActivity::class.java).apply {
|
||||||
putExtra("names", names)
|
putExtra("names", names.toTypedArray())
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import android.view.View
|
|||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.support.v7.app.AppCompatDelegate
|
import android.support.v7.app.AppCompatDelegate
|
||||||
import android.support.v7.view.menu.ActionMenuItem
|
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
@ -24,11 +23,10 @@ class MainActivity : AppCompatActivity() {
|
|||||||
var buttons: Array<Button> = emptyArray()
|
var buttons: Array<Button> = emptyArray()
|
||||||
var imageViews: Array<ImageView?> = emptyArray()
|
var imageViews: Array<ImageView?> = emptyArray()
|
||||||
var history: MutableList<State> = ArrayList()
|
var history: MutableList<State> = ArrayList()
|
||||||
var step: Int = 0
|
var game: Int = 0
|
||||||
var undo: MenuItem? = null
|
var undo: MenuItem? = null
|
||||||
var redo: MenuItem? = null
|
var redo: MenuItem? = null
|
||||||
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
||||||
@ -66,13 +64,13 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
||||||
R.id.action_undo -> {
|
R.id.action_undo -> {
|
||||||
step--
|
game--
|
||||||
reloadState()
|
reloadState()
|
||||||
redo?.isVisible = true
|
redo?.isVisible = true
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
R.id.action_redo -> {
|
R.id.action_redo -> {
|
||||||
step++
|
game++
|
||||||
reloadState()
|
reloadState()
|
||||||
undo?.isVisible = true
|
undo?.isVisible = true
|
||||||
true
|
true
|
||||||
@ -96,19 +94,19 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
fun saveState() {
|
fun saveState() {
|
||||||
val state = State(players.map { it.score }, serviceSide)
|
val state = State(players.map { it.score }, serviceSide)
|
||||||
if (step == history.size) {
|
if (game == history.size) {
|
||||||
history.add(state)
|
history.add(state)
|
||||||
} else {
|
} else {
|
||||||
history[step] = state
|
history[game] = state
|
||||||
history = history.subList(0, step+1).toMutableList()
|
history = history.subList(0, game+1).toMutableList()
|
||||||
}
|
}
|
||||||
if (step > 0) {
|
if (game > 0) {
|
||||||
undo?.isVisible = true
|
undo?.isVisible = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reloadState() {
|
fun reloadState() {
|
||||||
history[step].let{
|
history[game].let{
|
||||||
players.zip(it.score).forEach{(player, score) -> player.score = score}
|
players.zip(it.score).forEach{(player, score) -> player.score = score}
|
||||||
serviceSide = it.serviceSide
|
serviceSide = it.serviceSide
|
||||||
relaunchSide = when(serviceSide) {
|
relaunchSide = when(serviceSide) {
|
||||||
@ -116,11 +114,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
Side.RIGHT -> Side.LEFT
|
Side.RIGHT -> Side.LEFT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
when(step){
|
when(game){
|
||||||
0 -> undo?.isVisible = false
|
0 -> undo?.isVisible = false
|
||||||
history.size - 1 -> redo?.isVisible = false
|
history.size - 1 -> redo?.isVisible = false
|
||||||
}
|
}
|
||||||
this.step = step
|
this.game = game
|
||||||
updateUI()
|
updateUI()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,18 +154,12 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onClickLeftPlayer(view: View) {
|
fun updateScore(view: View) {
|
||||||
updateScore(players[Side.LEFT.value])
|
for (side in enumValues<Side>()) {
|
||||||
}
|
if (view == buttons[side.value]) {
|
||||||
|
|
||||||
fun onClickRightPlayer(view: View) {
|
|
||||||
updateScore(players[Side.RIGHT.value])
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateScore(scoringPlayer: Player) {
|
|
||||||
if (!matchIsFinished()) {
|
if (!matchIsFinished()) {
|
||||||
step++
|
game++
|
||||||
scoringPlayer.score++
|
players[side.value].score++
|
||||||
if (players.sumBy { it.score } % 2 == 0) {
|
if (players.sumBy { it.score } % 2 == 0) {
|
||||||
serviceSide = relaunchSide.also { relaunchSide = serviceSide }
|
serviceSide = relaunchSide.also { relaunchSide = serviceSide }
|
||||||
}
|
}
|
||||||
@ -178,6 +170,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
EndOfMatchDialog().show(supportFragmentManager, "EndOfMatchDialog")
|
EndOfMatchDialog().show(supportFragmentManager, "EndOfMatchDialog")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun matchIsFinished(): Boolean {
|
fun matchIsFinished(): Boolean {
|
||||||
val (minScore, maxScore) = players.map { it.score }.sorted()
|
val (minScore, maxScore) = players.map { it.score }.sorted()
|
||||||
|
@ -93,7 +93,7 @@
|
|||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:background="@color/colorAccent"
|
android:background="@color/colorAccent"
|
||||||
android:bufferType="spannable"
|
android:bufferType="spannable"
|
||||||
android:onClick="onClickLeftPlayer"
|
android:onClick="updateScore"
|
||||||
android:textAllCaps="false"
|
android:textAllCaps="false"
|
||||||
android:textAppearance="@style/TextAppearance.AppCompat.Button"
|
android:textAppearance="@style/TextAppearance.AppCompat.Button"
|
||||||
android:textSize="24sp"
|
android:textSize="24sp"
|
||||||
@ -117,7 +117,7 @@
|
|||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:background="@color/colorAccent"
|
android:background="@color/colorAccent"
|
||||||
android:bufferType="spannable"
|
android:bufferType="spannable"
|
||||||
android:onClick="onClickRightPlayer"
|
android:onClick="updateScore"
|
||||||
android:textAllCaps="false"
|
android:textAllCaps="false"
|
||||||
android:textAppearance="@style/TextAppearance.AppCompat.Button"
|
android:textAppearance="@style/TextAppearance.AppCompat.Button"
|
||||||
android:textSize="24sp"
|
android:textSize="24sp"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user