python小练习8

编程入门 行业动态 更新时间:2024-10-28 13:27:25

<a href=https://www.elefans.com/category/jswz/34/1770869.html style=python小练习8"/>

python小练习8

core.py: 游戏核心逻辑

from location import Location, Direction
import randomclass GameControlCore:def __init__(self):self.__data_list = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]@propertydef data_list(self):return self.__data_listdef generator_a_new_element(self):empty_list = self.__get_empty_position()if len(empty_list) > 0:loc = random.choice(empty_list)if random.randint(1, 10) > 8:self.__data_list[loc.row][loc.col] = 4else:self.__data_list[loc.row][loc.col] = 2def __get_empty_position(self):empty_list = []for row in range(len(self.__data_list)):for col in range(len(self.__data_list)):if self.__data_list[row][col] == 0:empty_list.append(Location(row, col))return empty_listdef move(self, dir=Direction.RIGHT):if dir == Direction.UP:self.__move_up()elif dir == Direction.DOWN:self.__move_down()elif dir == Direction.LEFT:self.__move_left()elif dir == Direction.RIGHT:self.__move_right()else:raise ValueError("dir should be a value in [Direction.UP, Direction.DOWN,Direction.LEFT, Direction.RIGHT]")def __move_left(self):for row in range(len(self.__data_list)):self.__data_list[row] = self.__move_zero_to_end(self.__data_list[row])self.__data_list[row] = self.__merge_same_elements_head_to_tail(self.__data_list[row])def __move_right(self):for row in range(len(self.__data_list)):self.__data_list[row] = self.__move_zero_to_head(self.__data_list[row])self.__data_list[row] = self.__merege_same_elemets_tail_to_head(self.__data_list[row])def __move_up(self):self.__tanspose()self.__move_left()self.__tanspose()def __move_down(self):self.__tanspose()self.__move_right()self.__tanspose()def __tanspose(self):for i in range(len(self.__data_list) - 1):for j in range(i + 1, len(self.__data_list)):temp = self.__data_list[i][j]self.__data_list[i][j] = self.__data_list[j][i]self.__data_list[j][i] = tempdef __move_zero_to_end(self, line):"""列表中的所有零元素移动到列表末尾,其它元素的顺序不变从列表末尾开始,每个元素如果是0,则删除这个元素,并在末尾添加一个零:return: 满足条件的列表"""for i in range(-1, -len(line) - 1, -1):if line[i] == 0:del line[i]line.append(0)return linedef __move_zero_to_head(self, line):"""列表中的所有零元素移动到列表行首,其它元素的顺序不变:return: 满足条件的列表"""reverse = line[-1:-len(line) - 1:-1]self.__move_zero_to_end(reverse)line = reverse[-1:-len(line) - 1:-1]return linedef __merge_same_elements_head_to_tail(self, line):"""把零元素移动到末尾,其它元素顺序不变,并且把列表中相邻相同整数元素进行合并,只合并一次:return: 操作后的列表"""for index in range(len(line) - 1):if line[index] == 0:return lineif line[index] == line[index + 1]:line[index] = line[index] * 2for next in range(index + 2, len(line)):line[next - 1] = line[next]line[len(line) - 1] = 0return linedef __merege_same_elemets_tail_to_head(self, line):line = line[-1:-1 - len(line):-1]line = self.__merge_same_elements_head_to_tail(line)return line[-1:-len(line) - 1:-1]def print_info(self):for row in self.__data_list:for col in row:print(col, end=" ")def is_game_over(self):if len(self.__get_empty_position()) == 0:return Truefor i in range(len(self.__data_list)):for j in range(len(self.__data_list[i]) - 1):if self.__data_list[i][j] == self.__data_list[i][j + 1] and self.__data_list[i][j] != 0:return Falseif self.__data_list[j][i] == self.__data_list[j + 1][i] and self.__data_list[j][i] != 0:return Falsereturn True

location.py: 工具类和常量

class Location:def __init__(self, row, col):self.row = rowself.col = colclass Direction:UP = 0DOWN = 1RIGHT = 2LEFT = 3

view.py: 视图

from core import GameControlCore
from location import Location, Directionclass View:def __init__(self):self.__control = GameControlCore()def __start(self, prompt):print(prompt)self.__control.generator_a_new_element()self.__control.generator_a_new_element()self.__control.generator_a_new_element()self.__control.generator_a_new_element()self.__control.generator_a_new_element()self.__control.generator_a_new_element()self.__display()def __update(self):while True:chose = input("请输入你要移动的方向(a:左,d:右,w:上,s:下):")if chose == "a":self.__control.move(Direction.LEFT)elif chose == "d":self.__control.move(Direction.RIGHT)elif chose == "w":self.__control.move(Direction.UP)elif chose == "s":self.__control.move(Direction.DOWN)else:print("input only in [a,d,w,s]")continueself.__control.generator_a_new_element()self.__control.generator_a_new_element()self.__display()if self.__control.is_game_over():print("游戏结束")breakdef __display(self):lst = self.__control.data_listprint("*" * 50)for row in lst:for element in row:print("%5d" % element, end=" ")print()print("*" * 50)def main(self):self.__start("游戏开始:")self.__update()

main.py: 游戏入口

from view import Viewif __name__ == "__main__":view = View()view.main()

测试结果:

更多推荐

python小练习8

本文发布于:2024-03-13 15:39:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1734302.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!