private void Attack(object parameter)
        {
            Button button = parameter as Button;

            int row    = (int)button.GetValue(Grid.RowProperty);
            int column = (int)button.GetValue(Grid.ColumnProperty);

            ResultOfAttack result = _battleshipGridModel.AttackAtCoordinate(row, column);

            button.IsEnabled = false;

            if (result == ResultOfAttack.Hit)
            {
                button.Content = TARGET_HIT_CHAR;
                MessageBox.Show("It's a hit");
            }
            else if (result == ResultOfAttack.Sank)
            {
                button.Content = TARGET_HIT_CHAR;
                MessageBox.Show("It sank");
            }
            else if (result == ResultOfAttack.GameOver)
            {
                button.Content = TARGET_HIT_CHAR;
                MessageBox.Show("It's game over");
            }
            else if (result == ResultOfAttack.Miss)
            {
                button.Content = TARGET_MISSED_CHAR;
                MessageBox.Show("It's a miss");
            }
        }
        public ResultOfAttack AttackAtCoordinate(int row, int column)
        {
            ResultOfAttack result = _battleshipGrid.Attack(new Coordinate(row, column));

            IsNotGameOver = result == ResultOfAttack.GameOver ? false : true;
            return(result);
        }
Example #3
0
 public void Decode(NetIncomingMessage im)
 {
     this.Value = (ResultOfAttack) im.ReadInt32();
        this.Text = im.ReadString();
        this.Row = im.ReadInt32();
        this.Col = im.ReadInt32();
 }
 /// <summary>
 /// Set the _Value to the PossibleAttack value
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 public AttackResult(ResultOfAttack value, string text, int row, int column)
 {
     Value  = value;
     Text   = text;
     Row    = row;
     Column = column;
 }
 /*
  *         Summary: Set the _value to the PossibleAttack value
  *         Parameter: value - Either hit, miss, destroyed, shotalready
  *         Parameter: text - Text to display
  *         Parameter: row - Row to display
  *         Parameter: column - Column to display
  */
 public AttackResult(ResultOfAttack value, string text, int row, int column)
 {
     _value  = value;
     _text   = text;
     _ship   = null;
     _row    = row;
     _column = column;
 }
Example #6
0
 /// <summary>
 /// Set the _Value to the PossibleAttack value
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 public AttackResult(ResultOfAttack value, string text, int row, int column)
 {
     _Value  = value;
     _Text   = text;
     _Ship   = null;
     _Row    = row;
     _Column = column;
 }
Example #7
0
        public void TestAttack_IsMiss()
        {
            InitializeTheBattleshipGrid();
            Coordinate     pointOfAttack = new Coordinate(6, 5);
            ResultOfAttack result        = _battleshipGrid.Attack(pointOfAttack);

            Assert.IsTrue(result == ResultOfAttack.Miss);
        }
 /// <summary>
 /// Set the _Value to the PossibleAttack value
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 /// <param name="text">textual description of the result</param>
 /// <param name="row">row value of the cell attacked</param>
 /// <param name="column">column value of the cell attacked</param>   
 public AttackResult(ResultOfAttack value, string text, int row, int column)
 {
     _Value = value;
     _Text = text;
     _Ship = null;
     _Row = row;
     _Column = column;
 }
Example #9
0
        public void TestAttack_IsSank_OnSmallAssaultShip()
        {
            InitializeTheBattleshipGrid();
            Coordinate     pointOfAttack = new Coordinate(8, 9);
            ResultOfAttack result        = _battleshipGrid.Attack(pointOfAttack);

            Assert.IsTrue(result == ResultOfAttack.Sank);
        }
Example #10
0
 public AttackResult(ResultOfAttack value, string text, int row, int column)
 {
     Value = value;
        Text = text;
        Ship = 0;
        Row = row;
        Column = column;
 }
Example #11
0
        public void Attack_IsGameOverReturnTrue_ResultGameOver()
        {
            _fleetMock.Setup(x => x.IsGameOver()).Returns(true);

            ResultOfAttack result = _subjectUnderTest.Attack(_pointOfAttack);

            Assert.AreEqual(ResultOfAttack.GameOver, result);
        }
Example #12
0
        public void Attack_DidSankReturnsTrue_ResultSank()
        {
            _fleetMock.Setup(x => x.DidSank(_pointOfAttack)).Returns(true);

            ResultOfAttack result = _subjectUnderTest.Attack(_pointOfAttack);

            Assert.AreEqual(ResultOfAttack.Sank, result);
        }
