python闯关3——国际象棋问题
来源:互联网 发布:淘宝怎么买二手冰箱 编辑:程序博客网 时间:2024/06/04 19:41
来自一个python的闯关网站,从初级到高级。
题干
Almost everyone in the world knows about the ancient game Chess and has at least a basic understanding of its rules. It has various units with a wide range of movement patterns allowing for a huge number of possible different game positions (for example Number of possible chess games at the end of the n-th plies.) For this mission, we will examine the movements and behavior of chess pawns.
Chess is a two-player strategy game played on a checkered game board laid out in eight rows (called ranks and denoted with numbers 1 to 8) and eight columns (called files and denoted with letters a to h) of squares. Each square of the chessboard is identified by a unique coordinate pair — a letter and a number (ex, “a1”, “h8”, “d6”). For this mission we only need to concern ourselves with pawns. A pawn may capture an opponent’s piece on a square diagonally in front of it on an adjacent file, by moving to that square. For white pawns the front squares are squares with greater row than their.
A pawn is generally a weak unit, but we have 8 of them which we can use to build a pawn defense wall. With this strategy, one pawn defends the others. A pawn is safe if another pawn can capture a unit on that square. We have several white pawns on the chess board and only white pawns. You should design your code to find how many pawns are safe.
pawns
You are given a set of square coordinates where we have placed white pawns. You should count how many pawns are safe.
Input: Placed pawns coordinates as a set of strings.
Output: The number of safe pawns as a integer.
Example:
safe_pawns({“b4”, “d4”, “f4”, “c3”, “e3”, “g5”, “d2”}) == 6
safe_pawns({“b4”, “c4”, “d4”, “e4”, “f4”, “g4”, “e5”}) == 1
How it is used: For a game AI one of the important tasks is the ability to estimate game state. This concept will show how you can do this on the simple chess figures positions.
Precondition:
0 < pawns ≤ 8
我的代码
def safe_pawns(pawns): answer = 0 for pawn in pawns : if chr(ord(pawn[0])-1)+str(int(pawn[1])-1) in pawns or chr(ord(pawn[0])+1)+str(int(pawn[1])-1) in pawns : answer +=1 return answerif __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert safe_pawns({"b4", "d4", "f4", "c3", "e3", "g5", "d2"}) == 6 assert safe_pawns({"b4", "c4", "d4", "e4", "f4", "g4", "e5"}) == 1
总结
ord函数:找到字母的ascii码
chr():ascii转为字符
- python闯关3——国际象棋问题
- python闯关4——最大最小
- Python Challenge闯关游戏——持续更新
- python闯关
- Python闯关
- Python 闯关
- 爬虫闯关——1
- 爬虫闯关——2
- 关于国际象棋皇后的递归问题——经典为8皇后
- 国际象棋跳马问题
- 【智力题】国际象棋问题
- Python闯关2
- Python闯关pickle
- 9.12测试(二)——国际象棋
- Python挑战游戏( PythonChallenge)闯关之路Level- 3
- 国际象棋
- 国际象棋
- 米粒和国际象棋棋盘问题
- stm32之继电器驱动
- 编译Caffe的Matlab接口
- C语言指针
- Java内存机制
- matlab gui add table
- python闯关3——国际象棋问题
- [Android] adb命令如何获取android手机屏幕分辨率
- DedeCMS
- begin/end学习
- POJ 1523 SPF tarjan求割点模板
- 【Hibernate】配置文档详解
- 网易面试题 对String和java调用方法的考察
- 开始吧!Arduino 少年(安装,编译,第一个程序)
- Leetcode 121. Best Time to Buy and Sell Stock