/// <summary> /// Returns whether or not inputs in the form are currently valid ones. /// </summary> private bool ValidateInput() { bool valid = true; string errorText = ""; if (textBoxOutputFilename.Text.Length == 0) { valid = false; errorText += TCFG_ERROR_MSG.NO_OUTPUT_FILE; } if (numericUpDownDist.Value == 0) { valid = false; errorText += TCFG_ERROR_MSG.NO_AVG_DIST; } else if (FormUtil.GetIntegerValue(numericUpDownDist) == 0) { valid = false; errorText += TCFG_ERROR_MSG.DIST_IS_ZERO; } if (comboBoxAlgorithm.Text.Length == 0) { valid = false; errorText += TCFG_ERROR_MSG.NO_ALGORITHM; } if (numericUpDownNOfRuns.Value == 0) { valid = false; errorText += TCFG_ERROR_MSG.NO_NUMBER_OF_RUNS; } else if (FormUtil.GetIntegerValue(numericUpDownNOfRuns) == 0) { valid = false; errorText += TCFG_ERROR_MSG.RUNS_IS_ZERO; } if (!valid) { MessageBox.Show("Invalid input: \n" + errorText, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(valid); }
/// <summary> /// Generate button event. Generates a new map based on the given inputs, and outputs the resulting map to a file. /// </summary> private void buttonGenerate_Click(object sender, EventArgs e) { string errMsg = ""; if (textBoxName.Text.Length == 0) { errMsg += MG_ERROR_MSG.NO_NAME; } if (errMsg.Length == 0) { // Obtain input data from form string name = textBoxName.Text; int gridSize = FormUtil.GetIntegerValue(numericUpDownGridSize); int percentCoverage = FormUtil.GetIntegerValue(numericUpDownPercBlocked); // Create tile array int[,] tiles = new int[gridSize, gridSize]; // Initialize tile array for (int x = 0; x < gridSize; x++) { for (int y = 0; y < gridSize; y++) { tiles[x, y] = 0; } } // Calculate the number of tiles that need to be blocked int totalTiles = gridSize * gridSize; double totalTilesToCover = ((double)percentCoverage / 100f) * totalTiles; // Seed a random generator Random rand = new Random(DateTime.Now.Millisecond); // Ensure that number of tiles are blocked for (int i = 0; i < totalTilesToCover; i++) { Coord2 newBlocked; do { newBlocked = new Coord2(rand.Next(0, gridSize), rand.Next(0, gridSize)); } while (tiles[newBlocked.X, newBlocked.Y] == 1); tiles[newBlocked.X, newBlocked.Y] = 1; } // Output the map data StreamWriter sw = new StreamWriter("Content/Maps/" + name + ".map"); for (int y = 0; y < gridSize; y++) { for (int x = 0; x < gridSize; x++) { if (tiles[x, y] == 1) { sw.Write('#'); } else { sw.Write('.'); } } sw.Write(sw.NewLine); } sw.Flush(); sw.Close(); this.Close(); } else { MessageBox.Show("Invalid input: \n" + errMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }