Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please change this code to c++

from random import randint

R, L, D, U, SOLVE = 1, 2, 4, 8, 16RLDU = [R, L, D, U]MOVE_X = [0, 0, 1, -1]
MOVE_Y = [1, -1, 0, 0]



def make_way(x1, y1, x2, y2, org_maze, solve=0):
x_size = len(org_maze)
y_size = len(org_maze[0])
cnt = 0


while cnt < x_size * y_size:
cnt += 1
x, y = x1, y1
maze = [list(row) for row in org_maze]

visited = []
while True:
direction = randint(0, 3)
if direction in visited:
continue
else:
visited += [direction]

next_x = x + MOVE_X[direction]
next_y = y + MOVE_Y[direction]





if -1 < next_x < x_size and -1 < next_y < y_size and not is_way_xy(next_x, next_y, maze):

maze[x][y] |= RLDU[direction]
if solve == SOLVE:
maze[x][y] |= SOLVE
x = next_x
y = next_y
visited = []
if next_x == x2 and next_y == y2:
return maze


if len(visited) == 4:
break


def random_xy(maze, is_way):
x_size = len(maze)
y_size = len(maze[0])
while True:
x = randint(0, x_size - 1)
y = randint(0, y_size - 1)
if (x == 0 and y == 0) or (x == x_size - 1 and y == y_size - 1):
continue
if is_way and maze[x][y] != 0:
return x, y
if not is_way and not is_way_xy(x, y, maze):
return x, y


def is_way_xy(x, y, maze):
if maze[x][y] != 0:
return True
else:

if y > 0 and maze[x][y - 1] & R:
return True

if y < y_size - 1 and maze[x][y + 1] & L:
return True

if x > 0 and maze[x - 1][y] & D:
return True

if x < x_size - 1 and maze[x + 1][y] & U:
return True
return False



x_size = 10
y_size = 10
wrong_way = 10
maze = [[0] * y_size for i in range(x_size)]


maze = make_way(0, 0, x_size - 1, y_size - 1, maze, SOLVE)


for i in range(wrong_way):
while True:

x1, y1 = random_xy(maze, True)

x2, y2 = random_xy(maze, False)
next_maze = make_way(x1, y1, x2, y2, maze)
if next_maze:
break
maze = next_maze

What I have tried:

i don't know python.
please help me.
Posted
Updated 9-Sep-18 7:40am

There are two options here:
1) We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

Or
2) We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.

And both of those ignore that translated code doesn't mean good code in the target language - there are often much simpler ways to do some things in each language.
 
Share this answer
 
Quote:
i don't know python.

You don't show the slightest effort, you are not stuck, you don't have a question, So it is easier to just ask us to do your HomeWork.
We don't do your HomeWork.

I guess you will not like the third option:
3) Learn Python, it is not so complicated.

Internet is full of resources if you dare to search.
 
Share this answer
 
v2
You need to understand that python code at first. At best you ask some mate or collegue for that. It shouldnt be so hard, if I remember right the "def" is starting a function and "return" ends it.

For writing it in C++ you need to learn some C++ tutorial. In C++ is also a fine rand function.

Than bake all together in YOUR homework. ;-)
 
Share this answer
 

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