import random YourGrid = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] EnemyGrid = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Outposts = [0,0] def AskQuestion(prompt): gotAns = False retVal = "" while(not gotAns): ans = input("{0}? ".format(prompt)) if(ans == ""): print("PLEASE ANSWER THE QUESTION.") else: gotAns = True retVal = ans.upper() return retVal def IsAnInt(input): retVal = True try: i = int(input) except: retVal = False return retVal def GetIntInput(prompt): gotAns = False retVal = 0 while(not gotAns): ans = input("{0}: ".format(prompt)) if(IsAnInt(ans) == True): gotAns = True retVal = int(ans) else: print("YOU DID NOT ENTER AN INTEGER.") return retVal def AskToPlayAgain(): gotAns = False retVal = False while(not gotAns): ans = AskQuestion("DO YOU WANT TO PLAY AGAIN") if(ans == "YES" or ans == "Y"): retVal = True gotAns = True elif(ans == "NO" or ans == "N"): retVal = False gotAns = True else: print("PLEASE ANSWER \"YES\" OR \"NO\".") return retVal def AskToSeeTheRules(): gotAns = False retVal = False while(not gotAns): ans = AskQuestion("DO YOU WANT TO SEE THE RULES") if(ans == "YES" or ans == "Y"): retVal = True gotAns = True elif(ans == "NO" or ans == "N"): retVal = False gotAns = True else: print("PLEASE ANSWER \"YES\" OR \"NO\".") return retVal def GenerateRandomNumber(minimum, maximum, show): random.seed() retVal = random.randint(minimum, maximum) if(show): print("{0}".format(retVal)) return retVal def DisplayRules(): print ("YOU ARE ON A BATTLEFIELD WITH 4 PLATOONS AND YOU") print ("HAVE 25 OUTPOSTS AVAILABLE WHERE THEY MAY BE PLACED.") print ("YOU CAN ONLY PLACE ONE PLATOON AT ANY ONE OUTPOST.") print ("THE COMPUTER DOES THE SAME WITH ITS FOUR PLATOONS.") print (" ") print ("THE OBJECT OF THE GAME IS TO FIRE MISSILES AT THE") print ("OUTPOSTS OF THE COMPUTER. IT WILL DO THE SAME TO YOU.") print ("THE ONE WHO DESTROYS ALL FOUR OF THE ENEMY'S PLATOONS") print ("FIRST IS THE WINNER.") print (" ") print ("GOOD LUCK...AND TELL US WHERE YOU WANT THE BODIES SENT.") print (" ") def CheckWinner(): if (Outposts[0] == 0): return 2 elif (Outposts[1] == 0): return 1 else: return 0 def DisplayYourGrid(): print("=======================") print("YOUR GRID") print("=======================") print("X | 1 | 2 | 3 | 4 | 5 |") print("Y |---+---+---+---+---|") for x in range(1,6): gStart = (x - 1) * 5 gEnd = x * 5 displayLine = str(x) + " |" for g in range(gStart, gEnd): if (YourGrid[g] == -1 ): displayLine = displayLine + " X |" elif(YourGrid[g] == 1): displayLine = displayLine + " O |" else: displayLine = displayLine + " |" print(displayLine) print(" |---+---+---+---+---|") def DisplayEnemyGrid(): print("=======================") print("ENEMY GRID") print("=======================") print("X | 1 | 2 | 3 | 4 | 5 |") print("Y |---+---+---+---+---|") for x in range(1,6): gStart = (x - 1) * 5 gEnd = x * 5 displayLine = str(x) + " |" for g in range(gStart, gEnd): if (EnemyGrid[g] == -1 ): displayLine = displayLine + " X |" else: displayLine = displayLine + " |" print(displayLine) print(" |---+---+---+---+---|") def ClearGrids(): for g in range(0, 25): YourGrid[g] = 0 EnemyGrid[g] = 0 def SetOutposts(): Outposts[0] = 4 outpost = 0 for o in range(0,4): DisplayYourGrid() outpostSet = False while (not outpostSet): print("PLACE YOUR OUTPOST #{0}".format(o + 1)) x = GetIntInput("X") if (x < 1 or x > 5) : print("X MUST BE BETWEEN 1 AND 5.") else: y = GetIntInput("Y") if (y < 1 or y > 5) : print("Y MUST BE BETWEEN 1 AND 5.") else: outpost = ((y - 1) * 5) + (x - 1) if (YourGrid[outpost] == 0): outpostSet = True else: print("YOU ALREADY HAVE AN OUTPOST THERE.") YourGrid[outpost] = 1 def ComputerSetOutposts(): Outposts[1] = 4 outpost = 0 for o in range(0,4): outpostSet = False while (not outpostSet): outpost = GenerateRandomNumber(0,24,False) if (EnemyGrid[outpost] == 0): outpostSet = True EnemyGrid[outpost] = 1 def ComputerTurn(): print("MY TURN...") loc = 0 x = 0 y = 0 gotLoc = False while(not gotLoc): x = GenerateRandomNumber(1,5,False) y = GenerateRandomNumber(1,5,False) loc = ((y - 1) * 5) + (x - 1) if(YourGrid[loc] > -1): gotLoc = True print("..........BOOM!") if(YourGrid[loc] == 0): print("I MISSED YOU, YOU DIRTY RAT.") print("I PICKED LOCATION ({0},{1}).".format(x,y)) else: print("I GOT YOU. IT WON'T BE LONG NOW!") print("OUTPOST AT ({0},{1}) WAS HIT.".format(x,y)) YourGrid[loc] = -1 Outposts[0] = Outposts[0] - 1 if(Outposts[0] == 3): print("YOU ONLY HAVE THREE OUTPOSTS LEFT.") elif(Outposts[0] == 2): print("YOU ONLY HAVE TWO OUTPOSTS LEFT.") elif(Outposts[0] == 1): print("YOU ONLY HAVE ONE OUTPOST LEFT.") else: print("YOU ARE DEAD! YOU HAVE NOT OUTPOSTS LEFT.") def FireMissile(): print("WHERE DO YOU WANT TO FIRE YOUR MISSILE?") x = 0 y = 0 gotLoc = False while(not gotLoc): x = GetIntInput("X") if (x < 1 or x > 5) : print("PLEASE ENTER A NUMBER BETWEEN 1 AND 5.") else: gotLoc = True gotLoc = False while(not gotLoc): y = GetIntInput("Y") if (y < 1 or y > 5) : print("PLEASE ENTER A NUMBER BETWEEN 1 AND 5.") else: gotLoc = True loc = ((y - 1) * 5) + (x - 1) print("..........BOOM!") if(EnemyGrid[loc] == -1): print("YOU FOOL, YOU ALREADY ATTACKED THERE!") elif(EnemyGrid[loc] == 0): print("HA HA, YOU MISSED!") else: print("YOU GOT ONE OF MY OUTPOSTS!") EnemyGrid[loc] = -1 Outposts[1] = Outposts[1] - 1 if(Outposts[1] == 3): print("ONE DOWN, THREE TO GO.") elif(Outposts[1] == 2): print("TWO DOWN, TWO TO GO.") elif(Outposts[1] == 1): print("THREE DOWN, ONE TO GO.") else: print("YOU GOT THEM ALL.") def Run(): print("*** BOMBARDMENT : PYTHON ***") turn = 0 random.seed() Running = True if(AskToSeeTheRules() == True): DisplayRules() while(Running): ClearGrids() ComputerSetOutposts() SetOutposts() GameOver = False print("---------- LET THE WAR BEGIN ----------") while(not GameOver): print("-----------------------") print("TURN #{0}".format(turn + 1)) print("-----------------------") ans = input("PRESS [ENTER] TO BEGIN YOUR TURN") if(turn % 2 == 0): DisplayYourGrid() DisplayEnemyGrid() FireMissile() else: ComputerTurn() winner = CheckWinner() if(winner == 0): turn = turn + 1 else: DisplayYourGrid() DisplayEnemyGrid() if(winner == 1): print("YOU WIN!!!") else: print("TOO BAD, BETTER LUCK NEXT TIME.") GameOver = True print("*** GAME OVER ***") Running = AskToPlayAgain() print("THANKS FOR PLAYING!") Run()