Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
project did not solve the error.I did minesweeper game but row and column gives the error.

link to my project

https://app.box.com/s/3go54y99ewfd3jdoa1um[^]

C#
//Box opening function function in the neighbor list
       //Kutu açma fonksiyonu içindeki komşu listesi fonksiyonu
       List<Box> ListOfNeighbours(int Row, int Column)
       {

           List<Box> TheList = new List<Box>();

           for (int r = -1; r <= 1; r++)
           {
               for (int c = -1; c <= 1; c++)
               {
                   if ((r == 0 && c == 0) || (Column + c < 0) || (Row + r < 0) || ((Column + c) >= RowCount) || (Row + r >= ColumnCount))
                   {
                       continue;
                   }

                   TheList.Add(ListOfBoxes[Row + r, Column + c]);

               }
           }

           return TheList;
       }

[EDIT - posting OP's code from solution which will disappear soon]
Consider all projects
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Cet.MineSweeperGame
{
    public partial class MineGame : Form
    {
        public MineGame()
        {
            InitializeComponent();
        }
        public int RowCount;
        public int ColumnCount;
        public int BombCount;
         //Firstclickswhenthe bombwill be used toreset the game.
        //İlk tıklamada bomba çıktığında oyunu yenilemek için kullanılacak.
        bool FirstClick = true;

      // All boxes will be kept in a list.
        Box[,] ListOfBoxes;

        //Geçen zamanı tutacak.
        int timeCount= 0;

        //Kalan bayrak sayısını tutacak.
        int flagCount;

        //Zaman sayacı
        private void timer_Tick(object sender, EventArgs e)
        {
            txtTime.Text = timeCount++.ToString();
        }

        //Yenileme düğmesinin olayı
        private void btnRenew_Click(object sender, EventArgs e)
        {
            if (numTotalBomb.Value < numRowCount.Value * numColumnCount.Value)
            {
                timer.Stop();
                FirstClick = true;
                ResetBoxBoard();
                GenerateGame();
            }
            else
            {
                MessageBox.Show("Total bomb should be less than " + (numRowCount.Value * numColumnCount.Value).ToString());
            }
        }
        //Refresh button cleaning function on the board
        //Yenileme düğmesindeki tahtayı temizleme fonksiyonu
        private void ResetBoxBoard()
        {
            BoxBoard.Enabled = true;
            BoxBoard.Controls.Clear();
        }
        //When the form is installed just install the game.
        //Form yüklendiğinde sadece oyunu kur.
        private void MineGame_Load(object sender, EventArgs e)
        {
            GenerateGame();
        }
        //Game setting function
        //Oyunu kurma fonksiyonu
        private void GenerateGame()
        {
            RowCount = Convert.ToInt32(numRowCount.Value);
            ColumnCount = Convert.ToInt32(numColumnCount.Value);
            BombCount = Convert.ToInt32(numTotalBomb.Value);

            timeCount = 0;
            txtTime.Text = "0";
            flagCount = Convert.ToInt32(numTotalBomb.Value);
            txtFlag.Text = flagCount.ToString();

            ListOfBoxes = new Box[RowCount, ColumnCount];

            BoxBoard.Width = RowCount * Box.BOX_WIDTH;
            this.Width = RowCount * Box.BOX_WIDTH + 13;
            BoxBoard.Height = RowCount * Box.BOX_HEIGHT;
            this.Height = BoxBoard.Height + BoxBoard.Top + 35;

            //Create boxes and knees.
            for (int r = 0; r < RowCount; r++)
            {
                for (int c = 0; c < ColumnCount; c++)
                {
                    Box b = new Box();
                    ListOfBoxes[r, c] = b;
                    b.Column = c;
                    b.Row = r;
                    b.Top = r * Box.BOX_HEIGHT;
                    b.Left = c * Box.BOX_WIDTH;

                    //Hem sağ hem sol tık için
                    b.MouseUp += new MouseEventHandler(b_MouseUp);
                    BoxBoard.Controls.Add(b);
                }
            }

            int currentBomb = 0;

            //Random put a bomb.
            Random rnd = new Random();
            do
            {
                int random = rnd.Next(RowCount * ColumnCount);
                int rndR = random / RowCount;
                int rndC = random % ColumnCount;

                if (!ListOfBoxes[rndR, rndC].isBomb)
                {
                    ListOfBoxes[rndR, rndC].isBomb = true;
                    currentBomb++;

                    foreach (Box neighbour in ListOfNeighbours(rndR, rndC)) { neighbour.NeighboursBombCount++; }

                }
            } while (currentBomb < BombCount);
        }
        //Games in the function setting button assigned to mouse events
        //Oyun kurma fonksiyonu içinde düğmelere atanan fare olayları
        void b_MouseUp(object sender, MouseEventArgs e)
        {
            //left click
            //Sol tıklama
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Box clickedBox = sender as Box;

                if (!clickedBox.isFlagged)
                {
                    if (clickedBox.isBomb)
                    {
                        if (FirstClick == false)
                        {
                            timer.Stop();
                            BoxBoard.Enabled = false;
                            ShowAllBombs();
                            //Bir de tıklanan bomba vurgulanacak. One bomb also clicked will be highlighted
                            clickedBox.BackColor = System.Drawing.Color.Red;
                        }
                        else
                        {
                            MessageBox.Show("Unlucky! Game will be renewed.");
                            ResetBoxBoard();
                            GenerateGame();
                        }
                    }

                    else
                    {
                        OpenBox(clickedBox);
                        if (timeCount == 0)
                        {   //The delayed onset of the next line counter have to take care of the problem.
                            //Bir sonraki satır sayacın geç başlaması sorununu halletmek için var.
                            timeCount++;
                            timer.Start();
                            FirstClick = false;
                        }
                        //The game is finished or not checking a box is opened.
                        //Oyunun bitip bitmediğini bir kutu açıldığında kontrol et.
                        if (isFinished())
                        {
                            timer.Stop();
                            BoxBoard.Enabled = false;
                            MessageBox.Show("Finished!");
                        }

                    }

                }
            }

            //Sağ tıklama right click
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Box b = (Box)sender;
                if (!b.isFlagged)
                {
                    b.isFlagged = true;
                    b.BackgroundImage = Image.FromFile(Application.StartupPath + "\\Flag.png");
                    b.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                    flagCount--;
                    txtFlag.Text = flagCount.ToString();
                }
                else
                {
                    b.isFlagged = false;
                    b.BackgroundImage = null;
                    flagCount++;
                    txtFlag.Text = flagCount.ToString();
                }
                //Games that run down the control flag of sheep.
                //Oyunun bitip bitmediğini bayrak konunca da kontrol et.  
                if (isFinished())
                {
                    timer.Stop();
                    BoxBoard.Enabled = false;
                    MessageBox.Show("Bitti.");
                }

            }
        }
        //Mouse events inside the box opening function
        //Fare olayları içindeki kutu açma fonksiyonu
        private void OpenBox(Box clickedBox)
        {
            if (clickedBox.NeighboursBombCount > 0)
            {   
                clickedBox.Enabled = false;
                clickedBox.Text = clickedBox.NeighboursBombCount.ToString();
                clickedBox.isOpen = true;
            }
            else
            {
                clickedBox.Visible=false;
                clickedBox.isOpen = true;

                foreach (Box b in ListOfNeighbours(clickedBox.Row, clickedBox.Column))
                {
                    if (!b.isOpen && !b.isFlagged) { OpenBox(b); }   
                }
            }
           
        }
        //Box opening function function in the neighbor list
        //Kutu açma fonksiyonu içindeki komşu listesi fonksiyonu
        List<box> ListOfNeighbours(int Row, int Column)
        {

            List<box> TheList = new List<box>();
            
            for (int r = -1; r <= 1; r++)
            {
                for (int c = -1; c <= 1; c++)
                {
                    if ((r == 0 && c == 0) || (Column + c < 0) || (Row + r < 0) || ((Column + c) >= RowCount) || (Row + r >= ColumnCount))
                    {
                        continue; 
                    }

                    TheList.Add(ListOfBoxes[Row + r, Column + c]);

                }
            }

            return TheList;
        }
        //Clicking bomb bombs and false flag no flag indicating function
        //Bomba tıklanınca bayraksız bombaları ve yanlış bayrakları gösteren fonksiyon
        private void ShowAllBombs()
        {
            for (int r = 0; r < RowCount; r++)
            {
                for (int c = 0; c < ColumnCount; c++)
                {   // Original Showing bombs in the game just no flag.
                    //Orijinal oyunda sadece bayraksız bombalar gösteriliyor.
                    if (ListOfBoxes[r, c].isBomb && !ListOfBoxes[r, c].isFlagged)
                    {
                        ListOfBoxes[r, c].BackgroundImage = Image.FromFile(Application.StartupPath + "\\Bomb.png");
                        ListOfBoxes[r, c].BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                    }
                    //Bomb put in place non-faulty flags
                    //Bomba olmayan yere konan hatalı bayraklar
                    if (!ListOfBoxes[r, c].isBomb && ListOfBoxes[r, c].isFlagged)
                    {
                        ListOfBoxes[r, c].BackgroundImage = Image.FromFile(Application.StartupPath + "\\WrongFlag.png");
                    }

                }
            }
        }
        //Function which checks whether the game is finished or not
        //Oyunun bitip bitmediğini kontrol eden fonksiyon
        private bool isFinished()
        {
            bool result = true;
            
            for (int r = 0; r < RowCount; r++)
            {
                for (int c = 0; c < ColumnCount; c++)
                {
                    if (ListOfBoxes[r, c].isBomb && !ListOfBoxes[r, c].isFlagged)  
                    {
                        result = false;
                    }

                    if (!ListOfBoxes[r, c].isBomb && !ListOfBoxes[r, c].isOpen)
                    {
                        result = false;
                    }
                }
            }

            return result;
        }



    }
}
</box></box></box>
Posted
Updated 9-Jan-14 7:04am
v2
Comments
bowlturner 9-Jan-14 12:19pm    
Sorry what are you trying to do? The if statement raises red flags, but I don't know what you actually wanted it to do, so can't suggest on how to fix it.

shouldn't RowCount and ColumnCount be reversed?

C#
if ((r == 0 && c == 0) || (Column + c < 0) || (Row + r < 0) || ((Column + c) >= ColumnCount) || (Row + r >= RowCount))
                    {
 
Share this answer
 
row= 7
column=15
bomb=10
given the values ​​gives an error
does not apply to every value
 
Share this answer
 
Comments
bowlturner 9-Jan-14 14:35pm    
Did you take a look at my answer? you're If statement appears to be wrong. try using my If statement in place of yours.
Hasan Yildirim 9-Jan-14 15:20pm    
your answer is getting the same errors that are similar
bowlturner 9-Jan-14 15:26pm    
Mind posting the actual error? because my solution did fix a bug. But since I don't know what is broke, I can't help fix it.
Hasan Yildirim 9-Jan-14 15:29pm    
Think you're good.
but there are still mistakes
Hasan Yildirim 9-Jan-14 15:31pm    
row= 7
column=15
bomb=10
enter these values
then you will see your error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900