public Gunnary() { m_enemyGrid = new EnemyGrid(); m_tactics = new RandomFiring(m_enemyGrid, Fleet.ShipLengths[0]); m_tacticsState = Tactics.Random; m_squaresHit = new List<Square>(); m_shipsLeft = new List<int>(Fleet.ShipLengths); }
public void NextTargetTest() { EnemyGrid grid = new EnemyGrid(); Square firstSquareHit = grid.GetSquare(9, 0); //firstSquareHit.Occupy(); int targetShipLength = 5; AfterFirstHitFiring tactics = new AfterFirstHitFiring(grid, firstSquareHit, targetShipLength); Square nextTarget = tactics.NextTarget(); Assert.IsTrue(nextTarget == grid.GetSquare(9, 1) || nextTarget == grid.GetSquare(8, 0)); Assert.IsFalse(nextTarget == grid.GetSquare(9, 0)); Assert.IsFalse(nextTarget == grid.GetSquare(8, 1)); firstSquareHit = grid.GetSquare(3, 3); //firstSquareHit.Occupy(); tactics = new AfterFirstHitFiring(grid, firstSquareHit, targetShipLength); nextTarget = tactics.NextTarget(); Assert.IsTrue(nextTarget == grid.GetSquare(3, 2) || nextTarget == grid.GetSquare(2, 3) || nextTarget == grid.GetSquare(3, 4) || nextTarget == grid.GetSquare(4, 3)); Assert.IsFalse(nextTarget == grid.GetSquare(3, 3)); }
void OnLevelWasLoaded(int level) { player = player.GetComponent<PlayerController> (); enemies = enemies.GetComponent<EnemyGrid> (); }
/// <summary> /// ''' GenerateCoordinates should generate random shooting coordinates /// ''' only when it has not found a ship, or has destroyed a ship and /// ''' needs new shooting coordinates /// ''' </summary> /// ''' <param name="row">the generated row</param> /// ''' <param name="column">the generated column</param> protected override void GenerateCoords(ref int row, ref int column) { do { // check which state the AI is in and uppon that choose which coordinate generation // method will be used. switch (_CurrentState) { case AIStates.Searching: { SearchCoords(ref row, ref column); break; } case AIStates.TargetingShip: { TargetCoords(ref row, ref column); break; } default: { throw new ApplicationException("AI has gone in an imvalid state"); } } }while ((row < 0 || column < 0 || row >= EnemyGrid.Height || column >= EnemyGrid.Width || EnemyGrid.Item(row, column) != TileView.Sea)); // while inside the grid and not a sea tile do the search }
public void NextTargetTest() { // test for a horizontal ship in the middle of playground EnemyGrid grid = new EnemyGrid(); Square sq1 = grid.GetSquare(3, 3); Square sq2 = grid.GetSquare(3, 4); Square sq3 = grid.GetSquare(3, 5); Square[] squaresHit = new Square[] { sq1, sq2, sq3 }; int targetShipLength = 4; OrientationalFiring tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); Square next = tactics.NextTarget(); Assert.IsTrue(next.Row == 3 && (next.Column == 2 || next.Column == 6)); // now occupy a square at one of ends grid.GetSquare(3, 2).Occupy(); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 3 && next.Column == 6); // test for a vertical ship at the bottom of playground grid = new EnemyGrid(); sq1 = grid.GetSquare(7, 1); sq2 = grid.GetSquare(8, 1); sq3 = grid.GetSquare(9, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 6 && next.Column == 1); // test for a vertical ship in the middle of playground grid = new EnemyGrid(); sq1 = grid.GetSquare(3, 1); sq2 = grid.GetSquare(4, 1); sq3 = grid.GetSquare(5, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); next = tactics.NextTarget(); Assert.IsTrue((next.Row == 6 || next.Row == 2) && next.Column == 1); // now occupy a square at one of ends grid.GetSquare(6, 1).Occupy(); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 2 && next.Column == 1); // test for a vertical ship at the bottom of playground grid = new EnemyGrid(); sq1 = grid.GetSquare(7, 1); sq2 = grid.GetSquare(8, 1); sq3 = grid.GetSquare(9, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 6 && next.Column == 1); grid = new EnemyGrid(); sq1 = grid.GetSquare(3, 1); sq2 = grid.GetSquare(5, 1); sq3 = grid.GetSquare(4, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); tactics.AddNewHit(grid.GetSquare(6, 1)); next = tactics.NextTarget(); Assert.IsTrue((next.Row == 7 || next.Row == 2) && next.Column == 1); }
private void InitializeGrids() { _grid = FindObjectOfType <Grid>(); _enemyGrid = FindObjectOfType <EnemyGrid>(); }
/// <summary> /// ''' AddTarget will add the targets it will shoot onto a stack /// ''' </summary> /// ''' <param name="row">the row of the targets location</param> /// ''' <param name="column">the column of the targets location</param> private void AddTarget(int row, int column) { if (row >= 0 && column >= 0 && row < EnemyGrid.Height && column < EnemyGrid.Width && EnemyGrid.Item(row, column) == TileView.Sea) { _Targets.Push(new Location(row, column)); } }
/// <summary> /// GenerateCoordinates should generate random shooting coordinates /// only when it has not found a ship, or has destroyed a ship and /// needs new shooting coordinates /// </summary> /// <param name="row">the generated row</param> /// <param name="column">the generated column</param> protected override void GenerateCoords(ref int row, ref int column) { do { // AI random row and column to shot SearchCoords(ref row, ref column); } while ((row < 0 || column < 0 || row >= EnemyGrid.Height || column >= EnemyGrid.Width || EnemyGrid.Item(row, column) != TileView.Sea)); //while inside the grid and not a sea tile do the search }
// <summary> // GenerateCoordinates should generate random shooting coordinates // only when it has not found a ship, or has destroyed a ship and // needs new shooting coordinates // </summary> // <param name="row">the generated row</param> // <param name="column">the generated column</param> protected override void GenerateCoords(ref int row, ref int column) { do { // Check which state the AI is in and upon that choose which coordinate generation // method will be used. // Stop looking for coordinates once it reaches outside the grid or finds a tile // that has an enemy ship. switch (_CurrentState) { case AIStates.Searching: SearchCoords(ref row, ref column); break; case AIStates.TargetingShip: TargetCoords(ref row, ref column); break; default: throw new ApplicationException("AI has gone in an imvalid state"); } } while (row < 0 || column < 0 || row >= EnemyGrid.Height || column >= EnemyGrid.Width || EnemyGrid.get_Item(row, column) != TileView.Sea); //while inside the grid and not a sea tile do the search }
private void Start() { _grid = GetComponent <EnemyGrid>(); pool = GetComponent <Pool>(); SpawnRows(true); }
/// <summary> /// GenerateCoordinates should generate random shooting coordinates /// only when it has not found a ship, or has destroyed a ship and /// needs new shooting coordinates /// </summary> /// <param name="row">the generated row</param> /// <param name="column">the generated column</param> protected override void GenerateCoords(ref int row, ref int column) { do { //check which state the AI is in and uppon that choose which coordinate generation //method will be used. SearchCoords(ref row, ref column); } while ((row < 0 || column < 0 || row >= EnemyGrid.Height || column >= EnemyGrid.Width || EnemyGrid.Item(row, column) != TileView.Sea)); //while inside the grid and not a sea tile do the search }
public AfterFirstHitFiring(EnemyGrid grid, Square firstSquareHit, int targetShipLength) { m_grid = grid; m_firstSquare = firstSquareHit; m_targetShipLength = targetShipLength; }
public void GameCommandRecieved(object sender, CommandEventArgs e) { Console.WriteLine("Game command received. data:{0} type:{1}", e.Command.Data, e.Command.CommandType); if (e.Command.CommandType == CommandType.GameStartInform) { if (e.Command.Data.ToLower() == "true") { myTurn = true; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("firstShotMe"));; }); btnFire.BackColor = System.Drawing.Color.Red; btnFire.ForeColor = System.Drawing.Color.White; //btnFire.Enabled = true; } else if (e.Command.Data.ToLower() == "false") { myTurn = false; btnFire.Enabled = false; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("firstShotEnemy"));; }); } } if (e.Command.CommandType == CommandType.GameShotResult) { if (e.Command.Data.ToLower().Equals("hit")) { //Deal with hits // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightBomb.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("shotHit")); }); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.ShipHit; pictureBox.Tag = "ShipHit"; }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.Enabled = false; }); myTurn = false; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("waitEnemyShot"));; }); }).Start(); } else if (e.Command.Data.ToLower().Equals("miss")) { //deal with misses // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightWater.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("shotMissed")); }); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.WaterMiss; pictureBox.Tag = "WaterMiss"; }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.Enabled = false; }); myTurn = false; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("waitEnemyShot"));; }); }).Start(); } } if (e.Command.CommandType == CommandType.GameHitInform) { // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightBomb.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("oneShipHit")); }); gridTarget.x = int.Parse(e.Command.Data.Split(',')[0]); gridTarget.y = int.Parse(e.Command.Data.Split(',')[1]); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.ShipHit; }); myTurn = true; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("turnToShot")); }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.BackColor = System.Drawing.Color.Red; btnFire.ForeColor = System.Drawing.Color.White; }); }).Start(); } if (e.Command.CommandType == CommandType.GameMissInform) { // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightWater.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("opponentMissedFleet")); }); gridTarget.x = int.Parse(e.Command.Data.Split(',')[0]); gridTarget.y = int.Parse(e.Command.Data.Split(',')[1]); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.WaterMiss; }); myTurn = true; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("turnToShot")); }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.BackColor = System.Drawing.Color.Red; btnFire.ForeColor = System.Drawing.Color.White; }); }).Start(); } if (e.Command.CommandType == CommandType.GameOverInform) { if (e.Command.Data.ToLower() == "win") { Command cmdInform = new Command(CommandType.GameOverInform, client.ServerIP, gameID + ":" + "win"); cmdInform.TargetPort = client.ServerPort; cmdInform.SenderIP = client.IP; cmdInform.SenderPort = client.Port; cmdInform.SenderName = client.Username; client.Wins++; client.SendCommand(cmdInform); MessageBox.Show(i18n.GetText("wonCongratulations", client.Username), i18n.GetText("winner"), MessageBoxButtons.OK); Close(); } else if (e.Command.Data.ToLower() == "loss") { Command cmdInform = new Command(CommandType.GameOverInform, client.ServerIP, gameID + ":" + "loss"); cmdInform.TargetPort = client.ServerPort; cmdInform.SenderIP = client.IP; cmdInform.SenderPort = client.Port; cmdInform.SenderName = client.Username; client.Losses++; client.SendCommand(cmdInform); MessageBox.Show(i18n.GetText("lossGame", client.Username), i18n.GetText("gameLost"), MessageBoxButtons.OK); Close(); } } }
private void EnemyGrid_Click(object sender, EventArgs e) { if (myTurn) { Control control = (Control)sender; if (gridTarget.x > -1 && gridTarget.y > -1) { if (pictureBox != null) { if (pictureBox.Tag.ToString() == "WaterTarget") { pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.Water; pictureBox.Tag = "Water"; } } } gridTarget = new GridPosition(EnemyGrid.GetPositionFromControl(control).Column, EnemyGrid.GetPositionFromControl(control).Row); pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); if (pictureBox.Tag.ToString() == "Water") { pictureBox.Image = Properties.Resources.WaterTarget; pictureBox.Tag = "WaterTarget"; btnFire.Enabled = true; } else { btnFire.Enabled = false; } } }
public RandomFiring(EnemyGrid grid, int targetShipLength) { m_grid = grid; m_targetShipLength = targetShipLength; }
// '' <summary> // '' GenerateCoordinates should generate random shooting coordinates // '' only when it has not found a ship, or has destroyed a ship and // '' needs new shooting coordinates // '' </summary> // '' <param name="row">the generated row</param> // '' <param name="column">the generated column</param> protected override void GenerateCoords(ref int row, ref int column) { do { switch (_CurrentState) { case AIStates.Searching: SearchCoords(ref row, ref column); break; case AIStates.TargetingShip: TargetCoords(ref row, ref column); break; default: throw new ApplicationException("AI has gone in an imvalid state"); break; } } while ((row < 0 || column < 0 || row >= EnemyGrid.Height || column >= EnemyGrid.Width || EnemyGrid.Item(row, column) != TileView.Sea)); }
public OrientationalFiring(EnemyGrid grid, Square[] squaresHit, int targetShipLength) { m_grid = grid; m_squaresHit = squaresHit; m_targetShipLength = targetShipLength; }
public static void Init(float width, int sizeX, int sizeY, Vector2 start) { Instance = new EnemyGrid(width, sizeX, sizeY, start); }