Today saw a long time ago in school when a pseudo-code, tells a strange story, today with Python to do exercises.
The pseudo code is as follows:
Class Monster {char monster name; int blood; monster (monster name N, blood N)/* constructor */{monster name = Monster name n; blood = Blood n;} hit function () {blood reduction;} judgment function () {if (Blood ==0) printf ("You win! "); } Play game () {while (Blood > 0) {is hit function (); judging function ();}}}
import os,random,time,threading,random,string# #定义一个monster类, the attribute has the name, the blood quantity, the method that is beaten, the Judgment Method Class monster (): def __init__ (Self, name, blood): self.name = name self.blood = blood print ' hahaha ..... I am a tiger monster ' def hit (self, n, times): self.blood = self.blood - n if self.blood < 0: self.blood = 0 print ' Round %s monster\ ' s blood - %s ' % (times, n) print ' monster\ ' s blood is %s ' % self.blood def checklife (self): if self.blood <= 0: print ' finally, you win! ' # #定义一个hunter类,Attribute has name, attack blood volume, play strange Method, rest Method Class hunter (): def __init__ (Self, name, hitblo): self.hitblo = hitblo self.name = name Def hitmonster (self): print ' i am hunter-%s , my hitnum is %s ' % (self.name, self.hitblo) print ' Ouye ... Hunter hit monster %s blood ' % self.hitblo return Self.hitblo def haverest (self): print ' Hunter %s need have a rest. ' % self.name time.sleep (Random.random ()) # #定义一个全局怪物tigerglobal mm = monster (' Tiger ', 800) numround = 1 # #定义一个计数变量, calculate the fight round # #锁lock1 = threAding. Lock () # #定义游戏流程函数, the built-in if judgment is necessary, otherwise the last fight judgment, 3 threads while is true, but the first time the blame has ended the process, after two threads wait for the lock to release, or use before the true judgment def PlayGame (Hname, hitblo): print '%s :fighting begin...\n ' %hname h = hunter (Hname, hitblo) while m.blood >0: lock1.acquire () try: if m.blood > 0: h.hitmonster () h.haverest () global numround m.hit (H.hitblo, numround) M.checklife () numround+=1 print '--------------------------------------- ---' fiNally: lock1.release () # #定义一个函数, Randomly generated hunter information Def mkobj (n): global hunters hunters = [] for i in range (0,n): strname = string.join (Random.sample ([' A ', ' B ' , ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J '], 3). Replace (" ", "") hitnum = Random.randrange (60, 151) hunters.append (Hunter (Strname,hitnum)) Mkobj (3) t0 = threading. Thread (target=playgame, args= (Hunters[0].name, hunters[0].hitblo,)) t1 = threading. Thread (target=playgame, args= (Hunters[1].name, hunters[1].hitblo,)) t2 = threading. Thread (target=playgame, args= (Hunters[2].name, hunters[2].hitblo,)) T0.start () T1.start () T2.start () T0.join () T1.join () T2.join ()
The results are pretty interesting.
Hahaha ..... I am a tiger monsterafg :fighting begin...gdh :fighting begin...cbf :fighting begin ... i am hunter-afg , my hitnum is 119ouye...hunter hit monster 119 bloodhunter afg need have a rest. Round 1 monster ' S blood - 119monster ' s blood is 681------------------- -----------------------I am hunter-gdh , my hitnum is 109ouye...hunter hit monster 109 bloodhunter gdh need have a rest. Round 2 monster ' S blood - 109monster ' s blood is 572------------------- -----------------------i am hunter-cbf , my hitnum is 64ouye...hunter Hit monster 64 bloodhunter cbf need have a rest. Round 3 monster ' S blood - 64monstEr ' s blood is 508------------------------------------------i am hunter-afg my hitnum is 119ouye...hunter hit monster 119 bloodhunter afg need have a rest. Round 4 monster ' S blood - 119monster ' s blood is 389------------------- -----------------------I am hunter-gdh , my hitnum is 109ouye...hunter hit monster 109 bloodhunter gdh need have a rest. Round 5 monster ' S blood - 109monster ' s blood is 280------------------- -----------------------i am hunter-cbf , my hitnum is 64ouye...hunter Hit monster 64 bloodhunter cbf need have a rest. Round 6 monster ' S blood - 64monster ' s blood is 216-------------------- ----------------------i am hunter-afg , my hitnum is 119ouye...hunter hit monster 119 bloodhunter afg need have a rest. Round 7 monster ' S blood - 119monster ' s blood is 97-------------------- ----------------------i am hunter-gdh , my hitnum is 109ouye...hunter Hit monster 109 bloodhunter gdh need have a rest. Round 8 monster ' S blood - 109monster ' s blood is 0finally, you win!------------------------------------------
"Python learning" a classic example of multithreading and object-oriented integration