2048小游戏后端的实现
生活随笔
收集整理的這篇文章主要介紹了
2048小游戏后端的实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
游戲的由來(lái)
2048小游戲是首先在github上發(fā)布的一款開(kāi)源的小游戲, 游戲的規(guī)則十分簡(jiǎn)單, 但也意外的獲得了世界上很多人的青睞。在這里, 我想介紹一下我實(shí)現(xiàn)2048小游戲后端的一些小算法的設(shè)計(jì)。A.棧的使用
在2048的游戲當(dāng)中, 最主要的操作便是上下左右的操作, 而這里, 我們需要使用一個(gè)常用的數(shù)據(jù)結(jié)構(gòu)--棧。在移動(dòng)方塊的時(shí)候, 相同數(shù)字的方塊會(huì)在移動(dòng)的過(guò)程中融合。這和棧只允許棧頂作操作的特點(diǎn)十分吻合。所以, 首先需要實(shí)現(xiàn)的是一個(gè)棧(當(dāng)然大部分語(yǔ)言的標(biāo)準(zhǔn)庫(kù)內(nèi)都有這樣的內(nèi)容, 詳情看下面的代碼)B.新方塊的產(chǎn)生
在2048當(dāng)中, 在每一次的移動(dòng)之后后端需要做兩項(xiàng)操作, 首先是判斷游戲是否可以通過(guò)移動(dòng)來(lái)產(chǎn)生方塊的融合, 其次則是找到一個(gè)任意的空位來(lái)產(chǎn)生一個(gè)新的方塊。首先是判斷游戲是否結(jié)束。在這里我們可以采用BFS的方法, 對(duì)每個(gè)方塊進(jìn)行盡量的廣度搜索, 從而找到可以融合的地方。其次是找到一個(gè)任意的空位。我們將空位的x,y坐標(biāo)都用線性表儲(chǔ)存起來(lái), 然后對(duì)這個(gè)線性表執(zhí)行一次洗牌操作, 就可以任意找到一個(gè)空白的位置來(lái)插入一個(gè)新的方塊了, 這樣大致2048游戲的后端就完成了。下面是實(shí)現(xiàn)的代碼:
首先是線性棧, 因?yàn)闊o(wú)聊所以寫了一個(gè):
下面是主要操作的實(shí)現(xiàn):
import random from stack import *class puzzle:#create the puzzle of the game of a given sizedef __init__(self, n):self.__board =[]for i in range(n):self.__board.append(n*[0])self.__size =n#set the initial size and board#find a proper position to insert the elementdef creatInsertLocation(self):#use a list to decide the location of insertionstack =[]for i in range(self.__size):for j in range(self.__size):if(self.__board[i][j]==0):stack.append([i,j])#decide which number to insertx =random.randint(1,2)if(len(stack)>0):random.shuffle(stack)z =stack[0][0] y =stack[0][1]self.__board[z][y] =2**xreturn Trueelse:return False#This function aims to print the whole boarddef showBoard(self):print self.__board#this method is designed for judge the game can go ondef moveable(self):flag =False #to record if the puzzle is moveablemvx =[1,-1,0,0]mvy =[0,0,1,-1]for i in range(self.__size):for j in range(self.__size):for k in range(4):mx =i+mvx[k]my =j+mvy[k]#if the index is out of range, then continueif(mx<0 or mx>=self.__size or my <0 or my >=self.__size):continue#if we find a movable position, then the whole puzzle is movableif(self.__board[i][j] ==self.__board[mx][my]):flag =Truebreakif(flag):break;if(flag):break;return flag#this function defines the up movedef upMove(self):for i in range(self.__size):s =Stack()for j in range(self.__size):if(self.__board[j][i] ==0):continueelif self.__board[j][i] ==s.front():s.pop()s.insert(2*self.__board[j][i])elif self.__board[j][i] !=s.front():s.insert(self.__board[j][i])self.__board[j][i] =0for j in range(s.length-1, -1, -1):self.__board[j][i] =s.front()s.pop()#this function is designed for the puzzle down movedef downMove(self):for i in range(self.__size):s =Stack()for j in range(self.__size -1, -1, -1):if(self.__board[j][i] ==0):continueelif self.__board[j][i] ==s.front():s.pop()s.insert(2*self.__board[j][i])elif self.__board[j][i] !=s.front():s.insert(self.__board[j][i])self.__board[j][i] =0for j in range(s.length, 0, -1):self.__board[self.__size -j][i] =s.front()s.pop()#this function is designed for the left move of the puzzledef leftMove(self):for i in range(self.__size):s =Stack()for j in range(self.__size):if(self.__board[i][j] ==0):continueelif self.__board[i][j] ==s.front():s.pop()s.insert(2*self.__board[i][j])elif self.__board[i][j] !=s.front():s.insert(self.__board[i][j])self.__board[i][j] =0for j in range(s.length -1, -1, -1):self.__board[i][j] =s.front()s.pop()#this function is designed for the right move of the puzzledef rightMove(self):for i in range(self.__size):s =Stack()for j in range(self.__size-1, -1, -1):if(self.__board[i][j] ==0):continueelif self.__board[i][j] ==s.front():s.pop()s.insert(2*self.__board[i][j])elif self.__board[i][j] !=s.front():s.insert(self.__board[i][j])self.__board[i][j] =0for j in range(s.length, 0, -1):self.__board[i][self.__size -j] =s.front()s.pop()def getElem(self,i, j):return self.__board[i][j]結(jié)語(yǔ)
十分遺憾的是, 由于本人的前端設(shè)計(jì)實(shí)在太渣, 所以沒(méi)有實(shí)現(xiàn)相應(yīng)的前端。在此如果有實(shí)現(xiàn)前端設(shè)計(jì)的同道中人歡迎將代碼發(fā)送到我的郵箱wyc8094@gmail.com 謝謝!!
總結(jié)
以上是生活随笔為你收集整理的2048小游戏后端的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OPC学习
- 下一篇: lattice开发错误集合