diff options
Diffstat (limited to 'app/src')
| -rw-r--r-- | app/src/main/java/space/jakob/mines/GameActivity.kt | 12 | ||||
| -rw-r--r-- | app/src/main/java/space/jakob/mines/SetupActivity.kt | 83 |
2 files changed, 93 insertions, 2 deletions
diff --git a/app/src/main/java/space/jakob/mines/GameActivity.kt b/app/src/main/java/space/jakob/mines/GameActivity.kt index 927955e..49b301e 100644 --- a/app/src/main/java/space/jakob/mines/GameActivity.kt +++ b/app/src/main/java/space/jakob/mines/GameActivity.kt @@ -47,8 +47,18 @@ class GameActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_game) + var width = 8 + var height = 8 + var mines = 10 + + getIntent()?.let { + width = it.getIntExtra("WIDTH", width) + height = it.getIntExtra("HEIGHT", height) + mines = it.getIntExtra("MINES", mines) + } + gameView = findViewById(R.id.view) as GameView - gameView.grid = Grid(8, 8, 10) + gameView.grid = Grid(width, height, mines) gestureDetector = GestureDetectorCompat(this, gestureListener) } diff --git a/app/src/main/java/space/jakob/mines/SetupActivity.kt b/app/src/main/java/space/jakob/mines/SetupActivity.kt index 002b6e6..61b678c 100644 --- a/app/src/main/java/space/jakob/mines/SetupActivity.kt +++ b/app/src/main/java/space/jakob/mines/SetupActivity.kt @@ -12,19 +12,100 @@ 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 { - startActivity(Intent(this, GameActivity::class.java)) + 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) + }) + } } } } |