Search
Latest topics
Who is online?
In total there are 3 users online :: 0 Registered, 0 Hidden and 3 Guests :: 1 BotNone
Most users ever online was 38 on Sun Mar 19, 2023 10:07 pm
Most Viewed Topics
Battle ship in python
2 posters
Page 1 of 1
Battle ship in python
So I made this battle ship game in python a while back, decided to post it here.
Code:
Code:
#made by HZ admin
board = [] #declares the game board
for x in range(5):
board.append(["O"] * 5)#fills the board with o's
def print_board(board):#makes a function to print board.
for row in board:
print " ".join(row)
def play_game():
print "Let's play Battleship!"
player1=raw_input('What is your name:')
print 'Let your opponent enter his name'
player2=raw_input('What is your name:')
print player1 + ' ' + 'is going first. He will hide his battleship and'+ ' ' + player2 +' '+ 'will guess the location.'
point1=0
point2=0
turns=int(raw_input("How many times do you want to play the game?"))
while turns!=0:#this is where the entire game is played.
print_board(board)
ship_row=int(raw_input("What row is your battleship on?"))
ship_col=int(raw_input("What column is your battleship on?"))
if (ship_row < 0 or ship_row > 4) or (ship_col < 0 or ship_col > 4):
print "Oops, that's not even in the ocean."
print 'Enter a number greater than zero and less than five'
ship_row=int(raw_input("What row is your battleship on?"))
ship_col=int(raw_input("What column is your battleship on?"))
elif(board[ship_row][ship_col] == "X"):
print "You entered that one already."
ship_row=int(raw_input("What row is your battleship on:"))
ship_col=int(raw_input("What column is your battleship on:"))
for i in range(0,27):
print ' '
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
point2+=1
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
point1+=1
if turns==1:
print_board(board)
turns-=1
print player1+':'+str(point1)
print player2+':'+str(point2)
if point1>point2:
print 'Congragulations' +' ' + player1 +' ' + 'you beat' + ' ' + player2 + ' ' + 'in battleship.'
elif point2>point1:
print 'Congragulations' + ' '+player2 +' ' + 'you beat' + ' ' + player1 + ' ' + 'in battleship.'
else:
print 'Wow you both tied each other'
play_game()
board = [] #declares the game board
for x in range(5):
board.append(["O"] * 5)#fills the board with o's
def print_board(board):#makes a function to print board.
for row in board:
print " ".join(row)
def play_game():
print "Let's play Battleship!"
player1=raw_input('What is your name:')
print 'Let your opponent enter his name'
player2=raw_input('What is your name:')
print player1 + ' ' + 'is going first. He will hide his battleship and'+ ' ' + player2 +' '+ 'will guess the location.'
point1=0
point2=0
turns=int(raw_input("How many times do you want to play the game?"))
while turns!=0:#this is where the entire game is played.
print_board(board)
ship_row=int(raw_input("What row is your battleship on?"))
ship_col=int(raw_input("What column is your battleship on?"))
if (ship_row < 0 or ship_row > 4) or (ship_col < 0 or ship_col > 4):
print "Oops, that's not even in the ocean."
print 'Enter a number greater than zero and less than five'
ship_row=int(raw_input("What row is your battleship on?"))
ship_col=int(raw_input("What column is your battleship on?"))
elif(board[ship_row][ship_col] == "X"):
print "You entered that one already."
ship_row=int(raw_input("What row is your battleship on:"))
ship_col=int(raw_input("What column is your battleship on:"))
for i in range(0,27):
print ' '
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
point2+=1
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
point1+=1
if turns==1:
print_board(board)
turns-=1
print player1+':'+str(point1)
print player2+':'+str(point2)
if point1>point2:
print 'Congragulations' +' ' + player1 +' ' + 'you beat' + ' ' + player2 + ' ' + 'in battleship.'
elif point2>point1:
print 'Congragulations' + ' '+player2 +' ' + 'you beat' + ' ' + player1 + ' ' + 'in battleship.'
else:
print 'Wow you both tied each other'
play_game()
Re: Battle ship in python
This is pretty freakin aweome!!!! Great work!!!
kyle5647- Member
- Posts : 40
Join date : 2014-04-08
Similar topics
» Python to exe
» Multiencoder (Python)
» Creating a new Python script
» Executing and debugging Python
» Setting breakpoints using Python
» Multiencoder (Python)
» Creating a new Python script
» Executing and debugging Python
» Setting breakpoints using Python
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Tue Feb 02, 2021 7:12 am by manas41
» SQL injection and Quote escaping
Sun Jun 28, 2015 11:42 am by ADS1
» [TUT] Chmod: Files & Permissions [TUT]
Thu Jun 04, 2015 12:45 pm by Guest
» Reaver pixiewps
Thu Jun 04, 2015 12:23 pm by voidfletcher
» How To Crash Someone's Skype in 10 SECONDS
Thu Jun 04, 2015 12:20 pm by voidfletcher
» Internet Security & IP Security (IPSec)
Mon May 18, 2015 9:00 pm by voidfletcher
» [Python] Infinite / Definite File Generator
Mon May 18, 2015 8:58 pm by ADS1
» [C#] String Case-Inversion
Mon May 18, 2015 8:57 pm by ADS1
» Rekall Memory Forensic Framework
Sat May 16, 2015 8:55 pm by ADS1