I'm attempting to create an Oracle (read: Magic 8 Ball). The idea behind it is that on each button press, a sound file with wise words is played (picked at random). I have it working using switches, however I'm searching for a way to make it more.. logical.
This is how it currently looks, with the switches going on and on:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _8ball
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
int choices = rnd.Next(0, 62);
switch(choices)
{
case 0:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\Lyde\0.wav");
player.Play();
break;
case 1:
System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(@"c:\Lyde\1.wav");
player1.Play();
break;
case 2:
System.Media.SoundPlayer player2 = new System.Media.SoundPlayer(@"c:\Lyde\2.wav");
player2.Play();
break;
case 3:
System.Media.SoundPlayer player3 = new System.Media.SoundPlayer(@"c:\Lyde\3.wav");
player3.Play();
break;
Surely there is a way to program it as such so that it looks in a given folder, then picks a random file, without having said file stated in the program itself (like how it was done with switches). I stumbled upon folder enumeration (http://code.msdn.microsoft.com/windowsapps/Folder-enumeration-sample-33ebd000), but I'm uncertain as to how to implement it in my given scenario.