python简单版斗兽棋

纯粹练手哈,简单的实现了人机对战, 吃光为胜。电脑随机走棋= =

image

jungle.py:

#_*_coding:utf-8_*_ 
''' 
Created on 2012-3-27 
简单版斗兽棋 
@author: myter7 
''' 
from animal import Animal 
from board import Board 
import random 
import traceback 
from compiler.ast import For

class Jungle: 
    def __init__(self): 
        #初始化本方动物 
        self.animals = [Animal("e", "象", ["t", "c"], 1, 1), Animal("t", "虎", ["c", "m"], 1, 2), Animal("c", "猫", ["m"], 2, 2), Animal("m", "鼠", ["e"], 2, 1)] 
        #初始化对方动物 
        self.other_animals = [Animal("e", "象", ["t", "c"], 1, 5), Animal("t", "虎", ["c", "m"], 1, 6), Animal("c", "猫", ["m"], 2, 6), Animal("m", "鼠", ["e"], 2, 5)] 
        #初始化棋盘 
        self.board = Board(4, 8) 
   

    def get_animal(self, id, animals): 
        for animal in animals: 
            if animal.id == id: 
                return animal 
        
    def main(self): 
        directions = ("u", "d", "l", "r") 
        animal_ids = ("e", "t", "c", "m") 
       
        while len(self.animals) > 0 and len(self.other_animals) > 0: 
            
            #print "你还存活%s只动物" %len(self.animals) 
            #print "对方还存活%s只动物" %len(self.other_animals) 
            print "==================^ ^简单版斗兽棋, 吃光为胜^ ^==================" 
            self.board.show(self.animals + self.other_animals) 
            print "==========================================================="
            
            try: 
                #选择动物 
                animal_id = raw_input("操作动物:   e:象    t:虎    c:猫    m:鼠   请输入:") 
                animal = self.get_animal(animal_id, self.animals) 
                other_animal = self.get_animal(animal_ids[random.randint(0, 3)], self.other_animals) 
                #other_animal = self.get_animal(animal_ids[3], self.other_animals) 
                
                while animal == None: 
                    print "你选择的动物已经死亡或者不存在, 请重新选择!" 
                    animal_id = raw_input("操作动物:   e:象    t:虎    c:猫    m:鼠   请输入:") 
                
                while other_animal == None: 
                    other_animal = self.get_animal(animal_ids[random.randint(0, 3)], self.other_animals) 
                    
                #选择方向 
                direction = raw_input("移动到:   u:上    d:下    l:左    r:右   请输入:") 
                other_direction = directions[random.randint(0, len(directions) - 1)] 
                #other_direction = directions[0] 
                
                #print "你 向  【%s】 移动了【%s】" % (direction, a.name) 
                #print "对方 向  【%s】 移动了【%s】" % (otherDirection, other.name)                              
                
                #移动动物 
                animal.move(direction, self.animals, self.other_animals, self.board) 
                other_animal.move(other_direction, self.other_animals, self.animals, self.board) 
            except Exception, e: 
                print "请输入正确的数字!", e 
                exstr = traceback.format_exc() 
                print   exstr


if __name__ == "__main__": 
    jungle = Jungle() 
    jungle.main()

animal.py:

#_*_coding:utf-8_*_ 
''' 
Created on 2012-3-27

@author: 11 
''' 
class Animal: 
    def __init__(self, id, name, food, x, y): 
        self.id = id 
        self.name = name 
        self.food = food 
        self.x = x 
        self.y = y 
    #吃  
    def eat(self, other): 
        if other.id in self.food: 
            other.status = False 
            print "%s吃掉了%s,味道还不错~" % (self.name, other.name) 
            return True 
        else: 
            print "%s太硬了,不能吃哦~" % other.name 
            return False 
        
    #移动之后的处理 
    def handler(self, x, y, animals, other_animals, board): 
        for a in other_animals: 
                if x == a.x and y == a.y: 
                    if self.eat(a): 
                        self.x = x 
                        self.y = y 
                        other_animals.remove(a) 
                    else: 
                        return 
                        
        else: 
            if(board.check_move(x, y, animals)): 
                self.x = x 
                self.y = y 
            else: 
                print "不能移动到该位置^ ^" 
          
    #动物移动          
    def move(self, direction, animals, other_animals, board): 
        flag = True 
        x = self.x 
        y = self.y 
        
        if direction == 'u': 
            y = y - 1 
        elif direction == 'd': 
            y = y + 1 
        elif direction == 'l': 
            x = x - 1 
        elif direction == 'r': 
            x = x + 1 
        else: 
            flag = False 
            print "输入方向错误" 
            
        if flag: 
            self.handler(x, y, animals, other_animals, board)

               
       

board.py:

# _*_ coding:utf-8_*_ 
''' 
Created on 2012-3-27

@author: 11 
''' 
class Board: 
    def __init__(self, w, h): 
        #棋盘高 
        self.h = h 
        #棋盘宽 
        self.w = w 
        #棋盘坐标集合 
        p = [] 
        for y in range(h): 
            for x in range(w): 
                p.append((x, y)); 
        self.board = tuple(p) 
        
    #检查当前坐标是不是动物 
    def check_is_animal(self, x, y, animals): 
        for a in animals: 
                if a.y == y and a.x == x: 
                    return a.name 
    
    #检查棋子是否可以移动到目标位置 
    def check_move(self, x, y, animals): 
        for a in animals: 
                if x == a.x and y == a.y: 
                    return False 
                
        if((x, y) not in self.board): 
            return False 
        return True 
                
    #显示棋盘            
    def show(self, animals): 
        for y in range(self.h): 
            if y == self.h/2: 
                print 
            for x in range(self.w): 
                aname = self.check_is_animal(x, y, animals); 
                if aname: 
                    print " "*18, aname, 
                    #print aname, "(", x, y, ")", 
                else: 
                    print " "*18, '口', 
                    #print '口', "(", x, y, ")", 
            print '\n',

转载于:https://my.oschina.net/myter7/blog/57674

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值