程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use a simple algorithm to realize hunt the Wumpus Python games in two hours

編輯:Python

“Hunt the Wumpus” What is it? ?

quote wiki Encyclopedias :

Hunt the Wumpus yes Gregory Yob On 1973 A text-based Adventure games . In the game , Players walk through a series of connected caves , These caves are arranged as the apex of the dodecahedron , They hunted a man named Wumpus The monster of . In turn based games, players try to avoid deadly bottomless holes and “ Super bat ”, Will move the cave system around them ; The goal is to launch them through the cave “ Curved arrow ” One to kill Wumpus.

Our goal

The original work is more complicated , Here we make the following simplification :

  1. The original dodecahedron can be expanded to possess 20 vertices ( cave ) The map of . We simplify it to simpler N*N Rectangular map . Each point represents a cave . Tentatively 5 x 5 common 25 It's a cave .
  2. Every cave ( spot ) Up, down, left and right cross ( Non Bada )🤪
  3. Only one arrow , That is, there is only one chance to hit . If you miss and fail to eliminate the monster, you will fail .
  4. If you accidentally fall into the monster hole, it is also a failure .
  5. The map will not appear Bottomless pit , Super bat And other elements .
  6. Hidden information , Monsters' caves are invisible , When approaching a grid distance from the monster cave , Will prompt “ You smell the smell of umbas ”. It is only displayed when victory or defeat .

Finished product display

Though sparrows are small , But the five internal organs are complete ! First, let's take a look at the final effect , Duotu kills cats ~

Because of a miscalculation , Entering the monster's cave directly leads to failure : Because there is only one arrow , The wrong direction of archery leads to the survival and failure of monsters : Rely on intelligence , Succeed in defeating monsters , The outcome of victory ️

key technology

1. Welcome interface and start menu

This is of course the simplest part , Use character string Output print A welcome message or game help message . Then receive the user's choice input, And make judgments according to different conditions if

def menu():
print("=== Welcome to the world of umbas ===")
ch = input("1. Start \n2. sign out \n->")
if ch == "1":
begin()
if ch == "2":
print(" ok , See you next time ")

2. Map generation .

Of course, the map is the most important part , There are many ways to do it , Here we use the simplest and easiest to understand List nesting ( Two dimensional array ).

def create_map(x=5, y=5):
# Generate maps 
yj = []
for _ in range(y):
xi = []
for _ in range(x):
xi.append(SPACE)
yj.append(xi)
wumpas_local, player_local = create_player_local(x, y)
yj[wumpas_local[0]][wumpas_local[1]] = WUMPUS
yj[player_local[0]][player_local[1]] = PLAYER
global p_local
p_local = [player_local[0], player_local[1]]
return yj

Players and monsters coordinate namely Two dimensional array The subscript , Generate using random numbers . Here is Be careful Slightly ! The random coordinates of players and monsters may coincide ! There are many ways to avoid the repetition of random numbers , Use here A recursive algorithm Generate unique coordinates . First, randomly generate two coordinates , If the coordinates are the same, call the function that generates the coordinates again , Until two different coordinates are generated .

def random_local(x, y) -> tuple:
""" Randomly generate coordinates """
return random.randint(0, x - 1), random.randint(0, y - 1)
def create_player_local(x, y) -> tuple:
""" Generate unique coordinates for monsters and players wumpas_local Monster coordinates player_local Player coordinates """
wumpas_local = random_local(x, y)
player_local = random_local(x, y)
if wumpas_local == player_local:
wumpas_local, player_local = create_player_local(x, y)
if wumpas_local != player_local:
return (wumpas_local, player_local)

3. Map display

The map has been generated , The display is very simple ! Simply replace the string of the map according to the winning or losing status .

  1. Victory character effect

  1. Failed character effect

4. Displacement , Breath and archery

Player displacement , Umbas' breath and archery logic are the same . The essence is to deal with the coordinates of the four directions of the lattice ( Breath needs to deal with the surrounding eight grids ).

Ha , It only uses addition and subtraction within two ( Add one , Minus one ), Easy ? what ? Have been dizzy ? Get a pen and paper and draw ~.

def smell(x, y) -> bool:
""" Judge whether there are monsters nearby """
x1 = MAP_X - 1 if p_local[0] == 0 else p_local[0] - 1
x2 = 0 if p_local[0] == MAP_X - 1 else p_local[0] + 1
y1 = MAP_Y - 1 if p_local[1] == 0 else p_local[1] - 1
y2 = 0 if p_local[1] == MAP_Y - 1 else p_local[1] + 1
round = [(x1, y), (x2, y), (x, y1), (x, y2), (x1, y1), (x1, y2), (x2, y1), (x2, y2)]
for r in round:
if MAP[r[0]][r[1]] == WUMPUS:
return True
return False

5. Da Yuanman

Yes , The whole game is actually a state machine , All in all victory , Failure , In the game Three states . Then we can use a while sentence , Start a The event loop . You can always play happily ! Happy or not ?

Although this program has no complex algorithm , But it's easy to use The order , Judge , loop Sentence and yidudu A recursive algorithm You can realize a little complicated Games .

As the saying goes : The greatest truths are the simplest A heavy sword has no edge It's a coincidence

reflection

This game also has a lot of expandable content , Here are a few thinking questions , Make the game more fun ~

  1. What other ways can random coordinate de duplication be achieved ?
  2. Join in Bottomless pit and Super bat Elements .
  3. Join in Game points , Game timing And Game archive function .

See source code : github.com/spaceack/Hu… Or pay attention to the official account 【 The dance of programming 】 reply wumpus receive .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved