Professional Documents
Culture Documents
import os
# Importing the random module
import random
# Importing the time module
import time
"""
GAME SPECIFIC AUTHOR CONFIGURATION
"""
# Application Name
app_name = "BattleShip Multiplayer"
# Application Version
app_version = "1.3"
# Application Author
app_author = "CodeBarbarian"
"""""""""""""""""""""""""""""""""
GAME SPECIFIC VARIABLES BELOW
"""""""""""""""""""""""""""""""""
# Player boards
player1_board = []
player2_board = []
# Player Names
player1_name = ""
player2_name = ""
# Overall Game Conditions
gameWon = False
gameTurns = 1
gameWinner = ""
gameLoser = ""
# When the game begins in seconds
gameStart_time = 0
# When the game ends in seconds
gameEnd_time = 0
_ _ _
||||||
_____ _
/ ___| | (_)
print "
print
print
def randomizePosition(board):
return random.randint(0, len(player1_board) -1)
"""""""""""""""""""""""""""""""""
GAME SPECIFIC LOGIC
"""""""""""""""""""""""""""""""""
# Appending the vowel oceanChar to the boards
for i in range(boardColRow):
player1_board.append([str(oceanChar)] * boardColRow)
player2_board.append([str(oceanChar)] * boardColRow)
player1_ship_col = randomizePosition(player1_board)
# Setting the position of player two's ship
player2_ship_row = randomizePosition(player2_board)
player2_ship_col = randomizePosition(player2_board)
"""""""""""""""""""""""""""""""""
GAME START
"""""""""""""""""""""""""""""""""
# Clear the screen to make it prettier
clearScreen()
# Print the game logo
gameLogo()
# Set the player names
player1_name = raw_input("Player One's Name: ")
player2_name = raw_input("Player Two's Name: ")
# Begin the take time sequence
gameStart_time = time.time()
# The gameloop
while 1:
if gameTurns <= 1:
acknowledge(player1_name, player2_name)
"""
Player One's Gaming Conditions
"""
clearScreen()
gameLogo()
print
print "Number of turns played: " + str(gameTurns)
print
print str(player1_name) + "'s turn!"
print
print displayMessage(5, player2_name)
# Print Player 2's Ocean without the ship on it
printPlayerOppositeBoard(player2_board, boardChar)
print
print
print displayMessage(6, player2_name)
# Print Player 1's Ocean with ship on it
printPlayerBoard(player1_board, player1_ship_row, player1_ship_col, boardChar,
shipChar)
# General Game Conditions
print
print
player1_guessedRow = input(str(player1_name) + " Guess Row: ")
player1_guessedCol = input(str(player1_name) + " Guess Col: ")
if player1_guessedRow == player2_ship_row and player1_guessedCol ==
player2_ship_col:
print displayMessage(1, player2_name)
gameWon = True
gameWiner = str(player1_name)
gameLoser = str(player2_name)
break
else:
if (player1_guessedRow < 0 or player1_guessedRow >= boardColRow) \
or (player1_guessedCol < 0 or player1_guessedCol >= boardColRow):
print
print displayMessage(2)
print
acknowledgeFailure()
acknowledge(player2_name, player1_name)
elif player2_board[player1_guessedRow][player1_guessedCol] == str(firedChar):
print
print displayMessage(3)
print
acknowledgeFailure()
acknowledge(player2_name, player1_name)
else:
print
print displayMessage(4, player2_name)
print
acknowledgeFailure()
player2_board[player1_guessedRow][player1_guessedCol] = str(firedChar)
acknowledge(player2_name, player1_name)
"""
Player Two's Gaming Conditions
"""
clearScreen()
gameLogo()
print
print "Number of turns played: " + str(gameTurns)
print
print str(player2_name) + "'s turn!"
print
if gameWon:
# End the take time sequence
gameEnd_time = time.time()
# Print congratulation message
print "Congratulations " + str(gameWinner) + " for shooting down " +
str(gameLoser) + "'s ship! in " + str(gameTurns) + " turns!"
print
# Print game time
print "The game took, " + str(gameEnd_time - gameStart_time) + " seconds to
play!"
def print_board(s,board):
# WARNING: This function was crafted with a lot of attention. Please be aware
that any
#
print "The " + player + "'s board look like this: \n"
for i in range(10):
elif s == "u":
print board[i][j],
elif s == "c":
if board[i][j] == "*" or board[i][j] == "$":
print board[i][j],
else:
print " ",
if j != 9:
print " | ",
print
def user_place_ships(board,ships):
print_board("u",board)
print "Placing a/an " + ship
x,y = get_coor()
ori = v_or_h()
valid = validate(board,ships[ship],x,y,ori)
if not valid:
print "Cannot place a ship there.\nPlease take a look at
the board and try again."
raw_input("Hit ENTER to continue")
def computer_place_ships(board,ships):
x = random.randint(1,10)-1
y = random.randint(1,10)-1
o = random.randint(0,1)
if o == 0:
ori = "v"
else:
ori = "h"
valid = validate(board,ships[ship],x,y,ori)
return board
def place_ship(board,ship,s,ori,x,y):
return board
def validate(board,ship,x,y,ori):
return True
def v_or_h():
return user_input
else:
print "Invalid input. Please only enter v or h"
def get_coor():
while (True):
user_input = raw_input("Please enter coordinates (row,col) ? ")
try:
#see that user entered 2 values seprated by comma
coor = user_input.split(",")
if len(coor) != 2:
raise Exception("Invalid entry, too few/many
coordinates.");
except ValueError:
print "Invalid entry. Please enter only numeric values for
coordinates"
except Exception as e:
print e
def make_move(board,x,y):
#make a move on the board and return the result, hit, miss or try again for
repeat hit
if board[x][y] == -1:
return "miss"
elif board[x][y] == '*' or board[x][y] == '$':
return "try again"
else:
return "hit"
def user_move(board):
check_sink(board,x,y)
board[x][y] = '$'
if check_win(board):
return "WIN"
elif res == "miss":
print "Sorry, " + str(x+1) + "," + str(y+1) + " is a miss."
board[x][y] = "*"
elif res == "try again":
print "Sorry, that coordinate was already hit. Please try again"
def computer_move(board):
#generate user coordinates from the user and try to make move
#if move is a hit, check ship sunk and win condition
while(True):
x = random.randint(1,10)-1
y = random.randint(1,10)-1
res = make_move(board,x,y)
if res == "hit":
print "Hit at " + str(x+1) + "," + str(y+1)
check_sink(board,x,y)
board[x][y] = '$'
if check_win(board):
return "WIN"
elif res == "miss":
print "Sorry, " + str(x+1) + "," + str(y+1) + " is a miss."
board[x][y] = "*"
return board
def check_sink(board,x,y):
def check_win(board):
def main():
def gameLogo():
print '''
______
| ___ \
_ _ _
||||||
_____ _
/ ___| | (_)
print "
print
print
gameLogo()
#types of ships
ships = {"Aircraft Carrier":5,
"Battleship":4,
"Submarine":3,
"Destroyer":3,
"Patrol Boat":2}
#ship placement
user_board = user_place_ships(user_board,ships)
comp_board = computer_place_ships(comp_board,ships)
#user move
print_board("c",comp_board)
comp_board = user_move(comp_board)
#computer move
user_board = computer_move(user_board)
if __name__=="__main__":
main()
# Application Name
app_name = "BattleShip Multiplayer"
# Application Version
app_version = "1.3"
# Application Author
app_author = "Samudhbhav"
def print_board(s,board):
# WARNING: This function was crafted with a lot of attention. Please be aware
that any
#
print "The " + player + "'s board look like this: \n"
for i in range(10):
print board[i][j],
elif s == "c":
if board[i][j] == "*" or board[i][j] == "$":
print board[i][j],
else:
print " ",
if j != 9:
print " | ",
print
def user_place_ships(board,ships):
print_board("u",board)
def computer_place_ships(board,ships):
x = random.randint(1,10)-1
y = random.randint(1,10)-1
o = random.randint(0,1)
if o == 0:
ori = "v"
else:
ori = "h"
valid = validate(board,ships[ship],x,y,ori)
return board
def place_ship(board,ship,s,ori,x,y):
return board
def validate(board,ship,x,y,ori):
return True
def v_or_h():
else:
print "Invalid input. Please only enter v or h"
def get_coor():
while (True):
user_input = raw_input("Please enter coordinates (row,col) ? ")
try:
#see that user entered 2 values seprated by comma
coor = user_input.split(",")
if len(coor) != 2:
raise Exception("Invalid entry, too few/many
coordinates.");
except ValueError:
print "Invalid entry. Please enter only numeric values for
coordinates"
except Exception as e:
print e
def make_move(board,x,y):
#make a move on the board and return the result, hit, miss or try again for
repeat hit
if board[x][y] == -1:
return "miss"
elif board[x][y] == '*' or board[x][y] == '$':
return "try again"
else:
return "hit"
def user_move(board):
board[x][y] = '$'
if check_win(board):
return "WIN"
elif res == "miss":
print "Sorry, " + str(x+1) + "," + str(y+1) + " is a miss."
board[x][y] = "*"
elif res == "try again":
print "Sorry, that coordinate was already hit. Please try again"
def computer_move(board):
#generate user coordinates from the user and try to make move
#if move is a hit, check ship sunk and win condition
while(True):
x = random.randint(1,10)-1
y = random.randint(1,10)-1
res = make_move(board,x,y)
if res == "hit":
print "Hit at " + str(x+1) + "," + str(y+1)
check_sink(board,x,y)
board[x][y] = '$'
if check_win(board):
return "WIN"
return board
def check_sink(board,x,y):
def check_win(board):
def gameLogo():
print '''
______
| ___ \
_ _ _
||||||
_____ _
/ ___| | (_)
'''
print "
print "
print
print
def main():
gameLogo()
#types of ships
ships = {"Aircraft Carrier":5,
"Battleship":4,
"Submarine":3,
"Destroyer":3,
"Patrol Boat":2}
user_board.append(copy.deepcopy(ships))
comp_board.append(copy.deepcopy(ships))
#ship placement
user_board = user_place_ships(user_board,ships)
comp_board = computer_place_ships(comp_board,ships)
#user move
print_board("c",comp_board)
comp_board = user_move(comp_board)
#computer move
user_board = computer_move(user_board)
if __name__=="__main__":
main()