public FieldForm(Game GameObject) { InitializeComponent(); this.GameObject = GameObject; MyField = new FieldViewer(10, new System.Drawing.Point(1, 1), 50); EnemyField = new FieldViewer(10, new System.Drawing.Point(756, 1), 50); EnemyField.SelectionEnabled = true; this.Controls.Add(MyField); for (int i = 0; i < GameObject.MyField.FieldSize; ++i) { for (int j = 0; j < GameObject.MyField.FieldSize; ++j) { MyField.updateCellState(GameObject.MyField.getCell(i, j).State, new Point(i, j)); } } this.Controls.Add(EnemyField); EnemyField.CellClick += new DataGridViewCellEventHandler(EnemyCellClick); Program.ConnectionManager.OnConnectionLost += new EventHandler <EventArgs>(OnConnectionLostHandler); Program.ConnectionManager.OnMessageReceive += new EventHandler <GameData>(GameObject.ReceiveMsg); GameObject.SendMessage += new EventHandler <GameData>(Program.ConnectionManager.SendMsg); GameObject.OnDataUpdate += new EventHandler <DataUpdateEventArguments>(UpdateData); GameObject.OnGameFinished += new EventHandler <bool>(OnGameFinished); GameObject.ChangeMove += new EventHandler(ChangeMove); updateEnemyLabels(); updatePlayerLabels(); updateMoveLabel(); PlayerSelect = new Point(-1, -1); MyField.Enabled = false; }
private void SafeCellStateUpdate(FieldViewer Viewer, CellState State, Point pos) { if (Viewer.InvokeRequired) { SafeCellUpdate UpdateState = new SafeCellUpdate(SafeCellStateUpdate); this.Invoke(UpdateState, Viewer, State, pos); } else { Viewer.updateCellState(State, pos); } }
private void CellClickHandler(object sender, DataGridViewCellEventArgs e) { if (!deleteMode) { if (ShipCounter > 0) //if there are still more decks of ship to map { /* * if last pos - undefined * check whether around cells have enougth space, if not ignore this call * otherwise, set last pos, and mark cell at the field, decrement counters */ int xst = e.ColumnIndex - 1; int yst = e.RowIndex - 1; if (xst < 0) { xst++; //if around cells is beyond the field increment the counter } if (yst < 0) { yst++; } if (LastPos.X == -1 && LastPos.Y == -1) { for (int x = xst; x <= e.ColumnIndex + 1 && x < field.FieldSize; ++x) { for (int y = yst; y <= e.RowIndex + 1 && y < field.FieldSize; ++y) { if (field.getCell(x, y).State == CellState.Occupied) { return; //if at least one cell around is specified then position of new ship is invalid } } } LastPos.X = e.ColumnIndex; LastPos.Y = e.RowIndex; CurShipId = e.ColumnIndex.ToString() + e.RowIndex.ToString(); field.setCell(e.ColumnIndex, e.RowIndex, new Field.Cell(CellState.Occupied, CurShipType, CurShipId)); FieldView.updateCellState(CellState.Occupied, new Point(e.ColumnIndex, e.RowIndex)); ShipCounter--; } else { /*if last pos is specified, * check does the orientation specified, * if not check does the new deck is nearby the previous one, then specify orientation */ bool DeckIsNear = false; if (CurShipOrientation == ShipOrientation.None) { for (int x = xst; x <= e.ColumnIndex + 1 && x < field.FieldSize; ++x) { for (int y = yst; y <= e.RowIndex + 1 && y < field.FieldSize; ++y) //iterating over the cells around current cell, { //all cells around must be free, excluding the previous deck if (field.getCell(x, y).State == CellState.Occupied) //if the cell is occupied { if (x == LastPos.X && y == LastPos.Y) //and that occupied cell - last pos { DeckIsNear = true; if ((e.ColumnIndex - 1 == x && e.RowIndex == y) || (e.ColumnIndex + 1 == x && e.RowIndex == y)) //determine the orientation { CurShipOrientation = ShipOrientation.Horizontal; continue; } if ((e.ColumnIndex == x && e.RowIndex - 1 == y) || (e.ColumnIndex == x && e.RowIndex + 1 == y)) { CurShipOrientation = ShipOrientation.Vertical; continue; } } return; } } } if (!DeckIsNear) { return; } LastPos.X = e.ColumnIndex; LastPos.Y = e.RowIndex; //set cell to the field and decrement counter field.setCell(e.ColumnIndex, e.RowIndex, new Field.Cell(CellState.Occupied, CurShipType, CurShipId)); FieldView.updateCellState(CellState.Occupied, new Point(e.ColumnIndex, e.RowIndex)); ShipCounter--; } else { //here check orientation & proximity, for (int x = xst; x <= e.ColumnIndex + 1 && x < field.FieldSize; ++x) { for (int y = yst; y <= e.RowIndex + 1 && y < field.FieldSize; ++y) //iterating over all cells around current one { if (field.getCell(x, y).State == CellState.Occupied) //if occupied cell encountered { if (field.getCell(x, y).Shipid == CurShipId) //and if occupied cell have the same id as the current ship { DeckIsNear = true; switch (CurShipOrientation) //check the orientation { case ShipOrientation.Vertical: if ((e.ColumnIndex == x && e.RowIndex - 1 == y) || (e.ColumnIndex == x && e.RowIndex + 1 == y)) { continue; } break; //check both side simuataneosly case ShipOrientation.Horizontal: if ((e.ColumnIndex - 1 == x && e.RowIndex == y) || (e.ColumnIndex + 1 == x && e.RowIndex == y)) { continue; } break; } } return; //if encounter unknown occupied cell } } } if (!DeckIsNear) { return; } LastPos.X = e.ColumnIndex; LastPos.Y = e.RowIndex; //set cell and decrement counter field.setCell(e.ColumnIndex, e.RowIndex, new Field.Cell(CellState.Occupied, CurShipType, CurShipId)); FieldView.updateCellState(CellState.Occupied, new Point(e.ColumnIndex, e.RowIndex)); ShipCounter--; } } } } else { ShipOrientation DeletingShipOrientation = ShipOrientation.None; //check does the selected cell is occipied if (field.getCell(e.ColumnIndex, e.RowIndex).State == CellState.Occupied) { ShipType sh_type = field.getCell(e.ColumnIndex, e.RowIndex).Type; CurCounters[(byte)sh_type]++; switch (field.getCell(e.ColumnIndex, e.RowIndex).Type) { case ShipType.FourDeck: FourLabel.Text = CurCounters[(byte)sh_type].ToString(); break; case ShipType.ThreeDeck: ThreeLabel.Text = CurCounters[(byte)sh_type].ToString(); break; case ShipType.TwoDeck: TwoLabel.Text = CurCounters[(byte)sh_type].ToString(); break; case ShipType.OneDeck: OneLabel.Text = CurCounters[(byte)sh_type].ToString(); break; } //scan ways //first vertical if ((e.RowIndex - 1 >= 0 && field.getCell(e.ColumnIndex, e.RowIndex - 1).State == CellState.Occupied) || (e.RowIndex + 1 < field.FieldSize && field.getCell(e.ColumnIndex, e.RowIndex + 1).State == CellState.Occupied)) { DeletingShipOrientation = ShipOrientation.Vertical; } if (DeletingShipOrientation != ShipOrientation.Vertical) { if ((e.ColumnIndex - 1 >= 0 && field.getCell(e.ColumnIndex - 1, e.RowIndex).State == CellState.Occupied) || (e.ColumnIndex + 1 < field.FieldSize && field.getCell(e.ColumnIndex + 1, e.RowIndex).State == CellState.Occupied)) { DeletingShipOrientation = ShipOrientation.Horizontal; } else { DeletingShipOrientation = ShipOrientation.None; } } if (DeletingShipOrientation == ShipOrientation.None) { DeleteShipFromOneDeck(new Point(e.ColumnIndex, e.RowIndex), DeletingShipOrientation); } else if (DeletingShipOrientation == ShipOrientation.Vertical) { DeleteShipFromOneDeck(new Point(e.ColumnIndex, e.RowIndex), DeletingShipOrientation); } else { DeleteShipFromOneDeck(new Point(e.ColumnIndex, e.RowIndex), DeletingShipOrientation); } } deleteMode = false; FieldView.Enabled = false; //restore another buttons availability Map1Btn.Enabled = true; Map2Btn.Enabled = true; Map3Btn.Enabled = true; Map4Btn.Enabled = true; } }