summaryrefslogtreecommitdiff
path: root/app/src/main/java/space/jakob/mines/SetupActivity.kt
blob: 61b678caa6a78cc3b3f80022787fbd27d07d816b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Mines is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// Mines is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along with
// Mines. If not, see <https://www.gnu.org/licenses/>.

package space.jakob.mines

import android.app.AlertDialog
import android.content.DialogInterface
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.content.Intent

private const val MIN_WIDTH = 8
private const val MIN_HEIGHT = 8
private const val MIN_MINES = 10
private const val MAX_WIDTH = 30
private const val MAX_HEIGHT = 24

class SetupActivity : AppCompatActivity() {
    /**
     * Shows a basic notification dialog with no callbacks.
     *
     * @param[message] The message to show in the dialog.
     */
    private fun popMessage(message: String) {
        val builder = AlertDialog.Builder(this)
        builder.setMessage(message)
        builder.setNeutralButton(
            "Okay...",
            object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface, id: Int) {
                    dialog.cancel()
                }
            }
        )

        builder.create().show()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setup)

        // Inputs
        val heightEntry = findViewById(R.id.heightEntry) as EditText
        val widthEntry = findViewById(R.id.widthEntry) as EditText
        val minesEntry = findViewById(R.id.minesEntry) as EditText

        // Difficulties
        val selectEasy = findViewById(R.id.selectEasy) as Button
        val selectIntermediate = findViewById(R.id.selectIntermediate) as Button
        val selectExpert = findViewById(R.id.selectExpert) as Button

        selectEasy.setOnClickListener {
            heightEntry.setText("8")
            widthEntry.setText("8")
            minesEntry.setText("10")
        }
        selectIntermediate.setOnClickListener {
            heightEntry.setText("16")
            widthEntry.setText("16")
            minesEntry.setText("40")
        }
        selectExpert.setOnClickListener {
            heightEntry.setText("16")
            widthEntry.setText("30")
            minesEntry.setText("99")
        }


        val goButton = findViewById(R.id.goButton) as Button

        goButton.setOnClickListener {
            val width = widthEntry.text.toString().toInt()
            val height = heightEntry.text.toString().toInt()
            val mines = minesEntry.text.toString().toInt()

            val maxMines = (width - 1) * (height - 1)

            if (width < MIN_WIDTH) {
                popMessage("Currently, the minimum allowable width is ${MIN_WIDTH}.")
            } else if (width > MAX_WIDTH) {
                popMessage("Currently, the maximum allowable width is ${MAX_WIDTH}.")
            } else if (height < MIN_HEIGHT) {
                popMessage("Currently, the minimum allowable height is ${MIN_HEIGHT}.")
            } else if (height > MAX_HEIGHT) {
                popMessage("Currently, the maximum allowable height is ${MAX_HEIGHT}.")
            } else if (mines < MIN_MINES) {
                popMessage("The number of mines must be at least ${MIN_MINES}.")
            } else if (mines > maxMines) {
                popMessage("Seriously, tone it back. That's waaaay too many mines.")
            } else {
                startActivity(with (Intent(this, GameActivity::class.java)) {
                    putExtra("WIDTH", width)
                    putExtra("HEIGHT", height)
                    putExtra("MINES", mines)
                })
            }
        }
    }
}