import copy import random class map(): chess_number=None; livcell=[]; def init(self, chess_number=60, density=0.5): self.livcell = [[0] * chess_number for i in range(chess_number)] for i in range(chess_number): for j in range(chess_number): pwall = random.random() if pwall < density: self.livcell[i][j] = 1 self.chess_number=chess_number self.density=density def neighborcell(self,pos): neighborList = [] x = pos[0] y = pos[1] neighborList = [[x - 1, y - 1], [x - 1, y], [x - 1, y + 1], [x, y - 1], [x, y + 1], [x + 1, y - 1], [x + 1, y], [x + 1, y + 1]] realnList = copy.deepcopy(neighborList) for i in neighborList: if i[0] < 0 or i[0] > self.chess_number - 1 or i[1] < 0 or i[1] > self.chess_number - 1: realnList.remove(i) return realnList 使用pytext写一个上述代码的单元测试

时间: 2024-04-29 19:19:45 浏览: 18
import unittest from map import Map class TestMap(unittest.TestCase): def test_init(self): m = Map() self.assertEqual(m.chess_number, 60) self.assertEqual(m.density, 0.5) def test_neighborcell(self): m = Map() n = m.neighborcell([0, 0]) self.assertEqual(len(n), 3) self.assertIn([0, 1], n) self.assertIn([1, 0], n) self.assertIn([1, 1], n) def test_livcell(self): m = Map() self.assertEqual(len(m.livcell), 60) self.assertEqual(len(m.livcell[0]), 60) self.assertIn(0, m.livcell[0]) self.assertIn(1, m.livcell[0]) if __name__ == '__main__': unittest.main()
相关问题

chess_location_b = chess_location[0::2]

这是一个Python中的切片语法,意思是从列表 `chess_location` 中取出所有偶数索引位置的元素,并将它们放入一个新的列表 `chess_location_b` 中。具体来说,这个语法中的 `0::2` 表示从索引位置0开始,每隔两个位置取一个元素,直到取完整个列表。例如,如果 `chess_location` 是 `[1, 2, 3, 4, 5, 6, 7, 8]`,那么 `chess_location_b` 就会是 `[1, 3, 5, 7]`。

def count_line(self,direct,row,col,chess_color): ''' :param direct: :param row: :param col: :param chess_color: :return: ''' c1,c2,c3 = 1,1,1 for i in range(2): deta_row, deta_col = direct[i] flag_continous = True for i in range(1,6): next_row,next_col = row + i *deta_row,col + i * deta_col if not self.is_pos_inside(next_row,next_col): break if self.matrix[next_row][next_col] != 3-chess_color: c3 += 1 if self.matrix[next_row][next_col]: if flag_continous: c1 += 1 c2 += 1 else: flag_continous = False else: break return c1,c2,c3

这段代码是一个类的一个方法`count_line`,它接受一些参数,并返回三个计数值。这个方法用于计算给定位置在某个方向上的连续棋子数。 在方法中,首先初始化三个计数器`c1`、`c2`、`c3`的值为1。然后使用一个循环两次,每次迭代处理一个方向。在每个方向上,通过给定的行、列和方向来计算下一个位置的行和列。然后,检查下一个位置是否在棋盘内,如果不在,则跳出循环。如果下一个位置的棋子颜色不是与给定的棋子颜色相反,则增加`c3`计数器的值。如果下一个位置有棋子,根据连续性标志`flag_continous`的值来增加计数器`c1`和`c2`的值。如果下一个位置没有棋子,则将连续性标志设置为False。如果下一个位置的棋子颜色与给定的棋子颜色相反,则跳出循环。 最后,返回三个计数器的值。 这个方法可能是用于评估给定位置在某个方向上的连续棋子数,并用于计算分数或决策下一步的最佳位置。

相关推荐

