// 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 . 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) }) } } } }