Random .wav file generator C# .NET

Topics on common programming languages
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Random .wav file generator C# .NET

Post by Trebor29 » 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 :roll:

Also, does anyone know how I would adjust the Form size based on the selected grid size?? :lol:

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();
        }
   
    }

}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Random .wav file generator C# .NET

Post by Neo » Thu Nov 18, 2010 8:21 am

How about you assign an array of sounds and play them in a random order for normal operations?

Define this on top,

Code: Select all

SoundPlayer simpleSounds[10];
Random random = new Random();
May be under InitializeComponent, You load all sounds to the array.

Code: Select all

simpleSound[0] = new SoundPlayer(@"D:\OO Programing\ArrayTestApp\ArrayTestApp\bin\Debug\sounds\1.wav");
simpleSound[1] = new SoundPlayer(@"D:\OO Programing\ArrayTestApp\ArrayTestApp\bin\Debug\sounds\2.wav");
simpleSound[2] = new SoundPlayer(@"D:\OO Programing\ArrayTestApp\ArrayTestApp\bin\Debug\sounds\3.wav");
..........
..........
simpleSound[9] = new SoundPlayer(@"D:\OO Programing\ArrayTestApp\ArrayTestApp\bin\Debug\sounds\9.wav");
Play them random,

Code: Select all

simpleSound[random.Next(0, 9)].Play();
To make it more attractive, lets add more sounds for welcome(start of programme), game start, game win, game loose & goodbye (exit from programme) :)

how I would adjust the Form size based on the selected grid size??
You can use this,

Code: Select all

Form1.Size = new System.Drawing.Size(100, 100);
Say one square is 10px X 10px, then for 10x10 squares, you need 100px X 100 px board. You know how much you already need for placing other controls such as buttons, etc... so you will have to add them to that.
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Random .wav file generator C# .NET

Post by Trebor29 » Thu Nov 18, 2010 7:01 pm

Thats a quality piece of code that 'SoundPlayer'.... thanks for that Neo :lol:

This game will have all ther bells n whistles on it if I can! I will have a go at resizing the form (based on grid size) later, got a group assignment to tackle now.. might still have VS on in the corner though.. lol :lol:

Do you know if there is a .NET library anywhere I can get my hands on?? so I have something to look to when im in a pickel...
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Random .wav file generator C# .NET

Post by Neo » Thu Nov 18, 2010 7:12 pm

Thats a quality piece of code that 'SoundPlayer'.... thanks for that Neo :lol:
You are welcome bro...
Do you know if there is a .NET library anywhere I can get my hands on?? so I have something to look to when im in a pickel...
What's the library you are looking for? I mean what functionality do you expect?
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Random .wav file generator C# .NET

Post by Trebor29 » Fri Nov 19, 2010 8:11 pm

Ooh... Im not sure!

I was thinking there might be a library / dictionary with... maybe not all as I know there are thousands of them, but things like the SoundPlayer / DateTime / Random (my friend is telling me they're .NET namespaces..??? :? )

Before I posted on here, for the resizing of the form based on the grid size, I was thinking something like
From.Load.Size = "100 x 100";
Now Ive seen yours I know that wouldn't have worked.. So, it would be great if I had something to look at when doing basic stuff like this... :roll:

Does this post make sense??? I was being distracted before bby my friend yappin in my ear! :)
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Random .wav file generator C# .NET

Post by Trebor29 » Sun Nov 21, 2010 6:02 pm

I suppose it would be for Windows.Forms.Applications.... for now.... :lol:
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Random .wav file generator C# .NET

Post by Neo » Sun Nov 21, 2010 7:14 pm

I'm still not clear on what you really need.. sorry.
Are you looking for a book/website where you can see a list of functions, etc... ?
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Random .wav file generator C# .NET

Post by Trebor29 » Sun Nov 21, 2010 8:48 pm

Ye, website or ebook... just some way of learning with a visual aid as well as learning through trial and error.

If thats possible? :idea:
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Random .wav file generator C# .NET

Post by Neo » Sun Nov 21, 2010 10:25 pm

See Visual C# Language in Microsoft site.
Under that, you may find C# Programmer's Reference and then C# Tutorials.

Hope these will help!!!
Post Reply

Return to “.Net & Other Programming”