编写象棋界面

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代码编写象棋界面,棋盘覆盖问题的更多相关文章

  1. Python代码编写规范

    Python代码编写规范 编码: a)     如无特殊情况,文件一律使用UTF-8编码 b)     如无需特殊情况,文件头部必须加入#-*-coding:utf-8-*- 缩进 a)     统一 ...

  2. 150+行Python代码实现带界面的数独游戏

    150行代码实现图形化数独游戏 Github地址,欢迎各位大佬们fork.star啥的,感谢: 今天闲着没事干,以前做过html+js版的数独,这次做个python版本的,界面由pygame完成,数独 ...

  3. 利用Python代码编写计算器小程序

    import tkinter import tkinter.messagebox import math class JSQ: def __init__(self): #创建主界面 self.root ...

  4. python 代码编写规范

    一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...

  5. Python代码编写规范,你真的会吗?

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:yangjiajia123456  最近两年的工作都是和运维相关,有时 ...

  6. 小白突破百度翻译反爬机制,33行Python代码实现汉译英小工具!

    表弟17岁就没读书了,在我家呆了差不多一年吧. 呆的前几个月,每天上网打游戏,我又不好怎么在言语上管教他,就琢磨着看他要不要跟我学习Python编程.他开始问我Python编程什么?我打开了我给学生上 ...

  7. python代码 构建验证码

    1.python代码编写 (随机验证码): #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ...

  8. 使用python制作ArcGIS插件(2)代码编写

    使用python制作ArcGIS插件(2)代码编写 by 李远祥 上一章节已经介绍了如何去搭建AddIn的界面,接下来要实现具体的功能,则到了具体的编程环节.由于使用的是python语言进行编程,则开 ...

  9. JavaScript编写棋盘覆盖

    一.前言 之前做了一个算法作业,叫做棋盘覆盖,本来需要用c语言来编写的,但是因为我的c语言是半桶水(哈哈),所以索性就把网上的c语言写法改成JavaScript写法,并且把它的覆盖效果显示出来 二.关 ...

随机推荐

  1. pip 使用国内源安装第三方库

    pip3 install django -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com​

  2. 【PV和PVC】kubernetes存储 persistent volume(持久化硬盘)和 persistent volume claim(持久化硬盘请求)

    报错:pod has unbound immediate PersistentVolumeClaims (repeated 11 times) pv没有满足pvc需求 https://www.cnbl ...

  3. jquery创建一个新的节点对象(自定义结构/内容)的好方法

    jq创建一个新的节点对象,这对一些自定义功能很有帮助,而且可以随意控制对象的结构与内容,何乐而不为呢,看到这里,相信有些朋友已经按耐不住了,好记下来为大家介绍实现方法,感兴趣的朋友可以了解下哦 < ...

  4. PHPStudy后门事件分析

    PHP环境集成程序包phpStudy被公告疑似遭遇供应链攻击,程序包自带PHP的php_xmlrpc.dll模块隐藏有后门.经过分析除了有反向连接木马之外,还可以正向执行任意php代码. 影响版本 P ...

  5. C# Hook原理及EasyHook

    C# Hook原理及EasyHook简易教程 前言 在说C# Hook之前,我们先来说说什么是Hook技术.相信大家都接触过外挂,不管是修改游戏客户端的也好,盗取密码的也罢,它们都是如何实现的呢? 实 ...

  6. HTTP权威指南-概述

    URI 统一资源标识符 类似于邮件地址,邮箱. URL 统一资源定位符 URN 统一资源名 HTTP方法 get post put delete post head 状态码 200 OK 302 重定 ...

  7. [转帖]K8s 工程师必懂的 10 种 Ingress 控制器

    K8s 工程师必懂的 10 种 Ingress 控制器 https://www.kubernetes.org.cn/5948.html 控制器有好多啊. 2019-10-18 23:07 中文社区 分 ...

  8. git的快速入门

    Git是目前世界上最先进的分布式版本控制系统(注意,仅仅是一个程序,而不是正真意义上的系统). Why为什么需要版本控制? 场景1:大学毕业前夕,你在完成毕业论文,初稿A写好了,找老师修改,老师提出意 ...

  9. Linux基础-04-权限

    1. 查看文件的权限 1) 使用ls –l命令查看文件上所设定的权限. -rw-r--r-- 1 root root 605 Mar 18 20:28 .jp1.tar.gz 权限信息 属主 属组 文 ...

  10. 元组的简单介绍——参考Python编程从入门到实践

    元组 用于存储一系列不可修改的元素 1. 元组的定义 dimensions = (200, 50) # 定义一个元组,即将元素用圆括号括起来 print(dimensions[0]) # 打印元组中的 ...