from random import randint as rand
string = '|'
Range = rand(0,50)
for y in range(Range):
for x in range(Range):
string = string + str(rand(1,9)) + '|'
print string
string = '|'
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.
pretentious wrote:
- Code: Select all
from random import randint as rand
string = '|'
Range = rand(0,50)
for y in range(Range):
for x in range(Range):
string = string + str(rand(1,9)) + '|'
print string
string = '|'
If I read the brief correctly.
[9, 9, 0, 0]
[9, 9, 0, 0]
[9, 9, 0, 0]
[0, 1, 1, 1]
import random
def main():
n = input('enter n, and this program will make a board size, n by n: ') #Board size
board = generate_board(n) #Creates a random board
print_board(board) #Prints the board so that we can see it
health_lost = 0 #Var for how much health is lost
x , y = -1, -1 #Starting position
string = "You started on the first "
var = True #True until you make it to the top, makes the loop work
while var == True:
list = find_best_move(board, x, y) #Returns a list
x, y = list[0], list[1] #Moves your character
health_lost += list[2] #Takes away health
var = list[3] #Checks if you are at the top or not
string = string + list[4] #So that you can know what path you took
print string + str(health_lost) + ' health.'
def print_board(board):
n = len(board) - 1
while n > -1:
print str(board[n]) + ' = ' + str(n)
n -= 1
def generate_board(n): #Randomly creates a list with n lists in it, and n random numbers
#in each mini list
list = []
for i in range(0, n):
list.append([])
for j in range(0, n):
list[i].append(random.randint(0, 9))
return list
def find_best_move(board, x, y): #Finds the best move
l = len(board[0])
try:
smallest_num = 10
y += 1
if y == 0: #First row, x is undefined so far
for i in range(0, l):
if board[y][i] < smallest_num:
smallest_num = board[y][i]
x = i #x = smallest number location
elif y < len(board): #If y is not on the first row
if x == 0: #If you are on the left side
if board[y][x] < smallest_num:
smallest_num = board[y][x]
if board[y][x+1] < smallest_num:
smallest_num = board[y][x+1]
x += 1
elif x < l: #If you are in the middle
if board[y][x-1] < smallest_num:
smallest_num = board[y][x-1]
x -= 1
if board[y][x] < smallest_num:
smallest_num = board[y][x]
if board[y][x+1] < smallest_num:
smallest_num = board[y][x+1]
x += 1
elif x == l: #If you are on the right
if board[y][x-1] < smallest_num:
smallest_num = board[y][x-i]
x -= 1
if board[y][x] < smallest_num:
smallest_num = board[y][x]
else:
print board[y] #This intentionally causes an index error, which is then caught
#by the except statement, which defines var as False and ends
#The Main Loop
except IndexError:
return (x, y-1, 0, False, 'end. You lost a total of ')
return (x, y, smallest_num, True, str(smallest_num) + ', then went to the ')
if __name__ == '__main__':
main()
anon976 wrote:Well yes, but it would be nice to have a grid so that you can test whether or not your algorithm for getting to the top works.
anon976 wrote:Up next, I'm gonna make it so that It checks every path possible and returns the best one, because that would be a lot more efficient then just moving to smallest number possible each time. What do you guys think?
Users browsing this forum: No registered users and 0 guests