Example #1
0
        public void TestMove()
        {
            GameObject GameObject = new GameObject();
              GameObject.whites = new List<Figure>();
              GameObject.blacks = new List<Figure>();
              King wKing = new King(GameObject, 1, 1, Color.white);
              King bKing = new King(GameObject, 8, 8, Color.black);
              Knight bKnight = new Knight(GameObject, 5, 5, Color.black);
              Knight wKnight = new Knight(GameObject, 5, 4, Color.white);
              GameObject.whites.Add(wKing);
              GameObject.blacks.Add(bKing);
              GameObject.blacks.Add(bKnight);
              GameObject.whites.Add(wKnight);

              // knights in the middle of the field
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wKnight.MoveFields.Count == 8);
              Assert.IsTrue(wKnight.CanMoveToPosition(3, 5));
              Assert.IsTrue(wKnight.CanMoveToPosition(3, 3));
              Assert.IsTrue(wKnight.CanMoveToPosition(4, 6));
              Assert.IsTrue(wKnight.CanMoveToPosition(4, 2));
              Assert.IsTrue(wKnight.CanMoveToPosition(6, 6));
              Assert.IsTrue(wKnight.CanMoveToPosition(6, 2));
              Assert.IsTrue(wKnight.CanMoveToPosition(7, 3));
              Assert.IsTrue(wKnight.CanMoveToPosition(7, 5));

              Assert.IsTrue(bKnight.MoveFields.Count == 8);
              Assert.IsTrue(bKnight.CanMoveToPosition(3, 6));
              Assert.IsTrue(bKnight.CanMoveToPosition(3, 4));
              Assert.IsTrue(bKnight.CanMoveToPosition(4, 7));
              Assert.IsTrue(bKnight.CanMoveToPosition(4, 3));
              Assert.IsTrue(bKnight.CanMoveToPosition(6, 7));
              Assert.IsTrue(bKnight.CanMoveToPosition(6, 3));
              Assert.IsTrue(bKnight.CanMoveToPosition(7, 4));
              Assert.IsTrue(bKnight.CanMoveToPosition(7, 6));

              // knights at the border of the field
              bKnight.field.x = 8;
              bKnight.field.y = 7;
              wKnight.field.x = 3;
              wKnight.field.y = 2;
              GameObject.UpdateAllBeatFields();

              Assert.IsTrue(wKnight.MoveFields.Count == 5);
              Assert.IsTrue(wKnight.CanMoveToPosition(1, 3));
              Assert.IsTrue(wKnight.CanMoveToPosition(2, 4));
              Assert.IsTrue(wKnight.CanMoveToPosition(4, 4));
              Assert.IsTrue(wKnight.CanMoveToPosition(5, 3));
              Assert.IsTrue(wKnight.CanMoveToPosition(5, 1));

              Assert.IsTrue(bKnight.MoveFields.Count == 3);
              Assert.IsTrue(bKnight.CanMoveToPosition(6, 8));
              Assert.IsTrue(bKnight.CanMoveToPosition(6, 6));
              Assert.IsTrue(bKnight.CanMoveToPosition(7, 5));

              // Add white rook
              Rook wRook = new Rook(GameObject, 8, 2, Color.white);
              GameObject.whites.Add(wRook);
              GameObject.UpdateAllBeatFields();

              Assert.IsTrue(wRook.Move(8, 3));
              Assert.IsFalse(bKnight.Move(6, 8)); // Can't move knight - open attack on king
              Assert.IsTrue(bKnight.field.x == 8 && bKnight.field.y == 7);
              Assert.IsFalse(bKnight.Move(6, 6)); // Can't move knight - open attack on king
              Assert.IsTrue(bKnight.field.x == 8 && bKnight.field.y == 7);
              Assert.IsFalse(bKnight.Move(7, 5)); // Can't move knight - open attack on king
              Assert.IsTrue(bKnight.field.x == 8 && bKnight.field.y == 7);

              // Add white pawn and beat it by knight
              Pawn p1 = new Pawn(GameObject, 6, 6, Color.white);
              GameObject.whites.Add(p1);
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(bKnight.Move(6, 6));

              // remove rook
              GameObject.whites.Remove(wRook);
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(bKnight.Move(6, 6));

              // add white rook
              wRook.field.x = 5;
              wRook.field.y = 1;
              GameObject.whites.Add(wRook);
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wRook.Move(5, 8));
              Assert.IsTrue(bKnight.Move(5, 8));

              // add white rook again
              wRook.field.x = 1;
              wRook.field.y = 2;
              bKnight.field.x = 6;
              bKnight.field.y = 6;
              GameObject.whites.Add(wRook);
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wRook.Move(1, 8));
              Assert.IsFalse(bKnight.Move(5, 4)); //king not covered
              Assert.IsFalse(bKnight.Move(7, 4)); //king not covered
              Assert.IsTrue(bKnight.Move(7, 8)); //cover king
        }
