Advertisement
D10d3

battleship.py

Jan 18th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.54 KB | None | 0 0
  1. import os
  2. import random
  3. os.system('cls')
  4.  
  5. """ Single Player BattleShip Game Info:
  6.     Boards have 100 cells in 10 rows numbered A0 through J9
  7.     (chose to use 0-9 instead of traditional 1-10 to simplify code)
  8.             Cells will have 3 states:
  9.             "-" = empty or unknown
  10.             "O" = filled
  11.             "X" = bombed
  12.             "@" = known hit
  13.     players have 7 ships in their fleet:
  14.         Aircraft Carrier (5 cells)
  15.         Battleship (4 cells)
  16.         Cruiser (3 cells)
  17.         Destroyer 1 (2 cells)
  18.         Destroyer 2 (2 cells)
  19.         Submarine 1 (1 cell)
  20.         Submarine 2 (1 cell)
  21.     """
  22.  
  23. def initialize_player(player_board): #initialize blank Player game board
  24.     player_board = {
  25.         "A0":"-","A1":"-","A2":"-","A3":"-","A4":"-","A5":"-","A6":"-","A7":"-","A8":"-","A9":"-",
  26.         "B0":"-","B1":"-","B2":"-","B3":"-","B4":"-","B5":"-","B6":"-","B7":"-","B8":"-","B9":"-",
  27.         "C0":"-","C1":"-","C2":"-","C3":"-","C4":"-","C5":"-","C6":"-","C7":"-","C8":"-","C9":"-",
  28.         "D0":"-","D1":"-","D2":"-","D3":"-","D4":"-","D5":"-","D6":"-","D7":"-","D8":"-","D9":"-",
  29.         "E0":"-","E1":"-","E2":"-","E3":"-","E4":"-","E5":"-","E6":"-","E7":"-","E8":"-","E9":"-",
  30.         "F0":"-","F1":"-","F2":"-","F3":"-","F4":"-","F5":"-","F6":"-","F7":"-","F8":"-","F9":"-",
  31.         "G0":"-","G1":"-","G2":"-","G3":"-","G4":"-","G5":"-","G6":"-","G7":"-","G8":"-","G9":"-",
  32.         "H0":"-","H1":"-","H2":"-","H3":"-","H4":"-","H5":"-","H6":"-","H7":"-","H8":"-","H9":"-",
  33.         "I0":"-","I1":"-","I2":"-","I3":"-","I4":"-","I5":"-","I6":"-","I7":"-","I8":"-","I9":"-",
  34.         "J0":"-","J1":"-","J2":"-","J3":"-","J4":"-","J5":"-","J6":"-","J7":"-","J8":"-","J9":"-",
  35.         }
  36.     return player_board
  37.  
  38. def initialize_computer(computer_board):#initialize blank Computer game board
  39.     computer_board = {
  40.         "A0":"-","A1":"-","A2":"-","A3":"-","A4":"-","A5":"-","A6":"-","A7":"-","A8":"-","A9":"-",
  41.         "B0":"-","B1":"-","B2":"-","B3":"-","B4":"-","B5":"-","B6":"-","B7":"-","B8":"-","B9":"-",
  42.         "C0":"-","C1":"-","C2":"-","C3":"-","C4":"-","C5":"-","C6":"-","C7":"-","C8":"-","C9":"-",
  43.         "D0":"-","D1":"-","D2":"-","D3":"-","D4":"-","D5":"-","D6":"-","D7":"-","D8":"-","D9":"-",
  44.         "E0":"-","E1":"-","E2":"-","E3":"-","E4":"-","E5":"-","E6":"-","E7":"-","E8":"-","E9":"-",
  45.         "F0":"-","F1":"-","F2":"-","F3":"-","F4":"-","F5":"-","F6":"-","F7":"-","F8":"-","F9":"-",
  46.         "G0":"-","G1":"-","G2":"-","G3":"-","G4":"-","G5":"-","G6":"-","G7":"-","G8":"-","G9":"-",
  47.         "H0":"-","H1":"-","H2":"-","H3":"-","H4":"-","H5":"-","H6":"-","H7":"-","H8":"-","H9":"-",
  48.         "I0":"-","I1":"-","I2":"-","I3":"-","I4":"-","I5":"-","I6":"-","I7":"-","I8":"-","I9":"-",
  49.         "J0":"-","J1":"-","J2":"-","J3":"-","J4":"-","J5":"-","J6":"-","J7":"-","J8":"-","J9":"-",
  50.         }
  51.     return computer_board
  52.  
  53. def initialize_hit(hit_board):#initialize blank board to displayer player shots
  54.     hit_board = {
  55.         "A0":"-","A1":"-","A2":"-","A3":"-","A4":"-","A5":"-","A6":"-","A7":"-","A8":"-","A9":"-",
  56.         "B0":"-","B1":"-","B2":"-","B3":"-","B4":"-","B5":"-","B6":"-","B7":"-","B8":"-","B9":"-",
  57.         "C0":"-","C1":"-","C2":"-","C3":"-","C4":"-","C5":"-","C6":"-","C7":"-","C8":"-","C9":"-",
  58.         "D0":"-","D1":"-","D2":"-","D3":"-","D4":"-","D5":"-","D6":"-","D7":"-","D8":"-","D9":"-",
  59.         "E0":"-","E1":"-","E2":"-","E3":"-","E4":"-","E5":"-","E6":"-","E7":"-","E8":"-","E9":"-",
  60.         "F0":"-","F1":"-","F2":"-","F3":"-","F4":"-","F5":"-","F6":"-","F7":"-","F8":"-","F9":"-",
  61.         "G0":"-","G1":"-","G2":"-","G3":"-","G4":"-","G5":"-","G6":"-","G7":"-","G8":"-","G9":"-",
  62.         "H0":"-","H1":"-","H2":"-","H3":"-","H4":"-","H5":"-","H6":"-","H7":"-","H8":"-","H9":"-",
  63.         "I0":"-","I1":"-","I2":"-","I3":"-","I4":"-","I5":"-","I6":"-","I7":"-","I8":"-","I9":"-",
  64.         "J0":"-","J1":"-","J2":"-","J3":"-","J4":"-","J5":"-","J6":"-","J7":"-","J8":"-","J9":"-",
  65.         }
  66.     return hit_board
  67.  
  68. def error(): #if I screwed up
  69.     print "D10D3, your code sucks again"
  70.     quit()
  71.    
  72. def display_board(board): #displays selected board neatly
  73.     print "    0 1 2 3 4 5 6 7 8 9"
  74.     print "   ____________________"
  75.     print "A |"+" "+board["A0"]+" "+board["A1"]+" "+board["A2"]+" "+board["A3"]+" "+board["A4"]+" "+board["A5"]+" "+board["A6"]+" "+board["A7"]+" "+board["A8"]+" "+board["A9"]+"|"
  76.     print "B |"+" "+board["B0"]+" "+board["B1"]+" "+board["B2"]+" "+board["B3"]+" "+board["B4"]+" "+board["B5"]+" "+board["B6"]+" "+board["B7"]+" "+board["B8"]+" "+board["B9"]+"|"
  77.     print "C |"+" "+board["C0"]+" "+board["C1"]+" "+board["C2"]+" "+board["C3"]+" "+board["C4"]+" "+board["C5"]+" "+board["C6"]+" "+board["C7"]+" "+board["C8"]+" "+board["C9"]+"|"
  78.     print "D |"+" "+board["D0"]+" "+board["D1"]+" "+board["D2"]+" "+board["D3"]+" "+board["D4"]+" "+board["D5"]+" "+board["D6"]+" "+board["D7"]+" "+board["D8"]+" "+board["D9"]+"|"
  79.     print "E |"+" "+board["E0"]+" "+board["E1"]+" "+board["E2"]+" "+board["E3"]+" "+board["E4"]+" "+board["E5"]+" "+board["E6"]+" "+board["E7"]+" "+board["E8"]+" "+board["E9"]+"|"
  80.     print "F |"+" "+board["F0"]+" "+board["F1"]+" "+board["F2"]+" "+board["F3"]+" "+board["F4"]+" "+board["F5"]+" "+board["F6"]+" "+board["F7"]+" "+board["F8"]+" "+board["F9"]+"|"
  81.     print "G |"+" "+board["G0"]+" "+board["G1"]+" "+board["G2"]+" "+board["G3"]+" "+board["G4"]+" "+board["G5"]+" "+board["G6"]+" "+board["G7"]+" "+board["G8"]+" "+board["G9"]+"|"
  82.     print "H |"+" "+board["H0"]+" "+board["H1"]+" "+board["H2"]+" "+board["H3"]+" "+board["H4"]+" "+board["H5"]+" "+board["H6"]+" "+board["H7"]+" "+board["H8"]+" "+board["H9"]+"|"
  83.     print "I |"+" "+board["I0"]+" "+board["I1"]+" "+board["I2"]+" "+board["I3"]+" "+board["I4"]+" "+board["I5"]+" "+board["I6"]+" "+board["I7"]+" "+board["I8"]+" "+board["I9"]+"|"
  84.     print "J |"+" "+board["J0"]+" "+board["J1"]+" "+board["J2"]+" "+board["J3"]+" "+board["J4"]+" "+board["J5"]+" "+board["J6"]+" "+board["J7"]+" "+board["J8"]+" "+board["J9"]+"|"
  85.     print "  ----------------------"
  86.    
  87. def shipname(ship): #convert ships cell to ship name
  88.     shipname = ""
  89.     if ship == 1:
  90.         shipname = "Aircraft Carrier (5 cells)"
  91.     elif ship == 2:
  92.         shipname = "BattleShip (4 cells)"
  93.     elif ship == 3:
  94.         shipname = "Cruiser (3 cells)"
  95.     elif ship == 4:
  96.         shipname = "Destroyer 1 (2 cells)"
  97.     elif ship == 5:
  98.         shipname = "Destroyer 2 (2 cells)"
  99.     elif ship == 6:
  100.         shipname = "Submarine 1 (1 cell)"
  101.     elif ship == 7:
  102.         shipname = "Submarine 2 (1 cell)"
  103.     else:
  104.         error()
  105.     return shipname
  106.  
  107. def player_setup(board): #Player places his ships
  108.     for i in range (1,8):
  109.         while True:
  110.             os.system('cls')
  111.             intro_banner()
  112.             ships = [5,4,3,2,2,1,1] #cell size of each of the ships
  113.             ship_length = ships[i-1]
  114.             display_board(board)
  115.             print "-= PLAYER BOARD SETUP =-"
  116.             print ""
  117.             print "   For each selection, first choose the top left ship position,"
  118.             print "   then choose to fill to the right or down"
  119.             print "   Where would you like to place %s" % shipname(i)
  120.             choice = choose_cell() #player chooses coordinate
  121.             while True:
  122.                 direction = raw_input ('   Fill in ship across or down? (A or D)> ')
  123.                 direction.lower()
  124.                 if direction == 'a':
  125.                     break
  126.                 elif direction == 'd':
  127.                     break
  128.                 else:
  129.                     print "   Please enter A or D"
  130.             selection = generate_selection(choice,ship_length,direction)
  131.             test = test_selection(selection,board)
  132.             if test == True:
  133.                 board = write_ship(selection,board)
  134.                 break
  135.             else:
  136.                 print ""
  137.                 print "   That ship won't fit there, choose another spot"
  138.                 raw_input ('   <Press a Enter to select again>')
  139.     return board
  140.    
  141. def AI_setup(board): #AI places ships
  142.     for i in range (1,8):
  143.         while True:
  144.             ships = [5,4,3,2,2,1,1] #cell size of each of the ships (+1 to help for/next iteration)
  145.             ship_length = ships[i-1]
  146.             choice = choose_cell_AI() #Computer chooses coordinate
  147.             dir_pos = ["a","d"] #possible orientations: Across or Down
  148.             direction = random.choice(dir_pos)
  149.             selection = generate_selection(choice,ship_length,direction)
  150.             test = test_selection(selection,board)
  151.             if test == True:
  152.                 board = write_ship(selection,board)
  153.                 print "Generating game boards..."
  154.                 break
  155.             else:
  156.                 print "Generating game boards..."
  157.     return board
  158.  
  159. def choose_cell(): #Asks player for cell choice, returns value
  160.     while True:
  161.         row = raw_input ('   What Row? (A-J)> ')
  162.         row = row.upper()
  163.         letters = "ABCDEFGHIJ"
  164.         if row in letters:
  165.             break
  166.         else:
  167.             print "   Please type a letter from A to J"
  168.             raw_input ('   <Press a Enter to select again>')
  169.     while True:
  170.         column = raw_input('   What Column? (0-9)> ')
  171.         #column = int(column)
  172.         numbers = '0,1,2,3,4,5,6,7,8,9'
  173.         if column in numbers:
  174.             break
  175.         else:
  176.             print "   Please type a number from 1 to 10"
  177.             raw_input ('   <Press a Enter to select again>')
  178.     choice = ""
  179.     choice = choice + row + column
  180.     return choice
  181.    
  182. def choose_cell_AI(): #Generates random cell choice, returns value
  183.     letters = ["A","B","C","D","E","F","G","H","I","J"]
  184.     numbers = ["0","1","2","3","4","5","6","7","8","9"]
  185.     row = random.choice(letters)
  186.     column = random.choice(numbers)
  187.     choice = ""
  188.     choice = choice + row + column
  189.     return choice
  190.  
  191. def generate_selection(choice,ship_length,direction): #Generates list of selected cells to place ship
  192.     selection = [choice]
  193.     if direction == 'a':
  194.         #across stuff
  195.         for i in range(1,ship_length):
  196.             addrow = choice[0]
  197.             addnum = choice[1]
  198.             addnum = int(addnum)
  199.             addnum += i
  200.             addnum = str(addnum)
  201.             addselect = ""
  202.             addselect = addselect + addrow + addnum
  203.             selection.append(addselect)
  204.     else:
  205.         #down stuff
  206.         for i in range(1,ship_length):
  207.             addrow = ord(choice[0]) #translates the letter into and ascii number
  208.             addrow += (i) #iterates it by 1
  209.             addrow = chr(addrow) #translates it back into a letter!
  210.             addnum = choice[1]
  211.             addselect = ""
  212.             addselect = addselect + addrow + addnum
  213.             selection.append(addselect)
  214.     return selection
  215.  
  216. def test_selection(selection,board): #checks if all cells for selected ship placement are allowed
  217.     test = False
  218.     badcell = False
  219.     for i in range(0,len(selection)): #creates loop based on length of selection
  220.         if selection[i] in board:
  221.             if board[selection[i]] == "-":
  222.                 fnord = "fnord"
  223.             else:
  224.                 badcell = True         
  225.         else:
  226.             badcell = True
  227.     if badcell == True:
  228.         test = False
  229.     else:
  230.         test = True
  231.     return test
  232.    
  233. def test_shot(selection,board): #checks if shot is valid
  234.     test = False
  235.     badcell = False
  236.     for i in range(0,len(selection)): #creates loop based on length of selection
  237.         if selection[i] in board:
  238.             if board[selection[i]] == "-":
  239.                 fnord = "fnord"
  240.             elif board[selection[i]] == "O":
  241.                 fnord = "fnord"
  242.             elif board[selection[i]] == "X":
  243.                 badcell = True
  244.             elif board[selection[i]] == "@":
  245.                 badcell = True
  246.             else:
  247.                 badcell = True         
  248.         else:
  249.             badcell = True
  250.     if badcell == True:
  251.         test = False
  252.     else:
  253.         test = True
  254.     return test
  255.  
  256. def write_ship(selection,board): #writes the (now checked) ship selection to the board
  257.     writecell = 0
  258.     for i in range(0,len(selection)): #creates loop based on length of selection
  259.         board[selection[i]] = "O"
  260.     return board
  261.    
  262. def intro_banner(): #fancy graphics
  263.     print"""
  264.     ______  ___ _____ _____ _      _____ _____ _   _ ___________
  265.     | ___ \/ _ \_   _|_   _| |    |  ___/  ___| | | |_   _| ___ \      
  266.     | |_/ / /_\ \| |   | | | |    | |__ \ `--.| |_| | | | | |_/ /
  267.     | ___ \ _  || |   | | | |    |  __| `--. \ _  | | | |  __/
  268.     | |_/ / | | || |   | | | |____| |___/\__/ / | | |_| |_| |    
  269.     \____/\_| |_/\_/   \_/ \_____/\____/\____/\_| |_/\___/\_|    
  270.    
  271.     Ver 0.5  by D10D3
  272.     """
  273.  
  274. def did_anyone_win(p_board,c_board):
  275.     o_count_player = 0
  276.     o_count_computer = 0
  277.     for i in range(0,10):
  278.         loop = i
  279.         loop = str(loop)
  280.         index_A = "A" + loop
  281.         index_B = "B" + loop
  282.         index_C = "C" + loop
  283.         index_D = "D" + loop
  284.         index_E = "E" + loop
  285.         index_F = "F" + loop
  286.         index_G = "G" + loop
  287.         index_H = "H" + loop
  288.         index_I = "I" + loop
  289.         index_J = "J" + loop
  290.         #check player board for "O"s
  291.         if p_board[index_A] == "O":
  292.             o_count_player += 1
  293.         elif p_board[index_B] == "O":
  294.             o_count_player += 1
  295.         elif p_board[index_C] == "O":
  296.             o_count_player += 1
  297.         elif p_board[index_D] == "O":
  298.             o_count_player += 1
  299.         elif p_board[index_E] == "O":
  300.             o_count_player += 1
  301.         elif p_board[index_F] == "O":
  302.             o_count_player += 1
  303.         elif p_board[index_G] == "O":
  304.             o_count_player += 1
  305.         elif p_board[index_H] == "O":
  306.             o_count_player += 1
  307.         elif p_board[index_I] == "O":
  308.             o_count_player += 1
  309.         elif p_board[index_J] == "O":
  310.             o_count_player += 1
  311.         #check computer board for "O"s
  312.         if c_board[index_A] == "O":
  313.             o_count_computer += 1
  314.         elif c_board[index_B] == "O":
  315.             o_count_computer += 1
  316.         elif c_board[index_C] == "O":
  317.             o_count_computer += 1
  318.         elif c_board[index_D] == "O":
  319.             o_count_computer += 1
  320.         elif c_board[index_E] == "O":
  321.             o_count_computer += 1
  322.         elif c_board[index_F] == "O":
  323.             o_count_computer += 1
  324.         elif c_board[index_G] == "O":
  325.             o_count_computer += 1
  326.         elif c_board[index_H] == "O":
  327.             o_count_computer += 1
  328.         elif c_board[index_I] == "O":
  329.             o_count_computer += 1
  330.         elif c_board[index_J] == "O":
  331.             o_count_computer += 1
  332.         else:
  333.             fnord = "fnord"
  334.     if o_count_player == 0:
  335.         winner = "computer"
  336.     elif o_count_computer == 0:
  337.         winner = "player"
  338.     else:
  339.         winner = "null"
  340.     return winner
  341.  
  342. def win():
  343.     print "You Win!"
  344.     while True:
  345.         again = raw_input ('   Play Again? Y or N> ')
  346.         again.lower
  347.         if again == "y":
  348.             break
  349.         elif again == "n":
  350.             print "Thanks for playing!"
  351.             quit()
  352.         else:
  353.             print "   Enter Y or N"
  354.            
  355. def lose():
  356.     print "You Lose!"
  357.     while True:
  358.         again = raw_input ('   Play Again? Y or N> ')
  359.         again.lower
  360.         if again == "y":
  361.             break
  362.         elif again == "n":
  363.             print "Thanks for playing!"
  364.             quit()
  365.         else:
  366.             print "   Enter Y or N"
  367.  
  368. def man_or_ran(player_board): #choose manual or random player board setup
  369.     while True: #choose manual or random player ship placement
  370.         print ""
  371.         print '   Would you like to place your ships manually or randomly?'
  372.         setup = raw_input('   <M>anual or <R>andom? > ')
  373.         setup.lower()
  374.         if setup == 'm':
  375.             player_board = player_setup(player_board)
  376.             break
  377.         elif setup == 'r':
  378.             player_board = AI_setup(player_board)
  379.             break
  380.         else:
  381.             print "   Please enter M or R"
  382.     return player_board
  383.  
  384. #***INITIALIZE AND SETUP***
  385. os.system('cls')
  386. intro_banner()
  387. player_board = {}
  388. computer_board = {}
  389. hit_board = {}
  390. player_board = initialize_player(player_board)
  391. computer_board = initialize_computer(computer_board)
  392. hit_board = initialize_hit(hit_board)
  393. player_board = man_or_ran(player_board)
  394. computer_board = AI_setup(computer_board)
  395.  
  396.  
  397. #***BEGIN GAME LOOP***
  398. while True:
  399.     while True: #did anyone win? if so play again?
  400.         winner = did_anyone_win(player_board,computer_board)
  401.         if winner == "player":
  402.             win()
  403.             os.system('cls')
  404.             intro_banner()
  405.             player_board = {}
  406.             computer_board = {}
  407.             hit_board = {}
  408.             player_board = initialize_player(player_board)
  409.             computer_board = initialize_computer(computer_board)
  410.             hit_board = initialize_hit(hit_board)
  411.             player_board = man_or_ran(player_board)
  412.             computer_board = AI_setup(computer_board)
  413.             break
  414.         elif winner == "computer":
  415.             lose()
  416.             os.system('cls')
  417.             intro_banner()
  418.             player_board = {}
  419.             computer_board = {}
  420.             hit_board = {}
  421.             player_board = initialize_player(player_board)
  422.             computer_board = initialize_computer(computer_board)
  423.             hit_board = initialize_hit(hit_board)
  424.             player_board = man_or_ran(player_board)
  425.             computer_board = AI_setup(computer_board)
  426.             break
  427.         else:
  428.             fnord = "fnord"
  429.             break
  430.     os.system('cls')
  431.     intro_banner()
  432.     print "-= PLAYER SHIPS =-"
  433.     print ""
  434.     display_board(player_board)
  435.     print ""
  436.     print "-= Enemy =-"
  437.     display_board(hit_board) #Show the player where they have shot(and hit) so far
  438.     print ""
  439.    
  440.     #Player Attack
  441.     while True:
  442.         print "   Choose A Cell to Attack!"
  443.         target = choose_cell() #player chooses coordinate
  444.         shooting = [target]
  445.         test = test_shot(shooting,computer_board)
  446.         print ""
  447.         if test == True:
  448.             if computer_board[target] == "-":
  449.                 computer_board[target] = "X"
  450.                 hit_board[target] = "X"
  451.                 print "   You didn't hit anything."
  452.                 break
  453.             else:
  454.                 computer_board[target] = "@"
  455.                 print "You hit and enemy ship!"
  456.                 hit_board[target] = "@"
  457.                 break
  458.         else:
  459.             print ""
  460.             print "   That is not a valid target!"
  461.             raw_input ('   <Press a Enter to select again>')
  462.    
  463.     #Computer Attack
  464.     while True:
  465.         target = choose_cell_AI() #AI chooses coordinate
  466.         shooting = [target]
  467.         test = test_shot(shooting,player_board)
  468.         if test == True:
  469.             if player_board[target] == "-":
  470.                 player_board[target] = "X"
  471.                 print "   Computer attacks %s and misses" % target
  472.                 raw_input ('   <Press a Enter to continue>')
  473.                 break
  474.             else:
  475.                 player_board[target] = "@"
  476.                 print "   Computer attacks %s and hits!" % target
  477.                 raw_input ('   <Press a Enter to continue>')
  478.                 break
  479.         else:
  480.             fnord = "fnord"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement