随机"路径"代码

时间:2016-04-17 05:07:12

标签: wolfram-mathematica

我正在为我正在重新创建的游戏生成随机路径。它基于" Thin Ice"在企鹅俱乐部玩游戏,你扮演一个火人角色,穿过一条冰路,然后到达出口,沿途融化尽可能多的冰块。 (链接播放:http://justdoodiecp.weebly.com/play-thin-ice.html)  这个游戏有预设板。我正在制作的游戏将随机生成。我遇到了很多问题。

我目前正在使用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]]
  ]

输出:

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}}}]

我已经在互联网上寻找各种路径生成提示和技巧,但没有一个是我正在寻找的。大多数人允许路径返回自身,或沿对角线移动,或生成迷宫。这些都行不通。

任何人都可以帮我揭开正确的逻辑吗?如果我不清楚,请提出问题! :)

谢谢!

0 个答案:

没有答案
相关问题