Example #2
0
 public bool Transform(sbyte x, sbyte y, FigureTypes type)
 {
     Figure p = this.GetFigureByXY(x, y);
       if (p.type != FigureTypes.Pawn)
     return false;
       Figure newFigure;
       switch (type)
       {
     case FigureTypes.Knight:
       newFigure = new Knight(this, p.field.x, p.field.y, p.color);
       break;
     case FigureTypes.Bishop:
       newFigure = new Bishop(this, p.field.x, p.field.y, p.color);
       break;
     case FigureTypes.Queen:
       newFigure = new Queen(this, p.field.x, p.field.y, p.color);
       break;
     case FigureTypes.Rook:
       newFigure = new Rook(this, p.field.x, p.field.y, p.color);
       break;
     default:
       return false;
       }
       if (p.color == Color.white)
       {
     this.whites.Remove(p);
     this.whites.Add(newFigure);
       }
       else
       {
     this.blacks.Remove(p);
     this.blacks.Add(newFigure);
       }
       this.UpdateAllBeatFields();
       this.IsGameOvered();
       return true;
 }
Example #3
0
        public void TestMove()
        {
            GameObject GameObject = new GameObject();
              GameObject.whites = new List<Figure>();
              GameObject.blacks = new List<Figure>();
              King wKing = new King(GameObject, 2, 1, Color.white);
              Queen wQueen = new Queen(GameObject, 2, 2, Color.white);
              King bKing = new King(GameObject, 7, 8, Color.black);
              Queen bQueen = new Queen(GameObject, 7, 7, Color.black);
              Rook bRook1 = new Rook(GameObject, 1, 8, Color.black);
              Rook bRook2 = new Rook(GameObject, 8, 8, Color.black);
              Rook wRook1 = new Rook(GameObject, 1, 1, Color.white);
              Rook wRook2 = new Rook(GameObject, 8, 1, Color.white);
              GameObject.whites.Add(wKing);
              GameObject.blacks.Add(bKing);
              GameObject.blacks.Add(bQueen);
              GameObject.whites.Add(wQueen);
              GameObject.UpdateAllBeatFields();

              Assert.IsTrue(wKing.MoveFields.Count == 4);
              Assert.IsTrue(wKing.CanMoveToPosition(1, 1));
              Assert.IsTrue(wKing.CanMoveToPosition(1, 2));
              Assert.IsTrue(wKing.CanMoveToPosition(3, 1));
              Assert.IsTrue(wKing.CanMoveToPosition(3, 2));

              Assert.IsTrue(bKing.MoveFields.Count == 4);
              Assert.IsTrue(bKing.CanMoveToPosition(6, 8));
              Assert.IsTrue(bKing.CanMoveToPosition(6, 7));
              Assert.IsTrue(bKing.CanMoveToPosition(8, 8));
              Assert.IsTrue(bKing.CanMoveToPosition(8, 7));

              Assert.IsTrue(wKing.Move(3, 2));
              Assert.IsTrue(bKing.Move(6, 7));
              Assert.IsFalse(wKing.Move(3, 3));
              Assert.IsTrue(wQueen.Move(7, 7));
              Assert.IsTrue(GameObject.blacks.Count == 1);
              Assert.IsFalse(bKing.Move(5, 7));
              Assert.IsFalse(bKing.Move(6, 8));
              Assert.IsTrue(bKing.Move(7, 7));
              Assert.IsTrue(GameObject.whites.Count == 1);

              // verify castling
              GameObject.blacks.Add(bRook1);
              GameObject.blacks.Add(bRook2);
              GameObject.whites.Add(wRook1);
              GameObject.whites.Add(wRook2);
              wKing.field = new Field(5, 1);
              bKing.field = new Field(5, 8);
              wKing.moveCount = 0;
              bKing.moveCount = 0;
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wKing.CanMoveToPosition(7, 1));
              Assert.IsTrue(wKing.CanMoveToPosition(3, 1));
              wKing.moveCount = 1;
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.CanMoveToPosition(7, 1));
              Assert.IsFalse(wKing.CanMoveToPosition(3, 1));
              wRook2.moveCount = 1;
              wRook1.moveCount = 1;
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.CanMoveToPosition(7, 1));
              Assert.IsFalse(wKing.CanMoveToPosition(3, 1));
              wKing.moveCount = 0;
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.CanMoveToPosition(7, 1));
              Assert.IsFalse(wKing.CanMoveToPosition(3, 1));
              wRook2.moveCount = 0;
              wRook1.moveCount = 0;
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wKing.CanMoveToPosition(7, 1));
              Assert.IsTrue(wKing.CanMoveToPosition(3, 1));

              bRook1.field = new Field(2, 7);
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wKing.CanMoveToPosition(3, 1));
              bRook1.field = new Field(3, 7);
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.CanMoveToPosition(3, 1));
              bRook1.field = new Field(4, 7);
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.CanMoveToPosition(3, 1));
              bRook1.field = new Field(5, 7);
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.CanMoveToPosition(7, 1));
              Assert.IsFalse(wKing.CanMoveToPosition(3, 1));
              bRook1.field = new Field(6, 7);
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wKing.CanMoveToPosition(3, 1));
              Assert.IsFalse(wKing.CanMoveToPosition(7, 1));
              bRook1.field = new Field(7, 7);
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.CanMoveToPosition(7, 1));

              // check figure taking by king
              bRook1.field = new Field(5, 2);
              bRook2.field = new Field(7, 2);
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.Move(5, 2));

              bRook2.field = new Field(7, 3);
              bKing.field = new Field(6, 3);
              GameObject.UpdateAllBeatFields();
              Assert.IsFalse(wKing.Move(5, 2));

              bRook2.field = new Field(7, 3);
              bKing.field = new Field(6, 4);
              GameObject.UpdateAllBeatFields();
              Assert.IsTrue(wKing.Move(5, 2));
              Assert.IsFalse(bKing.Move(6, 3));
              Assert.IsFalse(bKing.Move(7, 3));
              Assert.IsTrue(bKing.Move(5, 4));
        }
