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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2048小游戏后端的实现

發布時間:2023/12/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2048小游戏后端的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

游戲的由來

2048小游戲是首先在github上發布的一款開源的小游戲, 游戲的規則十分簡單, 但也意外的獲得了世界上很多人的青睞。在這里, 我想介紹一下我實現2048小游戲后端的一些小算法的設計。

A.棧的使用

在2048的游戲當中, 最主要的操作便是上下左右的操作, 而這里, 我們需要使用一個常用的數據結構--棧。在移動方塊的時候, 相同數字的方塊會在移動的過程中融合。這和棧只允許棧頂作操作的特點十分吻合。所以, 首先需要實現的是一個棧(當然大部分語言的標準庫內都有這樣的內容, 詳情看下面的代碼)

B.新方塊的產生

在2048當中, 在每一次的移動之后后端需要做兩項操作, 首先是判斷游戲是否可以通過移動來產生方塊的融合, 其次則是找到一個任意的空位來產生一個新的方塊。首先是判斷游戲是否結束。在這里我們可以采用BFS的方法, 對每個方塊進行盡量的廣度搜索, 從而找到可以融合的地方。其次是找到一個任意的空位。我們將空位的x,y坐標都用線性表儲存起來, 然后對這個線性表執行一次洗牌操作, 就可以任意找到一個空白的位置來插入一個新的方塊了, 這樣大致2048游戲的后端就完成了。

下面是實現的代碼:
首先是線性棧, 因為無聊所以寫了一個:

#the stack module is designed for the movement of #puzzle.class Stack:def __init__(self):#the base is the element listself.__base =[]#the number of the element in the stackself.length =0#the top points to the top element of the stackself.__top =0#the insert function is for element insertiondef insert(self, n):self.__base.append(n)self.length +=1self.__top +=1#change the lenght and the top pointers#to pop the head element in the stackdef pop(self):if self.__top >0:self.__base.pop(self.__top-1)self.length -=1self.__top -=1#return the head element in the stackdef front(self):if(self.__top >0):return self.__base[self.__top-1]else: return 0

下面是主要操作的實現:

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]

結語

十分遺憾的是, 由于本人的前端設計實在太渣, 所以沒有實現相應的前端。在此如果有實現前端設計的同道中人歡迎將代碼發送到我的郵箱wyc8094@gmail.com 謝謝!!

總結

以上是生活随笔為你收集整理的2048小游戏后端的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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