雷を掃く小さなゲーム-改善

7758 ワード

ポイント機能が改良され、周囲の雷の個数を表示できます.コードを直接見る:
# -*- coding: utf-8 -*-
'''@author: YeahMan_Nic
   @version: 2.0
   @note:       (2012-9-18)
              :python3.2.2
'''
import sys
import random
import string

class MineSweeping():
    '''     
    '''
    def __init__(self):
        '''     
        ''' 
        self.ROW = 8
        self.LINE = 8
        self.SCORE = 0 #     
        self.MineNum = 15 #    
        self.xy_list=   [[0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0]]
        
                   
    def searchRound(self, x, y):
        count = 0
        if x - 1 >= 0 and y - 1 >= 0 and self.xy_list[x - 1][y - 1] == 1:
            count = count + 1
        if x - 1 >= 0 and self.xy_list[x - 1][y] == 1:
            count = count + 1
        if x - 1 >= 0 and y + 1 < 8 and self.xy_list[x - 1][y + 1] == 1:
            count = count + 1
        if y - 1 >= 0 and self.xy_list[x][y - 1] == 1:
            count = count + 1
        if y + 1 < 8 and self.xy_list[x][y + 1] == 1:
            count = count + 1
        if x + 1 < 8 and y - 1 >= 0 and self.xy_list[x + 1][y - 1] == 1:
            count = count + 1
        if x + 1 < 8 and self.xy_list[x + 1][y] == 1:
            count = count + 1
        if x + 1 < 8 and y + 1 < 8 and self.xy_list[x + 1][y + 1] == 1:
            count = count + 1
        return count      

    def iniData(self):
        '''x,y         
           0-    ;1-   
        '''

         #           
        for l in range(self.LINE):
            for r in range(self.ROW):
                self.xy_list[l][r]= 0
        
        Max = self.MineNum
        for x in range(self.LINE):
            for y in range(self.ROW):
                if 0 > Max:
                    self.xy_list[x][y]= 0
                else:
                    #          ,  0 4   
                    if 1 == random.randint(0,4): 
                        self.xy_list[x][y]= 1
                        Max = Max - 1
                        
    
    def getX(self):
        '''  x   
           @return :   x   
           @type : int
        '''
        sys.stdout.write('X=')
        xRet = input()
        while xRet=='' or (False == self.isNumber(xRet))\
             or 0 > int(xRet) or int(xRet)>self.ROW:
            print('Wrong number!(please input 0-7)')
            sys.stdout.write('X=')
            xRet = input()
        return int(xRet)
        
        
    def getY(self):
        '''  y   
           @return :   y   
           @type : int
        '''
        sys.stdout.write('Y=')
        yRet = input()
        while yRet=='' or (False == self.isNumber(yRet))\
             or 0>int(yRet) or int(yRet)>self.LINE:
            print('Wrong number!(please input 0-7)') 
            sys.stdout.write('Y=')
            yRet = input()
        return int(yRet)
    
    
    def isNumber(self,strVal):
        '''      
              ,          
           @param :         
           @type : str
        ''' 
        
        nums = string.digits
        for i in strVal:
            if i not in nums:
                return False
        return True
    

    def checkMine(self,xPos,yPos):
        '''          
           0-    ;1-   ;2-    
           @param 1: x  
           @type : int
           @param 2: y  
           @type : int
           @return : 0-    ;1-   ;2-    
           @rtype : int
        ''' 

        if 0 == self.xy_list[xPos][yPos]:
            self.xy_list[xPos][yPos] = 2
            return 0
        elif 2 == self.xy_list[xPos][yPos]:
            return 2
        else:
            return 1
        
                
    def play(self):
        '''      
        '''

        self.display(1)
        self.SCORE = 0
        self.iniData()
        #print self.xy_list
        
        while(1):
            x = self.getX() 
            y = self.getY()
            while(2 == self.checkMine(x,y)):
                print('values of x,y had inputed,please input new values!')
                x = self.getX()
                y = self.getY()
            if 1 == self.checkMine(x,y):
                self.end()
                break
            else:
                self.display(2)
               #self.SCORE = self.SCORE + 1
                
            
    def end(self):
        '''      
        '''
        
        self.display(3)
        print ('+======================+')
        print ('+      Game Over       +')
        print ('+======================+')
        print ('   Your score is: %d    '%self.SCORE)
        

  
    
    def display(self,kind):
        '''      
           @param:1-  ;2-  ;3-  
           @type:int
        '''
        
        if kind==1:
            print ('+======================+')
            print ('+      Game Start      +')
            print ('+======================+')
            print ('*-----------------*')
            for i in range(self.LINE):
                print ('| 1 1 1 1 1 1 1 1 |')
            print ('*-----------------*')
            print ('Please input values of x,y(0-7):')
        elif kind==2:

            self.SCORE = self.SCORE + 1
            print ('*-----------------*')
            for i in range(self.LINE):
                sys.stdout.write('| ')
                for k in range(self.ROW):
                    if 2 == self.xy_list[i][k]:
                        count = self.searchRound(i, k)
                        strCount = '%d' %count
                        sys.stdout.write(strCount + ' ')
                    else:
                        sys.stdout.write('? ')
                print('|')
            print('*-----------------*')
            print('Please input values of x,y(0-7):')           
        else:
            #              
            print('*-----------------*')
            for i in range(self.LINE):
                sys.stdout.write('| ')
                for k in range(self.ROW):
                    if 2 == self.xy_list[i][k]:
                        count = self.searchRound(i, k)
                        strCount = '%d' %count
                        sys.stdout.write(strCount + ' ')
                    elif 1== self.xy_list[i][k]:
                        sys.stdout.write('X ')
                    else:
                        count = self.searchRound(i, k)
                        strCount = '%d' %count
                        sys.stdout.write(strCount + ' ')
                print('|')
            print('*-----------------*')


if __name__ == '__main__':
    '''   
    '''

    ms = MineSweeping()
    while(1):
        ms.play()
        print('
----------------------------------------------') print('Quit game press \'q\',otherwise press other key!') print('----------------------------------------------') inputVal = input() if 'q' == inputVal: break