Advertisement
plasticuproject

Python BattleShip

Feb 26th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.28 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #Written by plasticuproject
  4.  
  5. from random import randint, choice
  6. import numpy as np
  7. import pandas as pd
  8.  
  9.  
  10. count = 50
  11. board = []
  12. carrier = []
  13. battleship = []
  14. cruiser = []
  15. submarine = []
  16. destroyer = []
  17. mark = 0
  18.  
  19.  
  20. df = pd.DataFrame({'A' : [1, 11, 21, 31, 41, 51, 61, 71, 81, 91],
  21.                    'B' : [2, 12, 22, 32, 42, 52, 62, 72, 82, 92],
  22.                    'C' : [3, 13, 23, 33, 43, 53, 63, 73, 83, 93],
  23.                    'D' : [4, 14, 24, 34, 44, 54, 64, 74, 84, 94],
  24.                    'E' : [5, 15, 25, 35, 45, 55, 65, 75, 85, 95],
  25.                    'F' : [6, 16, 26, 36, 46, 56, 66, 76, 86, 96],
  26.                    'G' : [7, 17, 27, 37, 47, 57, 67, 77, 87, 97],
  27.                    'H' : [8, 18, 28, 38, 48, 58, 68, 78, 88, 98],
  28.                    'I' : [9, 19, 29, 39, 49, 59, 69, 79, 89, 99],
  29.                    'J' : [10, 20, 30, 40, 50, 60, 70, 80 , 90, 100]})
  30.  
  31. def make_game():
  32.  
  33.     carrier_start_space = [1, 2, 3, 4, 5, 6,
  34.                            11, 12, 13, 14, 15, 16,
  35.                            21, 22, 23, 24, 25, 26,
  36.                            31, 32, 33, 34, 35, 36,
  37.                            41, 42, 43, 44, 45, 46,
  38.                            51, 52, 53, 54, 55, 56]
  39.  
  40.     battleship_start_space = [1, 2, 3, 4, 5, 6, 7,
  41.                               21, 22, 23, 24, 25, 26, 27,
  42.                               31, 32, 33, 34, 35, 36, 37,
  43.                               41, 42, 43, 44, 45, 46, 47,
  44.                               51, 52, 53, 54, 55, 56, 57,
  45.                               61, 62, 63, 64, 65, 66, 67]
  46.  
  47.     cruiser_start_space = [1, 2, 3, 4, 5, 6, 7, 8,
  48.                            11, 12, 13, 14, 15, 16, 17, 18,
  49.                            21, 22, 23, 24, 25, 26, 27, 28,
  50.                            31, 32, 33, 34, 35, 36, 37, 38,
  51.                            41, 42, 43, 44, 45, 46, 47, 48,
  52.                            51, 52, 53, 54, 55, 56, 57, 58,
  53.                            61, 62, 63, 64, 65, 66, 67, 68,
  54.                            71, 72, 73, 74, 75, 76, 77, 78]
  55.  
  56.     submarine_start_space = cruiser_start_space
  57.  
  58.     destroyer_start_space = [1, 2, 3, 4, 5, 6, 7, 8, 9,
  59.                              11, 12, 13, 14, 15, 16, 17, 18, 19,
  60.                              21, 22, 23, 24, 25, 26, 27, 28, 29,
  61.                              31, 32, 33, 34, 35, 36, 37, 38, 39,
  62.                              41, 42, 43, 44, 45, 46, 47, 48, 49,
  63.                              51, 52, 53, 54, 55, 56, 57, 58, 59,
  64.                              61, 62, 63, 64, 65, 66, 67, 68, 69,
  65.                              71, 72, 73, 74, 75, 76, 77, 78, 79,
  66.                              81, 82, 83, 84, 85, 86, 87, 88, 89]
  67.  
  68.     for i in range(1, 101):
  69.         board.append(i)
  70.  
  71.     v1 = choice(carrier_start_space)
  72.     v2 = randint(1, 2)
  73.  
  74.     if v2 == 1:
  75.  
  76.         carrier.extend((v1, v1 + 1, v1 + 2, v1 + 3, v1 + 4))
  77.  
  78.         battleship_bad_4 = [v1 - 1, v1 - 2, v1 - 3, v1 - 4,
  79.                             v1 - 10, v1 - 10 + 1, v1 - 10 + 2,
  80.                             v1 - 10 + 3, v1 - 10 + 4, v1 - 20,
  81.                             v1 - 20 + 1, v1 - 20 + 2, v1 - 20 + 3,
  82.                             v1 - 20 + 4, v1 - 30, v1 - 30 + 1,
  83.                             v1 - 30 + 2, v1 - 30 + 3, v1 - 30 + 4,
  84.                             v1 - 40, v1 - 40 + 1, v1 - 40 + 2,
  85.                             v1 - 40 + 3, v1 - 40 + 4]
  86.  
  87.         cruiser_bad_4 = [v1 - 1, v1 - 2, v1 - 3, v1 - 4,
  88.                          v1 - 10, v1 - 10 + 1, v1 - 10 + 2,
  89.                          v1 - 10 + 3, v1 - 10 + 4, v1 - 20,
  90.                          v1 - 20 + 1, v1 - 20 + 2, v1 - 20 + 3,
  91.                          v1 - 20 + 4, v1 - 30, v1 - 30 + 1,
  92.                          v1 - 30 + 2, v1 - 30 + 3, v1 - 30 + 4]
  93.  
  94.         submarine_bad_4 = [v1 - 1, v1 - 2, v1 - 3, v1 - 4,
  95.                            v1 - 10, v1 - 10 + 1, v1 - 10 + 2,
  96.                            v1 - 10 + 3, v1 - 10 + 4, v1 - 20,
  97.                            v1 - 20 + 1, v1 - 20 + 2, v1 - 20 + 3,
  98.                            v1 - 20 + 4, v1 - 30, v1 - 30 + 1,
  99.                            v1 - 30 + 2, v1 - 30 + 3, v1 - 30 + 4]
  100.  
  101.         destroyer_bad_4 = [v1 - 1, v1 - 2, v1 - 3, v1 - 4,
  102.                            v1 - 10, v1 - 10 + 1, v1 - 10 + 2,
  103.                            v1 - 10 + 3, v1 - 10 + 4, v1 - 20,
  104.                            v1 - 20 + 1, v1 - 20 + 2, v1 - 20 + 3,
  105.                            v1 - 20 + 4]
  106.  
  107.     elif v2 == 2:
  108.  
  109.         carrier.extend((v1, v1 + 10, v1 + 20, v1 + 30, v1 + 40))
  110.  
  111.         battleship_bad_4 = [v1 - 10, v1 - 20, v1 - 30, v1 - 1,
  112.                             v1 - 2, v1 - 3, v1 + 10, v1 + 10 - 1,
  113.                             v1 + 10 - 2, v1 + 10 - 3, v1 + 20,
  114.                             v1 + 20 - 1, v1 + 20 - 2, v1 + 20 - 3,
  115.                             v1 + 30, v1 + 30 - 1, v1 + 30 - 2,
  116.                             v1 + 30 - 3, v1 + 40, v1 + 40 - 1,
  117.                             v1 + 40 - 2, v1 + 40 - 3]
  118.  
  119.         cruiser_bad_4 = [v1 - 10, v1 - 20, v1 - 1, v1 - 2, v1 + 10,
  120.                          v1 + 10 - 1, v1 + 10 - 2, v1 + 20, v1 + 20 - 1,
  121.                          v1 + 20 - 2, v1 + 30, v1 + 30 - 1, v1 + 30 - 2,
  122.                          v1 + 40, v1 + 40 - 1, v1 + 40 - 2]
  123.  
  124.         submarine_bad_4 = [v1 - 10, v1 - 20, v1 - 1, v1 - 2, v1 + 10,
  125.                            v1 + 10 - 1, v1 + 10 - 2, v1 + 20, v1 + 20 - 1,
  126.                            v1 + 20 - 2, v1 + 30, v1 + 30 - 1, v1 + 30 - 2,
  127.                            v1 + 40, v1 + 40 - 1, v1 + 40 - 2]
  128.  
  129.         destroyer_bad_4 = [v1 - 10, v1 - 1, v1 + 10, v1 + 10 - 1, v1 + 20,
  130.                            v1 + 20 - 1, v1 + 30, v1 + 30 - 1, v1 + 40,
  131.                            v1 + 40 - 1]
  132.  
  133.     for i in destroyer_bad_4:
  134.         if i in destroyer_start_space:
  135.             destroyer_start_space.remove(i)
  136.     for i in cruiser_bad_4:
  137.         if i in battleship_start_space:
  138.             battleship_start_space.remove(i)
  139.     for i in submarine_bad_4:
  140.         if i in cruiser_start_space:
  141.             cruiser_start_space.remove(i)
  142.     for i in destroyer_bad_4:
  143.         if i in submarine_start_space:
  144.             submarine_start_space.remove(i)
  145.  
  146.     for i in carrier:
  147.         if i in board:
  148.             board.remove(i)
  149.         if i in battleship_start_space:
  150.             battleship_start_space.remove(i)
  151.         if i in cruiser_start_space:
  152.             cruiser_start_space.remove(i)
  153.         if i in submarine_start_space:
  154.             submarine_start_space.remove(i)
  155.         if i in destroyer_start_space:
  156.             destroyer_start_space.remove(i)
  157.  
  158.     v1 = choice(battleship_start_space)
  159.     v2 = randint(1, 2)
  160.  
  161.     if v2 == 1:
  162.  
  163.         battleship.extend((v1, v1 + 1, v1 + 2, v1 + 3))
  164.  
  165.         cruiser_bad_4 = [v1 - 1, v1 - 2, v1 - 10, v1 - 10 + 1,
  166.                          v1 - 10 + 2, v1 - 10 + 3, v1 - 20,
  167.                          v1 - 20 + 1, v1 - 20 + 2, v1 - 20 + 3]
  168.  
  169.         submarine_bad_4 = [v1 - 1, v1 - 2, v1 - 10, v1 - 10 + 1,
  170.                            v1 - 10 + 2, v1 - 10 + 3, v1 - 20,
  171.                            v1 - 20 + 1, v1 - 20 + 2, v1 - 20 + 3]
  172.  
  173.         destroyer_bad_4 = [v1 - 1, v1 - 10, v1 - 10 + 1, v1 - 10 + 2,
  174.                            v1 - 10 + 3]
  175.  
  176.     elif v2 == 2:
  177.  
  178.         battleship.extend((v1, v1 + 10, v1 + 20, v1 + 30))
  179.  
  180.         cruiser_bad_4=[v1 - 10, v1 - 20, v1 - 30, v1 - 1, v1 - 2,
  181.                        v1 + 10 - 1, v1 + 10 - 2, v1 + 20 - 1,
  182.                        v1 + 20 - 2, v1 + 30 - 1, v1 + 30 - 2]
  183.  
  184.         submarine_bad_4=[v1 - 10, v1 - 20, v1 - 30, v1 - 1, v1 - 2,
  185.                          v1 + 10 - 1, v1 + 10 - 2, v1 + 20 - 1,
  186.                          v1 + 20 - 2, v1 + 30 - 1, v1 + 30 - 2]
  187.  
  188.         destroyer_bad_4 = [v1 - 10, v1 - 1, v1 + 10 - 1, v1 + 20 - 1,
  189.                            v1 + 30 - 1]
  190.  
  191.     for i in cruiser_bad_4:
  192.         if i in  cruiser_start_space:
  193.             cruiser_start_space.remove(i)
  194.     for i in destroyer_bad_4:
  195.         if i in destroyer_start_space:
  196.             destroyer_start_space.remove(i)
  197.     for i in submarine_bad_4:
  198.         if i in submarine_start_space:
  199.             submarine_start_space.remove(i)
  200.  
  201.     for i in battleship:
  202.         if i in board:
  203.             board.remove(i)
  204.         if i in cruiser_start_space:
  205.             cruiser_start_space.remove(i)
  206.         if i in submarine_start_space:
  207.             submarine_start_space.remove(i)
  208.         if i in destroyer_start_space:
  209.             destroyer_start_space.remove(i)
  210.  
  211.     v1 = choice(cruiser_start_space)
  212.     v2 = randint(1, 2)
  213.  
  214.     if v2 == 1:
  215.  
  216.         cruiser.extend((v1, v1 + 1, v1 + 2))
  217.  
  218.         submarine_bad_4 = [v1 - 1, v1 - 2, v1 - 10, v1 - 10 + 1,
  219.                            v1 - 10 + 2, v1 - 20, v1 - 20 + 1,
  220.                            v1 - 20 + 2]
  221.  
  222.         destroyer_bad_4 = [v1 - 1, v1 - 10, v1 - 10 + 1, v1 - 10 + 2]
  223.  
  224.     elif v2 == 2:
  225.  
  226.         cruiser.extend((v1, v1 + 10, v1 + 20))
  227.  
  228.         submarine_bad_4=[v1 - 10, v1 - 20, v1 - 30, v1 - 1, v1 - 2,
  229.                          v1 + 10 - 1, v1 + 10 - 2, v1 + 20 - 1,
  230.                          v1 + 20 - 2]
  231.  
  232.         destroyer_bad_4 = [v1 - 10, v1 - 1, v1 + 10 - 1, v1 + 20 - 1,]
  233.  
  234.     for i in destroyer_bad_4:
  235.         if i in destroyer_start_space:
  236.             destroyer_start_space.remove(i)
  237.     for i in submarine_bad_4:
  238.         if i in submarine_start_space:
  239.             submarine_start_space.remove(i)
  240.  
  241.     for i in cruiser:
  242.         if i in board:
  243.             board.remove(i)
  244.         if i in submarine_start_space:
  245.             submarine_start_space.remove(i)
  246.         if i in destroyer_start_space:
  247.             destroyer_start_space.remove(i)
  248.  
  249.     v1 = choice(submarine_start_space)
  250.     v2 = randint(1, 2)
  251.  
  252.     if v2 == 1:
  253.  
  254.         submarine.extend((v1, v1 + 1, v1 + 2))
  255.  
  256.         destroyer_bad_4 = [v1 - 1, v1 - 10, v1 - 10 + 1, v1 - 10 + 2]
  257.  
  258.     elif v2 == 2:
  259.  
  260.         submarine.extend((v1, v1 + 10, v1 + 20))
  261.  
  262.         destroyer_bad_4 = [v1 - 10, v1 - 1, v1 + 10 - 1, v1 + 20 -1]
  263.  
  264.     for i in destroyer_bad_4:
  265.         if i in destroyer_start_space:
  266.             destroyer_start_space.remove(i)
  267.  
  268.     for i in submarine:
  269.         if i in board:
  270.             board.remove(i)
  271.         if i in destroyer_start_space:
  272.             destroyer_start_space.remove(i)
  273.  
  274.     v1 = choice(destroyer_start_space)
  275.     v2 = randint(1, 2)
  276.  
  277.     if v2 == 1:
  278.  
  279.         destroyer.extend((v1, v1 + 1))
  280.  
  281.     elif v2 == 2:
  282.  
  283.         destroyer.extend((v1, v1 + 10))
  284.  
  285.     for i in destroyer:
  286.         if i in board:
  287.             board.remove(i)
  288.  
  289. def play():
  290.     global mark
  291.     global x
  292.     x = input('Choose your location to attack (1-100): ')
  293.     print()
  294.     print()
  295.     print()
  296.     print()
  297.     if x == 'q':
  298.         quit()
  299.     try:
  300.         x = int(x)
  301.     except:
  302.         print('You input was not understood, Mr. President.')
  303.  
  304.     if x in cruiser:
  305.         mark = 'XX'
  306.     elif x in carrier:
  307.         mark = 'XX'
  308.     elif x in battleship:
  309.         mark = 'XX'
  310.     elif x in destroyer:
  311.         mark = 'XX'
  312.     elif x in submarine:
  313.         mark = 'XX'
  314.     else:
  315.         mark = '**'
  316.  
  317.     if x in board:
  318.         print('MISS!!')
  319.         board.remove(x)
  320.     elif x in  carrier:
  321.         print('HIT!!')
  322.         if len(carrier) == 1:
  323.             print()
  324.             print('You sunk the Russian Carrier.')
  325.         carrier.remove(x)
  326.     elif x in cruiser:
  327.         print('HIT!!')
  328.         if len(cruiser) == 1:
  329.             print()
  330.             print('You sunk the Russian Cruiser.')
  331.         cruiser.remove(x)
  332.     elif x in submarine:
  333.         print('HIT!!')
  334.         if len(submarine) == 1:
  335.             print()
  336.             print('You sunk the Russian Submarine.')
  337.         submarine.remove(x)
  338.     elif x in battleship:
  339.         print('HIT!!')
  340.         battleship.remove(x)
  341.     elif x in destroyer:
  342.         print('HIT!!')
  343.         if len(destroyer) == 1:
  344.             print()
  345.             print('You sunk the Russian Destroyer.')
  346.         destroyer.remove(x)
  347.     else:
  348.         print('Please choose a valid target to attack.')
  349.  
  350.     if len(battleship) == 0:
  351.         print()
  352.         print('You sunk my Battleship!!')
  353.         graphic()
  354.         quit()
  355.  
  356. def graphic():
  357.     global df
  358.     global x
  359.     global mark
  360.     print()
  361.     df = df.replace(x, mark)
  362.     print(df.to_string(index=False))
  363.     print()
  364.     print()
  365.  
  366. def main():
  367.     global count
  368.     running = True
  369.     make_game()
  370.     print()
  371.     print()
  372.     print('\t\t\tBATTLESHIP!!!')
  373.     print("\t\tPress 'q' to quit at any time.")
  374.     print()
  375.     print()
  376.     print('President Trump:')
  377.     print()
  378.     print('\tYou must sink the Russian Battleship before their NUKE detonates')
  379.     print('\tand kills us all!! You have 50 hours. Godspeed, Mr. President.')
  380.     print()
  381.     print(df.to_string(index=False))
  382.     while running:
  383.         if count > 1:
  384.             print()
  385.             print(count, 'hours remaining until nuclear annilation.')
  386.             print()
  387.         if count== 1:
  388.             print()
  389.             print('You have 1 hour to stop the Russians or you are responsible')
  390.             print('for the death of every American in the Country!!')
  391.             print()
  392.         count -= 1
  393.         if count == -1:
  394.             print()
  395.             print('\t\t\tBOOOOOOOOOOMMMMMMM!!!!!!')
  396.             print()
  397.             print('(you daed)')
  398.             quit()
  399.         play()
  400.         print()
  401.         graphic()
  402.  
  403.  
  404. if __name__=='__main__':
  405.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement