日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python代码200行左右_200行Python代码实现2048

發(fā)布時間:2024/8/23 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python代码200行左右_200行Python代码实现2048 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

import curses

from random import randrange,chioce

from collections import defaultdict

actions=['Up','Left','Down','Right','Restart','Exit']

letter_codes=[ord(ch) for ch in 'WASDRQwasdrq']

action_dict=dict(zip(letter_codes,actions*2))

def main(stdscr):

def init():

#重置游戲棋盤

game_field.reset()

return 'Game'

def not_game(state):

#畫出GomeOver 或WIN 的界面

game_field.draw(stdscr)

#讀取用戶輸入得到action,判斷重啟游戲還是結束游戲

action = get_user_action(stdscr)

responses = defaultdict(lambda :state)#默認當前狀態(tài)

responses['Restart'],responses['Exit']='Init','Exit'

return responses[actions]

def game():

#畫出當前棋盤狀態(tài)

game_field.draw(stdscr)

#讀取用戶的action

action = get_user_action(stdscr)

if action == 'Restart':

return 'Init'

if action == 'Exit':

return 'Exit'

if game_field.move(action): # move successful

if game_field.is_win():

return 'Win'

if game_field.is_gameover():

return 'Gameover'

return 'Game'

state_actions = {

'Init':init,

'Win':lambda: not_game('Win'),

'Gameover':lambda: not_game('Gameover'),

'Game':game}

curses.use_default_colors()

game_field = GameField(win=32)

state = 'Init'

#狀態(tài)機開始循環(huán)

while state!='Exit':

state = state_actions[state]()

def get_user_action(keyboard):

char = 'N'

while char not in action_dict:

char = keyboard.getch()

return action_dict[char]

def transpose(field):

return [list(row) for row in zip(*field)]

def invert(field):#矩陣逆轉,非逆矩陣

return [row[::-1] for row in field]

class GameField(object):

def draw(self, screen):

help_string1 = '(W)Up (S)Down (A)Left (D)Right'

help_string2 = ' (R)Restart (Q)Exit'

gameover_string = ' GAME OVER'

win_string = ' YOU WIN!'

def cast(string):

screen.addstr(string + '\n')

# 繪制水平分割線

def draw_hor_separator():

line = '+' + ('+------' * self.width + '+')[1:]

separator = defaultdict(lambda: line)

if not hasattr(draw_hor_separator, "counter"):

draw_hor_separator.counter = 0

cast(separator[draw_hor_separator.counter])

draw_hor_separator.counter += 1

def draw_row(row):

cast(''.join('|{: ^5} '.format(num) if num > 0 else '| ' for num in row) + '|')

screen.clear()

cast('SCORE: ' + str(self.score))

if 0 != self.highscore:

cast('HGHSCORE: ' + str(self.highscore))

for row in self.field:

draw_hor_separator()

draw_row(row)

draw_hor_separator()

if self.is_win():

cast(win_string)

else:

if self.is_gameover():

cast(gameover_string)

else:

cast(help_string1)

cast(help_string2)

def init(self,height=4,width=4,win=2048):

self.height = height

self.width = width

self.win_vlaue = 2048

self.score = 0

self.highscore = 0

self.reset() #重置

def spawn(self):#隨機生成2或4

new_element = 4 if randrange(100)>89 else 2

(i,j) = chioce([(i,j) for i in range(self.width)

for j in range(self.height)

if self.field[i][j]==0])

self.field[i][j] = new_element

def reset(self):

if self.score > self.highscore:

self.highscore = self.score

self.score = 0

self.field = [[0 for i in range(self.width)]

for j in range(self.height)]

self.spawn()

self.spawn()

def move(self,direction):

def move_row_left(row):

def tighten(row):#把零散的非零單元擠到一塊

new_row = [i for i in row if i!=0]

new_row += [0 for i in range(len(row)-

len(new_row))]

return new_row

def merge(row):#相鄰元素拼合

pair = False

new_row = []

for i in range(len(row)):

if pair:

new_row.append(2row[i])

self.score += 2row[i]

pair = False

else:

if i+1 < len(row) and row[i] ==row[i+1]:

pair = True

new_row.append(0)

else:

new_row.append(row[i])

assert len(new_row) == len(row)

return new_row

return tighten(merge(tighten(row)))#先擠再合并再擠

moves = {}

moves['Left'] = lambda field: [move_row_left(row)

for row in field]

moves['Right'] = lambda field: invert(moves['Left']

(invert(field)))

moves['Up'] = lambda field: transpose(moves['Left']

(transpose(field)))

moves['Down'] = lambda field: transpose(moves['Right']

(transpose(field)))

if direction in moves:

if self.move_is_possible(direction):

self.field = movesdirection

self.spawn()

return True

else:

return False

def is_win(self):

return any(any(i >= self.win_vlaue for i in row)

for row in self.field)

def is_gameover(self):

return not any(self.move_is_possible(move) for move in actions)

def move_is_possible(self,direction):

def row_is_left_movable(row):

def change(i):

if row[i] == 0 and row[i+1] != 0:

return True

if row[i] != 0 and row[i+1] == row[i]:

return True

return False

return any(change(i) for i in range(len(row)-1))

check = {}

check['Left'] = lambda field: any(row_is_left_movable(row)

for row in field)

check['Right'] = lambda field: check'Left'

check['Up'] = lambda field: check'Left'

check['Down'] = lambda field: check'Right'

if direction in check:

return checkdirection

else:

return False

curses.wrapper(main)

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python代码200行左右_200行Python代码实现2048的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。