2D数组3x9矩阵获取当前索引接下来两个元素的索引并检查是否等于0

发布于 2025-01-14 05:09:02 字数 4329 浏览 1 评论 0原文

我是一个编程新手,我一直在关注一些与 housie 票生成器相关的数组教程。我遇到的问题是,我必须检查每一行,并且 3x9 矩阵的每一行不应该有更多的两个空单元格,或者不能有两个以上相邻的单元格填充。我在数组上放置随机数并尝试验证规则,但是程序崩溃了。有人可以给我一个主意吗?

这就是我尝试过的。

for(int columnIndex=0;columnIndex<=6;columnIndex++)
          {
              if(game[i,columnIndex+2]!=0)
              {
                  return -1;
              }
          }

这是整个代码

using System;

namespace HelloWorld
{
  class Program
  {
      public static void Main (String[] args)
        {
            for(int times=0;times<2;times++)
            {
                startGame();
                Console.WriteLine("******************************************************************");
            }
            
        }

         private static void startGame()
        {
            int[,] game = new int[3, 9];
            int occupancyLimit = 15;

            while (occupancyLimit > 0)
            {
                int i = getRandomNumber(3);
                int j = getRandomNumber(9);
                //Console.Write(i);
                //Console.Write(j);
                // Console.Write(game[i,j]+" ");
                int data = validateAndReturnNumber(i, j, game);
            
                if (data>0)
                {
                    game[i, j] = data;
                    occupancyLimit--;
                    //Console.WriteLine(game[i,j]);
                }

            }
         for (int i = 0; i < game.GetLength(0); i++)
        {
            for (int j = 0; j < game.GetLength(1); j++)
            {
                Console.Write(game[i,j] + "\t");
            }
            Console.WriteLine();
        }
           
        } 

     
        private static  int validateAndReturnNumber(int i, int j, int[,] game)
        {
            //do not override existing elements in array
            if (game[i,j] != 0)
            {
                return -1;
            }
            //column cannot have more then two elements
            int columncounter = 0;
            for(int c=0;c<3;c++)
            {
                if(game[c,j]!=0)
                {
                    columncounter++;
                }
            }
            if(columncounter>=2)
            {
                return -1;
            }
/*
            //rows cannot have more then two cells filled in and 
            //rows cannot have more then two empty cells
          for(int columnIndex=0;columnIndex<=6;columnIndex++)
          {
              if(game[i,columnIndex+2]!=0)
              {
                  return -1;
              }
          }
     */      
            // rows cannot have more then 5 elements
            int rowcounter = 0;
            for(int r=0;r<9;r++)
            {     
                if(game[i,r]!=0)
                {       
                    rowcounter++;
                }
                
            }
            //Applying, rows cannot have more then 5 elements
            if(rowcounter>=5)
            {
                return -1;
            }

            //return getRandomNumberForColumn(j);
            int data = 0;
            Boolean isValueSet = false;
            do
            {
                data = getRandomNumberForColumn(j);
                isValueSet = isValueExistsInCol(game, i, j, data);
            } while (isValueSet);
            return data;
        }

        private static bool isValueExistsInCol(int[,] game, int i, int j, int data)
        {
            Boolean status = false;
            for(int k=0;k<3;k++)
            {
                if(game[k,j]==data)
                {
                    status = true;
                    break;
                }
            }
            return status;
        }

        private static int getRandomNumberForColumn(int high)
        {
          if(high==0)
            {
                high = 10;

            }
            else
            {
                high=(high + 1) * 10;
            }
            int low = high - 9;
            Random random = new Random();
            return random.Next(high-low)+low;
        }

        private static int getRandomNumber(int max)
        {
            Random random = new Random();
            int num=random.Next(max);
            return (num);
            
        }
    }
}

I am a total newbie to programming and i have been following some tutorials on array related to housie ticket generator.The point where I am stuck is that, I have to check each rows and each rows of the 3x9 matrix should not have more the two empty cells or it cannot have more then two cells filled next to each other.I am putting random numbers on the arrays and trying to validate the rules but,the program crashes. Can someone please give me an idea.?

This is what i've tried.

for(int columnIndex=0;columnIndex<=6;columnIndex++)
          {
              if(game[i,columnIndex+2]!=0)
              {
                  return -1;
              }
          }

And this is the whole code

using System;

