MIS315 Coin Taking Game PDF

Title MIS315 Coin Taking Game
Author Marcus de la Cruz
Course Business Applications Programming
Institution San Diego State University
Pages 4
File Size 46.4 KB
File Type PDF
Total Downloads 21
Total Views 151

Summary

Luwen Huangfu...


Description

In this practice you will use a 'while' loop to build a two-player coin-taking game. You will also need to use some variables and some conditional statements. Here are the rules of the game: a) The game starts with 22 coins in a common pool. b) The goal of the game is to take the last coin. c) Players take turns taking 1, 2, or 3 coins from the pool. d) A player may not take more coins than remain in the pool. e) After each turn, the program announces how many coins remain in the pool f) When the last coin is taken, the program announces the winner. Your program must enforce all the rules stated above. Task 1: finish the program and try three test cases Task 2: modify the program so that: 2.1 make the program ask if the players wish to play again after the game is over, and if they do start another round 2.2 make the pool start with a random number ([20, 30] inclusive) of coins 2.3 make the computer act as Player 1, give it the winning strategy for deciding how many coins to take Finish Task 2 and try three test cases Task 3: Extend the game to have two piles of coins, allow the players to specify which pile they take the coins from, as well as how many coins they take Finish Task 3 and try three test cases

Task 1. def task1(): noc=22 print("Number of coins remaining is ",noc) i=0 while(True): if(i%2==0): print("Player 1 : How many coins you wish to take") n=int(input()) if(n>noc): print("Please take lesser number of coins") continue if(n3): print("Please take atmost three coins") continue noc-=n i+=1 if(noc==0): print("Player 1 wins") break else: print("Player 2 : How many coins you wish to take") n=int(input()) if(n>noc):

print("Please take lesser number of coins") continue if(n3): print("Please take atmost three coins") continue noc-=n i+=1 if(noc==0): print("Player 2 wins") break print("Number of coins remaining is ",noc)

Task 2.

import random def task2(): noc=random.randint(20,30) print("Number of coins remaining is ",noc) i=0 while(True): if(i%2==0): if((noc-1)%4==0): noc-=1 elif((noc-2)%4==0): noc-=2 elif((noc-3)%4==0): noc-=3 else: noc-1 i+=1 print("Number of coins remaining is ",noc) if(noc==0): print("Computer wins") print("Press 1 if you would like to play again else plress 0") if(int(input())==1): noc=random.randint(20,31) continue break else: print("Player 2 : How many coins you wish to take") n=int(input())

if(n>noc): print("Please take lesser number of coins") continue if(n3): print("Please take atmost three coins") continue noc-=n i+=1

Task 3. def task3(): pile1=random.randint(20,30) pile2=random.randint(20,30) t=random.randint(0,10) if(t%2==0): print("Player 1 select your pile, 1 for pile 1 and 2 for pile 2") n=int(input()) if(n==1): p1=pile1 p2=pile2 else: p1=pile2 p2=pile1 else: print("Player 2 select your pile, 1 for pile 1 and 2 for pile 2") n=int(input()) if(n==1): p2=pile1 p1=pile2 else: p2=pile2 p1=pile1 i=0 while(True): if(i%2==0): print("Number of coins in pile of player 1 is ",p1) print("Player 1 : How many coins you wish to take") n=int(input()) if(n>p1): print("Please take lesser number of coins") continue if(n3): print("Please take atmost three coins") continue p1-=n i+=1 if(p1==0): print("Player 1 wins") break else: print("Number of coins in pile of player 2 is ",p2) print("Player 2 : How many coins you wish to take") n=int(input()) if(n>p2): print("Please take lesser number of coins") continue if(n3): print("Please take atmost three coins") continue p2-=n i+=1 if(p2==0): print("Player 2 wins") break...


Similar Free PDFs