昨天使用c语言构造扫雷的棋盘之后,觉得使用c/C++来做最后的游戏的可能性不大,java做的不好在别的机器上运行,而使用Python做的可以转换到exe文件,可能方便移植,那么将棋盘移植到Python可能更好一点。顺便练习一下Python编程。
使用Python方便的面向对象功能,可以将棋盘设计的复杂一点。C版本的棋盘必须是正方形的,而Python我将其改为可调整的矩形,默认当然是正方形 的。就这这个矩形的设计,导致很多地方出现了问题,在判断有多少个雷的时候。当然后来改正了。因为是OO的使用class,可以有private的属性, 所以向棋盘大小这种数据就没有像C版本中那样传递参数来getcount()了,这也是比C版本的扫雷更好的一点地方。
另外,由于随即生成大量数据的时候可能出现重复,Python版本更好的考虑了这个问题,最后实际计算出雷的数目,可能不等于用户给定的数目,但是相差不大。
Python源码如下:
其一个生成的棋盘如下图:
使用Python方便的面向对象功能,可以将棋盘设计的复杂一点。C版本的棋盘必须是正方形的,而Python我将其改为可调整的矩形,默认当然是正方形 的。就这这个矩形的设计,导致很多地方出现了问题,在判断有多少个雷的时候。当然后来改正了。因为是OO的使用class,可以有private的属性, 所以向棋盘大小这种数据就没有像C版本中那样传递参数来getcount()了,这也是比C版本的扫雷更好的一点地方。
另外,由于随即生成大量数据的时候可能出现重复,Python版本更好的考虑了这个问题,最后实际计算出雷的数目,可能不等于用户给定的数目,但是相差不大。
Python源码如下:
- #coding:UTF-8
- #Author: Hegc Huang
- import time, random
- import sys
- #class for Mines
- class Mines:
- """Class for the Mine Game's data structure.
- when initializing the game, note that:
- Mine(minecount=16, width=9, height=None)
- width: Chessboard's width.
- minecount: Count of mines.
- height: Chessboard's height.
- minecount must less than width*height, and if height isn't set, it equals width as default.
- so, Mines() means a 9*9 chessboard, and 16 mines;
- Mines(17, 10) means 10*10 chessboard, and 17 mines;
- Mines(17, 10, 11) means 10*11 chessboard, and 17 mines.
- as a result of random generator, minecount may change to a realistic number.
- """
- def __init__ (self, minecount=16, width=9, height=None):
- if height == None:
- height = width
- self.__width = width #for private use
- self.__minecount = minecount #for private use
- self.__height = height #
- print "width= %d, height= %d" % (width, height)
- if self.__minecount>=self.__width * self.__height:
- print 'too small a chessboard. to exit.'
- sys.exit()
- self.mines = [0 for x in range(minecount)] #each 0, total minecount
- self.chessboard = [[0 for x in range(width)] for y in range(height)]#size: width*height
- print self.chessboard
- self.__initialize()
- def __initialize (self):
- random.seed(time.time()) #set seed for random
- count = 0
- size = self.__width * self.__height - 1
- while count<self.__minecount:
- randresult = int(random.random()*size + 1) #random for chess
- if not self.check(count, randresult):
- randresult = int(random.random()*size + 1) #random for chess
- self.mines[count] = randresult
- count += 1
- del randresult
- #end initialize mines[]
- print "Mines : ", self.mines
- #chessboard init
- for r in self.mines:
- x = r//self.__width
- y = r%self.__height
- #print 'x = %d, y = %d' % (x, y)
- self.chessboard[x][y] = -1
- #
- allmines = 0 #all indeed mines
- cx = 0
- while cx<self.__height:
- cy = 0
- while cy<self.__width:
- c = self.getcount(cx, cy)
- #print 'c = ', c
- if c==-1:
- allmines += 1
- self.chessboard[cx][cy] = c
- cy += 1
- #print self.chessboard[cx][:self.__height]
- cx += 1
- self.minecount = allmines
- #end initialize chessboard[][]
- print 'All mines = ', self.minecount
- def check (self, count, rr):
- if self.mines[:count].__contains__(rr):
- return False
- return True
- def getcount (self, x, y):
- #print 'x=%d, y=%d' % (x, y)
- ret = 0;
- if self.chessboard[x][y]==-1:
- ret =-1
- elif x==0 and y==0 and self.chessboard[x][y]!=-1:
- if self.chessboard[x+1][y] == -1:
- ret += 1
- if self.chessboard[x+1][y+1] == -1:
- ret +=1
- if self.chessboard[x][y+1] == -1:
- ret +=1
- elif x==self.__height-1 and y==self.__width-1 and self.chessboard[x][y]!=-1:
- if self.chessboard[x-1][y] == -1:
- ret += 1
- if self.chessboard[x-1][y-1] == -1:
- ret +=1
- if self.chessboard[x][y-1] == -1:
- ret +=1
- elif y==0 and self.chessboard[x][y]!=-1:
- if self.chessboard[x-1][y] == -1:
- ret += 1
- if self.chessboard[x-1][y+1] == -1:
- ret +=1
- if self.chessboard[x][y+1] == -1:
- ret +=1
- if x < self.__height-1:
- if self.chessboard[x+1][y] == -1:
- ret += 1
- if self.chessboard[x+1][y+1] == -1:
- ret += 1
- elif x==0 and self.chessboard[x][y]!=-1:
- if self.chessboard[x+1][y] == -1:
- ret += 1
- if self.chessboard[x+1][y-1] == -1:
- ret +=1
- if self.chessboard[x][y-1] == -1:
- ret +=1
- if y < self.__width-1:
- if self.chessboard[x+1][y+1] == -1:
- ret += 1
- if self.chessboard[x][y+1] == -1:
- ret += 1
- elif x==self.__height-1 and self.chessboard[x][y]!=-1:
- if self.chessboard[x-1][y] == -1:
- ret += 1
- if self.chessboard[x-1][y-1] == -1:
- ret +=1
- if self.chessboard[x][y-1] == -1:
- ret +=1
- if self.chessboard[x-1][y+1] == -1:
- ret += 1
- if self.chessboard[x][y+1] == -1:
- ret += 1
- elif y==self.__width-1 and self.chessboard[x][y]!=-1:
- if self.chessboard[x-1][y] == -1:
- ret += 1
- if self.chessboard[x+1][y] == -1:
- ret +=1
- if self.chessboard[x][y-1] == -1:
- ret +=1
- if self.chessboard[x+1][y-1] == -1:
- ret += 1
- if self.chessboard[x-1][y-1] == -1:
- ret += 1
- elif self.chessboard[x][y]!=-1:
- if self.chessboard[x-1][y-1] == -1:
- ret += 1
- if self.chessboard[x-1][y] == -1:
- ret +=1
- if self.chessboard[x-1][y+1] == -1:
- ret +=1
- if self.chessboard[x][y+1] == -1:
- ret += 1
- if self.chessboard[x+1][y+1] == -1:
- ret += 1
- if self.chessboard[x+1][y] == -1:
- ret +=1
- if self.chessboard[x+1][y-1] == -1:
- ret += 1
- if self.chessboard[x][y-1] == -1:
- ret += 1
- return ret
- #end getcount
- mines = Mines(6, 5)
- print Mines.__doc__
- _l = len(mines.chessboard)
- l = 0
- while l<_l:
- for i in mines.chessboard[:][l]:
- if i==-1:
- i = '/x0F'
- print i, ' ',/
- print ''
- l += 1
其一个生成的棋盘如下图: