Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- TODO
- Make multiple battleships:
- You will need to be careful because you need to make sure that you do not place battleships on top of each other on the game board.
- You will also want to make sure that you balance the size of the board with the number of ships so the game is still challenging and fun to play.
- Make battleships of different sizes:
- This is trickier than it sounds. All the parts of the battleship need to be vertically or horizontally touching.
- You will need to make sure you do not accidentally place part of a ship off the side of the board.
- Make your game a two-player game:
- Use functions to allow your game to have more features like rematches, statistics and more!
- """
- import os
- import time
- from random import randint
- #cls function
- clear = lambda: os.system('cls')
- #declares board as an empty list
- board = []
- ships = {
- "Carrier": 5,
- "Battleship": 4,
- "Cruiser": 3,
- "Submarine": 3,
- "Destroyer": 2
- }
- turn = 0
- for x in range(11):
- board.append(["O"] * 10)
- def print_menuscreen():
- print
- print " Battleship! Written by samillwong" + "\n" + " ",
- line()
- print
- print " Arrange your ships on the grid by inputting the coordinates" + "\n" + " (e.g. A6) and orientation (e.g. H)." + "\n"
- print " Take turns firing a salvo at your enemy by inputting the" + "\n" + " coordinate you wish to fire at. (e.g. C3)" + "\n"
- print " The first player to sink all opposing ships wins." + "\n" + "\n" + " ",
- line()
- print
- ask_username()
- def userlogin():
- user = raw_input("Username: ")
- passw = raw_input("Password: ")
- f = open("users.txt", "w+")
- for line in f.readlines():
- us, pw = line.strip().split("|")
- if (user in us) and (passw in pw):
- print "Login successful!"
- return True
- print "Wrong username/password"
- return False
- def ask_username():
- username = ""
- while username == "":
- username = raw_input(" Enter your username to start: ")
- #temp
- print "\n" + " Welcome to Battleship, " + username + "!"
- time.sleep(3)
- #WORK IN PROGRESS
- """
- f = open("players.txt", "a+")
- f.seek(0)
- first_char = f.read(1)
- if not first_char:
- print f.write("List of players:")
- else:
- f.seek(0)
- for line in f.readlines():
- if username in line:
- print "\n" + " Welcome back, " + username + "!"
- time.sleep(3)
- else:
- print "\n" + " Welcome to Battleship, " + username + "!"
- time.sleep(3)
- f.write(username)
- f.close()
- """
- def print_board(board):
- print_header()
- #print horizontal grid numbers
- print " ",
- for i in range(10):
- print " " + str(i+1) + " ",
- print "\n"
- #print horizontal lines and cell dividers
- for i in range(10):
- if i != 9:
- print " ",
- print " -----------------------------------------------------------"
- for j in range(11):
- if j != 11:
- print " | ",
- print
- print " -----------------------------------------------------------"
- def print_header():
- print "\n" + " Turn", turn + 1
- print " ",
- line()
- def line():
- print "-----------------------------------------------------------------" #65 dashes
- def random_row(board):
- return randint(1, len(board) - 1)
- def random_col(board):
- return randint(1, len(board[0]) - 1)
- clear()
- print_menuscreen()
- clear()
- print "\n" + " Let's play Battleship!"
- print_board(board)
- ship_row = random_row(board)
- ship_col = random_col(board)
- #that aint cheating, thats debugging :^)
- print "Debug:", "x =", ship_row + 1,",", "y =", ship_col + 1
- for turn in range(3):
- turn = turn + 1
- guess_row = int(raw_input("\n" + " Guess Row: ")) - 1
- guess_col = int(raw_input("Guess Column: ")) - 1
- if guess_row == ship_row and guess_col == ship_col:
- clear()
- print "\n" + "Congratulations! You sunk my battleship!" + "\n"
- print_header()
- print_board(board)
- break
- else:
- if (guess_row >= 5) or (guess_col >= 5):
- clear()
- print "Oops, that's not even in the ocean."
- elif(board[guess_row][guess_col] == "X"):
- clear()
- print "You guessed that one already."
- else:
- if turn == 3:
- clear()
- print "\n" + "You ran out of turns, game over." + "\n" + "\n" + "Turn 4" + "\n" + "------------------------------"
- else:
- clear()
- print "\n" + "You missed my battleship!"
- board[guess_row][guess_col] = "X"
- print "\n" + "Turn", turn + 1, "\n" + "------------------------------"
- # Print (turn + 1) here!
- print_board(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement