6

如何用Python的标准print函数打印一个彩色的围棋局面?

 3 years ago
source link: https://blog.csdn.net/xufive/article/details/112363412
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如何用Python的标准print函数打印一个彩色的围棋局面?

天元浪子 2021-01-08 15:55:27 5445

尽管print函数是初学者最先接触到的第一个Python标准函数,但很多人并没有真正了解它。我曾经在《用 print() 函数实现的三个特效》一文中展示了print函数的一些实用技巧,受到读者热捧。今天,我再给大家介绍print函数的另一个技巧:打印彩色文字和图案,并在最后定义一个打印围棋局面的函数,可以打印出下图这样的效果。

在这里插入图片描述

毕竟是在文本模式下,print函数支持的彩色比较少,只有8种,如下表所示。

前景代码背景代码颜色3040黑3141红3242绿3343黄3444蓝3545紫3646青3747白

print支持以下几种显示模式:

  • 4 - 下划线
  • 22 - 去高亮
  • 24 - 去下划线
  • 25 - 去闪烁
  • 27 - 去反白
  • 28 - 去隐藏

使用print函数打印彩色文字或图案时,每一行以前缀“\033[”开始,其后紧跟显示模式、前景色和背景色,三者中间以分号分割,后接小写字母“m”。在显示内容之后,一般以后缀“\033[0m”结束。

print("\033[0;31;47m" + "默认模式,白底红字" + "\033[0m")
print("\033[5;34;43m" + "闪烁模式,黄底蓝字" + "\033[0m")

上面的代码显示效果如下图所示。
在这里插入图片描述

不过,如果你是在Windows环境中运行的话,估计不会出现这个期望的结果。我猜测这应该是Python的一个非常古怪且难以解决的bug,从Py2时代就一直如此。解决方案也很奇葩,使用os模块的system函数运行一次空命令就OK了。代码如下:

import os
os.system('')

print("\033[0;31;47m" + "默认模式,白底红字" + "\033[0m")
print("\033[5;34;43m" + "闪烁模式,黄底蓝字" + "\033[0m")

好,讲完了预备知识,是时候打印一个彩色的围棋局面了。我们约定围棋局面用一个二维的NumPy数组来表示。黑子、白子、和空,我们分别用Unicode字符集中的0x25cf、0x25cb、和0x253c来表示,边角也使用各自的对应符号。这个符号,我们可以在IDLE中直观地显示。

>>> chr(0x25cf)
'●'
>>> chr(0x25cb)
'○'
>>> chr(0x253c)
'┼'
>>> chr(0x250c)
'┌'
>>> chr(0x2510)
'┐'

接下来,应用上面这些知识点,就可以写出一个打印围棋局面的函数了。

import numpy as np
import os
os.system('')

def show_phase(phase):
    """显示局面"""
    
    for i in range(19):
        for j in range(19):
            if phase[i,j] == 1: 
                chessman = chr(0x25cf)
            elif phase[i,j] == 2:
                chessman = chr(0x25cb)
            elif phase[i,j] == 9:
                chessman = chr(0x2606)
            else:
                if i == 0:
                    if j == 0:
                        chessman = '%s '%chr(0x250c)
                    elif j == 18:
                        chessman = '%s '%chr(0x2510)
                    else:
                        chessman = '%s '%chr(0x252c)
                elif i == 18:
                    if j == 0:
                        chessman = '%s '%chr(0x2514)
                    elif j == 18:
                        chessman = '%s '%chr(0x2518)
                    else:
                        chessman = '%s '%chr(0x2534)
                elif j == 0:
                    chessman = '%s '%chr(0x251c)
                elif j == 18:
                    chessman = '%s '%chr(0x2524)
                else:
                    chessman = '%s '%chr(0x253c)
            print('\033[0;30;43m' + chessman + '\033[0m', end='')
        print()

if __name__ == '__main__':
    phase = np.array([
        [0,0,2,1,1,0,1,1,1,2,0,2,0,2,1,0,1,0,0],
        [0,0,2,1,0,1,1,1,2,0,2,0,2,2,1,1,1,0,0],
        [0,0,2,1,1,0,0,1,2,2,0,2,0,2,1,0,1,0,0],
        [0,2,1,0,1,1,0,1,2,0,2,2,2,0,2,1,0,1,0],
        [0,2,1,1,0,1,1,2,2,2,2,0,0,2,2,1,0,1,0],
        [0,0,2,1,1,1,1,2,0,2,0,2,0,0,2,1,0,0,0],
        [0,0,2,2,2,2,1,2,2,0,0,0,0,0,2,1,0,0,0],
        [2,2,2,0,0,0,2,1,1,2,0,2,0,0,2,1,0,0,0],
        [1,1,2,0,0,0,2,2,1,2,0,0,0,0,2,1,0,0,0],
        [1,0,1,2,0,2,1,1,1,1,2,2,2,0,2,1,1,1,1],
        [0,1,1,2,0,2,1,0,0,0,1,2,0,2,2,1,0,0,1],
        [1,1,2,2,2,2,2,1,0,0,1,2,2,0,2,1,0,0,0],
        [2,2,0,2,2,0,2,1,0,0,1,2,0,2,2,2,1,0,0],
        [0,2,0,0,0,0,2,1,0,1,1,2,2,0,2,1,0,0,0],
        [0,2,0,0,0,2,1,0,0,1,0,1,1,2,2,1,0,0,0],
        [0,0,2,0,2,2,1,1,1,1,0,1,0,1,1,0,0,0,0],
        [0,2,2,0,2,1,0,0,0,0,1,0,0,0,0,1,1,0,0],
        [0,0,2,0,2,1,0,1,1,0,0,1,0,1,0,1,0,0,0],
        [0,0,0,2,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0]
    ], dtype=np.ubyte)
    
    show_phase(phase)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK