> 文档中心 > 大富翁(简版Python)

大富翁(简版Python)

大富翁游戏(简版Python代码
这是自己做着玩的,有兴趣的朋友可以试着玩,有些地方偷懒了,请多多包涵
地图是一个CSV文件(Map.csv:有些地图参数需要去阅读代码后可以理解)
内容如下:
0,Start,0,0,0,3
1,Postmonth St,1,100,1000,0
2,Charless St,1,1000,1500,0
3,M23,1,1030,2000,0
4,Mount Waverley Police Station,2,0,0,-1
5,Mummery St,1,1000,200,0
6,Willam St,1,2000,1500,0
7,Mount Waverley Shopping Center,4,0,0,2
8,Bruce St,1,200,10000,0
9,Gallery PI,1,2000,200,0
10,Valley Rd,1,9000,100,0
11,Petrol Station,3,0,0,1
12,Will Ave,1,1000,1000,0
13,Bond St,1,4000,5000,0
14,Ophir Rd,1,2200,1000,0
15,Walmarie Dr,1,2000,3000,0
16,Waverley Private hospital,2,0,0,-1
17,The Hwy,1,100,200,0
18,Saving Ave,1,3000,500,0
19,Genoa Ct,1,4000,4000,0

import randomimport csvnum=random.randint(1,6)def map_load(): #loading the game map from the map file(Map.csv)  MapList=[]  with open('Map.csv','r') as csvFile:    reader = csv.reader(csvFile)    #print(reader)    for row in reader:      Map_detail=row+['--','0']      MapList.append(Map_detail)  csvFile.close()  return MapListmapView=map_load()def CreatUser(UserNum):# Create the players  b=[]  for i in range(UserNum):    b.append([i,0,10000,0])  return bdef SpecEx(Statu):#In the special area, the player will follow the area order that move front or back to another area, or stop one turn  if(Statu=='-1'):    print("you will stop on one turn!")    return [0,-1]  elif (Statu=='2'):    luck=input("Try to get your Lucky(Y or N):")    if(luck=='Y'):      num=random.randint(1,6)      if(num%2==0): result=random.randint(1,6) print("You are luck! Keep moving: "+str(result)+" Step") return [result,0]      else: result=random.randint(1,6) print("Oh! Too bad You need back: "+str(result)+" Steps") return [-result,0]    else:      return [0,0]  else:    result=random.randint(1,6)    print("You are luck! Keep moving: "+str(result)+" Step")    return [result,0]def moveplayer_mapCheck(Num,i):# To public area, the player will decide to pay the money for buying the area, building house, loaning the area to pass baced on the different situation  if mapView[Num][5]=='0':    if mapView[Num][6]=='--':      print("Locate: "+mapView[Num][1]+" Area Price/Building Price: "+mapView[Num][3]+"/"+mapView[Num][4])#For make sure the situation of this area (The Area Price and Building Price)      yn=input("Do you want to buy this area(Y or N):")      if yn=='Y' and Player[i][2]>=int(mapView[Num][3]): mapView[Num][6]=str(i) Player[i][2]=Player[i][2]-int(mapView[Num][3])      elif yn=='Y' and Player[i][2]<int(mapView[Num][3]): print("Sorry you haven't enoug money!")      else: pass    elif mapView[Num][6]==str(i):      if(mapView[Num][7]=='0'): print("Locate: "+mapView[Num][1]+" Area Price/Building Price: "+mapView[Num][3]+"/"+mapView[Num][4]) Byn=input("Do you want to bulid house(Y or N):") if Byn=='Y' and Player[i][2]>=int(mapView[Num][4]):   Player[i][2]-=int(mapView[Num][4])   mapView[Num][7]='1' elif Byn=='Y' and Player[i][2]<int(mapView[Num][4]):   print("Sorry you haven't enoug money!") else:   pass    else:      print("Locate: "+mapView[Num][1]+" Area Price/Building Price: "+mapView[Num][3]+"/"+mapView[Num][4])      try_paid=input("Paid loan(L) or Paid the area(A):")      if try_paid=='A' and Player[i][2]>=(int(mapView[Num][3])+int(mapView[Num][4]))*1.1 and mapView[Num][7]=='1':  mapView[Num][7]='0' Player[i][2]-=(int(mapView[Num][4])+int(mapView[Num][4]))*1.1 Player[int(mapView[Num][6])][2]+=(int(mapView[Num][4])+int(mapView[Num][4]))*1.1 mapView[Num][6]=str(i) print("Paid is successful!")      elif try_paid=='A' and Player[i][2]>=int(mapView[Num][3])*1.1 and mapView[Num][7]=='0': Player[i][2]-=int(mapView[Num][3])*1.1 Player[int(mapView[Num][6])][2]+=int(mapView[Num][3])*1.1 mapView[Num][6]=str(i) print("Paid is successful!")      elif Player[i][2]>=(int(mapView[Num][3])+int(mapView[Num][4]))*0.01 and mapView[Num][7]=='1': Player[i][2]-=(int(mapView[Num][3])+int(mapView[Num][4]))*0.01 Player[int(mapView[Num][6])][2]+=(int(mapView[Num][3])+int(mapView[Num][4]))*0.01      elif Player[i][2]>=int(mapView[Num][3])*0.01 and mapView[Num][7]=='0': Player[i][2]-=int(mapView[Num][3])*0.01 Player[int(mapView[Num][6])][2]+=int(mapView[Num][3])*0.01      else: print("Your Game is over!!!") Player[i][3]=404  else:    res=SpecEx(mapView[Num][5])    if res[0]==0:      Player[i][3]=-1    else:      Player[i][1]=(Player[i][1]+20+res[0])%20      moveplayer_mapCheck(Player[i][1],i)#The Game begin.UserNum=int(input("Enter the user Number:"))# identify the number of the playersPlayer=CreatUser(UserNum)# Create the player setprint("-----------------Player------------------")#show the playerfor a in range(UserNum):  print("Player: "+str(Player[a][0])+" local: "+str(Player[a][1])+" Money: "+str(Player[a][2]))x=1# the variable for control whether the game is over while x==1:#Make sure whether the game is over  data_rec=0  for i in range(UserNum):    n=0    if Player[i][3]==0:      while n==0: n=input("Play "+str(i)+" :Do you roll(yes/1 or No/0):")      num=random.randint(1,6)#identify the number of the roll     Player[i][1]=(num+int(Player[i][1]))%20      moveplayer_mapCheck(Player[i][1],i)      print("-----------------Player------------------")#show the location of the player in the map      for i in range(UserNum): print("Player: "+str(Player[i][0])+" local: "+str(Player[i][1])+" Money: "+str(Player[i][2]))      print("---------------MAP--------------------")#show the map      for i in range(20): if(mapView[i][5]=='0'):   print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Area Price/Building Price: "+mapView[i][3]+"/"+mapView[i][4]+" Belonger: "+mapView[i][6]+" is_built :"+mapView[i][7]) elif(mapView[i][5]=='-1'):   print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Please wait one turn") elif(mapView[i][5]=='1'):   print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Please roll again") elif(mapView[i][5]=='2'):   print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Good luck") else:   print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Starting")      print("_____________________________________")    elif Player[i][3]==-1:      Player[i][3]=0    else:      pass  for i in range(UserNum):    if Player[i][3]==404:      data_rec+=1  if UserNum==data_rec+1:#make sure the game is over. Just one player is survive.    x=0    for i in range(UserNum):#make sure that who is Winner      if Player[i][3]!=404: print("Player "+str(i+1)+"You are Winner!") break

哈尔滨心理咨询网