teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2019/05/14 08:39

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -1,13 +1,23 @@
1
1
  単純にループで処理すればよいでのは?
2
+ また、移動範囲も制限しました。範囲を越えた移動はキャンセルするようにしています。
2
3
  ```Python
3
4
  import random
5
+ from pprint import pprint
4
6
 
5
7
  data = [[10,10]]
6
8
  c = [[0,-1],[0,1],[-1,0],[1,0]]
7
9
 
8
- for i in range(4):
10
+ for i in range(100):
9
11
  rand_0 = random.randint(0,3)
10
- data.append( [data[-1][0]+c[rand_0][0], data[-1][1]+c[rand_0][1]])
12
+ pos = [data[-1][0]+c[rand_0][0], data[-1][1]+c[rand_0][1]]
11
13
 
14
+ # 移動範囲の制限=移動しないように
15
+ if pos[0] < 0: pos[0] = 0
16
+ if pos[0] > 12: pos[0] =12
17
+ if pos[1] < 0: pos[1] = 0
18
+ if pos[1] > 12: pos[1] =12
19
+
20
+ data.append(pos)
21
+
12
- print(data) # [[10, 10], [10, 11], [9, 11], [8, 11], [9, 11]]
22
+ pprint(data) # [[10, 10], [10, 11], [9, 11], [8, 11], [9, 11]]
13
23
  ```