用python代码编写象棋界面,棋盘覆盖问题
编写象棋界面
import turtle
t=turtle.Pen()
t.speed(100)
def angle(x,y):
t.penup()
t.goto(x+3,y+3)
t.pendown()
t.setheading(0)
t.forward(5)
t.goto(x+3,y+3)
t.left(90)
t.forward(5)
t.penup()
t.goto(x+3,y-3)
t.pendown()
t.setheading(0)
t.forward(5)
t.goto(x+3,y-3)
t.left(90)
t.forward(-5)
t.penup()
t.goto(x-3,y+3)
t.pendown()
t.setheading(0)
t.forward(-5)
t.goto(x-3,y+3)
t.left(90)
t.forward(5)
t.penup()
t.goto(x-3,y-3)
t.pendown()
t.setheading(0)
t.forward(-5)
t.goto(x-3,y-3)
t.left(90)
t.forward(-5)
def v(x,y):
t.penup()
t.goto(x+3,y+3)
t.pendown()
t.setheading(0)
t.forward(5)
t.goto(x+3,y+3)
t.left(90)
t.forward(5)
t.penup()
t.goto(x+3,y-3)
t.pendown()
t.setheading(0)
t.forward(5)
t.goto(x+3,y-3)
t.left(90)
t.forward(-5)
t.penup()
def a(x,y):
t.penup()
t.goto(x-3,y+3)
t.pendown()
t.setheading(0)
t.forward(-5)
t.goto(x-3,y+3)
t.left(90)
t.forward(5)
t.penup()
t.goto(x-3,y-3)
t.pendown()
t.setheading(0)
t.forward(-5)
t.goto(x-3,y-3)
t.left(90)
t.forward(-5)
#1.绘制所有横线
t.penup()
t.goto(-80,90)
t.pendown()
for i in range(1,6,1):
t.forward(160)
t.penup()
t.right(90)
t.forward(20)
t.right(90)
t.pendown()
t.forward(160)
t.penup()
t.left(90)
t.forward(20)
t.left(90)
t.pendown()
#2.绘制所有竖线
t.left(90)
t.penup()
t.forward(20)
t.pendown()
for i in range(1,5,1):
t.forward(80)
t.penup()
t.forward(20)
t.pendown()
t.forward(80)
t.right(90)
t.forward(20)
t.right(90)
t.forward(80)
t.penup()
t.forward(20)
t.pendown()
t.forward(80)
t.left(90)
t.forward(20)
t.left(90)
t.forward(180)
t.left(90)
t.forward(160)
t.left(90)
t.forward(180)
#3.绘制斜线
t.left(90)
t.forward(60)
t.left(45)
t.forward(40*1.414)
t.left(45)
t.forward(-40)
t.left(45)
t.forward(40*1.414)
t.penup()
t.goto(-20,90)
t.pendown()
t.right(180)
t.forward(40*1.414)
t.right(45)
t.forward(-40)
t.right(45)
t.forward(40*1.414)
#4.绘制炮和兵的位置
angle(60,50)
angle(-60,50)
angle(60,-50)
angle(-60,-50)
angle(40,30)
angle(-40,30)
angle(40,-30)
angle(-40,-30)
angle(0,30)
angle(0,-30) a(80,30)
a(80,-30)
v(-80,-30)
v(-80,30)
#5.绘制外围线 绘制一个长方形,设置笔的粗细
t.penup()
t.goto(-90,-100)
t.pendown()
t.pensize(10)
t.forward(200)
t.right(90)
t.forward(180)
t.right(90)
t.forward(200)
t.right(90)
t.forward(180)
t.right(90)
棋盘覆盖问题
在2^k*2^k个方格组成的棋盘中,有一个方格被占用,用下图的4种L型骨牌覆盖所有棋盘上的其余所有方格,不能重叠。
代码如下:
def chess(tr,tc,pr,pc,size):
global mark
global table
mark+=1
count=mark
if size==1:
return
half=size//2
if pr<tr+half and pc<tc+half:
chess(tr,tc,pr,pc,half)
else:
table[tr+half-1][tc+half-1]=count
chess(tr,tc,tr+half-1,tc+half-1,half)
if pr<tr+half and pc>=tc+half:
chess(tr,tc+half,pr,pc,half)
else:
table[tr+half-1][tc+half]=count
chess(tr,tc+half,tr+half-1,tc+half,half)
if pr>=tr+half and pc<tc+half:
chess(tr+half,tc,pr,pc,half)
else:
table[tr+half][tc+half-1]=count
chess(tr+half,tc,tr+half,tc+half-1,half)
if pr>=tr+half and pc>=tc+half:
chess(tr+half,tc+half,pr,pc,half)
else:
table[tr+half][tc+half]=count
chess(tr+half,tc+half,tr+half,tc+half,half) def show(table):
n=len(table)
for i in range(n):
for j in range(n):
print(table[i][j],end=' ')
print('') mark=0
n=8
table=[[-1 for x in range(n)] for y in range(n)]
chess(0,0,2,2,n)
show(table)
n是棋盘宽度,必须是2^k,本例中n=8,特殊格子在(2,2)位置,如下图所示:
采用分治法每次把棋盘分成4份,如果特殊格子在这个小棋盘中则继续分成4份,如果不在这个小棋盘中就把该小棋盘中靠近中央的那个格子置位,表示L型骨牌的1/3占据此处,每一次递归都会遍历查询4个小棋盘,三个不含有特殊格子的棋盘置位的3个格子正好在大棋盘中央构成一个完整的L型骨牌,依次类推,找到全部覆盖方法。运行结果如下:
用python代码编写象棋界面,棋盘覆盖问题的更多相关文章
- Python代码编写规范
Python代码编写规范 编码: a) 如无特殊情况,文件一律使用UTF-8编码 b) 如无需特殊情况,文件头部必须加入#-*-coding:utf-8-*- 缩进 a) 统一 ...
- 150+行Python代码实现带界面的数独游戏
150行代码实现图形化数独游戏 Github地址,欢迎各位大佬们fork.star啥的,感谢: 今天闲着没事干,以前做过html+js版的数独,这次做个python版本的,界面由pygame完成,数独 ...
- 利用Python代码编写计算器小程序
import tkinter import tkinter.messagebox import math class JSQ: def __init__(self): #创建主界面 self.root ...
- python 代码编写规范
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...
- Python代码编写规范,你真的会吗?
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:yangjiajia123456 最近两年的工作都是和运维相关,有时 ...
- 小白突破百度翻译反爬机制,33行Python代码实现汉译英小工具!
表弟17岁就没读书了,在我家呆了差不多一年吧. 呆的前几个月,每天上网打游戏,我又不好怎么在言语上管教他,就琢磨着看他要不要跟我学习Python编程.他开始问我Python编程什么?我打开了我给学生上 ...
- python代码 构建验证码
1.python代码编写 (随机验证码): #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ...
- 使用python制作ArcGIS插件(2)代码编写
使用python制作ArcGIS插件(2)代码编写 by 李远祥 上一章节已经介绍了如何去搭建AddIn的界面,接下来要实现具体的功能,则到了具体的编程环节.由于使用的是python语言进行编程,则开 ...
- JavaScript编写棋盘覆盖
一.前言 之前做了一个算法作业,叫做棋盘覆盖,本来需要用c语言来编写的,但是因为我的c语言是半桶水(哈哈),所以索性就把网上的c语言写法改成JavaScript写法,并且把它的覆盖效果显示出来 二.关 ...
随机推荐
- Django models中的\_\_repr__方法
先看个例子: class D(object): def __init__(self): pass def __str__(self): return "__str__" def _ ...
- windows7 + iis7 + fastcgi + php5 + netbeans + xdebug 调试 php
Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况. windows7 + iis7 + fastcgi + php5 + netbe ...
- SpringBoot学习笔记:单元测试
SpringBoot学习笔记:单元测试 单元测试 单元测试(英语:Unit Testing)又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部 ...
- 创建 LVM
1.将物理磁盘设备条带化为物理卷 # pvcreate /dev/sdb 查看物理卷: # pvs# pvdisplay 2.创建卷组,并添加 PV 到卷组 # vgcreate vg1 /dev/s ...
- dockerui 安装
meiya@meiya:~$ docker pull abh1nav/dockerui Using default tag: latest latest: Pulling from abh1nav/d ...
- mysql 连接闪断自动重连的方法(用在后台运行中的PHP代码)
mysql 连接闪断自动重连的方法(用在后台运行中的PHP代码)当mysql断开连接 $_instance这个还是有值得 所以会报错 MySQL server has gone away 这个地方需要 ...
- 017 Android 获取手机SIM卡序列号和读取联系人
1.获取手机SIM卡序列号 //5.存储sim卡系列号 //5.1获取sim卡系列号 TelephonyManager manager = (TelephonyManager) getSystemSe ...
- 在Asp.Net Core中集成Kafka(中)
在上一篇中我们主要介绍如何在Asp.Net Core中同步Kafka消息,通过上一篇的操作我们发现上面一篇中介绍的只能够进行简单的首发kafka消息并不能够消息重发.重复消费.乐观锁冲突等问题,这些问 ...
- github.com连接超时
https://blog.csdn.net/hanchao5272/article/details/79393393 1.错误信息 之前github都能用,但是今天git clone的时候居然连不上 ...
- 将oracle关键字作为字段名
对于关键字比如:Level.uid.group等 如果在数据库设计的时候,没有考虑oracle数据库的特殊性时,可能会使用关键字作为字段名,从而在建表的过程中,提示错误:ORA-00904: inva ...