def get_logic_pos(self,x,y): return (y-self.margin + self.cell_width//2)//self.cell_width, (x-self.margin + self.cell_width//2)//self.cell_width def judge_line(self,row,col,direct,chess_color): c = 1 for i in range(1,6): next_row, next_col = row + direct[0][0] * i, col + direct[0][1] * i if self.matrix[next_row][next_col] == chess_color: c +=1 else: break for i in range(1, 6): next_row, next_col = row + direct[1][0] * i, col + direct[1][1] * i if self.matrix[next_row][next_col] == chess_color: c +=1 else: break return c def judge(self,row,col,chess_color): for direct in [[(-1,0),(1,0)],[(0,-1),(0,1)],[(-1,1),(1,-1)],[(-1,-1),(1,1)]]: if self.judge_line(row,col,direct,chess_color) ==6: return chess_color if len(self.history) == self.n * self.n: return -1 return 0 def deal_with_judge(self, judge_result): if not judge_result: return if judge_result == 1: txt = 'Black Win' elif judge_result == 2: txt = 'White Win' elif judge_result == -1: txt = 'Draw Chess' self.gameboard.draw_box(txt) self.full_matrix(self.n) def put_chess(self,x,y): l = len(self.history) chess_color = (l+1) % 4 // 2+1 if chess_color == self.auto_color: row, col = self.AI.generate_next(self.history, 1 - len(self.history) % 2, chess_color) else: row,col = self.get_logic_pos(x,y) if self.matrix[row][col] == 0: self.history.append((row, col, chess_color)) self.matrix[row][col] = chess_color self.gameboard.drawchess(row, col, chess_color) self.gameboard.draw_now_chess(chess_color) self.deal_with_judge(self.judge(row,col,chess_color)) def full_matrix(self,n): for i in range(self.n): for j in range(self.n): self.matrix[i][j] = 1

def __next_step(self, x, y): if not self.judge_colory: self.__history += 0 else: self.__history += 1 self.color = 1 if self.__history % 2 == 0 else 2 if self.start_ai_game: if self.ai_color == self.color: row,col = self.ai_stage(self.ai_game()[0],self.ai_game()[1]) else: col = round((x-self.__margin*2)/self.__cell_width) row = round((y-self.__margin*2)/self.__cell_width) stage_row = (y-self.__margin)-(self.__cell_width*row+self.__margin) stage_col = (x-self.__margin)-(self.__cell_width*col+self.__margin) if stage_col < stage_row: self.direct= 1 else: self.direct= 0 else: col = round((x - self.__margin * 2) / self.__cell_width) row = round((y - self.__margin * 2) / self.__cell_width) stage_row = (y - self.__margin) - (self.__cell_width * row + self.__margin) stage_col = (x - self.__margin) - (self.__cell_width * col + self.__margin) if stage_col < stage_row: self.direct = 1 else: self.direct= 0 if self.valide(row, col, self.direct): if self.__history % 4 == 0 or (self.__history + 2) % 4 == 0: self.__game_board.drew_turn(2) else: self.__game_board.drew_turn(1) self.add_logic(row, col, self.color) self.__game_board.draw_chess(row, col, self.color, self.direct) if self.judge_owner(row, col, self.color, self.direct): self.__game_board.drew_turn(self.judge_next(self.color)) for i in self.judge_owner(row, col, self.color, self.direct): x,y=self.draw_owner(i) self.__game_board.drew_owner(self.color, y, x) else: self.__game_board.drew_turn(self.color) self.judge_color(row, col, self.color, self.direct) print(self.logic_board_state) if 0 not in self.logic_board_owner: self.__game_board.pop_win(self.judge_winner())

最新推荐

recommend-type

基于springboot+vue+MySQL实现的在线考试系统+源代码+文档

web期末作业设计网页 基于springboot+vue+MySQL实现的在线考试系统+源代码+文档
recommend-type

318_面向物联网机器视觉的目标跟踪方法设计与实现的详细信息-源码.zip

提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
recommend-type

FPGA Verilog 计算信号频率,基础时钟100Mhz,通过锁相环ip核生成200Mhz检测时钟,误差在10ns

结合等精度测量原理和原理示意图可得:被测时钟信号的时钟频率fx的相对误差与被测时钟信号无关;增大“软件闸门”的有效范围或者提高“标准时钟信号”的时钟频率fs,可以减小误差,提高测量精度。 实际闸门下被测时钟信号周期数为X,设被测信号时钟周期为Tfx,它的时钟频率fx = 1/Tfx,由此可得等式:X * Tfx = X / fx = Tx(实际闸门)。 其次,将两等式结合得到只包含各自时钟周期计数和时钟频率的等式:X / fx = Y / fs = Tx(实际闸门),等式变换,得到被测时钟信号时钟频率计算公式:fx = X * fs / Y。 最后,将已知量标准时钟信号时钟频率fs和测量量X、Y带入计算公式,得到被测时钟信号时钟频率fx。
recommend-type

校园二手商品交易系统三.wmv

校园二手商品交易系统三.wmv
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依