Example #13
0
        public void Attack_IsHitReturnsTrue_ResultHit()
        {
            _fleetMock.Setup(x => x.IsHit(_pointOfAttack)).Returns(true);

            ResultOfAttack result = _subjectUnderTest.Attack(_pointOfAttack);

            Assert.AreEqual(ResultOfAttack.Hit, result);
        }
Example #14
0
        public void Attack_IsMissReturnsTrue_ResultMiss()
        {
            _fleetMock.Setup(x => x.IsHit(_pointOfAttack)).Returns(false);
            _fleetMock.Setup(x => x.DidSank(_pointOfAttack)).Returns(false);
            _fleetMock.Setup(x => x.IsGameOver()).Returns(false);

            ResultOfAttack result = _subjectUnderTest.Attack(_pointOfAttack);

            Assert.AreEqual(ResultOfAttack.Miss, result);
        }
Example #15
0
        public void TestAttack_IsSank_OnDestroyerShip()
        {
            InitializeTheBattleshipGrid();
            Coordinate     pointOfAttack = new Coordinate(7, 5);
            ResultOfAttack result        = _battleshipGrid.Attack(pointOfAttack);

            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 8;
            pointOfAttack.Longitude = 5;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 9;
            pointOfAttack.Longitude = 5;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Sank);
        }
Example #16
0
        public ResultOfAttack Attack(Coordinate pointOfAttack)
        {
            ResultOfAttack returnValue = ResultOfAttack.None;

            if (_fleet.IsHit(pointOfAttack))
            {
                returnValue = ResultOfAttack.Hit;
            }
            if (_fleet.DidSank(pointOfAttack))
            {
                returnValue = ResultOfAttack.Sank;
            }
            if (_fleet.IsGameOver())
            {
                returnValue = ResultOfAttack.GameOver;
            }
            if (returnValue == ResultOfAttack.None)
            {
                returnValue = ResultOfAttack.Miss;
            }
            return(returnValue);
        }
Example #17
0
 /// <summary>
 /// Set the _Value to the PossibleAttack value, and the _Ship to the ship
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 /// <param name="ship">the ship information</param>
 public AttackResult(ResultOfAttack value, Ship ship, string text, int row, int column) : this(value, text, row, column)
 {
     _Ship = ship;
 }
 public AttackResult(ResultOfAttack value, Ship ship, string text, int row, int column, Players pl)
     : this(value, text, row, column)
 {
     _Ship = ship;
     _pl = pl;
 }
 /// <summary>
 /// Set the _Value to the PossibleAttack value, and the _Ship to the ship
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 /// <param name="ship">the ship information</param>
 /// <param name="text">textual description of the result</param>
 /// <param name="row">row value of the cell attacked</param>
 /// <param name="column">column value of the cell attacked</param>  
 public AttackResult(ResultOfAttack value, Ship ship, string text, int row, int column)
     : this(value, text, row, column)
 {
     _Ship = ship;
 }
Example #20
0
        public void TestAttack_IsGameOver()
        {
            InitializeTheBattleshipGrid();
            Coordinate     pointOfAttack = new Coordinate(7, 5);
            ResultOfAttack result        = _battleshipGrid.Attack(pointOfAttack);

            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 8;
            pointOfAttack.Longitude = 5;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 9;
            pointOfAttack.Longitude = 5;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Sank);

            pointOfAttack.Latitude  = 1;
            pointOfAttack.Longitude = 1;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 1;
            pointOfAttack.Longitude = 2;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 1;
            pointOfAttack.Longitude = 3;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 1;
            pointOfAttack.Longitude = 4;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 1;
            pointOfAttack.Longitude = 5;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Sank);

            pointOfAttack.Latitude  = 1;
            pointOfAttack.Longitude = 7;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 2;
            pointOfAttack.Longitude = 7;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 3;
            pointOfAttack.Longitude = 7;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 4;
            pointOfAttack.Longitude = 7;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Sank);

            pointOfAttack.Latitude  = 5;
            pointOfAttack.Longitude = 2;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 5;
            pointOfAttack.Longitude = 3;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Hit);

            pointOfAttack.Latitude  = 5;
            pointOfAttack.Longitude = 4;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.Sank);

            pointOfAttack.Latitude  = 8;
            pointOfAttack.Longitude = 9;
            result = _battleshipGrid.Attack(pointOfAttack);
            Assert.IsTrue(result == ResultOfAttack.GameOver);
        }