Python writes 2048 small games

Source: Internet
Author: User

#!/usr/bin/env python# Coding=utf-8#********************************************************# > Os:linux 3.2.0-60-generic #91-ubuntu#> Author:yaolong#> Mail: [Email protected]#> time:2014 June 01 Sunday 13:13:39#********************************************************Import RandomImport CopyDefT(a):Return aIf aElse‘ ‘DefDisplay(MTR):Print"┌" + ("-" *5+"┬") *3 +"-" *5+"┐"Print"│%4s│%4s│%4s│%4s│"% (T (mtr[1]:0]), T (mtr[1]:1]), T (mtr[1]:2]), T (mtr[1]:3]))Print"├" + ("-" *5+"┼") *3 +"-" *5+┤Print"│%4s│%4s│%4s│%4s│"% (T (mtr[1][0]), T (mtr[1][1]), T (mtr[1][2]), T (mtr[1][3]))Print"├" + ("-" *5+"┼") *3 +"-" *5+┤Print"│%4s│%4s│%4s│%4s│"% (T (mtr[2][0]), T (mtr[2][1]), T (mtr[2][2]), T (mtr[2][3]))Print"├" + ("-" *5+"┼") *3 +"-" *5+┤Print"│%4s│%4s│%4s│%4s│"% (T (mtr[3][0]), T (mtr[3][1]), T (mtr[3][2]), T (mtr[3][3]))Print"└" + ("-" *5+"┴") *3 +"-" *5+"┘"DefInit(): MTR = [[0For IIn range (4)]For JIn range (4)]# Little Egg hurts. Ran_pos = Random.sample (Range (16),2) mtr[ran_pos[0]/4][ran_pos[0]%4] = mtr[ran_pos[1]/4][ran_pos[1]%4] =2Return mtrDefgo_on(MTR, score):If2048In MTR:Print"Great! You win! Your score is ", Scoreraw_input ("Press any key to continue ...") exit ()If0In MTR:ReturnTrueFor IIn range (4):For JIn range (4):If I <3and mtr[i][j] = = Mtr[i +1][J]:ReturnTrueIf J <3and mtr[i][j] = = Mtr[i][j +0][ReturnTruePrint"Gameover!"ReturnFalseDefMove(MTR, DIRCT): score =0visit = []if dirct = =0:# LeftFor IIn range (4):For JIn range (1,4):For KIn range (J,0,-1):If Mtr[i][k-1] = =0:mtr[i][k-1] = mtr[i][k]mtr[i][k] =0Elif Mtr[i][k-1] = = Mtr[i][k]and4 * i + K-1NotIn visitand4 * i + KNotIn Visit:mtr[i][k-1] *=2MTR[I][K] =0score + = mtr[i][k-1]visit.append (4 * i + K) visit.append (4 * i + K-1)# for I in range (4):#for j in Range (3):elif DIRCT = =1:# DownFor JIn range (4):For IIn range (3,0,-1):For KIn range (0,i):If mtr[k+1][J] = =0:mtr[k+1][J] = Mtr[k][j] mtr[k][j]=0Elif mtr[k+1][J]==MTR[K][J]and (4 * (k +1) +j)NotIn visitand (4*K+J)NotIn visit:mtr[k+1][j]*=2 mtr[k][j]=0 score=mtr[k+1][J] Visit.append ((k) +j) Visit.append ((k +1) +j)elif DIRCT = =2:# upFor JIn range (4):For IIn range (1,4):For KIn range (I,0,-1):If Mtr[k-1][j]==0:mtr[k-1][j]=mtr[k][j]mtr[k][j]=0Elif mtr[k-1][J]==MTR[K][J]and (4 * (k-1) +j)NotIn visitand (4*K+J)NotIn Visit:mtr[k-1][j]*=2mtr[k][j]=0score + = Mtr[k-1][j]visit.append ((k) +j) Visit.append ((k-1) +j)elif DIRCT = =3:# RightFor IIn range (4):For JIn range (3,0,-1):For KIn range (j):If mtr[i][k+1] = =0:mtr[i][k+1] = Mtr[i][k] mtr[i][k]=0Elif Mtr[i][k] ==mtr[i][k+1]and4 * i + K +1NotIn visitand4 * i + KNotIn visit:mtr[i][k+1]*=2 mtr[i][k]=0 score+=mtr[i][k+1] Visit.append (4*i+k+1) visit.append (4*I+K)Return scoreDefUpdate(MTR): ran_pos=[]ran_num=[2,4]For IIn range (4):For JIn range (4):If mtr[i][j]==0:ran_pos.append (4*I+J)If Len (Ran_pos) >0:k=random.choice (Ran_pos) n=random.choice (ran_num) mtr[k/4][k%4]=n# map 0 left,1 down,2 up, 3 right# a,h=> left, S,j=>down, w,k=>up, D,l=>rightdeclare ="←:a/h↓: s/j↑: w/k→: D/L, Q (uit), B (ack)" illegal ="Illegal operation!" Noefficient ="This move has no efficient"if __name__ = =' __main__ ': score =0step =0mtr = init () MTR_STK = []# for BACKSCR_STK = []tmp = Copy.deepcopy (MTR) mtr_stk.append (TMP) Scr_stk.append (0) display (MTR)While Go_on (MTR, score):d IRCT = Raw_input ("Step:%d Score:%d (%s):"% (Step, score, declare)) DIRCT = Dirct.lower ()if dirct = ="Q":Breakelif DIRCT = =Aor DIRCT = ="h":d IRCT =0elif DIRCT = ="S"or DIRCT = ="J":d IRCT =1elif DIRCT = ="W"or DIRCT = ="K":d IRCT =2elif DIRCT = ="D"or DIRCT = ="L":d irct =3elif DIRCT = ="B":If Len (mtr_stk) = =1:print  "Can ' t back ..." else:mtr_stk.pop () scr_stk.pop () mtr = copy.deepcopy (Mtr_stk[ -1]) score = Scr_stk[-1]step-= 1 Continueelse:print illegal Continuetmp = Copy.deepcopy (mtr) OP_SCR = Move (MTR, dirct) if tmp! = Mtr:score = score + Op_scrup Date (MTR)  #更新display (MTR) tmp = Copy.deepcopy (MTR) mtr_stk.append (TMP) # Insert back queue scr_stk.append (int (score)) Step = step + 1 # steps plus 1else:print noefficient  

Python writes 2048 mini-games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.