Example #4
0
        public GameObject()
        {
            for (sbyte i = 1; i <= 8; i++)
              {
            Pawn p1 = new Pawn(this, i, 7, Color.black);
            this.blacks.Add(p1);

            Pawn wp1 = new Pawn(this, i, 2, Color.white);
            this.whites.Add(wp1);
              }

              Bishop b1 = new Bishop(this, 3, 8, Color.black);
              this.blacks.Add(b1);

              Bishop b2 = new Bishop(this, 6, 8, Color.black);
              this.blacks.Add(b2);

              Bishop wb1 = new Bishop(this, 3, 1, Color.white);
              this.whites.Add(wb1);

              Bishop wb2 = new Bishop(this, 6, 1, Color.white);
              this.whites.Add(wb2);

              Rook r1 = new Rook(this, 1, 8, Color.black);
              this.blacks.Add(r1);

              Rook r2 = new Rook(this, 8, 8, Color.black);
              this.blacks.Add(r2);

              Rook wr1 = new Rook(this, 1, 1, Color.white);
              this.whites.Add(wr1);

              Rook wr2 = new Rook(this, 8, 1, Color.white);
              this.whites.Add(wr2);

              Knight kn1 = new Knight(this, 2, 8, Color.black);
              this.blacks.Add(kn1);

              Knight kn2 = new Knight(this, 7, 8, Color.black);
              this.blacks.Add(kn2);

              Knight wkn1 = new Knight(this, 2, 1, Color.white);
              this.whites.Add(wkn1);

              Knight wkn2 = new Knight(this, 7, 1, Color.white);
              this.whites.Add(wkn2);

              var q = new Queen(this, 4, 8, Color.black);
              this.blacks.Add(q);

              var wq = new Queen(this, 4, 1, Color.white);
              this.whites.Add(wq);

              var k = new King(this, 5, 8, Color.black);
              this.blacks.Add(k);

              var wk = new King(this, 5, 1, Color.white);
              this.whites.Add(wk);

              this.UpdateAllBeatFields();
        }
