Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wolfram mathematica “随机”;路径“;生成代码_Wolfram Mathematica - Fatal编程技术网

Wolfram mathematica “随机”;路径“;生成代码

Wolfram mathematica “随机”;路径“;生成代码,wolfram-mathematica,Wolfram Mathematica,我目前正在为我正在重新创建的游戏生成一个随机路径。它以企鹅俱乐部的“薄冰”游戏为基础,在游戏中你扮演一个火角色,穿过一条冰路,到达出口,沿途融化尽可能多的冰块。(链接到play:) 这个游戏有预设的棋盘。我正在制作的游戏将随机生成棋盘。我在这方面遇到了很多问题 我目前正在用Mathematica编写代码,但如果有任何输入,我将不胜感激 到目前为止,我一直在正方形网格中创建“随机行走”。网格是由{}、{0}、{1}、{2}、{3}组成的二维列表。行走可以向上、向下、向左或向右移动,从不斜行,也从不

我目前正在为我正在重新创建的游戏生成一个随机路径。它以企鹅俱乐部的“薄冰”游戏为基础,在游戏中你扮演一个火角色,穿过一条冰路,到达出口,沿途融化尽可能多的冰块。(链接到play:) 这个游戏有预设的棋盘。我正在制作的游戏将随机生成棋盘。我在这方面遇到了很多问题

我目前正在用Mathematica编写代码,但如果有任何输入,我将不胜感激

到目前为止,我一直在正方形网格中创建“随机行走”。网格是由{}、{0}、{1}、{2}、{3}组成的二维列表。行走可以向上、向下、向左或向右移动,从不斜行,也从不停留在同一个地方。它也不能自己走回去。走在它要去的位置用{0}代替{},不能走到任何当前有0、1、2或3的位置。2点开始。我们的目标是让它最终停在3,但我想我可能会取消3的值,直到行走完成。它可以在路径的另一段附近移动,其间没有墙。此行走将继续,直到它自身陷入陷阱,或者{0}的数量等于尺寸减去应放置的障碍物的数量。调用函数时输入障碍物的数量。但是,我不希望函数陷井,而是用{0}填充电路板,直到{0}的数量等于维度减去应该放置的障碍物的数量。生成路径后,所有障碍物将放置在任何空白点,并用{1}替换

以下是我目前的代码:

 gameboard2[dim_, obs_] :=
 (*0 = blank, 1= obstacles, 2 = start, 3 = exit, 4 = water*)
 Module[{i, j, board = {}, boardPts = {}, x = 1, y = 1, endx = 1, 
   endy = 1, prevx = 1, prevy = 1, possibleValues = {}, pt = {}, 
   blankspc = dim^2 - 2 - obs},
  board = Table[{}, {i, 1, dim + 2}, {j, 1, dim + 2}];


  (*Generation of border*)
  Table[board[[1, i]] = {1}, {i, 1, dim + 2}];
  Table[board[[dim + 2, i]] = {1}, {i, 1, dim + 2}];
  For[i = 1, i <= Length[board], i++,
   board[[i, 1]] = {1};
   board[[i, dim + 2]] = {1};
   ];

  (*Random start point placement*)
  While[Count[board, {2}, 2] != 1,
   x = RandomChoice[Table[i, {i, 1, dim + 2}]];
   y = RandomChoice[Table[i, {i, 1, dim + 2}]];
   If[board[[x, y]] != {1}, board[[x, y]] = {2}];
   ];


  (*Random exit point placement*)
  While[Count[board, {3}, 2] != 1,
   endx = RandomChoice[Table[i, {i, 1, dim + 2}]];
   endy = RandomChoice[Table[i, {i, 1, dim + 2}]];
   If[board[[endx, endy]] != {1}, board[[endx, endy]] = {3}];
   ];




(*Random path generation*)
  (*x needs to be between 2 and dim+1 and y needs to be between 2 and dim+1*)
  For[i = 1, i <= blankspc, i++,
   prevx = x; prevy = y;
   possibleValues = {};

(*Testing position of x and y, making sure they are not on the side or the corner of the board*)
   If[x == 2,
    If[y == 2, (*bottom left*) possibleValues = {{x, y + 1}, {x + 1, y}},
     If[y == dim + 1, (*top left*) 
      possibleValues = {{x, y - 1}, {x + 1, y}},
      (*left side*) possibleValues = {{x, y + 1}, {x, y - 1}, {x + 1, y}}]],
    If[x == dim + 1,
     If[y == 2, (*bottom right*) possibleValues = {{x, y + 1}, {x - 1, y}},
      If[y == dim + 1, (*top right*) 
       possibleValues = {{x, y - 1}, {x - 1, y}},
       (*right side*)
       possibleValues = {{x, y + 1}, {x, y - 1}, {x - 1, y}}]],
     If[y == 2, (*bottom*) 
      possibleValues = {{x, y - 1}, {x - 1, y}, {x + 1, y}},
      If[y == dim + 1,(*top*) 
       possibleValues = {{x, y + 1}, {x - 1, y}, {x + 1, 
          y}},(*Not on any side or corner*)
       If[x > 2 && x < dim + 1 && y > 2 && y < dim + 1, 
        possibleValues = {{x, y + 1}, {x, y - 1}, {x + 1, y}, {x - 1, 
           y}}, possibleValues = {{x, y}}]
       ]
      ]
     ]
    ];

(*Ensure every position in possibleValues is equal to {} on the board*)
   For[j = 1, j <= Length[possibleValues], j++,

    If[board[[possibleValues[[j, 1]], possibleValues[[j, 2]]]] != {},
      possibleValues[[j]] = {}];
    ];
   possibleValues = 
    Delete[possibleValues, Position[possibleValues, {}]];


(*Random choosing of a point to move to*)
   pt = RandomChoice[possibleValues];
   x = pt[[1]];
   y = pt[[2]];
   If[board[[x, y]] == {}, board[[x, y]] = {0}];

   ];

(*Prints amount of {} and the length of the path to see if it became the right length*)
  Print[blankspc];
  Print[Count[board, {0}, 2]];

  Return[TableForm[board]]
  ]
