Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Assignment Help !!

jirach_gagjirach_gag Posts: 3Member
Problem:
The given source code is a program that has four threads reacting to each other by checking and changing global variables. Some parts of the program are intentionally left blank. You will need to fill in some thing in the parts that have ? to make the program working. Additionally, you have to modify the program such that after all players have done 100 hits (in total), the program can report that how many hit each player did. This wont be 25 hits each because we have a random direction involving here.

(Hint#1 : you may add another thread to count and keep track of who/how many hits or you may use global variables to help you keep track of this)

(Hint#2 : import sys and sys.exit( ) can be used to exit your program)



------------------------------------------
and here is a program I already fill in the ? , but please
help me on how to count it !!
ps. I have try something but it not working
------------------------------------------




#----------- HEADER -------------
import time
import thread
import random
import sys
#--------------------------------

#--------- Global Variable ------
ballCourt = 0
ballDirection = 'L'
L0Hits = 0
L1Hits = 0
R0Hits = 0
R1Hits = 0
count = 0
#--------------------------------

#---------- Declare afunction ---------
def togg(direction):

global ballCourt
global ballDirection

# Change global parameter
ballDirection = direction

# Change another global parameter
if(ballCourt==0):
ballCourt=1
elif(ballCourt==1):
ballCourt=0
#---------------------------------------

def count(name):
global L0Hits
global L1Hits
global R0Hits
global R1Hits

if(strcmp(name,"0L")):
L0Hits+=1
elif(strcmp(name,"1L")):
L1Hits+=1
elif(strcmp(name,"0R")):
R0Hits+=1
else:
R1Hits+=1


#-------------------------------------- Declare afunction -------------------------
def TPlayer(PName,PSide,balltime,lock):
while 1:
global ballCourt
global ballDirection
global count

if(count==10):
sys.exit();
# Check if the ball is in your court and your side
if ((str(PName)==str(ballCourt))and(str(PSide)==str(ballDirection))):
lock.acquire()
count+=1
if(random.random()> 0.5 ):
returnDirection = 'L'
else:
returnDirection = 'R'

# throw it back
print str(PName) + str(PSide) +' throw the ball out to ' + str(returnDirection)

# wait until the ball reach the other side, in your opponent's hands
time.sleep(balltime)

# update ball location about courtSide and direction
togg(returnDirection)
lock.release()

else:
# if the ball is not in your court, relax a little bit
# you do not need to keep watching! takes too much resource
time.sleep(1)


#-------------------------------------------------------------------------------------------------------

#------------------ main() -------------------------
if __name__=="__main__":
lock=thread.allocate_lock()
thread.start_new_thread(TPlayer,("0","L",.1,lock,))
thread.start_new_thread(TPlayer,("0","R",.1,lock,))
thread.start_new_thread(TPlayer,("1","L",.1,lock,))
thread.start_new_thread(TPlayer,("1","R",.1,lock,))
#----------------------------------------------------
Sign In or Register to comment.