示例#1
0
 public Matrix(int number)
 {
     this.Rows = number;
     this.matrix = new int[this.Rows, this.Rows];
     this.currentPosition = new Position(0, 0);
     this.currentNumber = 1;
 }
示例#2
0
        public void Traverse()
        {
            while (!this.CheckIfFilled())
            {
                Position startingPosition = this.GetFreePosition();
                this.CurrentPosition = startingPosition;

                int rowMove = 1;
                int columnMove = 1;

                while (this.MoveAvailable())
                {
                    this.matrix[this.CurrentPosition.Row, this.CurrentPosition.Column] = this.currentNumber;
                    this.currentNumber++;
                    if (!this.MoveAvailable())
                    {
                        break;
                    }

                    if ((this.CurrentPosition.Row + rowMove >= this.Rows) || (this.CurrentPosition.Row + rowMove < 0) || (this.CurrentPosition.Column + columnMove >= this.Rows) || (this.CurrentPosition.Column + columnMove < 0) || (this.matrix[this.CurrentPosition.Row + rowMove, this.CurrentPosition.Column + columnMove] != 0))
                    {
                        while ((this.CurrentPosition.Row + rowMove >= this.Rows) || (this.CurrentPosition.Row + rowMove < 0) || (this.CurrentPosition.Column + columnMove >= this.Rows) || (this.CurrentPosition.Column + columnMove < 0) || (this.matrix[this.CurrentPosition.Row + rowMove, this.CurrentPosition.Column + columnMove] != 0))
                        {
                            this.MovePosition(ref rowMove, ref columnMove);
                        }
                    }

                    this.CurrentPosition.Row += rowMove;
                    this.CurrentPosition.Column += columnMove;
                }

                // This following line needs to be both in the inner and the outher cycle for the code to work.
                // Without it in the outer cycle, the setting of the last position either will never happen (if not present
                // at all) or will happen with a number that is +1 to the correct number (if put in the wrong place).
                if (!this.CheckIfFilled())
                {
                    this.matrix[this.CurrentPosition.Row, this.CurrentPosition.Column] = this.currentNumber;
                    this.currentNumber++;
                }
            }
        }
示例#3
0
 public void TestConstructorInvalidRowAndColumn()
 {
     Position position = new Position(-16, -34);
 }
示例#4
0
 public void TestConstructorInvalidColumn()
 {
     Position position = new Position(0, -34);
 }
示例#5
0
 public void TestConstructorInvalidRow()
 {
     Position position = new Position(-1, 3);
 }
示例#6
0
 public void TestConstructorValidArgumentsBothZero()
 {
     Position position = new Position(0, 0);
     Assert.AreEqual(0, position.Row);
     Assert.AreEqual(0, position.Column);
 }
示例#7
0
 public void TestConstructorValidArguments()
 {
     Position position = new Position(1, 4);
     Assert.AreEqual(1, position.Row);
     Assert.AreEqual(4, position.Column);
 }