python three_blocks.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python three_blocks.py相关的知识,希望对你有一定的参考价值。

import random

def mp(Matrix):
	for m in Matrix:
		print m

def fill(Matrix, pos, start_x, end_x, start_y, end_y):

	if (start_x == end_x) or (start_y == end_y):
		return

	mid_x = start_x + ( ( end_x - start_x) / 2)
	mid_y = start_y + ( ( end_y - start_y) / 2)

	q = int(pos[1] > mid_x)
	q = q + (int(pos[0] > mid_y)) * 2

	s = ['*', '#', '@', '1', '+', '~', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'X', 'U', 'L', 'W']
	it = s[random.randint(0, len(s)-1)]

	if q != 0:
		Matrix[mid_y][mid_x] = it
		fill(Matrix, (mid_y, mid_x), start_x, mid_x, start_y, mid_y)
	else:
		fill(Matrix, (pos[0], pos[1]), start_x, mid_x, start_y, mid_y)

	if q != 1:
		Matrix[mid_y][mid_x+1] = it
		fill(Matrix, (mid_y, mid_x+1), mid_x + 1, end_x, start_y, mid_y)
	else:
		fill(Matrix, (pos[0], pos[1]), mid_x + 1, end_x, start_y, mid_y)

	if q != 2:
		Matrix[mid_y+1][mid_x] = it
		fill(Matrix, (mid_y+1, mid_x), start_x, mid_x, mid_y + 1, end_y)
	else:
		fill(Matrix, (pos[0], pos[1]), start_x, mid_x, mid_y + 1, end_y)

	if q != 3:
		Matrix[mid_y+1][mid_x+1] = it
		fill(Matrix, (mid_y + 1, mid_x + 1), mid_x + 1, end_x, mid_y + 1, end_y)
	else:
		fill(Matrix, (pos[0], pos[1]), mid_x + 1, end_x, mid_y + 1, end_y)


exp = 5
size = 2**exp

Matrix = [[0 for x in range(size)] for x in range(size)]

rand = (random.randint(0, size-1), random.randint(0, size-1))
Matrix[rand[0]][rand[1]] = '0'

fill(Matrix, rand, 0, size - 1, 0, size - 1)

mp(Matrix)

以上是关于python three_blocks.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python