private void acceptShipsBtn_Click(object sender, EventArgs e) { if (seafight.AcceptShips()) { computerSeaFight = new SeaFight(this, 300, 100, 100, invPanel, true, game.gameID, returnPlayerID("Computer")); computerSeaFight.Square.BackgroundImage = Image.FromFile(Application.StartupPath + @"\Resources\BackgroundImage.png", true); computerSeaFight.MouseUp += ComputerSeaFight_MouseUp; game.GameStage = GS.Play; Form1_Resize(this, e); } }
private void Game_GameStarted(object sender, EventArgs e) { Menu.Hide(); seafight = new SeaFight(this, 300, 100, 100, invPanel, false, game.gameID, returnPlayerID(nicknameBox.Text)); seafight.Square.BackgroundImage = Image.FromFile(Application.StartupPath + @"\Resources\BackgroundImage.png", true); invPanel.Show(); this.MouseWheel += Form1_MouseWheel; this.FormBorderStyle = FormBorderStyle.Sizable; this.ClientSize = new Size(300, 300); Form1_Resize(this, e); seafight.MouseUp += Seafight_MouseUp; }
public bool ComputerMove(SeaFight Target) { Cell cell; if (!isTargetAcquired) { movePositions = Enum.GetValues(typeof(MovePosition)).Cast <MovePosition>().Where(v => v != availablemovePosition).ToList(); availablemovePosition = movePositions[rnd.Next(0, movePositions.Count)]; tempPosition = availablemovePosition; int xMin = 0, xMax = 0, yMin = 0, yMax = 0; switch (availablemovePosition) { case MovePosition.TOPLEFTCORNER: xMin = 0; xMax = 4; yMin = 0; yMax = 4; break; case MovePosition.TOPRIGHTCORNER: xMin = 5; xMax = 9; yMin = 0; yMax = 4; break; case MovePosition.BOTTOMLEFTCORNER: xMin = 0; xMax = 4; yMin = 5; yMax = 9; break; case MovePosition.BOTTOMRIGHTCORNER: xMin = 5; xMax = 9; yMin = 5; yMax = 9; break; } List <Cell> availableCells = cells.Where(v => v.position.X >= xMin && v.position.X <= xMax && v.position.Y >= yMin && v.position.Y <= yMax && v.type != TypeCell.Wounded && v.type != TypeCell.Miss).Cast <Cell>().ToList(); while (availableCells.Count == 0) { movePositions = movePositions.Where(v => v.ToString() != availablemovePosition.ToString()).ToList(); if (movePositions.Count == 0) { availablemovePosition = tempPosition; } else { availablemovePosition = movePositions[rnd.Next(0, movePositions.Count)]; } xMin = xMax = yMin = yMax = 0; switch (availablemovePosition) { case MovePosition.TOPLEFTCORNER: xMin = 0; xMax = 4; yMin = 0; yMax = 4; break; case MovePosition.TOPRIGHTCORNER: xMin = 5; xMax = 9; yMin = 0; yMax = 4; break; case MovePosition.BOTTOMLEFTCORNER: xMin = 0; xMax = 4; yMin = 5; yMax = 9; break; case MovePosition.BOTTOMRIGHTCORNER: xMin = 5; xMax = 9; yMin = 5; yMax = 9; break; } availableCells = cells.Where(v => v.position.X >= xMin && v.position.X <= xMax && v.position.Y >= yMin && v.position.Y <= yMax && v.type != TypeCell.Wounded && v.type != TypeCell.Miss).Cast <Cell>().ToList(); } cell = availableCells[rnd.Next(0, availableCells.Count)]; attackPosition = cell.position; } else { if (leftMove) { prevAttackPosition = new Point(prevAttackPosition.X - 1, prevAttackPosition.Y); cell = cells.FirstOrDefault(c => c.position == prevAttackPosition); if (cell == null || cell.type == TypeCell.Miss) { leftMove = false; rightMove = true; prevAttackPosition = targetPosition; } } if (rightMove) { prevAttackPosition = new Point(prevAttackPosition.X + 1, prevAttackPosition.Y); cell = cells.FirstOrDefault(c => c.position == prevAttackPosition); if (cell == null || cell.type == TypeCell.Miss) { rightMove = false; upMove = true; prevAttackPosition = targetPosition; } } if (upMove) { prevAttackPosition = new Point(prevAttackPosition.X, prevAttackPosition.Y - 1); cell = cells.FirstOrDefault(c => c.position == prevAttackPosition); if (cell == null || cell.type == TypeCell.Miss) { upMove = false; downMove = true; prevAttackPosition = targetPosition; } } if (downMove) { prevAttackPosition = new Point(prevAttackPosition.X, prevAttackPosition.Y + 1); cell = cells.FirstOrDefault(c => c.position == prevAttackPosition); if (cell == null || cell.type == TypeCell.Miss) { downMove = false; leftMove = true; prevAttackPosition = targetPosition; } } attackPosition = prevAttackPosition; } int result = Target.Fire(attackPosition); if (result == 1 && !isTargetAcquired) { isTargetAcquired = true; leftMove = true; rightMove = false; upMove = false; downMove = false; targetPosition = attackPosition; prevAttackPosition = attackPosition; } else if (result == 2) { isTargetAcquired = false; } else if (result == 0 && isTargetAcquired) { if (leftMove) { leftMove = false; rightMove = true; prevAttackPosition = targetPosition; } else if (rightMove) { rightMove = false; upMove = true; prevAttackPosition = targetPosition; } else if (upMove) { upMove = false; downMove = true; prevAttackPosition = targetPosition; } } return(result >= 1 ? true : false); }