[ create a new paste ] login | about

Link: http://codepad.org/mAqZ0zcj    [ raw code | fork ]

Python, pasted on Dec 9:
#This program will simulate a game of tennis.
from random import random
from Main import *
server=1
def play(player):
    global server
    #determine who starts serving
    while server > 0:
    	if server%2==0:
    		serve=player[0][0]
    	else:
    		serve=player[1][0]
    	print "%s is now serving" %(serve)
    	scoreCount=[15,30,40,41]
    	player0=player1=count0=count1=i=j=0
    	while player0<=40 and player1 <=40:
    		if serve==player[0][0]:
    			#SERVES AND RETURNSERVES
    			if player[0][1]>player[1][2]:
    				count0=count0+1
    			elif player[0][1]<player[1][2]:
    				count1=count1+1
    			else:
    				if random() > 0.5:
    					count0=count0+1
    				else:
    					count1=count1+1
    			#STROKES
    			if player[0][3]>player[1][3]:
    				count0=count0+1
    			elif player[0][3]<player[1][3]:
    				count1=count1=1
    			else:
    				if random() > 0.5:
    					count0=count0+1
    				else:
    					count1=count1+1
    			#FITNESS
    			if player[0][4] > player[1][4]:
    				count0=count0+1
    			elif player[0][4]<player[1][4]:
    				count1=count1+1
    			else:
    				if random() > 0.5:
    					count0=count0+1
    				else:
    					count1=count1+1
    			
    			if count0>count1:
    				player0=scoreCount[i]
    				i=i+1
    			elif count0<count1:
    				player1=scoreCount[j]
    				j=j+1
    			else:
    				if random()>0.5:
    					player0=scoreCount[i]
    					i=i+1
    				else:
    					player1=scoreCount[j]
    					j=j+1
    			if player0<=40 and player1<=40:
    				print "%s:%d - %d:%s" %(player[0][0],player0,player1,player[1][0])
    	 	else:
    	 		#SERVE AND RETURN OF SERVE FOR OTHER PLAYER
    			if player[0][2]>player[1][1]:
    				count0=count0+1
    			elif player[0][2]<player[1][1]:
    				count1=count1=+1
    			else:
    				if random() > 0.5:
    					count0=count0+1
    				else:
    					count1=count1+1
    			#STROKES
    			if player[0][3]>player[1][3]:
    				count0=count0+1
    			elif player[0][3]<player[1][3]:
    				count1=count1=1
    			else:
    				#print equal
    				if random() > 0.5:
    					count0=count0+1
    				else:
    					count1=count1+1
    			#FITNESS
    			if player[0][4] > player[1][4]:
    				count0=count0+1
    			elif player[0][4]<player[1][4]:
    				count1=count1+1
    			else:
    				#print equal
    				if random() > 0.5:
    					count0=count0+1
    				else:
    					count1=count1+1
    			#calculating if point was won.
    			if count0>count1:
    				player0=scoreCount[i]
    				i=i+1
    			elif count1>count0:
    				player1=scoreCount[j]
    				j=j+1
    			else:
    				if random()>0.5:
    					player0=scoreCount[i]
    					i=i+1
    				else:
    					player1=scoreCount[j]
    					j=j+1
    			if player0<=40 and player1<=40:
    				print "%s:%d - %d:%s" %(player[0][0],player0,player1,player[1][0])
    	
    		if player0>40:
    			print "%s has taken the next point and won the game" %(str(player[0][0]))
    		elif player1>40:
    			print "%s has taken the next point and won the game" %(str(player[1][0]))
    	
    	games(player0,player1)
#Counting the Number of games
game0=game1=0
def games(x,y):
    if x>y:
       global game0
       game0=game0+1
    else:
     	global game1
        game1=game1+1
    print player[0][0],game0,"-",game1,player[1][0]
    sets(game0,game1)

#check whether set has been completed
setScore=["","",""]
A=B=0
def sets(x,y):
    global game0
    global game1
    global A,B
    
    if x >=6 and x-y>=2: #check whether set conditions are met.
        print player[0][0], " has taken the set", x,"/",y
        A=A+1
        score0=str(x)+"/"+str(y)
        setScore[A+B]=score0  
        game0=game1=0 #sets all game values to 0, and begins a new set.
    elif y>=6 and y-x>=2:
        print player[1][0], "has taken the set", x,"/",y
        B=B+1
        score1=str(x)+"/"+str(y)
        setScore[B+A]=score1 #store the score in an array.
        game0=game1=0
    x=y=0 #sets a set to 0
    match(A,B,setScore)

#check who has won the match.
def match(x,y,z):
    global server
    if x>=2:
        print player[0][0], "has won by",x,"sets to",y
        print "The final score is", z
        A=B=0
        check=playOn(x)
        if check=="true":
             server=server+1
        else:
             server=0
             newGame()
    elif y>=2:
        print player[1][0], "has won by", y,"sets to",x
        print "the final score is", z
        A=B=0
        check=playOn(y)
        if check=="true":
            server=server+1
        else:
            server=0
            newGame()
        

#Check whether player whould like to continue.
def playOn(x):
    if x>=2:
        return "false"
    else:
        return "true"


def newGame():
    new=raw_input("New Game y/n: ")
    if new=='y':
        main()
    elif new=='n':
        main()
    else:
        print "Error, Wrong option"
        newGame()

#player=[["roger",10,10,10,10],["sam",1,2,3,4]]
#play(player)


Create a new paste based on this one


Comments: