{"id":958,"date":"2021-06-10T10:24:06","date_gmt":"2021-06-10T04:54:06","guid":{"rendered":"https:\/\/cbsepython.in\/?p=958"},"modified":"2021-09-29T22:12:40","modified_gmt":"2021-09-29T16:42:40","slug":"cricket-game-python-project","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/cricket-game-python-project\/","title":{"rendered":"Cricket Game- Python Project for CBSE Class 12"},"content":{"rendered":"<p>Cricket Game- Python Project for CBSE Class 12<\/p>\n<p>&nbsp;<\/p>\n<p>Source Code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Cricket Game\r\n \r\nprint(\"\"\" ~~~~~~~~~~ Game of Cricket ~~~~~~~~~~\r\n \r\nInstructions:\r\n \r\n1. You have to select any random number from 1 to 6.\r\n2. The computer will also select a number.\r\n3. While batting, if the number selected by you and computer is different, then your number will add to your runs.\r\n   If the number selected by you and computer is same, then you will lose your wicket.\r\n4. While bowling, if the number selected by you and computer is different, then the computer's number will add to its runs.\r\n   If the number selected by you and computer is same, then the computer will lose its wicket.\r\n5. Each player will get 2 wickets and 2 overs (12 balls) for batting and bowling.\r\n6. The innings will end after either the three wickets fell or the overs end.\r\n7. The player with maximum runs wins. \"\"\")\r\n \r\nprint(\"\\n---------- Start Game ----------\")\r\n \r\nimport random\r\n \r\n# Toss \r\n \r\nprint(\"\\nHere comes the Toss\")\r\ntoss = (input(\"Choose heads or tails: \")).lower()\r\n \r\nrandom_toss = random.randint(1,2)            # In random_toss (1 = Heads) and (2 = Tails)\r\nrandom_opt = random.randint(1,2)             # In random_opt (1 = bat) and (2 = ball)\r\n \r\nu_opt = 0\r\nc_opt = 0\r\n \r\nif random_toss == 1 and toss == \"heads\":\r\n    print(\"\\nYou won the toss\")\r\n    u_opt = (input(\"Choose bat or ball: \")).lower()\r\n \r\nelif random_toss == 2 and toss == \"tails\":\r\n    print(\"\\nYou won the toss\")\r\n    u_opt = (input(\"Choose bat or ball: \")).lower()    \r\n  \r\nelse:\r\n    print(\"\\nYou lost the toss\")\r\n \r\n    if random_opt == 1:\r\n        c_opt = \"bat\"\r\n        print(\"Computer choose to\",c_opt)\r\n \r\n    elif random_opt == 2:\r\n        c_opt = \"ball\"\r\n        print(\"Computer choose to\",c_opt)\r\n \r\n# First Innings \r\n \r\nprint(\"\\n---------- First Innings Begins ----------\")\r\n \r\nruns_1 = 0\r\nwickets_1 = 0\r\nballs_1 = 0\r\n \r\nwhile wickets_1 != 2 and balls_1 != 12:\r\n \r\n    u_choice = int(input(\"\\nChoose any number from 1 to 6: \"))\r\n    c_choice = random.randint(1,6)\r\n \r\n    if u_choice &lt; 1 or u_choice &gt; 6:\r\n        print(\"\\nPlease choose a value from 1 to 6.\")\r\n \r\n    else:\r\n        print(\"Your choice: \",u_choice,\"\\nComputer's choice: \",c_choice)\r\n \r\n        if u_choice == c_choice:\r\n            wickets_1 += 1\r\n \r\n        else:\r\n            if u_opt == \"bat\" or c_opt == \"ball\":\r\n                Bat_first = \"You\"\r\n                Ball_first = \"Computer\"\r\n                runs_1 += u_choice\r\n \r\n            elif u_opt == \"ball\" or c_opt == \"bat\":\r\n                Bat_first = \"Computer\"\r\n                Ball_first = \"You\"\r\n                runs_1 += c_choice\r\n \r\n        print(\"\\nScore =\",runs_1,\"\/\",wickets_1)\r\n \r\n        balls_1 += 1\r\n \r\n        if balls_1 == 6:\r\n            print(\"End of Over 1\")\r\n \r\n        elif balls_1 == 12:\r\n            print(\"End of Over 2\")\r\n \r\n        print(\"Balls remaining: \",12 - balls_1)\r\n \r\nprint(\"\\n---------- End of Innings ----------\") \r\n \r\nprint(\"\\nFinal Score:\")\r\nprint(\"Runs =\",runs_1)\r\nprint(\"wickets =\",wickets_1)\r\n \r\nprint(\"\\n\",Ball_first,\"needs\",runs_1 + 1,\"runs to win.\")\r\n \r\n# Second Innings \r\n \r\nprint(\"\\n---------- Second Innings Begins ----------\")\r\n \r\nruns_2 = 0\r\nwickets_2 = 0\r\nballs_2 = 0\r\n \r\nwhile wickets_2 != 2 and balls_2 != 12 and runs_2 &lt;= runs_1:\r\n \r\n    u_choice = int(input(\"\\nChoose any number from 1 to 6: \"))\r\n    c_choice = random.randint(1,6)\r\n \r\n    if u_choice &lt; 1 or u_choice &gt; 6:\r\n        print(\"\\nPlease choose a value from 1 to 6.\")\r\n \r\n    else:\r\n        print(\"Your choice: \",u_choice,\"\\nComputer's choice: \",c_choice)\r\n \r\n        if u_choice == c_choice:\r\n            wickets_2 += 1\r\n \r\n        else:\r\n            if Bat_first == \"Computer\": \r\n                runs_2 += u_choice\r\n                Bat_second = \"You\"\r\n \r\n            elif Bat_first == \"You\":\r\n                runs_2 += c_choice\r\n                Bat_second = \"Computer\"\r\n \r\n        print(\"\\nScore =\",runs_2,\"\/\",wickets_2)\r\n \r\n        balls_2 += 1\r\n \r\n        if balls_2 == 6:\r\n            print(\"End of Over 1\")\r\n \r\n        elif balls_2 == 12:\r\n            print(\"End of Over 2\")\r\n \r\n        if runs_2 &lt;= runs_1 and balls_2 &lt;= 11 and wickets_2 != 2:\r\n            print(\"To win:\",runs_1 - runs_2 + 1,\"runs needed from\",12 - balls_2,\"balls.\")\r\n \r\nprint(\"\\n---------- End of Innings ----------\") \r\n \r\nprint(\"\\nFinal Score:\")\r\nprint(\"Runs =\",runs_2)\r\nprint(\"wickets =\",wickets_2)\r\n \r\n# Result of Match \r\n \r\nprint(\"\\n~~~~~~~~~~ Result ~~~~~~~~~~\")\r\n \r\nif runs_1 &gt; runs_2:\r\n \r\n    if Bat_first == \"You\": \r\n        print(\"\\nCongratulations! You won the Match by\",runs_1 - runs_2,\"runs.\")\r\n \r\n    else:\r\n        print(\"\\nBetter luck next time! The Computer won the Match by\",runs_1 - runs_2,\"runs.\") \r\n \r\nelif runs_2 &gt; runs_1:\r\n \r\n    if Bat_second == \"You\": \r\n        print(\"\\nCongratulations! You won the Match by\",2 - wickets_2,\"wickets.\")\r\n \r\n    else:\r\n        print(\"\\nBetter luck next time! The Computer won the Match by\",2 - wickets_2,\"wickets.\")\r\n \r\nelse:\r\n    print(\"The Match is a Tie.\",\"\\nNo one Wins.\")\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<p>&nbsp;<\/p>\n<pre>~~~~~~~~~~ Game of Cricket ~~~~~~~~~~\r\n\r\nInstructions:\r\n\r\n1. You have to select any random number from 1 to 6.\r\n2. The computer will also select a number.\r\n3. While batting, if the number selected by you and computer is different, then your number will add to your runs.\r\nIf the number selected by you and computer is same, then you will lose your wicket.\r\n4. While bowling, if the number selected by you and computer is different, then the computer's number will add to its runs.\r\nIf the number selected by you and computer is same, then the computer will lose its wicket.\r\n5. Each player will get 2 wickets and 2 overs (12 balls) for batting and bowling.\r\n6. The innings will end after either the three wickets fell or the overs end.\r\n7. The player with maximum runs wins.\r\n\r\n---------- Start Game ----------\r\n\r\nHere comes the Toss\r\nChoose heads or tails: \"tails\"\r\n\r\nYou won the toss\r\nChoose bat or ball: \"bat\"\r\n\r\n---------- First Innings Begins ----------\r\n\r\nChoose any number from 1 to 6: 4\r\n('Your choice: ', 4, \"\\nComputer's choice: \", 2)\r\n('\\nScore =', 4, '\/', 0)\r\n('Balls remaining: ', 11)\r\n\r\nChoose any number from 1 to 6: 1\r\n('Your choice: ', 1, \"\\nComputer's choice: \", 2)\r\n('\\nScore =', 5, '\/', 0)\r\n('Balls remaining: ', 10)\r\n\r\nChoose any number from 1 to 6: 3\r\n('Your choice: ', 3, \"\\nComputer's choice: \", 4)\r\n('\\nScore =', 8, '\/', 0)\r\n('Balls remaining: ', 9)\r\n\r\nChoose any number from 1 to 6: 2\r\n('Your choice: ', 2, \"\\nComputer's choice: \", 2)\r\n('\\nScore =', 8, '\/', 1)\r\n('Balls remaining: ', 8)\r\n\r\nChoose any number from 1 to 6: 6\r\n('Your choice: ', 6, \"\\nComputer's choice: \", 4)\r\n('\\nScore =', 14, '\/', 1)\r\n('Balls remaining: ', 7)\r\n\r\nChoose any number from 1 to 6: 3\r\n('Your choice: ', 3, \"\\nComputer's choice: \", 6)\r\n('\\nScore =', 17, '\/', 1)\r\nEnd of Over 1\r\n('Balls remaining: ', 6)\r\n\r\nChoose any number from 1 to 6: 2\r\n('Your choice: ', 2, \"\\nComputer's choice: \", 6)\r\n('\\nScore =', 19, '\/', 1)\r\n('Balls remaining: ', 5)\r\n\r\nChoose any number from 1 to 6: 5\r\n('Your choice: ', 5, \"\\nComputer's choice: \", 6)\r\n('\\nScore =', 24, '\/', 1)\r\n('Balls remaining: ', 4)\r\n\r\nChoose any number from 1 to 6: 6\r\n('Your choice: ', 6, \"\\nComputer's choice: \", 5)\r\n('\\nScore =', 30, '\/', 1)\r\n('Balls remaining: ', 3)\r\n\r\nChoose any number from 1 to 6: 3\r\n('Your choice: ', 3, \"\\nComputer's choice: \", 3)\r\n('\\nScore =', 30, '\/', 2)\r\n('Balls remaining: ', 2)\r\n\r\n---------- End of Innings ----------\r\n\r\nFinal Score:\r\n('Runs =', 30)\r\n('wickets =', 2)\r\n('\\n', 'Computer', 'needs', 31, 'runs to win.')\r\n\r\n---------- Second Innings Begins ----------\r\n\r\nChoose any number from 1 to 6: 2\r\n('Your choice: ', 2, \"\\nComputer's choice: \", 1)\r\n('\\nScore =', 1, '\/', 0)\r\n('To win:', 30, 'runs needed from', 11, 'balls.')\r\n\r\nChoose any number from 1 to 6: 1\r\n('Your choice: ', 1, \"\\nComputer's choice: \", 2)\r\n('\\nScore =', 3, '\/', 0)\r\n('To win:', 28, 'runs needed from', 10, 'balls.')\r\n\r\nChoose any number from 1 to 6: 4\r\n('Your choice: ', 4, \"\\nComputer's choice: \", 5)\r\n('\\nScore =', 8, '\/', 0)\r\n('To win:', 23, 'runs needed from', 9, 'balls.')\r\n\r\nChoose any number from 1 to 6: 2\r\n('Your choice: ', 2, \"\\nComputer's choice: \", 2)\r\n('\\nScore =', 8, '\/', 1)\r\n('To win:', 23, 'runs needed from', 8, 'balls.')\r\n\r\nChoose any number from 1 to 6: 4\r\n('Your choice: ', 4, \"\\nComputer's choice: \", 6)\r\n('\\nScore =', 14, '\/', 1)\r\n('To win:', 17, 'runs needed from', 7, 'balls.')\r\n\r\nChoose any number from 1 to 6: 6\r\n('Your choice: ', 6, \"\\nComputer's choice: \", 5)\r\n('\\nScore =', 19, '\/', 1)\r\nEnd of Over 1\r\n('To win:', 12, 'runs needed from', 6, 'balls.')\r\n\r\nChoose any number from 1 to 6: 4\r\n('Your choice: ', 4, \"\\nComputer's choice: \", 6)\r\n('\\nScore =', 25, '\/', 1)\r\n('To win:', 6, 'runs needed from', 5, 'balls.')\r\n\r\nChoose any number from 1 to 6: 3\r\n('Your choice: ', 3, \"\\nComputer's choice: \", 5)\r\n('\\nScore =', 30, '\/', 1)\r\n('To win:', 1, 'runs needed from', 4, 'balls.')\r\n\r\nChoose any number from 1 to 6: 2\r\n('Your choice: ', 2, \"\\nComputer's choice: \", 5)\r\n('\\nScore =', 35, '\/', 1)\r\n\r\n---------- End of Innings ----------\r\n\r\nFinal Score:\r\n('Runs =', 35)\r\n('wickets =', 1)\r\n\r\n~~~~~~~~~~ Result ~~~~~~~~~~\r\n('\\nBetter luck next time! The Computer won the Match by', 1, 'wickets.')\r\n&gt;&gt;&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cricket Game- Python Project for CBSE Class 12 &nbsp; Source Code: # Cricket Game print(&#8220;&#8221;&#8221; ~~~~~~~~~~ Game of Cricket ~~~~~~~~~~ Instructions: 1. You have to select any random number from 1 to 6. 2. The computer will also select a number. 3. While batting, if the number selected by you and computer is different, then [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-958","post","type-post","status-publish","format-standard","hentry","category-python-projects"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=958"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/958\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}