namespace HelloWorld
{
  class Program
  {
      public static void Main (String[] args)
        {
            for(int times=0;times<2;times++)
            {
                startGame();
                Console.WriteLine("******************************************************************");
            }
            
        }

         private static void startGame()
        {
            int[,] game = new int[3, 9];
            int occupancyLimit = 15;

            while (occupancyLimit > 0)
            {
                int i = getRandomNumber(3);
                int j = getRandomNumber(9);
                //Console.Write(i);
                //Console.Write(j);
                // Console.Write(game[i,j]+" ");
                int data = validateAndReturnNumber(i, j, game);
            
                if (data>0)
                {
                    game[i, j] = data;
                    occupancyLimit--;
                    //Console.WriteLine(game[i,j]);
                }

            }
         for (int i = 0; i < game.GetLength(0); i++)
        {
            for (int j = 0; j < game.GetLength(1); j++)
            {
                Console.Write(game[i,j] + "\t");
            }
            Console.WriteLine();
        }
           
        } 

     
        private static  int validateAndReturnNumber(int i, int j, int[,] game)
        {
            //do not override existing elements in array
            if (game[i,j] != 0)
            {
                return -1;
            }
            //column cannot have more then two elements
            int columncounter = 0;
            for(int c=0;c<3;c++)
            {
                if(game[c,j]!=0)
                {
                    columncounter++;
                }
            }
            if(columncounter>=2)
            {
                return -1;
            }
/*
            //rows cannot have more then two cells filled in and 
            //rows cannot have more then two empty cells
          for(int columnIndex=0;columnIndex<=6;columnIndex++)
          {
              if(game[i,columnIndex+2]!=0)
              {
                  return -1;
              }
          }
     */      
            // rows cannot have more then 5 elements
            int rowcounter = 0;
            for(int r=0;r<9;r++)
            {     
                if(game[i,r]!=0)
                {       
                    rowcounter++;
                }
                
            }
            //Applying, rows cannot have more then 5 elements
            if(rowcounter>=5)
            {
                return -1;
            }

            //return getRandomNumberForColumn(j);
            int data = 0;
            Boolean isValueSet = false;
            do
            {
                data = getRandomNumberForColumn(j);
                isValueSet = isValueExistsInCol(game, i, j, data);
            } while (isValueSet);
            return data;
        }

        private static bool isValueExistsInCol(int[,] game, int i, int j, int data)
        {
            Boolean status = false;
            for(int k=0;k<3;k++)
            {
                if(game[k,j]==data)
                {
                    status = true;
                    break;
                }
            }
            return status;
        }

        private static int getRandomNumberForColumn(int high)
        {
          if(high==0)
            {
                high = 10;

            }
            else
            {
                high=(high + 1) * 10;
            }
            int low = high - 9;
            Random random = new Random();
            return random.Next(high-low)+low;
        }

