Ayuda con python!!!

Agrupa todas las consultas sobre los lenguajes de programacion como C, C++, Python, Java ...

Ayuda con python!!!

Notapor crispin-cross » Vie Ene 04, 2013 12:26 pm

Hola buenas,necesito ayuda con el juego "hundir la flota" o "el juego de los barcos" para hacerlo con python,si alguien lo tiene o lo sabria hacer,estaria agradecido de que me ayudase.


gracias,un saludo
crispin-cross
 
Mensajes: 2
Registrado: Vie Ene 04, 2013 12:13 pm

Re: Ayuda con python!!!

Notapor kaiser » Sab Ene 05, 2013 10:49 am

Hola crispin. Lo primero, bienvenido a la comunidad.

Perdóname si me equivoco, pero tu petición suena mucho a "En el colegio me han pedido que haga una práctica de programación con un juego de hundir la flota en python. ¿Me lo hacéis?". No es que vea nada malo en ello, pero estaría bien que lo trabajes un poco por tu cuenta y uses el foro para hacer preguntas más concretas tipo ¿Cómo se manejan arrays en python? o ¿Cómo podría implementar esta estrategia?.

Mientras tanto, aquí te dejo un buen libro para empezar con python (http://www.losersjuegos.com.ar/_media/referencia/libros/python_para_todos/python_para_todos.pdf) y la API de pygame traducida por esta comunidad (http://www.losersjuegos.com.ar/traducciones/pygame).

Que lo disfrutes y un saludo!
De la API al IDE y del IDE a la API.
Avatar de Usuario
kaiser
 
Mensajes: 121
Registrado: Mié Nov 24, 2010 7:47 pm
Ubicación: Madrid (España)

Re: Ayuda con python!!!

Notapor crispin-cross » Mar Ene 08, 2013 11:16 am

he encontrado esto,¿serviria para algo?

un saludo

from pprint import pprint
import random
import sys

def main():

aircraft_carrier = []
battle_ship = []
submarine = []
destroyer = []
patrol_boat = []

def new_board(rows = 10, columns = 10, default_value = 0):
board = []
for i in xrange(rows):
board.append([default_value] * columns)
return board

def fire_shot():
print "Select x, y coordinates to fire a shot."

fire = input('You may fire when ready: ')

x = fire[0]
y = fire[1]

if battleship[x][y] == 1:
print "\033[95m" + "hit!" + "\033[0m"
battleship[x][y] = 5
viewer[x][y] = 5
elif battleship[x][y] == 5:
print "\033[91m" + "already hit there!" + "\033[0m"
else:
print "\033[93m" + "miss!" + "\033[0m"
battleship[x][y] = 7
viewer[x][y] = 7

track_hits(x, y)
pprint(viewer)
fire_shot()

def track_hits(x, y):
all_ships = [aircraft_carrier] + [battle_ship] + [submarine] + [destroyer] + [patrol_boat]
if battleship[x][y] == 5:
for ship in all_ships:
for spot in ship:
if spot == [x, y]:
ship.remove(spot)

for ship in all_ships:
if len(ship) == 0:
if all_ships.index(ship) == 0:
print "You sunk the aircraft carrier!"
break
elif all_ships.index(ship) == 1:
print "You sunk the battle ship!"
break
elif all_ships.index(ship) == 2:
print "You sunk the submarine!"
break
elif all_ships.index(ship) == 3:
print "You sunk the destroyer!"
break
elif all_ships.index(ship) == 4:
print "You sunk the patrol boat!"
break

if len(all_ships) == 0:
print "You Win!"
sys.exit()

def find_open_spot(row, row_number, required_number, num_repeats, stop_after_match=True):
i = random.randrange(0, 9 - num_repeats)

while i < len(row):
if [required_number]*num_repeats == row[i:i + num_repeats]:
row[i:i+num_repeats] = [1]*num_repeats

placement = [k for k in range(len(row)) if row[k] == '1']

if num_repeats == 5:
for coords in xrange(5):
aircraft_carrier.append([row_number, i+coords])
elif num_repeats == 4:
for coords in xrange(4):
battle_ship.append([row_number, i+coords])
elif num_repeats == 3:
if len(submarine) < 3:
for coords in xrange(3):
submarine.append([row_number, i+coords])
else:
for coords in xrange(3):
destroyer.append([row_number, i+coords])
elif num_repeats == 2:
for coords in xrange(2):
patrol_boat.append([row_number, i+coords])

i += num_repeats
if stop_after_match:
break
else:
i += 1

return row

def transform_board(battleship, ship_length):
a = battleship
battleship = new_board()
for i in xrange(10):
for j in xrange(10):
battleship[i][j] = a[j][i]

for i in aircraft_carrier:
i.reverse()

for i in battle_ship:
i.reverse()

for i in submarine:
i.reverse()

for i in destroyer:
i.reverse()

for i in patrol_boat:
i.reverse()

return battleship

def place_ship(ship):
i = random.randrange(0, 10)
row = battleship[i]
add_ship = find_open_spot(row, i, 0, ship)
battleship[i] = add_ship
return battleship

#init battleship board
battleship = new_board(10, 10)

#init viewers board
viewer = new_board(10, 10)

#init ships to be placed
ships = [5, 4, 3, 3, 2]

for i in xrange(5):
battleship = place_ship(ships[i])
if (int(round(random.random())) == 1):
battleship = transform_board(battleship, i)

# debugging
#print 'aircraft carrier', aircraft_carrier
#print 'battle ship', battle_ship
#print 'submarine', submarine
#print 'destroyer', destroyer
#print 'patrol boat', patrol_boat

pprint(viewer)
fire_shot()

if __name__ == "__main__":
print 'Directions: Game board is 10 zero indexed rows, 10 zero indexed columns. Enter x, y coordinates to fire a shot. A hit is indicated by 5, a miss by 7.'
main()

# left to do
# game over is not working
# displaying the sunk ship seems to not work
# add error handling for wrong user input
# eventually support AI from computer
# add x, y numbers to help with coordinate selection
Última edición por hugoruscitti el Lun Ene 28, 2013 1:54 pm, editado 1 vez en total
Razón: Agregué los tags python para que el código se vea coloreado.
crispin-cross
 
Mensajes: 2
Registrado: Vie Ene 04, 2013 12:13 pm

Re: Ayuda con python!!!

Notapor shackra » Lun Ene 28, 2013 2:44 am

por favor, usar los tags [python] que da cáncer de ojo ver ese código así :-/
Avatar de Usuario
shackra
 
Mensajes: 308
Registrado: Lun Jun 15, 2009 4:10 pm
Ubicación: Costa Rica


Volver a Sobre lenguajes de programación

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 1 invitado