Pen Distribution Problem | Recursive function to play Game ; Box is empty , Game Over ! or Both have quit , Game Over ! ; P1 moves ; P2 moves ; Increment X ; Switch moves between P1 and P2 ; Function to find the number of pens remaining in the box and calculate score for each player ; Score of P1 ; Score of P2 ; Initialized to zero ; Move = 0 , P1 ' s ▁ turn ▁ ▁ Move ▁ = ▁ 1 , ▁ P2' s turn ; Has P1 quit ; Has P2 quit ; Recursively continue the game ; Driver Code
def solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) : NEW_LINE INDENT if ( N == 0 or ( QuitP1 and QuitP2 ) ) : NEW_LINE INDENT print ( " Number ▁ of ▁ pens ▁ remaining ▁ in ▁ the ▁ box : ▁ " , N ) NEW_LINE print ( " Number ▁ of ▁ pens ▁ collected ▁ by ▁ P1 : ▁ " , P1 ) NEW_LINE print ( " Number ▁ of ▁ pens ▁ collected ▁ by ▁ P2 : ▁ " , P2 ) NEW_LINE return NEW_LINE DEDENT if ( Move == 0 and QuitP1 == False ) : NEW_LINE INDENT req_P1 = int ( pow ( 2 , X ) ) NEW_LINE if ( req_P1 <= N ) : NEW_LINE INDENT P1 += req_P1 NEW_LINE N -= req_P1 NEW_LINE DEDENT else : NEW_LINE INDENT QuitP1 = True NEW_LINE DEDENT DEDENT elif ( Move == 1 and QuitP2 == False ) : NEW_LINE INDENT req_P2 = int ( pow ( 3 , X ) ) NEW_LINE if ( req_P2 <= N ) : NEW_LINE INDENT P2 += req_P2 NEW_LINE N -= req_P2 NEW_LINE DEDENT else : NEW_LINE INDENT QuitP2 = True NEW_LINE DEDENT DEDENT X += 1 NEW_LINE if ( Move == 1 ) : NEW_LINE INDENT Move = 0 NEW_LINE DEDENT else : NEW_LINE INDENT Move = 1 NEW_LINE DEDENT solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) NEW_LINE DEDENT def PenGame ( N ) : NEW_LINE INDENT P1 = 0 NEW_LINE P2 = 0 NEW_LINE X = 0 NEW_LINE Move = False NEW_LINE QuitP1 = False NEW_LINE QuitP2 = False NEW_LINE solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) NEW_LINE DEDENT N = 22 NEW_LINE PenGame ( N ) NEW_LINE