Random .wav file generator C# .NET
Posted: Wed Nov 17, 2010 9:30 pm
Hay ho..
Im doing a Noughts n Crosses (or tick tack toe) project using Visual Studio / C# / Windows.Forms.Application and would like to be able to play random sounds (.wav files) when each individual button is pressed. I can get it to play one sound for all buttons, but thats a bit borring! and do not want to assign each button an individual sound because I am also giving the player the option to choose a grid size, so they do not have to only play the original basic 3X3, but can choose up to 10X10 from two comboBoxes to make it more interesting... hence the need for a Random sound generator, so I can just use 10 or so sounds instead off finding a possible 100..!
This is my code below, sorry its abit hickledy pickledy but this is not the final project, only a rough go to try and sort out these complications. Thanks
Also, does anyone know how I would adjust the Form size based on the selected grid size??
Im doing a Noughts n Crosses (or tick tack toe) project using Visual Studio / C# / Windows.Forms.Application and would like to be able to play random sounds (.wav files) when each individual button is pressed. I can get it to play one sound for all buttons, but thats a bit borring! and do not want to assign each button an individual sound because I am also giving the player the option to choose a grid size, so they do not have to only play the original basic 3X3, but can choose up to 10X10 from two comboBoxes to make it more interesting... hence the need for a Random sound generator, so I can just use 10 or so sounds instead off finding a possible 100..!
This is my code below, sorry its abit hickledy pickledy but this is not the final project, only a rough go to try and sort out these complications. Thanks
Also, does anyone know how I would adjust the Form size based on the selected grid size??
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace ArrayTestApp
{
public partial class frmOXO : Form
{
public frmOXO()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cboGridSize1.SelectedIndex = 0;
cboGridSize2.SelectedIndex = 0;
}
private void Button_Click(object sender, EventArgs e)
{
SoundPlayer simpleSound = new SoundPlayer(@"D:\OO Programing\ArrayTestApp\ArrayTestApp\bin\Debug\sounds\2.wav");
simpleSound.Play();
Button clickedButton = (Button)sender;
string index = clickedButton.Name.Substring("tickTackButton".Length);
string targetTextBox = "tickTackButton" + index;
int myIndex = panel1.Controls.IndexOfKey(targetTextBox);
if (myIndex != -1)
{
//MessageBox.Show(index);
if (clickedButton.Text == " ")
{
clickedButton.Text = "O";
}
else if (clickedButton.Text == "O")
{
clickedButton.Text = "X";
}
else if (clickedButton.Text == "X")
{
clickedButton.Text = ("O");
}
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void cboGridSize1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void cboGridSize2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int grid1 = int.Parse(cboGridSize1.Text);
int grid2 = int.Parse(cboGridSize2.Text);
Button[,] tickTackButton = new Button[grid1, grid2];
for (int i = 0; i < grid1; i++)
{
for (int x = 0; x < grid2; x++)
{
tickTackButton[i, x] = new Button();
tickTackButton[i, x].Name = "tickTackButton" + i + ", " + x;
tickTackButton[i, x].Width = 50;
tickTackButton[i, x].Height = 50;
tickTackButton[i, x].Left = tickTackButton[i, x].Left + tickTackButton[i, x].Width + (i * 60);
tickTackButton[i, x].Top = tickTackButton[i, x].Top + tickTackButton[i, x].Top + 50 + (x * 60);//center the btn
tickTackButton[i, x].Text = " ";
panel1.Controls.Add(tickTackButton[i, x]);
tickTackButton[i, x].Click += new EventHandler(Button_Click);
}
//if (i > i * 60)
//{
// frmOXO.ActiveForm.Size = ; // Change form size based on selected grid size.
//}
}
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
}
}
}