コード例 #1
0
ファイル: FleetForm.cs プロジェクト: tlevacic/BattleShip
        private void DrawPanel(GridButton[,] panel, int left)
        {
            for (int i = 0; i < numOfRows; i++)
            {
                for (int j = 0; j < numOfCols; j++)
                {
                    panel[i, j]           = new GridButton();
                    panel[i, j].i         = i;
                    panel[i, j].j         = j;
                    panel[i, j].FlatStyle = FlatStyle.Flat;
                    panel[i, j].BackColor = Color.FromArgb(232, 232, 232);
                    panel[i, j].Location  = new System.Drawing.Point(left + i * 40, 120 + j * 40);
                    panel[i, j].Size      = new System.Drawing.Size(40, 40);
                    panel[i, j].TabStop   = false;
                    panel[i, j].Click    += ClickSquareEvent;
                    this.Controls.Add(panel[i, j]);
                }
            }
            char letter = 'A';

            for (int i = 0; i < numOfRows + 1; i++)
            {
                for (int j = 0; j < numOfCols + 1; j++)
                {
                    gridMarks[i, j] = new GridButton();
                    if (j != 0)
                    {
                        gridMarks[i, j].Text = j.ToString();
                    }
                    if (i != 0)
                    {
                        gridMarks[i, j].Text = letter.ToString();
                    }
                    gridMarks[i, j].BackColor = Color.FromArgb(232, 232, 232);
                    gridMarks[i, j].Location  = new System.Drawing.Point(left + i * 40 - 40, 120 + j * 40 - 40);
                    gridMarks[i, j].Size      = new System.Drawing.Size(40, 40);
                    gridMarks[i, j].TabStop   = false;
                    gridMarks[i, j].FlatStyle = FlatStyle.Flat;
                    gridMarks[i, j].Enabled   = false;
                    this.Controls.Add(gridMarks[i, j]);
                }
                if (i != 0)
                {
                    letter++;
                }
            }
        }
コード例 #2
0
ファイル: FleetForm.cs プロジェクト: tlevacic/BattleShip
        private void ClickSquareEvent(object sender, EventArgs e)
        {
            GridButton button        = sender as GridButton;
            Square     squareClicked = new Square(button.i, button.j);
            HitResult  result        = pcFleet.Hit(squareClicked);

            switch (result)
            {
            case HitResult.Hit:
            {
                button.BackColor = Color.FromArgb(255, 255, 0);
                break;
            }

            case HitResult.Missed:
            {
                button.BackColor           = Color.Red;
                whoPlays                   = Turn.PC;
                playerTurnButton.BackColor = Color.Red;
                pcTurnButton.BackColor     = Color.LawnGreen;
                break;
            }

            case HitResult.Sunken:
            {
                int i = 0;
                foreach (var sunkenSquare in pcFleet.Ships.Where(s => s.Squares.Contains(squareClicked)).SelectMany(s => s.Squares))
                {
                    pcPanel[sunkenSquare.Row, sunkenSquare.Col].BackColor = Color.DarkMagenta;
                    i++;
                }
                MarkLabelsOfPc(i);
                break;
            }
            }
            Play();
        }