        private static int getRandomNumber(int max)
        {
            Random random = new Random();
            int num=random.Next(max);
            return (num);
            
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2025-01-21 05:09:02

为什么你的 for 循环不起作用:

for (int columnIndex = 0; columnIndex <= 6; columnIndex++)
{
  if (game[i, columnIndex + 2] != 0)
  {
    return -1;
  }
}

这个循环没有考虑 j 。它正在测试之前添加的数字,一旦之前的数字未通过此测试,它将无限期地失败。这会产生无限循环。如果将数字放置在超过 1 的任意位置,此循环也会失败,而需要填充 5 个位置才能成功。这在数学上是不可能的。

这:
“不应有更多的两个空单元格,或者不能有超过两个单元格”在数学上也是不可能的。一行 9 的同时不能有少于 2 个满的和少于 2 个空的。

我认为您连续连续 2 个空或满。对于连续两个空的测试是无法实现的,因为它一开始是空的,并且您在填充它之前正在测试它。幸运的是,这是一个冗余测试,基于所有其他测试用例,它始终是正确的。

连续装满的数量不能超过 2 个,但也可能导致不可能的情况。我添加了一项检查,如果在 1000 次猜测后仍未找到解决方案,则会重置场景。

using System;

namespace HelloWorld
{
    class Program
    {
        public static void Main(String[] args)
        {
            for (int times = 0; times < 2; times++)
            {
                startGame();
                // Console.WriteLine("******************************************************************");
            }

        }

        private static void startGame()
        {
            int iCount = 0;
            int[,] game = new int[3, 9];
            int occupancyLimit = 15;

            while (occupancyLimit > 0)
            {
                int i = getRandomNumber(3);
                int j = getRandomNumber(9);
                //Console.Write(i);
                //Console.Write(j);
                // Console.Write(game[i,j]+" ");
                int data = validateAndReturnNumber(i, j, game);

                if (data > 0)
                {
                    game[i, j] = data;
                    occupancyLimit--;
                    //Console.WriteLine(game[i,j]);
                }
                else
                {
                    iCount++;
                    //Console.WriteLine(iCount);
                    //printGame(game);

                    // If X many fails, retry
                    if(iCount > 1000)
                    {
                        iCount = 0;
                        game = new int[3, 9];
                        occupancyLimit = 15;
                    }
                }

                // If you want to check for zeros you would need to do it here. And use while(true) above
                /*
                if( //Check for zeros)
                {
                    break; // Ends While loop
                }
                else
                {
                    // Reset and try again
                    iCount = 0;
                    game = new int[3, 9];
                    occupancyLimit = 15;
                }
                */
            }

            printGame(game);
        }


        private static void printGame(int[,] game)
        {
            for (int i = 0; i < game.GetLength(0); i++)
            {
                for (int j = 0; j < game.GetLength(1); j++)
                {
                    Console.Write(game[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("******************************************************************");
        }


        private static int validateAndReturnNumber(int i, int j, int[,] game)
        {
            //do not override existing elements in array
            if (game[i, j] != 0)
            {
                return -1;
            }
            //column cannot have more then two elements
            int columncounter = 0;
            for (int c = 0; c < 3; c++)
            {
                if (game[c, j] != 0)
                {
                    columncounter++;
                }
            }
            if (columncounter >= 2)
            {
                return -1;
            }

            if(
                (j != 0 && j != 1 && game[i, j - 2] != 0 && game[i, j - 1] != 0) || // 12X 
                (j != 0 && j != 8 && game[i, j - 1] != 0 && game[i, j + 1] != 0) || // 1X3
                (j != 7 && j != 8 && game[i, j + 1] != 0 && game[i, j + 2] != 0)    // X23
               )
            {
                return -1;
            }

            //for (int columnIndex = 0; columnIndex <= 6; columnIndex++)
            //{
            //    if (game[i, columnIndex + 2] != 0)
            //    {
            //        return -1;
            //    }
            //}

            // rows cannot have more then 5 elements
            int rowcounter = 0;
            for (int r = 0; r < 9; r++)
            {
                if (game[i, r] != 0)
                {
                    rowcounter++;
                }

            }
            //Applying, rows cannot have more then 5 elements
            if (rowcounter >= 5)
            {
                return -1;
            }

            //return getRandomNumberForColumn(j);
            int data = 0;
            Boolean isValueSet = false;
            do
            {
                data = getRandomNumberForColumn(j);
                isValueSet = isValueExistsInCol(game, i, j, data);
            } while (isValueSet);
            return data;
        }

        private static bool isValueExistsInCol(int[,] game, int i, int j, int data)
        {
            Boolean status = false;
            for (int k = 0; k < 3; k++)
            {
                if (game[k, j] == data)
                {
                    status = true;
                    break;
                }
            }
            return status;
        }

        private static int getRandomNumberForColumn(int high)
        {
            if (high == 0)
            {
                high = 10;

            }
            else
            {
                high = (high + 1) * 10;
            }
            int low = high - 9;
            Random random = new Random();
            return random.Next(high - low) + low;
        }

        private static int getRandomNumber(int max)
        {
            Random random = new Random();
            int num = random.Next(max);
            return (num);

        }
    }
}

干杯!

Why your for loop does not work:

for (int columnIndex = 0; columnIndex <= 6; columnIndex++)
{
  if (game[i, columnIndex + 2] != 0)
  {
    return -1;
  }
}

This loop does not take j into account. It is testing for previous numbers added, as soon as one previous number fails this test, it will fail indefinitely. This creates an infinite loop. This loop also fails if a number is placed in any position past 1, while it needs to fill 5 positions to succeed. This is mathematically impossible.

This:
'should not have more the two empty cells or it cannot have more then two cells' is also mathematically impossible. a row of 9 cannot have less than 2 full and less than 2 empty at the same time.

I think you are after 2 empty or full consecutively in a row. For that testing for two empty in a row cannot be achieved as it starts empty, and you are testing it before you fill it. Fortunately this is a redundant test that will always be true based on all of the other test cases.

No more than 2 full in a row is possible, but can also lead to impossible scenarios. I have added a check that resets the scenario if it has not found the solution after 1000 guesses.

using System;

namespace HelloWorld
{
    class Program
    {
        public static void Main(String[] args)
        {
            for (int times = 0; times < 2; times++)
            {
                startGame();
                // Console.WriteLine("******************************************************************");
            }

        }

        private static void startGame()
        {
            int iCount = 0;
            int[,] game = new int[3, 9];
            int occupancyLimit = 15;

            while (occupancyLimit > 0)
            {
                int i = getRandomNumber(3);
                int j = getRandomNumber(9);
                //Console.Write(i);
                //Console.Write(j);
                // Console.Write(game[i,j]+" ");
                int data = validateAndReturnNumber(i, j, game);

                if (data > 0)
                {
                    game[i, j] = data;
                    occupancyLimit--;
                    //Console.WriteLine(game[i,j]);
                }
                else
                {
                    iCount++;
                    //Console.WriteLine(iCount);
                    //printGame(game);

                    // If X many fails, retry
                    if(iCount > 1000)
                    {
                        iCount = 0;
                        game = new int[3, 9];
                        occupancyLimit = 15;
                    }
                }

                // If you want to check for zeros you would need to do it here. And use while(true) above
                /*
                if( //Check for zeros)
                {
                    break; // Ends While loop
                }
                else
                {
                    // Reset and try again
                    iCount = 0;
                    game = new int[3, 9];
                    occupancyLimit = 15;
                }
                */
            }

            printGame(game);
        }


        private static void printGame(int[,] game)
        {
            for (int i = 0; i < game.GetLength(0); i++)
            {
                for (int j = 0; j < game.GetLength(1); j++)
                {
                    Console.Write(game[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("******************************************************************");
        }


        private static int validateAndReturnNumber(int i, int j, int[,] game)
        {
            //do not override existing elements in array
            if (game[i, j] != 0)
            {
                return -1;
            }
            //column cannot have more then two elements
            int columncounter = 0;
            for (int c = 0; c < 3; c++)
            {
                if (game[c, j] != 0)
                {
                    columncounter++;
                }
            }
            if (columncounter >= 2)
            {
                return -1;
            }

            if(
                (j != 0 && j != 1 && game[i, j - 2] != 0 && game[i, j - 1] != 0) || // 12X 
                (j != 0 && j != 8 && game[i, j - 1] != 0 && game[i, j + 1] != 0) || // 1X3
                (j != 7 && j != 8 && game[i, j + 1] != 0 && game[i, j + 2] != 0)    // X23
               )
            {
                return -1;
            }

            //for (int columnIndex = 0; columnIndex <= 6; columnIndex++)
            //{
            //    if (game[i, columnIndex + 2] != 0)
            //    {
            //        return -1;
            //    }
            //}

            // rows cannot have more then 5 elements
            int rowcounter = 0;
            for (int r = 0; r < 9; r++)
            {
                if (game[i, r] != 0)
                {
                    rowcounter++;
                }

            }
            //Applying, rows cannot have more then 5 elements
            if (rowcounter >= 5)
            {
                return -1;
            }

            //return getRandomNumberForColumn(j);
            int data = 0;
            Boolean isValueSet = false;
            do
            {
                data = getRandomNumberForColumn(j);
                isValueSet = isValueExistsInCol(game, i, j, data);
            } while (isValueSet);
            return data;
        }

        private static bool isValueExistsInCol(int[,] game, int i, int j, int data)
        {
            Boolean status = false;
            for (int k = 0; k < 3; k++)
            {
                if (game[k, j] == data)
                {
                    status = true;
                    break;
                }
            }
            return status;
        }

        private static int getRandomNumberForColumn(int high)
        {
            if (high == 0)
            {
                high = 10;

            }
            else
            {
                high = (high + 1) * 10;
            }
            int low = high - 9;
            Random random = new Random();
            return random.Next(high - low) + low;
        }

        private static int getRandomNumber(int max)
        {
            Random random = new Random();
            int num = random.Next(max);
            return (num);

        }
    }
}

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文