Esempio n. 1
0
    public void CheckLinesTest()
    {
      Board b = new Board(7, 7, 3);
      Point p, pc;
      List<Point> result;
      List<Point> knownh, knownv, knowndl, knowndr, known;

      pc = new Point(2, 2);

      knownh = new List<Point>();
      ushort row;
      ushort col;
      ushort count;

      /* o o o o o o o o
       * o o o o o o o o
       * + + + + o o o o
       * o o o o o o o o
       * o o o o o o o o
       */
      for (row = pc.Row, col = 0, count = 0; count < 4; col++, count++)
      {
        p = new Point(row, col);
        knownh.Add(p);
        b.PlaceItem(p, 1);
      }

      result = b.CheckLines(knownh.ElementAt(0));
      Assert.AreEqual(0, result.Count);

      /* o o o o o o o o
       * o o o o o o o o
       * + + + + + o o o
       * o o o o o o o o
       * o o o o o o o o
       */
      p = new Point(row, col++);
      knownh.Add(p);
      b.PlaceItem(p, 1);

      // from right
      result = b.CheckLines(knownh.ElementAt(4));
      CollectionAssert.AreEquivalent(knownh, result);
      // from middle
      result = b.CheckLines(knownh.ElementAt(2));
      CollectionAssert.AreEquivalent(knownh, result);
      // from left
      result = b.CheckLines(knownh.ElementAt(0));
      CollectionAssert.AreEquivalent(knownh, result);

      // switch out the color
      b.ClearItem(pc);
      b.PlaceItem(pc, 2);
      // from left
      result = b.CheckLines(knownh.ElementAt(0));
      Assert.AreEqual(0, result.Count);
      // from middle
      result = b.CheckLines(knownh.ElementAt(2));
      Assert.AreEqual(0, result.Count);
      // from right
      result = b.CheckLines(knownh.ElementAt(4));
      Assert.AreEqual(0, result.Count);

      b.ClearItem(pc);
      b.PlaceItem(pc, 1);

      // more than 5
      /* o o o o o o o o
       * o o o o o o o o
       * + + + + + + o o
       * o o o o o o o o
       * o o o o o o o o
       */
      p = new Point(row, col++);
      knownh.Add(p);
      b.PlaceItem(p, 1);
      result = b.CheckLines(p);
      CollectionAssert.AreEquivalent(knownh, result);

      /* o o + o o o o o
       * o o + o o o o o
       * + + + + + + + o
       * o o + o o o o o
       * o o + o o o o o
       */
      knownv = new List<Point>();
      for (row = 0, col = pc.Col, count = 0; count < 5; row++, count++)
      {
        p = new Point(row, col);
        knownv.Add(p);
        if (!p.Equals(pc))
          b.PlaceItem(p, 1);
      }

      // from bottom
      result = b.CheckLines(p);
      CollectionAssert.AreEquivalent(knownv, result);
      // from top
      result = b.CheckLines(knownv.ElementAt(0));
      CollectionAssert.AreEquivalent(knownv, result);

      // two directions
      known = new List<Point>();
      known.AddRange(knownh.AsEnumerable());
      known.AddRange(knownv.AsEnumerable());
      // there is an extra point at the intersection
      known.Remove(pc);
      result = b.CheckLines(pc);
      CollectionAssert.AreEquivalent(known, result);

      /* o o + o o o o o
       * o o + o o o o o
       * + + + + + + + o
       * o o + + o o o o
       * o o + o + o o o
       * o o o o o + o o
       * o o o o o o + o
       */
      knowndl = new List<Point>();
      for (row = pc.Row, col = pc.Col, count = 0; count < 5; row++, col++, count++)
      {
        p = new Point(row, col);
        knowndl.Add(p);
        if (!p.Equals(pc))
          b.PlaceItem(p, 1);
      }

      // from top left (second)
      result = b.CheckLines(knowndl.ElementAt(1));
      CollectionAssert.AreEquivalent(knowndl, result);
      // from bottom right
      result = b.CheckLines(knowndl.ElementAt(4));
      CollectionAssert.AreEquivalent(knowndl, result);

      // three directions
      known.AddRange(knowndl.AsEnumerable());
      known.Remove(pc);
      result = b.CheckLines(pc);
      CollectionAssert.AreEquivalent(known, result);

      /* o o + o + o o o
       * o o + + o o o o
       * + + + + + + + o
       * o + + + o o o o
       * + o + o + o o o
       * o o + o o + o o
       * o o + o o o + o
       */
      knowndr = new List<Point>();
      for (row = 4, col = 0, count = 0; count < 5; row--, col++, count++)
      {
        p = new Point(row, col);
        knowndr.Add(p);
        if (!p.Equals(pc))
          b.PlaceItem(p, 1);
      }

      // from bottom left
      result = b.CheckLines(knowndr.ElementAt(0));
      CollectionAssert.AreEquivalent(knowndr, result);
      // from top right
      result = b.CheckLines(knowndr.ElementAt(4));
      CollectionAssert.AreEquivalent(knowndr, result);

      // four directions
      known.AddRange(knowndr.AsEnumerable());
      known.Remove(pc);
      result = b.CheckLines(pc);
      CollectionAssert.AreEquivalent(known, result);
    }
Esempio n. 2
0
File: Game.cs Progetto: cudima/lines
    public Game(ushort rows, ushort cols, ushort colors)
    {

      m_board = new Board(rows, cols, colors);

      m_view = new View(rows, cols, colors);
      m_view.OnClick += ClickHandler;
      m_view.OnMoveCompleted += MoveCompletedHandler;
      m_view.Show();


      // place first 5
      Tuple<Point, ushort>[] pieces = new Tuple<Point, ushort>[5];

      do
      {
        for (int i = 0; i < 5; i++)
          pieces[i] = m_board.PlaceRandom();

        // if we get a completed line, try again
        if (m_board.CheckLines(pieces[0].Item1).Count > 0)
          m_board.Clear();
        else
          break;
      } while (true);

      for (int i = 0; i < 5; i++)
        m_view.Place(pieces[i].Item1, pieces[i].Item2);

      m_state = GameState.On;
    }