//This event occurs when the Server player clicks on the Server grid private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e) { //each time the Server player clicks, we increment the clickCount to determine which action to take clickCount++; DataGridViewCell cell; int rowIndex = e.RowIndex; int colIndex = e.ColumnIndex; //If the user clicks in an invalid cell, it's corrected if (e.ColumnIndex == 0) colIndex = 1; if (e.RowIndex < 0) rowIndex = 0; //We grab the cell that the user clicked cell = dataGridView2.Rows[rowIndex].Cells[colIndex]; //If the user is placing ships, we check to see which ship is currently being placed if (placingShips) { if (currentShip.Equals("Battleship")) { //If this is the first click for the current ship, we only designate an anchoring position for the ship if (clickCount == 1) { //and ask the user to then choose an orientation by clicking a different square lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[0].Position_Row = cell.RowIndex; lstShips[0].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { //once the user clicks a different square //We check to see if it's to the left/right of the previous click or if it's above/below if (cell.RowIndex > lstShips[0].Position_Row || cell.RowIndex < lstShips[0].Position_Row) { //If it's above/below the original click, we place the ship vertically based on its size if (lstShips[0].Position_Row + 4 <= 9) { //This if statement ensures that the chosen placement will allow the ship to fit on the grid //if it can fit, we place it from top to bottom dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 3].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 4].Style.BackColor = Color.Gray; } else { //but if that won't fit on the grid, we place the ship bottom to top instead dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 3].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 4].Style.BackColor = Color.Gray; } lstShips[0].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[0].Position_Col || cell.ColumnIndex < lstShips[0].Position_Col) { //If the second chosen cell is left/right of the original click, we place the ship horizontally if (lstShips[0].Position_Col + 4 <= 10) { //and again ensure that this will fit on the grid from left to right dataGridView2[lstShips[0].Position_Col + 1, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col + 2, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col + 3, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col + 4, lstShips[0].Position_Row].Style.BackColor = Color.Gray; } else { //and if not, we place it right to left dataGridView2[lstShips[0].Position_Col - 1, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col - 2, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col - 3, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col - 4, lstShips[0].Position_Row].Style.BackColor = Color.Gray; } lstShips[0].Orientation = Ship.ShipOrientation.Horizontal; } //At this point, we change ships and repeat the same process until all ships have been placed currentShip = "Cruiser"; clickCount = 0; s = new Ship(); s.Name = "Cruiser"; s.ExtentUnits = 4; lstShips.Add(s); lStatus.Text = "Click a square on the Server grid to place your Cruiser."; } } //See previous comments for "Battle Ship" placement if (currentShip.Equals("Cruiser")) { if (clickCount == 1) { lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[1].Position_Row = cell.RowIndex; lstShips[1].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { if (cell.RowIndex > lstShips[1].Position_Row || cell.RowIndex < lstShips[1].Position_Row) { if (lstShips[1].Position_Row + 3 <= 9) { dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row + 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row + 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row + 3].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row - 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row - 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row - 3].Style.BackColor = Color.Gray; } lstShips[1].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[1].Position_Col || cell.ColumnIndex < lstShips[1].Position_Col) { if (lstShips[1].Position_Col + 3 <= 10) { dataGridView2[lstShips[1].Position_Col + 1, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col + 2, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col + 3, lstShips[1].Position_Row].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[1].Position_Col - 1, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col - 2, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col - 3, lstShips[1].Position_Row].Style.BackColor = Color.Gray; } lstShips[1].Orientation = Ship.ShipOrientation.Horizontal; } currentShip = "Submarine"; clickCount = 0; s = new Ship(); s.Name = "Submarine"; s.ExtentUnits = 3; lstShips.Add(s); lStatus.Text = "Click a square on the Server grid to place your Submarine."; } } //See previous comments for "Battle Ship" placement if (currentShip.Equals("Submarine")) { if (clickCount == 1) { lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[2].Position_Row = cell.RowIndex; lstShips[2].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { if (cell.RowIndex > lstShips[2].Position_Row || cell.RowIndex < lstShips[2].Position_Row) { if (lstShips[2].Position_Row + 2 <= 9) { dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row + 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row + 2].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row - 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row - 2].Style.BackColor = Color.Gray; } lstShips[2].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[2].Position_Col || cell.ColumnIndex < lstShips[2].Position_Col) { if (lstShips[2].Position_Col + 2 <= 10) { dataGridView2[lstShips[2].Position_Col + 1, lstShips[2].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col + 2, lstShips[2].Position_Row].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[2].Position_Col - 1, lstShips[2].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col - 2, lstShips[2].Position_Row].Style.BackColor = Color.Gray; } lstShips[2].Orientation = Ship.ShipOrientation.Horizontal; } currentShip = "Destroyer"; clickCount = 0; s = new Ship(); s.Name = "Destroyer"; s.ExtentUnits = 2; lstShips.Add(s); lStatus.Text = "Click a square on the Server grid to place your Destroyer."; } } //See previous comments for "Battle Ship" placement if (currentShip.Equals("Destroyer")) { if (clickCount == 1) { lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[3].Position_Row = cell.RowIndex; lstShips[3].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { if (cell.RowIndex > lstShips[3].Position_Row || cell.RowIndex < lstShips[3].Position_Row) { if (lstShips[3].Position_Row + 1 <= 9) { dataGridView2[lstShips[3].Position_Col, lstShips[3].Position_Row + 1].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[3].Position_Col, lstShips[3].Position_Row - 1].Style.BackColor = Color.Gray; } lstShips[3].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[3].Position_Col || cell.ColumnIndex < lstShips[3].Position_Col) { if (lstShips[3].Position_Col + 1 <= 10) { dataGridView2[lstShips[3].Position_Col + 1, lstShips[3].Position_Row].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[3].Position_Col - 1, lstShips[3].Position_Row].Style.BackColor = Color.Gray; } lstShips[3].Orientation = Ship.ShipOrientation.Horizontal; } clickCount = 0; currentShip = ""; placingShips = false; lStatus.Text = "Click a blank square on the Server grid to finish placing your ships."; //once all ships are placed, the user only needs to click anywhere on the grid to finish the placement //then we start the send/receive threads Program.receiver.Start(); Program.sender.Start(); } } } else { //once we're no longer placing ships, we disable the grid used in the ship placement if (cell.Style.BackColor == Color.Gray) cell.Style.SelectionBackColor = Color.Gray; else cell.Style.SelectionBackColor = Color.Aqua; dataGridView2.Enabled = false; dataGridView1.Enabled = true; lInstruct.Text = "Click a square on the Client's grid to attack!"; } }
//This event occurs when the Server player clicks on the Server grid private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e) { //each time the Server player clicks, we increment the clickCount to determine which action to take clickCount++; DataGridViewCell cell; int rowIndex = e.RowIndex; int colIndex = e.ColumnIndex; //If the user clicks in an invalid cell, it's corrected if (e.ColumnIndex == 0) { colIndex = 1; } if (e.RowIndex < 0) { rowIndex = 0; } //We grab the cell that the user clicked cell = dataGridView2.Rows[rowIndex].Cells[colIndex]; //If the user is placing ships, we check to see which ship is currently being placed if (placingShips) { if (currentShip.Equals("Battleship")) { //If this is the first click for the current ship, we only designate an anchoring position for the ship if (clickCount == 1) { //and ask the user to then choose an orientation by clicking a different square lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[0].Position_Row = cell.RowIndex; lstShips[0].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { //once the user clicks a different square //We check to see if it's to the left/right of the previous click or if it's above/below if (cell.RowIndex > lstShips[0].Position_Row || cell.RowIndex < lstShips[0].Position_Row) { //If it's above/below the original click, we place the ship vertically based on its size if (lstShips[0].Position_Row + 4 <= 9) { //This if statement ensures that the chosen placement will allow the ship to fit on the grid //if it can fit, we place it from top to bottom dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 3].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row + 4].Style.BackColor = Color.Gray; } else { //but if that won't fit on the grid, we place the ship bottom to top instead dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 3].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col, lstShips[0].Position_Row - 4].Style.BackColor = Color.Gray; } lstShips[0].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[0].Position_Col || cell.ColumnIndex < lstShips[0].Position_Col) { //If the second chosen cell is left/right of the original click, we place the ship horizontally if (lstShips[0].Position_Col + 4 <= 10) { //and again ensure that this will fit on the grid from left to right dataGridView2[lstShips[0].Position_Col + 1, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col + 2, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col + 3, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col + 4, lstShips[0].Position_Row].Style.BackColor = Color.Gray; } else { //and if not, we place it right to left dataGridView2[lstShips[0].Position_Col - 1, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col - 2, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col - 3, lstShips[0].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[0].Position_Col - 4, lstShips[0].Position_Row].Style.BackColor = Color.Gray; } lstShips[0].Orientation = Ship.ShipOrientation.Horizontal; } //At this point, we change ships and repeat the same process until all ships have been placed currentShip = "Cruiser"; clickCount = 0; s = new Ship(); s.Name = "Cruiser"; s.ExtentUnits = 4; lstShips.Add(s); lStatus.Text = "Click a square on the Server grid to place your Cruiser."; } } //See previous comments for "Battle Ship" placement if (currentShip.Equals("Cruiser")) { if (clickCount == 1) { lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[1].Position_Row = cell.RowIndex; lstShips[1].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { if (cell.RowIndex > lstShips[1].Position_Row || cell.RowIndex < lstShips[1].Position_Row) { if (lstShips[1].Position_Row + 3 <= 9) { dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row + 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row + 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row + 3].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row - 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row - 2].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col, lstShips[1].Position_Row - 3].Style.BackColor = Color.Gray; } lstShips[1].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[1].Position_Col || cell.ColumnIndex < lstShips[1].Position_Col) { if (lstShips[1].Position_Col + 3 <= 10) { dataGridView2[lstShips[1].Position_Col + 1, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col + 2, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col + 3, lstShips[1].Position_Row].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[1].Position_Col - 1, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col - 2, lstShips[1].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[1].Position_Col - 3, lstShips[1].Position_Row].Style.BackColor = Color.Gray; } lstShips[1].Orientation = Ship.ShipOrientation.Horizontal; } currentShip = "Submarine"; clickCount = 0; s = new Ship(); s.Name = "Submarine"; s.ExtentUnits = 3; lstShips.Add(s); lStatus.Text = "Click a square on the Server grid to place your Submarine."; } } //See previous comments for "Battle Ship" placement if (currentShip.Equals("Submarine")) { if (clickCount == 1) { lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[2].Position_Row = cell.RowIndex; lstShips[2].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { if (cell.RowIndex > lstShips[2].Position_Row || cell.RowIndex < lstShips[2].Position_Row) { if (lstShips[2].Position_Row + 2 <= 9) { dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row + 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row + 2].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row - 1].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col, lstShips[2].Position_Row - 2].Style.BackColor = Color.Gray; } lstShips[2].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[2].Position_Col || cell.ColumnIndex < lstShips[2].Position_Col) { if (lstShips[2].Position_Col + 2 <= 10) { dataGridView2[lstShips[2].Position_Col + 1, lstShips[2].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col + 2, lstShips[2].Position_Row].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[2].Position_Col - 1, lstShips[2].Position_Row].Style.BackColor = Color.Gray; dataGridView2[lstShips[2].Position_Col - 2, lstShips[2].Position_Row].Style.BackColor = Color.Gray; } lstShips[2].Orientation = Ship.ShipOrientation.Horizontal; } currentShip = "Destroyer"; clickCount = 0; s = new Ship(); s.Name = "Destroyer"; s.ExtentUnits = 2; lstShips.Add(s); lStatus.Text = "Click a square on the Server grid to place your Destroyer."; } } //See previous comments for "Battle Ship" placement if (currentShip.Equals("Destroyer")) { if (clickCount == 1) { lStatus.Text = "Click a different square to choose the orientation."; cell.Style.BackColor = Color.Gray; lstShips[3].Position_Row = cell.RowIndex; lstShips[3].Position_Col = cell.ColumnIndex; } if (clickCount == 2) { if (cell.RowIndex > lstShips[3].Position_Row || cell.RowIndex < lstShips[3].Position_Row) { if (lstShips[3].Position_Row + 1 <= 9) { dataGridView2[lstShips[3].Position_Col, lstShips[3].Position_Row + 1].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[3].Position_Col, lstShips[3].Position_Row - 1].Style.BackColor = Color.Gray; } lstShips[3].Orientation = Ship.ShipOrientation.Vertical; } else if (cell.ColumnIndex > lstShips[3].Position_Col || cell.ColumnIndex < lstShips[3].Position_Col) { if (lstShips[3].Position_Col + 1 <= 10) { dataGridView2[lstShips[3].Position_Col + 1, lstShips[3].Position_Row].Style.BackColor = Color.Gray; } else { dataGridView2[lstShips[3].Position_Col - 1, lstShips[3].Position_Row].Style.BackColor = Color.Gray; } lstShips[3].Orientation = Ship.ShipOrientation.Horizontal; } clickCount = 0; currentShip = ""; placingShips = false; lStatus.Text = "Click a blank square on the Server grid to finish placing your ships."; //once all ships are placed, the user only needs to click anywhere on the grid to finish the placement //then we start the send/receive threads Program.receiver.Start(); Program.sender.Start(); } } } else { //once we're no longer placing ships, we disable the grid used in the ship placement if (cell.Style.BackColor == Color.Gray) { cell.Style.SelectionBackColor = Color.Gray; } else { cell.Style.SelectionBackColor = Color.Aqua; } dataGridView2.Enabled = false; dataGridView1.Enabled = true; lInstruct.Text = "Click a square on the Client's grid to attack!"; } }