使用Python构造扫雷的棋盘

昨天使用c语言构造扫雷的棋盘之后,觉得使用c/C++来做最后的游戏的可能性不大,java做的不好在别的机器上运行,而使用Python做的可以转换到exe文件,可能方便移植,那么将棋盘移植到Python可能更好一点。顺便练习一下Python编程。
使用Python方便的面向对象功能,可以将棋盘设计的复杂一点。C版本的棋盘必须是正方形的,而Python我将其改为可调整的矩形,默认当然是正方形 的。就这这个矩形的设计,导致很多地方出现了问题,在判断有多少个雷的时候。当然后来改正了。因为是OO的使用class,可以有private的属性, 所以向棋盘大小这种数据就没有像C版本中那样传递参数来getcount()了,这也是比C版本的扫雷更好的一点地方。
另外,由于随即生成大量数据的时候可能出现重复,Python版本更好的考虑了这个问题,最后实际计算出雷的数目,可能不等于用户给定的数目,但是相差不大。
Python源码如下:
  1. #coding:UTF-8
  2. #Author: Hegc Huang
  3. import time, random
  4. import sys
  5. #class for Mines
  6. class Mines:
  7.     """Class for the Mine Game's data structure.
  8.     
  9.     when initializing the game, note that:
  10.     Mine(minecount=16, width=9, height=None)
  11.     width: Chessboard's width.
  12.     minecount: Count of mines.
  13.     height: Chessboard's height.
  14.     minecount must less than width*height, and if height isn't set, it equals width as default.
  15.     so, Mines() means a 9*9 chessboard, and 16 mines;
  16.     Mines(17, 10) means 10*10 chessboard, and 17 mines;
  17.     Mines(17, 10, 11) means 10*11 chessboard, and 17 mines.
  18.     as a result of random generator, minecount may change to a realistic number.
  19.     """
  20.     
  21.     def __init__ (self, minecount=16, width=9, height=None):
  22.         if height == None:
  23.             height = width
  24.         self.__width        = width       #for private use
  25.         self.__minecount    = minecount     #for private use
  26.         self.__height       = height       #
  27.         print "width= %d, height= %d" % (width, height)
  28.         if self.__minecount>=self.__width * self.__height:
  29.             print 'too small a chessboard. to exit.'
  30.             sys.exit()
  31.         
  32.         self.mines          = [0 for x in range(minecount)] #each 0, total minecount
  33.         self.chessboard     = [[0 for x in range(width)] for y in range(height)]#size: width*height
  34.         print self.chessboard
  35.         self.__initialize()
  36.     def __initialize (self):
  37.         random.seed(time.time())    #set seed for random
  38.         count   = 0
  39.         size    = self.__width * self.__height - 1
  40.         while count<self.__minecount:
  41.             randresult = int(random.random()*size + 1)    #random for chess
  42.             if not self.check(count, randresult):
  43.                 randresult = int(random.random()*size + 1)    #random for chess
  44.             self.mines[count] = randresult
  45.             count += 1
  46.             del randresult
  47.             #end initialize mines[]
  48.         print "Mines : "self.mines
  49.         #chessboard init
  50.         for r in self.mines:
  51.             x = r//self.__width
  52.             y = r%self.__height
  53.             #print 'x = %d, y = %d' % (x, y)
  54.             self.chessboard[x][y] = -1
  55.         #
  56.         allmines = 0    #all indeed mines
  57.         cx = 0
  58.         while cx<self.__height:
  59.             cy = 0
  60.             while cy<self.__width:
  61.                 c = self.getcount(cx, cy)
  62.                 #print 'c =  ', c
  63.                 if c==-1:
  64.                     allmines += 1
  65.                 self.chessboard[cx][cy] = c
  66.                 cy += 1
  67.             #print self.chessboard[cx][:self.__height]
  68.             cx += 1
  69.         self.minecount = allmines
  70.         #end initialize chessboard[][]
  71.         print 'All mines = 'self.minecount
  72.     def check (self, count, rr):
  73.         if self.mines[:count].__contains__(rr):
  74.             return False
  75.         return True
  76.     def getcount (self, x, y):
  77.         #print 'x=%d, y=%d' % (x, y)
  78.         ret = 0;
  79.         if self.chessboard[x][y]==-1:
  80.             ret =-1
  81.         elif x==0 and y==0 and self.chessboard[x][y]!=-1:
  82.             if self.chessboard[x+1][y] == -1:
  83.                 ret += 1
  84.             if self.chessboard[x+1][y+1] == -1:
  85.                 ret +=1
  86.             if self.chessboard[x][y+1] == -1:
  87.                 ret +=1
  88.         elif x==self.__height-1 and y==self.__width-1 and self.chessboard[x][y]!=-1:
  89.             if self.chessboard[x-1][y] == -1:
  90.                 ret += 1
  91.             if self.chessboard[x-1][y-1] == -1:
  92.                 ret +=1
  93.             if self.chessboard[x][y-1] == -1:
  94.                 ret +=1
  95.         elif y==0 and self.chessboard[x][y]!=-1:
  96.             if self.chessboard[x-1][y] == -1:
  97.                 ret += 1
  98.             if self.chessboard[x-1][y+1] == -1:
  99.                 ret +=1
  100.             if self.chessboard[x][y+1] == -1:
  101.                 ret +=1
  102.             if x < self.__height-1:
  103.                 if self.chessboard[x+1][y] == -1:
  104.                     ret += 1
  105.                 if self.chessboard[x+1][y+1] == -1:
  106.                     ret += 1
  107.         elif x==0 and self.chessboard[x][y]!=-1:
  108.             if self.chessboard[x+1][y] == -1:
  109.                 ret += 1
  110.             if self.chessboard[x+1][y-1] == -1:
  111.                 ret +=1
  112.             if self.chessboard[x][y-1] == -1:
  113.                 ret +=1
  114.             if y < self.__width-1:
  115.                 if self.chessboard[x+1][y+1] == -1:
  116.                     ret += 1
  117.                 if self.chessboard[x][y+1] == -1:
  118.                     ret += 1
  119.         elif x==self.__height-1 and self.chessboard[x][y]!=-1:
  120.             if self.chessboard[x-1][y] == -1:
  121.                 ret += 1
  122.             if self.chessboard[x-1][y-1] == -1:
  123.                 ret +=1
  124.             if self.chessboard[x][y-1] == -1:
  125.                 ret +=1
  126.             if self.chessboard[x-1][y+1] == -1:
  127.                 ret += 1
  128.             if self.chessboard[x][y+1] == -1:
  129.                 ret += 1
  130.         elif y==self.__width-1 and self.chessboard[x][y]!=-1:
  131.             if self.chessboard[x-1][y] == -1:
  132.                 ret += 1
  133.             if self.chessboard[x+1][y] == -1:
  134.                 ret +=1
  135.             if self.chessboard[x][y-1] == -1:
  136.                 ret +=1
  137.             if self.chessboard[x+1][y-1] == -1:
  138.                 ret += 1
  139.             if self.chessboard[x-1][y-1] == -1:
  140.                 ret += 1
  141.         elif self.chessboard[x][y]!=-1:
  142.             if self.chessboard[x-1][y-1] == -1:
  143.                 ret += 1
  144.             if self.chessboard[x-1][y] == -1:
  145.                 ret +=1
  146.             if self.chessboard[x-1][y+1] == -1:
  147.                 ret +=1
  148.             if self.chessboard[x][y+1] == -1:
  149.                 ret += 1
  150.             if self.chessboard[x+1][y+1] == -1:
  151.                 ret += 1
  152.             if self.chessboard[x+1][y] == -1:
  153.                 ret +=1
  154.             if self.chessboard[x+1][y-1] == -1:
  155.                 ret += 1
  156.             if self.chessboard[x][y-1] == -1:
  157.                 ret += 1
  158.         
  159.        
  160.         return ret
  161.         #end getcount
  162. mines = Mines(65)
  163. print Mines.__doc__
  164. _l = len(mines.chessboard)
  165. l = 0
  166. while l<_l:
  167.     for i in mines.chessboard[:][l]:
  168.         if i==-1:
  169.             i = '/x0F'
  170.         print i, ' ',/
  171.     
  172.     print ''
  173.     l += 1




其一个生成的棋盘如下图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值