XProgramming > XP Magazine > Delegates Code, pre-Status
COLLECTED TOPICS: Adventures in C# | Documentation in XP | Book Reviews
Delegates Code, pre-Status
Ron Jeffries
03/16/2006
Here, for reference, is the version of the code that uses delegates, prior to putting in the Status feature.

Pre-Status Code

This page is referred to by Frame Status in the Delegates Version, and does not appear in the central index.

using System;
using NUnit.Framework;

namespace BowlingForKickersDelegate {
  [TestFixture] public class BowlingTestFixture {
    Game game;

    public BowlingTestFixture() {
    }

    [SetUp] public void SetUp() {
      game = new Game();
    }

    public void RollMany(int count, int pins) {
      for ( int roll = 0; roll < count; roll++ ) {
        game.Roll(pins);
      }
    }

    public void RollAll(int[] rolls) {
      foreach ( int pins in rolls ) {
        game.Roll(pins);
      }
    }

    [Test] public void GutterBalls() {
      RollMany(20,0);
      Assert.AreEqual(0, game.Score());
    }

    [Test] public void OpenFrames() {
      RollAll( new int[] {1,2, 3,4, 1,2, 3,4, 1,2, 3,4, 1,2, 3,4, 1,2, 3,4 });
      Assert.AreEqual(50, game.Score());
    }

    [Test] public void Alternating() {
      RollAll( new int[] { 10, 6,4, 10, 6,4, 10, 6,4, 10, 6,4, 10, 6,4, 10});
      Assert.AreEqual(200, game.Score());
    }

    [Test] public void AlternatingSpareFirst() {
      RollAll( new int[] { 6,4, 10, 6,4, 10, 6,4, 10, 6,4, 10, 6,4, 10, 6, 4});
      Assert.AreEqual(200, game.Score());
    }
  }
}
using System;
using NUnit.Framework;

namespace BowlingForKickersDelegate {
  [TestFixture] public class FrameTestFixture {
    Frame frame;
  
    public FrameTestFixture() {
    }
    
    [SetUp] public void SetUp() {
      frame = new Frame();
    }
    
    [Test] public void OpenFrame() {
      Assert.IsTrue(frame.RollConsumed(3));
      Assert.IsTrue(frame.RollConsumed(4));
      Assert.IsFalse(frame.RollConsumed(2));
      Assert.AreEqual(7, frame.Score());
    }

    [Test] public void SpareFrame() {
      Assert.IsTrue(frame.RollConsumed(6), "Done too soon, roll 1");
      Assert.IsTrue(frame.RollConsumed(4), "Done too soon, roll 2");
      Assert.IsFalse(frame.RollConsumed(2), "Not done soon enough, roll 3");
      Assert.AreEqual(12, frame.Score());
    }

    [Test] public void SpareFrameWithZeroBonus() {
      Assert.IsTrue(frame.RollConsumed(6));
      Assert.IsTrue(frame.RollConsumed(4));
      Assert.IsFalse(frame.RollConsumed(0));
      Assert.IsFalse(frame.RollConsumed(2));
      Assert.AreEqual(10, frame.Score());
    }

    [Test] public void Strike() {
      Assert.IsTrue(frame.RollConsumed(10), "Strike");
      Assert.IsFalse(frame.RollConsumed(4), "First Bonus");
      Assert.IsFalse(frame.RollConsumed(5), "Second Bonus");
      Assert.IsFalse(frame.RollConsumed(2), "Ignored next roll");
      Assert.AreEqual(19, frame.Score());
    }

    [Test] public void TenthFrameConsumesEverything() {
      frame = new TenthFrame();
      Assert.IsTrue(frame.RollConsumed(6), "first");
      Assert.IsTrue(frame.RollConsumed(4), "second");
      Assert.IsTrue(frame.RollConsumed(10), "third");
    }
  }
}


using System;

namespace BowlingForKickersDelegate {
  public class Game {
    Frame[] frames;

    public Game() {
      frames = new Frame[10];
      for ( int i = 0; i < 9; i++ ) {
        frames[i] = new Frame();
      }
      frames[9] = new TenthFrame();
    }

    public void Roll(int pins) {
      int frameCounter = 0;
      while ( !frames[frameCounter].RollConsumed(pins))
        frameCounter++;
    }

    public int Score() {
      int score = 0;
      foreach ( Frame frame in frames ) {
        score += frame.Score();
      }
      return score;
    }
  }
}


using System;
using System.Collections;

namespace BowlingForKickersDelegate {
  public class Frame {
    private int score;
    delegate bool ProcessRoll(int pins);
    private ProcessRoll processor;

    public Frame() {
      score = 0;
      processor = new ProcessRoll(ProcessFirstRoll);
    }

    private bool ProcessFirstRoll(int pins) {
      score += pins;
      if (score == 10)
        processor = new ProcessRoll(TwoBonusRoll);
      else
        processor = new ProcessRoll(ProcessSecondRoll);
      return true;
    }

    private bool ProcessSecondRoll( int pins) {
      score += pins;
      if (score == 10) 
        processor = new ProcessRoll(OneBonusRoll);
      else
        processor = new ProcessRoll(IgnoreRoll);
      return true;
    }

    private bool TwoBonusRoll(int pins) {
      score += pins;
      processor = new ProcessRoll(OneBonusRoll);
      return false;
    }

    private bool OneBonusRoll(int pins) {
      score += pins;
      processor = new ProcessRoll(IgnoreRoll);
      return false;
    }

    private bool IgnoreRoll(int pins) {
      return false;
    }

    public void AddRoll(int pins) {
      score += pins;
    }

    public virtual bool RollConsumed(int pins) {
      return processor(pins);
    }

    public int Score() {
      return score;
    }
  }
}

namespace BowlingForKickersDelegate {
  public class TenthFrame : Frame {

    public override bool RollConsumed(int pins) {
      base.RollConsumed(pins);
      return true;
    }
  }
}

XProgramming > XP Magazine > Delegates Code, pre-Status
COLLECTED TOPICS: Adventures in C# | Documentation in XP | Book Reviews