Example #5
0
        public void TestDraw()
        {
            GameObject game = new GameObject();
              // get knight
              game.whites = new List<Figure>();
              game.blacks = new List<Figure>();
              King wKing = new King(game, 2, 2, Color.white);
              King bKing = new King(game, 2, 6, Color.black);
              Rook wRook = new Rook(game, 1, 1, Color.white);
              Rook bRook = new Rook(game, 1, 7, Color.black);
              Pawn wPawn = new Pawn(game, 5, 2, Color.white);
              game.whites.Add(wKing);
              game.whites.Add(wRook);
              game.blacks.Add(bKing);
              game.blacks.Add(bRook);
              game.whites.Add(wPawn);
              game.UpdateAllBeatFields();

              Assert.IsFalse(game.isDraw);
              wRook.Move(2, 1);
              bRook.Move(2, 7);
              Assert.IsTrue(game.movesToDraw == 2);
              wPawn.Move(5, 4);
              Assert.IsTrue(game.movesToDraw == 0);
              bRook.Move(3, 7);
              wPawn.Move(5, 5);
              Assert.IsTrue(game.movesToDraw == 0);
              bRook.Move(4, 7);
              wPawn.Move(5, 6);
              Assert.IsTrue(game.movesToDraw == 0);
              bRook.Move(1, 7);
              wPawn.Move(5, 7);
              Assert.IsTrue(game.movesToDraw == 0);
              bRook.Move(2, 7);
              wRook.Move(1, 1);
              Assert.IsTrue(game.movesToDraw == 2);
              bRook.Move(5, 7);
              Assert.IsTrue(game.movesToDraw == 0);

              sbyte new_x;
              for (int i = 0; i < 100; i++)
              {
            if (game.GetColorTurn() == Color.white)
            {
              new_x = (sbyte) (wRook.field.x == 1 ? 2 : 1);
              Assert.IsTrue(wRook.Move(new_x, 1));
            }
            else
            {
              new_x = (sbyte) (bRook.field.x == 1 ? 2 : 1);
              Assert.IsTrue(bRook.Move(new_x, 7));
            }
            if (game.movesToDraw == 100)
              Assert.IsTrue(game.isDraw);
              }
        }
Example #6
0
        public void GamePat()
        {
            GameObject game = new GameObject();
              game.whites = new List<Figure>();
              game.blacks = new List<Figure>();
              King wKing = new King(game, 2, 3, Color.white);
              King bKing = new King(game, 1, 1, Color.black);
              Knight bKnight = new Knight(game, 2, 1, Color.black);
              Rook wRook = new Rook(game, 7, 2, Color.white);
              game.whites.Add(wKing);
              game.whites.Add(wRook);
              game.blacks.Add(bKing);
              game.blacks.Add(bKnight);
              game.UpdateAllBeatFields();

              Assert.IsTrue(wRook.Move(2, 2));
              Assert.IsFalse(game.isPat);
              Assert.IsTrue(bKnight.Move(4, 2));
              Assert.IsTrue(wRook.Move(4, 2));
              Assert.IsTrue(bKing.Move(2, 1));
              Assert.IsTrue(wRook.Move(3, 2));
              Assert.IsTrue(bKing.Move(1, 1));
              Assert.IsTrue(wRook.Move(2, 2));
              Assert.IsTrue(game.isPat);
        }
Example #7
0
        public void GameMate()
        {
            // Variant 1
              GameObject game = new GameObject();
              Assert.IsTrue(game.GetFigureByXY(5, 2).Move(5, 4));
              Assert.IsTrue(game.GetFigureByXY(5, 7).Move(5, 5));
              Assert.IsTrue(game.GetFigureByXY(6, 1).Move(3, 4));
              Assert.IsTrue(game.GetFigureByXY(6, 8).Move(3, 5));
              Assert.IsTrue(game.GetFigureByXY(4, 1).Move(6, 3));
              Assert.IsTrue(game.GetFigureByXY(8, 7).Move(8, 5));
              Assert.IsTrue(game.GetFigureByXY(6, 3).Move(6, 7));
              Assert.IsTrue(game.isMat);

              // Variant 2
              game = new GameObject();
              game.whites = new List<Figure>();
              game.blacks = new List<Figure>();
              King wKing = new King(game, 2, 3, Color.white);
              King bKing = new King(game, 1, 1, Color.black);
              Knight bKnight = new Knight(game, 2, 1, Color.black);
              Rook wRook = new Rook(game, 7, 2, Color.white);
              game.whites.Add(wKing);
              game.whites.Add(wRook);
              game.blacks.Add(bKing);
              game.blacks.Add(bKnight);
              game.UpdateAllBeatFields();

              Assert.IsTrue(wRook.Move(1, 2));
              Assert.IsTrue(game.isMat);
        }
Example #8
0
 public bool Transform(FigureTypes figureTypes)
 {
     if ((field.y == 8 && color == Color.white) ||
       (field.y == 1 && color == Color.black))
       {
     Figure newFigure;
     switch (figureTypes)
     {
       case FigureTypes.Bishop:
     newFigure = new Bishop(GameObject, field.x, field.y, color);
     break;
       case FigureTypes.Knight:
     newFigure = new Knight(GameObject, field.x, field.y, color);
     break;
       case FigureTypes.Queen:
     newFigure = new Queen(GameObject, field.x, field.y, color);
     break;
       case FigureTypes.Rook:
     newFigure = new Rook(GameObject, field.x, field.y, color);
     break;
       default:
     return false;
     }
     GameObject.RemoveFigureByXY(field.x, field.y);
     if (color == Color.black)
       GameObject.blacks.Add(newFigure);
     else
       GameObject.whites.Add(newFigure);
     GameObject.UpdateAllBeatFields();
     return true;
       }
       return false;
 }