Thread: how to add sound in ubuntu...?

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Question how to add sound in ubuntu...?

    I am trying to add sound to this program so that for every generation that appears, it will beep. There is a seperate program that has to be opened with this that I have put below as well. Can anyone tell me names of libraries that I can download to ubuntu to work with the g++ compiler?
    Code:
    #include <iostream>
    #include "Board.cpp"
    #include <time.h>
    using namespace std;
    
    class Game
    {
    	public:
    		void enterInfo();
    		void numberToPlaceOnBoard();
    		void randomCellGenerator();
    		void EvaluationOfCells();
    		int getNumberSurrounding(int x, int y);
    		void liveOrDie(int S, int Y);
    		void showBoard(); 
    		void enterPlaceCells();
    		void howManyGenerations();
    		bool endOfBoard();
    		void nonRandomCellGenerator();
    		
    	
    	private:
    		Board ALF;
    		int mWidth;
    		int mHeight;
    		char mCells;
    		int placeCells;
    		
    };
    
    int main()
    {
    	Game Dog;
    	Dog.enterInfo();
    	Dog.EvaluationOfCells();  
    	Dog.enterPlaceCells();
    	char Fred;
    	starter:
    	cout << "How do you want to put cells on the board? ";
    	cout << "'r' = Randomly place cells" << endl;
    	cout << "'n' = Put cells on the board manually" << endl;
    	cin >> Fred;
    	if (Fred == 'r')
    	{
    		Dog.randomCellGenerator();
    	}
    	else if (Fred == 'n')
    	{
    		Dog.nonRandomCellGenerator();
    	}
    	else 
    	{
    		cout << "Invalid response." << endl;
    		goto starter;
    	}
    	Dog.showBoard();
    	Dog.howManyGenerations();
    	return 0;
    }
    
    void Game::enterInfo()
    {
    	cout << "How high would you like this board? ";
    	cin >> mWidth;
    	cout << endl;
    	cout << "How wide would you like this board? ";
    	cin >> mHeight;
    	cout << endl;
    	ALF = Board(mWidth, mHeight, ' ');
    	cout << ALF;
    	
    }
    
    void Game::enterPlaceCells()
    {
    	cout << "How many cells would you like to place on the board? ";
    	cin >> placeCells;
    	
    }
    
    
    
    void Game::randomCellGenerator()
    { 
        	int rand_intX; 
    	int rand_intY; 
    	int index = 0;
    	while (index < placeCells)
    	{
    		rand_intX = (rand()%mWidth); 
    		rand_intY = (rand()%mHeight);
    		if (! ALF.isOccupied(rand_intX, rand_intY))
    		{
    			index++;
    			ALF.put('X', rand_intX, rand_intY);
    		}
    	}
    }
    
    int Game::getNumberSurrounding(int x, int y)
    {
    	int count = 0;
    	if (x > 0 && y > 0)
    	{
    		if (x < mWidth && y < mHeight)
    		{
    			if (ALF.isOccupied(x-1,y-1) && ALF.get(x-1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,y-1) && ALF.get(x,y-1) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1,y-1) && ALF.get(x+1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y) && ALF.get(x-1,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x+1,y) && ALF.get(x+1,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1, y+1) && ALF.get(x+1,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, y+1) && ALF.get(x,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y+1) && ALF.get(x-1,y+1) == 'X')
    			{
    				count++;
    			}
    		}
    		else if (x == mWidth && y < mHeight)
    		{
    			if (ALF.isOccupied(x-1,y-1) && ALF.get(x-1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,y-1) && ALF.get(x,y-1) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(0,y-1) && ALF.get(0,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y) && ALF.get(x-1,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(0,y) && ALF.get(0,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(0, y+1) && ALF.get(0,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, y+1) && ALF.get(x,y+1) == 'X')
    			{
    				count++; 
    			}
    			if (ALF.isOccupied(x-1,y+1) && ALF.get(x-1,y+1) == 'X')
    			{
    				count++;
    			}
    		}
    		else if (x == mWidth && y == mHeight)
    		{
    			if (ALF.isOccupied(x-1,y-1) && ALF.get(x-1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,y-1) && ALF.get(x,y-1) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(0, y-1) && ALF.get(0,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y) && ALF.get(x-1,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(0,y) && ALF.get(0,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(0, 0) && ALF.get(0,0) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, 0) && ALF.get(x,0) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,0) && ALF.get(x-1,0) == 'X')
    			{
    				count++;
    			}
    		}
    		else if (x < mWidth && y == mHeight)
    		{
    			if (ALF.isOccupied(x-1,y-1) && ALF.get(x-1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,y-1) && ALF.get(x,y-1) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1,y-1) && ALF.get(x+1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y) && ALF.get(x-1,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x+1,y) && ALF.get(x+1,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1, 0) && ALF.get(x+1,0) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, 0) && ALF.get(x,0) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,0) && ALF.get(x-1,0) == 'X')
    			{
    				count++;
    			}
    		}
    	}
    	else if (x == 0 && y > 0)
    	{
    		if (y < mHeight)
    		{
    			if (ALF.isOccupied(mWidth,y-1) && ALF.get(mWidth,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,y-1) && ALF.get(x,y-1) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1,y-1) && ALF.get(x+1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(mWidth,y) && ALF.get(mWidth,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x+1,y) && ALF.get(x+1,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1, y+1) && ALF.get(x+1,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, y+1) && ALF.get(x,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(mWidth,y+1) && ALF.get(mWidth,y+1) == 'X')
    			{
    				count++;
    			}
    		}
    		else if (y == mHeight)
    		{
    			if (ALF.isOccupied(mWidth,y-1) && ALF.get(mWidth,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,y-1) && ALF.get(x,y-1) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1,y-1) && ALF.get(x+1,y-1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(mWidth,y) && ALF.get(mWidth,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x+1,y) && ALF.get(x+1,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1, 0) && ALF.get(x+1,0) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, 0) && ALF.get(x,0) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(mWidth,0) && ALF.get(mWidth, 0) == 'X')
    			{
    				count++;
    			}
    		}
    	}
    	else if (x == 0 && y == 0)
    	{
    		if (ALF.isOccupied(mWidth,mHeight) && ALF.get(mWidth,mHeight) == 'X')
    		{
    			count++;			}
    		if (ALF.isOccupied(x,mHeight) && ALF.get(x,mHeight) == 'X')
    		{
    			count++;			}	
    		if (ALF.isOccupied(x+1,mHeight) && ALF.get(x+1,mHeight) == 'X')
    		{
    			count++;			
    		}
    		if (ALF.isOccupied(mWidth,y) && ALF.get(mWidth,y) == 'X')
    		{
    			count++;
    		}
    		if (ALF.isOccupied(x+1,y) && ALF.get(x+1,y) == 'X')
    		{
    			count++;
    		}	
    		if (ALF.isOccupied(x+1, y+1) && ALF.get(x+1,y+1) == 'X')
    		{
    			count++;
    		}
    		if (ALF.isOccupied(x, y+1) && ALF.get(x,y+1) == 'X')
    		{
    			count++;
    		}
    		if (ALF.isOccupied(mWidth,y+1) && ALF.get(mWidth,y+1) == 'X')
    		{
    			count++;
    		}
    	}
    	else if (x > 0 && y == 0)
    	{
    		if (x < mWidth)
    		{
    			if (ALF.isOccupied(x-1,mHeight) && ALF.get(x-1,mHeight) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,mHeight) && ALF.get(x,mHeight) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1,mHeight) && ALF.get(x+1,mHeight) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y) && ALF.get(x-1,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x+1,y) && ALF.get(x+1,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(x+1, y+1) && ALF.get(x+1,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, y+1) && ALF.get(x,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y+1) && ALF.get(x-1,y+1) == 'X')
    			{
    				count++;
    			}
    		}	
    		else if (x == mWidth)
    		{
    			if (ALF.isOccupied(x-1,mHeight) && ALF.get(x-1,mHeight) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x,mHeight) && ALF.get(x,mHeight) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(0,mHeight) && ALF.get(0,mHeight) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y) && ALF.get(x-1,y) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(0,y) && ALF.get(0,y) == 'X')
    			{
    				count++;
    			}	
    			if (ALF.isOccupied(0, y+1) && ALF.get(0,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x, y+1) && ALF.get(x,y+1) == 'X')
    			{
    				count++;
    			}
    			if (ALF.isOccupied(x-1,y+1) && ALF.get(x-1,y+1) == 'X')
    			{
    				count++;
    			}	
    		}
    	}
    		return count;
    }
    
    void Game::EvaluationOfCells()
    {
    	for (int Row = 0; Row < mWidth; Row++)
    	{
    		for (int Column = 0; Column < mHeight; Column++)
    		{
    			liveOrDie(Row, Column);
    		}
    	
             }	
    }
    
    void Game::liveOrDie(int S, int T)
    {
    	if (ALF.get(S, T) == 'X')
    	{
    		if (getNumberSurrounding(S, T) < 2 || getNumberSurrounding(S, T) > 3)
    		{
    			ALF.del(S,T);
    		}
    	}
    	if (ALF.get(S, T) != 'X')
    	{
    		if (getNumberSurrounding(S, T) == 3)
    		ALF.put('X', S, T);
    	}
    }
    
            
    
    void Game::showBoard()
    {
    	cout << ALF;
    }
    		
    void Game::howManyGenerations()
    {
    	int gens;
    	cout << "How many generations would you like to show? ";
    	cin >> gens;
    	cout << endl << endl << endl;
    	int count = 0;
    	for (int i = 0; i < gens; i++)
    	{
    		EvaluationOfCells();
    		showBoard();
    		count++;
    		cout << endl;
    		cout << "Generation " << count << endl << endl << endl;
    		wait(1);
    	}
    	
    	
    	
    }
    
    
    
    void Game::nonRandomCellGenerator()
    {
    	int across;
    	int down;
    	for (int i=0; i<placeCells; i++)
    	{
    		cout << "Where would you like to place this cell? " << endl;
    		cout << "x coordinate: ";
    		cin >> across;
    		cout << "y coordinate: ";
    		cin >> down;
    		if (ALF.isEmpty(across, down))
    		{
    			ALF.put('X', across, down);
    		}
    		else
    		{
    			cout << "You have already put a cell at this address.";
    			cout << "Enter a different address." << endl;
    			i--;
    		}
    	}
    }
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <vector>
    #include <cassert>
    #include <ctime>
    
    using namespace std;
    //*********************************************************************************
    //  class Board encapsulates a 2 dimensional grid of characters.
    //    
    //    Each position on the board contains a single character and is referenced with 
    //    an X,Y reference where X refers to the row number and Y refers to the column number. 
    //
    //    When displayed or output, the board is setup like an Excel spreadsheet, not like 
    //    a standard Cartiesian coordinate system; visually, X=0, Y=0 refers to the upper 
    //    left corner of the board (not the center of the board).
    //
    //    To declare a Board object, you MUST use the constructor that takes height, width
    //    and a default background character. E.g.:
    //      
    //         Board GameBoard(20,30,' ') ;
    //
    //    creates a Board object names GameBoard that has 20 rows, and 30 columns, and
    //    uses the space character as its background.
    //
    //    The background character determines what an "empty" position is. In the example
    //    above if an X,Y position contains a ' ', then the space is empty. Anyother 
    //    character means the space is occupied. 
    //
    //**********************************************************************************
    
    class Board
    {
    public:
    
      // output the board
      friend ostream& operator << ( ostream& out, Board& B) ;
    
      Board () {} ; // Does nothing. Don't declare a board object without invoking 
                    //  the constructor below instead.
    
      // initializes an empty board of height times width
      //  all positions are filled with character backgroundCh.
      Board (int height, int width, char backgroundCh);
    
      char  get(int X, int Y) ;          // get the character in position X,Y
      void  put(char ch, int X, int Y) ; // places ch in position X,Y
      void  del(int X, int Y) ;          // delete the character in position X,Y
    
      char  getBackgroundCh();           // returns the default background character
    
      bool  isEmpty (int X, int Y);      // returns true if position X,Y is empty
                                         //   (that is it contains a character that is equal 
                                         //   to the default background char)
    
      bool  isOccupied (int X, int Y);   // returns true if position X,Y is NOT empty
                                         //   (that is it contains a character that is equal 
                                         //   to something OTHER than the default background char
                                         // Note: if X,Y is off the board, isOccupied returns false 
    
    
      void  fillBoard(char ch) ; // fill all positions on the board with char ch.
                                 //   Ignores the default background char.
    
      void  clearBoard() ;       // clears the board (overwrites all the cells
                                 //   with the default background character).
    
    
    
    private:
    
    
      int  mWidth;
      int  mHeight;
      char mBackgroundCh ;
      vector< vector <char> > mGrid ;
    
      // Assert a valid position on the board, if X,Y is 
      //  not valid, then abort program.
      void posAssert(int X, int Y);
    
      // Returns true if X,Y is a valid posistion on the board, false
      //   otherwise
      bool  isOnGrid(int X, int Y);  // used with isOccupied
    
    
    };
    
    //////////////////////////////
    //****************************
    // Global functions -- outside of the board class.
    //   can be called without using the dot operator.
    //
    // funtion wait will pause the CPU a certain number
    // of seconds at runtime.
      
    void wait ( int seconds );
    
    
    //***************************
    /////////////////////////////
    
    
    
    
    void Board::posAssert(int X, int Y)
    {
      if (X<0 || X>=mHeight || Y<0 || Y >= mWidth)
      {
           cout << "Error. Off of board. Aborting. ";
           system("pause");
           exit(1);
      }
    }
    char Board::get (int X, int Y)
    {
      posAssert(X,Y);
      return mGrid[X][Y];
    }
    void Board::put (char ch, int X, int Y)
    {
          posAssert(X,Y);
          mGrid[X][Y] = ch ;
    }
    void  Board::del(int X, int Y)
    {
         posAssert(X,Y);
         mGrid[X][Y] = mBackgroundCh ;
    }
    char  Board::getBackgroundCh()
    {
          return mBackgroundCh ;
    }
    bool  Board::isOccupied (int X, int Y)
    {
         if (isOnGrid(X,Y))
         {
         return (mGrid[X][Y]!=mBackgroundCh)?true:false;
         }
         else     
         {
            return false;     
         }     
    }
    
    bool  Board::isEmpty (int X, int Y)
    {
          return  !isOccupied(X,Y);
    }
    
    bool Board::isOnGrid(int X, int Y)
    {
      if (X<0 || X>=mHeight || Y<0 || Y >= mWidth)
      {
        return false; 
      }  
      else
      { 
        return true;  
      }
    }
    void  Board::clearBoard()
    {
    
      fillBoard (mBackgroundCh);
    }
    
    Board :: Board (int height, int width, char ch )
    {
    
       mHeight = height; mWidth = width; mBackgroundCh=ch ;
    
       // create a row
       vector <char> aRow;
       for (int j=0; j<width; j++)
       {
         aRow.push_back(mBackgroundCh);
       }
    
       for (int i=0; i<height; i++)
       {
          mGrid.push_back(aRow);
       }
    
    
    }
    void Board :: fillBoard (char ch)
    {
       for (int i=0; i<mHeight; i++)
       {
         for (int j=0; j<mWidth; j++)
         {
            mGrid[i][j] = ch;
         }
       }
    }
    ostream& operator << ( ostream& out, Board& B)
    {
       for (int i=0; i<B.mHeight; i++)
       {
         cout << "|" ;
         for (int j=0; j<B.mWidth; j++)
         {
            out << B.mGrid[i][j];
         }
         cout <<"|" ;
         out << endl;
       }
       return out;
    }
    
    /////////////////////////////////
    /////GLOBAL NON-Class functions
    /////////////////////////////////
    void wait ( int seconds )
    {
       clock_t endwait;
       endwait = clock () + seconds * CLOCKS_PER_SEC ;
       while (clock() < endwait) {}
    }

  2. #2
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    SDL_Mixer. System beep is ghetto for a game (which i'm assuming your code is).

    And i really don't think it was necessary to post all that code unless you had a specific question about the code itself!
    Sorry, but i'm a Code::Blocks man now.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    If all you want is a beep, try std::cout'ing an '\a'..
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed Please
    By jereland in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 05:30 AM
  2. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 04:33 PM
  3. Sound Recorder work...Advice me.
    By loobian in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-14-2003, 09:58 PM
  4. Can somebody test this code please
    By andy bee in forum C Programming
    Replies: 6
    Last Post: 10-09-2001, 03:08 PM