def judge_winner(self): b=0 r=0 for i in self.logic_board_owner: if i == 1: r+=1 if i == 2: b+=1 if r > b : return 1 elif b > r : return 2 else: return -1

时间: 2024-02-10 16:26:39 浏览: 25
这段代码是一个名为 `judge_winner` 的函数,用判断游戏的胜利者。函数首先初始化两个变量 `b` 和 `r`,分别表示蓝色方和红色方的计数。然后,通过遍历 `self.logic_board_owner` 列表,统计蓝色方和红色方的数量。 如果某个元素的值等于 1,说明该位置属于红色方,将红色方的计数 `r` 加 1。如果某个元素的值等于 2,说明该位置属于蓝色方,将蓝色方的计数 `b` 加 1。 最后,通过比较红色方计数 `r` 和蓝色方计数 `b` 的大小,进行判定。如果红色方计数大于蓝色方计数,返回 1 表示红色方胜利。如果蓝色方计数大于红色方计数,返回 2 表示蓝色方胜利。如果两者相等,返回 -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())

这段代码是一个名为 `__next_step` 的方法。它接收两个参数 `x` 和 `y`,代表鼠标点击的坐标位置。 首先,根据 `self.judge_colory` 的值来判断是否需要更新 `self.__history`。如果 `self.judge_colory` 为假,则 `self.__history` 不变,否则将 `self.__history` 加 1。 接下来,根据 `self.__history` 的奇偶性来确定当前的颜色。如果 `self.__history` 是偶数,则 `self.color` 设置为 1,否则设置为 2。 如果 `self.start_ai_game` 为真,则进入 AI 对战模式。根据当前的颜色和 AI 的颜色判断是否轮到 AI 下棋。如果是,则调用 `self.ai_stage` 方法,传入当前棋盘状态和当前颜色,获取 AI 下棋的结果,并将结果赋值给 `row` 和 `col`。 如果不是 AI 下棋,即玩家下棋,则将鼠标点击位置转换为行和列的索引,并计算出相对于棋盘格子的位置。根据相对位置的大小,确定下棋方向,并将结果赋值给 `self.direct`。 接下来,通过调用 `self.valide` 方法判断当前位置是否可下棋。如果可下棋,则根据当前回合数判断应该绘制哪种颜色的标记,并调用相应的方法在游戏界面上绘制标记和棋子。 然后,通过调用 `self.judge_owner` 方法判断是否有棋子归属变更,并返回变更的位置。如果有变更,根据变更的位置绘制相应颜色的棋子。 接下来,通过调用 `self.judge_color` 方法更新逻辑棋盘的状态。 然后,打印出当前逻辑棋盘的状态。 最后,判断逻辑棋盘是否已满。如果已满,则调用 `self.judge_winner` 方法判断胜利方,并在游戏界面上弹出胜利提示。

def __init__(self,cell_weith = 100 , n = 5, margin = 30): self.__margin = margin self.__cell_width = cell_weith self.__n = n self.logic_board_owner = [0]*((self.__n-1)*(self.__n-1)) self.logic_board_state = [[0]*(self.__n-1) for _ in range((self.__n-1)*(self.__n-1))] #[上,下,左,右] self.__history = 0 self.direct = 0 self.turelly_history = 0 self.ai_color = 1 self.color = 1 self.human_color = self.color self.start_ai_game = True self.judge_colory = False self.game_board = None

这是一个类的初始化方法,用于设置游戏的一些参数和变量。下面是每个参数的含义: - `cell_weith`: 单元格的宽度,默认为100。 - `n`: 游戏区域的大小,默认为5。 - `margin`: 边距的大小,默认为30。 这个方法还初始化了一些其他的变量: - `logic_board_owner`: 游戏区域的所有单元格的所有者,初始值为0。 - `logic_board_state`: 游戏区域的每个单元格的状态,初始值为0。 - `__history`: 历史记录。 - `direct`、`turelly_history`、`ai_color`、`color`、`human_color`、`start_ai_game`、`judge_colory`、`game_board`:一些其他的游戏变量。 这个方法没有返回值。

相关推荐

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

最新推荐

recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

matlab基于RRT和人工势场法混合算法的路径规划.zip

matlab基于RRT和人工势场法混合算法的路径规划.zip
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
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。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依