|
- ## This program is free software; you can redistribute it and/or modify
- ## it under the terms of the GNU General Public License as published by
- ## the Free Software Foundation; either version 2 of the License, or
- ## (at your option) any later version.
- ##
- ## This program is distributed in the hope that it will be useful,
- ## but WITHOUT ANY WARRANTY; without even the implied warranty of
- ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ## GNU General Public License for more details.
- ##
- ## You should have received a copy of the GNU General Public License
- ## along with this program; if not, write to the Free Software
- ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- ## Welcome to [email]rujingli@163.com[/email] with any suggestion
- import random,sys
- #paneldef is the pan def.
- #is 121 structs of
- # 0=(x,y)=pos
- # 1=(n1,n2...n6)=ML,MR,TR,BL,TL,BR M=middle L=Left R=Right T=Top B=buttom
- # 2=curvalue 0=Empty 1..6=Group1..Group6
- paneldef = [[] for i in range(121)]
- Battle=[0 for i in range(121)]
- MyGroup = [0,1,2,3,4,5,6,7,8,9]
- YourGroup = [111,112,113,114,115,116,117,118,119,120]
- Dirs={'ML':0,'MR':1,'TR':2,'BL':3,'TL':4,'BR':5}
- MaxFindWay=100
- #*********The code below used to create the pan def********************
- def CreatePan():
- ad=[1,2,3,4,13,12,11,10,9,10,11,12,13,4,3,2,1]
- curnum=0
- for n in range(0,17):
- if ad[n] % 2 == 0:
- for z in range(0,ad[n]):
- X= 13+(z-ad[n]/2)*2+1;
- paneldef[curnum].append([(X,n),[255,255,255,255,255,255]])
- curnum=curnum+1
- else :
- for z in range(0,ad[n]):
- X= 14 - ad[n] + 2 * z;
- paneldef[curnum].append([(X,n),[255,255,255,255,255,255]])
- curnum=curnum+1
- return
- #************************************************************************
- ##Function Used to print the pan state
- def PrintPanel(abattle):
- strs=[]
- for n in range(0,17):
- strs[1:1]=[' ']
- for n in range(0,121):
- strs[paneldef[n][0][0][1]]= strs[paneldef[n][0][0][1]][:paneldef[n][0][0][0]] + chr(abattle[n]) + strs[paneldef[n][0][0][1]][paneldef[n][0][0][0]+1:]
- for n in range(0,17):
- print strs[n]
- return
- ##Function Use to Calc the ML..BR index***************
- def CalcRelation():
- for index in range(0,121):
- for ckindex in range(0,121):
- #check ML
- if (paneldef[ckindex][0][0][0] == paneldef[index][0][0][0] - 2) and (paneldef[ckindex][0][0][1] == paneldef[index][0][0][1]) :
- paneldef[index][0][1][0] = ckindex
- #check MR
- if (paneldef[ckindex][0][0][0] == paneldef[index][0][0][0] + 2) and (paneldef[ckindex][0][0][1] == paneldef[index][0][0][1]) :
- paneldef[index][0][1][1] = ckindex
- #check TR
- if (paneldef[ckindex][0][0][0] == paneldef[index][0][0][0] + 1) and (paneldef[ckindex][0][0][1] == paneldef[index][0][0][1] - 1) :
- paneldef[index][0][1][2] = ckindex
- #check BL
- if (paneldef[ckindex][0][0][0] == paneldef[index][0][0][0] - 1) and (paneldef[ckindex][0][0][1] == paneldef[index][0][0][1] + 1) :
- paneldef[index][0][1][3] = ckindex
- #check TL
- if (paneldef[ckindex][0][0][0] == paneldef[index][0][0][0] - 1) and (paneldef[ckindex][0][0][1] == paneldef[index][0][0][1] - 1) :
- paneldef[index][0][1][4] = ckindex
- #check BR
- if (paneldef[ckindex][0][0][0] == paneldef[index][0][0][0] + 1) and (paneldef[ckindex][0][0][1] == paneldef[index][0][0][1] + 1) :
- paneldef[index][0][1][5] = ckindex
- return
- #******************************************************
- ##Function used to Get Relation of one***************
- def relation(Index):
- return paneldef[Index][0][1]
- ##******************************************************
- ##Function used to init one battle*************************************
- def initBattle():
- for n in range(0,10):
- Battle[n] = ord('*')
- for n in range(10,111):
- Battle[n] = ord('-')
- for n in range(111,121):
- Battle[n] = ord('0') + n - 111
- MyGroup = [0,1,2,3,4,5,6,7,8,9]
- YourGroup = [111,112,113,114,115,116,117,118,119,120]
- return
- ##********************************************************************
- def DirValue(index,dir):
- if Dirs.has_key(dir):
- return paneldef[index][0][1][Dirs[dir]]
- else:
- return -1
- def Move(index,dir,abattle):
- if Dirs.has_key(dir):
- dv = DirValue(index,dir)
- if dv != 255:
- if chr(abattle[dv]) in ('~','%','#','$','a','-','='):
- return (False,DirValue(index,dir))
- else :
- dv2 = DirValue(dv,dir)
- if dv2 != 255:
- if chr(abattle[dv2]) in ('~','%','#','$','a','-','='):
- return (True,dv2)
- return (False,-1)
- def DoYouMove(cmds):
- CurIndex = YourGroup[int(cmds[0])]
- JumpHis = [CurIndex]
- abattle=[]
- abattle[0:0]=Battle
- for n in range(1,len(cmds)):
- dest = Move(CurIndex,cmds[n],abattle)
- if dest[1] != -1 and (dest[0] or (n==1)):
- JumpHis[len(JumpHis):len(JumpHis)] = [dest[1]]
- CurIndex = dest[1]
- abattle[dest[1]] = ord(cmds[0][0])
- abattle[JumpHis[len(JumpHis)-2]] = ord('-')
- else :
- if cmds[n].strip() == "":
- continue
- print 'Bad Cmd:Unknown Keyword "%s" or Can not jump to "%s". \nUse "H" to help.' % (cmds[n],cmds[n])
- return False
- for n in range(0,len(JumpHis)):
- if n==0 :
- Battle[JumpHis[n]] = ord('~')
- elif n == len(JumpHis)-1 :
- Battle[JumpHis[n]] = ord(cmds[0][0])
- YourGroup[int(cmds[0])] = JumpHis[n]
- else :
- Battle[JumpHis[n]] = ord('-')
- return True
- # this is a loopback function to get all possible move function
- def TestMove(Index,Ret,RetIndex,abattle):
- for j in range(0,6):
- if Ret[0]>=MaxFindWay:
- return
- dest = Move(Index,Dirs.keys()[j],abattle)
- if dest[1] != -1:
- if RetIndex == -1:
- Ret[1][Ret[0]].append(Index)
- Ret[1][Ret[0]].append(dest[1])
- else:
- if dest[0] and (not (dest[1] in Ret[1][RetIndex])):
- Ret[1][Ret[0]][0:0] = Ret[1][RetIndex];
- Ret[1][Ret[0]].append(dest[1])
- else :
- continue
- Ret[0]=Ret[0]+1
- if Ret[0]>=MaxFindWay:
- return
- else:
- if dest[0]:
- TestMove(dest[1],Ret,Ret[0]-1,abattle)
- return
- def RateValueX(X):
- absv = abs(X - 13);
- if absv<5:
- return 0
- else:
- return absv * 9
- def RateValueY(Y):
- return Y * 10
- def MyTotalRate():
- TotalRate=0
- for n in range(0,10):
- TotalRate += (RateValueY(paneldef[MyGroup[n]][0][0][1]) - RateValueX(paneldef[MyGroup[n]][0][0][0]))
- return TotalRate
- def CheckIWin():
- for n in range(0,10):
- if MyGroup[n] < 111:
- return False
- return True
- def UpdateMyGroup(oldValue,newValue):
- for n in range(0,10):
- if MyGroup[n] == oldValue:
- MyGroup[n] = newValue
- return True
- return False
- def GetMaxRateValue(abattle,level):
- level-=1
- if level<=0:
- return MyTotalRate()
- else:
- path=[[] for i in range(MaxFindWay)]
- ret=[0,path]
- tmpbattle=[]
- tmpbattle[0:0]=abattle
- for n in range(0,10): #MyGroup
- CurIndex = MyGroup[n]
- TestMove(CurIndex,ret,-1,tmpbattle)
- MaxRateValue=0
- RateValue=0
- for n in range(0,ret[0]):
- #RateValue = MyTotalRate() #(RateValueY(paneldef[path[n][len(path[n])-1]][0][0][1]) - RateValueY(paneldef[path[n][0]][0][0][1])) - (RateValueX(paneldef[path[n][len(path[n])-1]][0][0][0]) - RateValueX(paneldef[path[n][0]][0][0][0]))
- #print 'Level %d Index %d Value %d' % (level,n,RateValue)
- tmpbattle[path[n][0]] = ord('-')
- tmpbattle[path[n][len(path[n])-1]] = ord('*')
- UpdateMyGroup(path[n][0],path[n][len(path[n])-1])
- if CheckIWin():
- UpdateMyGroup(path[n][len(path[n])-1],path[n][0])
- return sys.maxint
- RateValue = GetMaxRateValue(tmpbattle,level)
- UpdateMyGroup(path[n][len(path[n])-1],path[n][0])
- tmpbattle[path[n][0]] = ord('*')
- tmpbattle[path[n][len(path[n])-1]] = ord('-')
- if RateValue>MaxRateValue:
- MaxRateValue = RateValue
- return MaxRateValue
- def DoMyMove():
- path=[[] for i in range(MaxFindWay)]
- ret=[0,path]
- tmpbattle=[]
- tmpbattle[0:0]=Battle
- for n in range(0,10): #MyGroup
- CurIndex = MyGroup[n]
- TestMove(CurIndex,ret,-1,tmpbattle)
- SaveMyGroup=[]
- SaveMyGroup[0:0]=MyGroup
- MaxRateValue=0
- MinY=100
- MaxRateIndex=-1
- for n in range(0,ret[0]):
- #RateValue = (RateValueY(paneldef[path[n][len(path[n])-1]][0][0][1]) - RateValueY(paneldef[path[n][0]][0][0][1])) - (RateValueX(paneldef[path[n][len(path[n])-1]][0][0][0]) - RateValueX(paneldef[path[n][0]][0][0][0]))
- #suppose we do this change,what's the next max value we should conside
- tmpbattle[path[n][0]] = ord('-')
- tmpbattle[path[n][len(path[n])-1]] = ord('*')
- UpdateMyGroup(path[n][0],path[n][len(path[n])-1])
- if CheckIWin():
- MaxRateIndex = n
- break
- RateValue = GetMaxRateValue(tmpbattle,2)
- UpdateMyGroup(path[n][len(path[n])-1],path[n][0])
- tmpbattle[path[n][0]] = ord('*')
- tmpbattle[path[n][len(path[n])-1]] = ord('-')
-
- if RateValue>MaxRateValue:
- MaxRateValue = RateValue
- MaxRateIndex = n
- elif RateValue==MaxRateValue:
- if paneldef[path[n][0]][0][0][1] < MinY:
- MinY = paneldef[path[n][0]][0][0][1]
- MaxRateIndex = n
- MyGroup[0:] = SaveMyGroup
-
- if MaxRateIndex==-1:
- MaxRateIndex = random.randint(0,ret[0]-1)
- IndexInMyGroup=-1
- for n in range(0,10):
- if MyGroup[n] == path[MaxRateIndex][0]:
- IndexInMyGroup=n
- if IndexInMyGroup==-1:
- print 'Bad Result Path,This is internal error,report to author!'
- Finish = True
- else:
- for n in range(0,len(path[MaxRateIndex])):
- if n==0 :
- Battle[path[MaxRateIndex][n]] = ord('=')
- elif n == len(path[MaxRateIndex])-1 :
- Battle[path[MaxRateIndex][n]] = ord('m')
- MyGroup[IndexInMyGroup] = path[MaxRateIndex][n]
- else :
- Battle[path[MaxRateIndex][n]] = ord('-')
- return
- def CheckYouWin():
- for n in range(0,10):
- if YourGroup[n] > 9:
- return False
- return True
- def YourTotalRate():
- TotalRate=0
- for n in range(0,10):
- TotalRate += (RateValueY(paneldef[120-YourGroup[n]][0][0][1]) - RateValueX(paneldef[120-YourGroup[n]][0][0][0]))
- return TotalRate
- print 'Jump version 0.1 By wesley.wang at [email]rujingli@163.com[/email]'
- print '"H" for help\n'
- CreatePan()
- CalcRelation()
- initBattle()
- Finish = False
- while not Finish :
- PrintPanel(Battle)
- #print 'Your Total Rate Now :',YourTotalRate()
- #print 'My Total Rate Now :',MyTotalRate()
- if MyTotalRate()>=YourTotalRate():
- print 'HeHe...'
- else :
- print 'Oh My God!'
- #erase last round history
- for n in range(0,121):
- if chr(Battle[n]) in ('%','#','$','a','~','=') :
- Battle[n] = ord('-')
- if chr(Battle[n]) == 'm':
- Battle[n] = ord('*')
- Cmd = raw_input('\nYour Turn("Q" to quit):')
- Cmd = Cmd.upper().strip()
- if Cmd[0] == 'Q':
- print "Bye!!"
- break
- elif Cmd[0] == 'H':
- print 'the input should star with "0".."9" followed by Sequence of "ML,MR,TR,BL,TL,BR"'
- print 'ML = Middle Left'
- print 'MR = Middle Right'
- print 'TR = Top Right'
- print 'BL = Buttom Left'
- print 'TL = Top Right'
- print 'BR = Buttom Right'
- print 'For example:0 TL ML'
- else :
- cmds = Cmd.split(' ')
- if len(cmds)<2 or not ord(cmds[0][0]) in range(ord('0'),ord('9')+1) :
- print "Bad Input, 'H' for Help. Try Again!"
- else :
- if DoYouMove(cmds):
- DoMyMove()
- YouWin=CheckYouWin()
- IWin=CheckIWin()
- Finish = (YouWin or IWin)
- if Finish:
- PrintPanel(Battle)
- if (YouWin and (not IWin)):
- print 'You are so intelligent!'
- print 'You Win!'
- elif (IWin and (not YouWin)):
- print 'Sorry, You lose!'
- elif (YouWin and IWin):
- print 'Hehe, Same time!'
- raw_input('Press Return To Quit!')
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|