我在互联网上到处寻找各种路径生成技巧和窍门,但都不是我想要的。大多数情况下,路径会自行返回,或沿对角线移动,或生成迷宫。这些都行不通

有人能帮我找出正确的逻辑吗?如果我不清楚,请提问!:)


谢谢

只使用数字(而不是长度为1的列表)并使用一些数字来指定一个空白点会更干净。另外,您可以让我们使用的两个函数是
ArrayPad
Position
。你的一些while循环可以是
RandomChoice[Position[board,p_/;p!={1}]
例如,既然你知道了制作边框的诀窍,为什么要检查所有繁琐的边呢?您有大约三十行代码,可以简单地执行如下操作:
possiblevalues=Select[{{x,y+1},{x,y-1},{x+1,y},{x-1,y},board[[Sequence@@@#]!={1}&]
`@agentp递归函数对我来说相当混乱。我仍在努力掌握这个概念。我刚刚开始学习如何编码。上学期我参加了计算机科学1课程,学习了如何用Java编写代码,现在我参加了一个关于Mathematica的课程。非常感谢你的帮助。我真的不知道为什么我决定不这样做o做长度1列表,但后来我把它改成了数字。谢谢你指出这一点。我将用你给我的函数进行实验。
In[30]:= gameboard2[10, 30] 
(*Note how many errors there are. This occurs when the path gets trapped and cannot move to any squares around it*)

During evaluation of In[30]:= RandomChoice::lrwl: The items for choice {} should be a list or a rule weights -> choices. >>

During evaluation of In[30]:= Part::partw: Part 2 of RandomChoice[{}] does not exist. >>

During evaluation of In[30]:= Part::pkspec1: The expression RandomChoice[{}][[2]] cannot be used as a part specification. >>

During evaluation of In[30]:= RandomChoice::argt: RandomChoice called with 0 arguments; 1 or 2 arguments are expected. >>

During evaluation of In[30]:= Part::partw: Part 1 of RandomChoice[] does not exist. >>

During evaluation of In[30]:= Part::partw: Part 2 of RandomChoice[] does not exist. >>

During evaluation of In[30]:= General::stop: Further output of Part::partw will be suppressed during this calculation. >>

During evaluation of In[30]:= Part::pkspec1: The expression RandomChoice[][[1]] cannot be used as a part specification. >>

During evaluation of In[30]:= RandomChoice::argt: RandomChoice called with 0 arguments; 1 or 2 arguments are expected. >>

During evaluation of In[30]:= Part::pkspec1: The expression RandomChoice[][[1]] cannot be used as a part specification. >>

During evaluation of In[30]:= General::stop: Further output of Part::pkspec1 will be suppressed during this calculation. >>

During evaluation of In[30]:= RandomChoice::argt: RandomChoice called with 0 arguments; 1 or 2 arguments are expected. >>

During evaluation of In[30]:= General::stop: Further output of RandomChoice::argt will be suppressed during this calculation. >>

During evaluation of In[30]:= 68
(*This is how many {0} there should be*)

During evaluation of In[30]:= 45
(*This is how many there are*)

{{{1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}}, 
 {{1}, {},  {},  {},  {0}, {0}, {0}, {0}, {0}, {0}, {0}, {1}}, 
 {{1}, {},  {},  {},  {0}, {0}, {0}, {0}, {},  {},  {0}, {1}}, 
 {{1}, {},  {},  {},  {0}, {0}, {0}, {0}, {},  {},  {0}, {1}}, 
 {{1}, {},  {},  {},  {0}, {0}, {0}, {0}, {},  {},  {0}, {1}}, 
 {{1}, {},  {},  {},  {0}, {0}, {0}, {0}, {},  {},  {0}, {1}}, 
 {{1}, {},  {},  {},  {0}, {0}, {},  {},  {},  {},  {0}, {1}}, 
 {{1}, {},  {},  {},  {},  {3}, {},  {0}, {0}, {},  {0}, {1}}, 
 {{1}, {},  {},  {},  {},  {0}, {0}, {0}, {0}, {0}, {0}, {1}}, 
 {{1}, {},  {},  {},  {},  {0}, {},  {2}, {0}, {},  {},  {1}}, 
 {{1}, {},  {},  {},  {},  {0}, {0}, {0}, {0}, {},  {},  {1}}, 
 {{1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}}}]