Friday, December 2, 2016

A HandCricket Game In python

In my old post i have written an unfinished code of hand cricket game in python.That code is not finished and very much lengthy so i have decided to write a new code.Here I have finished the code you can just copy and paste on python IDLE and run it and see it works smoothly :D

Source Code
=========

import random

def batfirst(pl):
    o=0
    score=0
   
    if pl=='c':
        print'First Innings'
        print'============='
        while o==0:
            e=0
           
            while e==0:
                b=input('Enter Your Bowling choice 1-6:')
                if b>6 or b<1:
                   
                    e=0
                else:
                    r=random.randint(1,6)
                    e=1
           
            if b==r:
                o=1
                print 'computer used ',r
                print 'computer is out!'
                print 'score:',score
                break
            else:
                print 'computer scored ',r
                score=score+r
                print 'Score:',score
        return score
    else:
        print'First Innings'
        print'============='
        while o==0:
            e=0
            while e==0:
                r=input('Enter Your Batting choice 1-6:')
                if r>6 or r<1:
                   
                    e=0
                else:
                    e=1
            b=random.randint(1,6)
            if b==r:
                o=1
                print 'computer used ',b
                print 'You are out!'
                print 'score:',score
                break
            else:
                print 'computer used ',b
                score=score+r
                print 'Score:',score
        return score
def batsec(pl,sc):
    o=0
    score=0
    if pl=='c':
        print'Second Innings'
        print'=============='
        while o==0 and score<=sc:
            e=0
            r=random.randint(1,6)
            while e==0:
                b=input('Enter Your Bowling Choice 1-6:')
                if b>6 or b<1:
                   
                    e=0
                else:
                    e=1
            if b==r:
                o=1
                print 'computer scored ',r
                print 'computer is out!'
                print 'score:',score
                if score<sc:
                    print 'Computer loss by ',sc-score,' runs'
                elif score==sc:
                    print 'Match Drawn!'
                else:
                    continue
            else:
                score=score+r
                print 'Score:',score
                print (sc-score)+1,' runs to win!'
        if score>sc:
            print 'computer win!'
    else:
        print'Second Innings'
        print'=============='
        while o==0 and score<=sc:
            e=0
            while e==0:
                r=input('Enter Your Batting Choice 1-6:')
                if r>6 or r<1:
                    raise ValueError,'number Should Be between 1-6'
                    e=0
                else:
                    e=1
            b=random.randint(1,6)
            if b==r:
                o=1
                print 'computer used ',b
                print 'You are out!'
                print 'score:',score
                if score<sc:
                    print 'You loss by ',sc-score,' runs'
                elif score==sc:
                    print 'Match Drawn!'
            else:
                score=score+r
                print 'Score:',score
                print (sc-score)+1,' runs to win!'
        if score>sc:
            print 'You win!'
pl='y'
while pl=='y' or pl=='Y':
    print 'you can have 1-6 run a ball and one wicket'
    print 'Let us do the toss'
    t=random.randint(1,2)
    ch=str(raw_input('CHOOSE EVEN OR ODD(e/o):'))
    to=int(raw_input('ENTER YOUR NUMBER:'))
    toss=t+to
    print 'Computer:',t
    if toss%2==0:
        if ch=='e' or ch=='E':
            print 'You won the toss'
            u=1
        else:
            print 'You loss the toss'
            u=0
    else:
        if ch=='o' or ch=='O':
            print 'You won the toss'
            u=1
        else:
            print 'You loss the toss'
            u=0
    if u==1:
        d=int(raw_input('1.Bat or 2.Bowl?(1/2):'))
        if d==1:
            sc=batfirst('u')
            batsec('c',sc)
        else:
            sc=batfirst('c')
            batsec('u',sc)
    else:
        d=random.randint(1,2)
        if d==1:
            print 'computer choose to bat first!'
            sc=batfirst('c')
            batsec('u',sc)
        else:
            print 'Computer choose to bowl first!'
            sc=batfirst('u')
            batsec('c',sc)
    pl=str(raw_input('DO you wish to play again?(y/n):'))
   

           
       
       
       
           
             
       
   
   
   

1 comment:

  1. many errors in print statements and value errors please check

    ReplyDelete