Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language MC#

Date:03/09/07
Author:Vadim B. Guzev
URL:n/a
Comments:1
Info:http://u-pereslavl.botik.ru/~vadim/MCSharp/index.php
Score: (3.02 in 63 votes)
using System;

public class Barman {
 public int NumberOfBottlesAtTheBegining;
 public int CurrentNumberOfBottles;
 BDChannel barmanChannel;

 public Barman( int numberOfBottles ) {
  this.NumberOfBottlesAtTheBegining = numberOfBottles;
  this.CurrentNumberOfBottles = numberOfBottles;
  barmanChannel = new BDChannel();
 }

 public int HowManyBottlesOnTheWall() { // Ask the barman - How many bottles are left on the wall?
  HowManyBottlesLeft ! ();
  return (int) barmanChannel.Receive() [0];
 }

 public void AnswerHowManyBottlesLeft() & Channel HowManyBottlesLeft() {
  barmanChannel.Send( CurrentNumberOfBottles );
 }

 public void TakeOneDown() & Channel PassOneAround( string replicaFromSinger ) {
  Console.WriteLine( replicaFromSinger );
  CurrentNumberOfBottles--;

  if ( CurrentNumberOfBottles > 0 )
   Console.WriteLine( "Take one down and pass it around, {0} bottles of beer on the wall.",
CurrentNumberOfBottles );
  else if ( CurrentNumberOfBottles == 0 )
   Console.WriteLine( "Take one down and pass it around, no more bottles of beer on the wall." );
  else
   Console.WriteLine( "Go to the store and buy some more, {0} bottles of beer on the wall.",
NumberOfBottlesAtTheBegining );
 }

 public void Serve() {
  for ( int i = NumberOfBottlesAtTheBegining; i >=0 ; i-- ) {
   AnswerHowManyBottlesLeft();
   TakeOneDown();
  }
 }
}

public class Singer {
 movable SingAndDrink( BDChannel prevSinger, BDChannel nextSinger, Barman bm ) {
  prevSinger.Receive(); // Waiting for microphone from previous singer or barman
  int nBottles = bm.HowManyBottlesOnTheWall();
  if ( nBottles == 1 )
   bm.PassOneAround ! ( "1 bottle of beer on the wall, 1 bottle of beer." );
  else if ( nBottles == 0 )
   bm.PassOneAround ! ( "No more bottles of beer on the wall, no more bottles of beer." );
  else
   bm.PassOneAround ! ( nBottles + " bottles of beer on the wall, " + nBottles + " bottles of beer."
);
  // Drinking here...
  if ( nextSinger != null ) nextSinger.Send( "microphone" ); // Giving the microphone to the next
singer
 }
}

public class Bar {
 public static void StartParty() {
  // We need at least one barman for a good party... isn`t it? ;)
  Barman bm = new Barman( 99 );

  BDChannel prevSinger = null;
  BDChannel nextSinger = new BDChannel();
  BDChannel firstSinger = nextSinger;

  // We`ll have 100 drunk singers in our bar :)
  for ( int i = 0; i <= bm.NumberOfBottlesAtTheBegining; i++ ) {
   Singer s = new Singer();
   prevSinger = nextSinger;
   nextSinger = new BDChannel();
   s.SingAndDrink( prevSinger, nextSinger, bm );
  }
  firstSinger.Send( "microphone" ); // Come on guys! Let`s start!

  bm.Serve(); // Yeah... barman is working on parties...
 }

 public static void Main() {
  Bar.StartParty();
 }
}

Download Source | Write Comment

Alternative Versions

Comments

>>  Noah said on 11/18/07 03:17:07

Noah This is absolutely great 'cause the singers can run on different machines within a cluster passing the microphone via a channel, LOL! MC# rules anyways, at least if you have your own cluster.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: