13 / 19 - Battleship ! Work Code with Bonus!


#1



from random import randint
plateau = []

for x in range(0, 5):
    plateau.append(["O"] * 5)

def afficher_plateau(plateau):
    for ligne in plateau:
        print " ".join(ligne)

def alea_ligne(plateau):
    return randint(0, len(plateau) - 1)

def alea_col(plateau):
    return randint(0, len(plateau[0]) - 1)

bateau_x = alea_ligne(plateau)
bateau_y = alea_col(plateau)

# From here Everything is in the loop for! 

for tour in range(5):
    tir_x = tir_y = "" # very important to Reset values of Variables every turn.
    print "\n" * 11    # Clear screen every turn.
    afficher_plateau(plateau)
    print "Solution : line= ",bateau_x+1, "  ", "column= ",bateau_y+1 # do not cheat remove this line !!!.
    print "Number of remaining chances > " + str(5-tour)
    
    while type(tir_x) != int:
        
        tir_x = raw_input("Which line do you want to Choose ?")
        
        if tir_x in ('1','2','3','4','5'): # Check if tir_x is one of these Numbers.
            tir_x = int(tir_x) - 1
        
        else:
            print "Enter a valid line number Please ! "
            print "Between 1 and 5"

    while type(tir_y) != int:
        
        tir_y = raw_input("Which column do you want to Choose ?")
        
        if tir_y in ('1','2','3','4','5'):
            tir_y = int(tir_y) - 1   
        else:
            print "Enter a valid column number Please ! "
            print "Between 1 and 5"

    if tir_x == bateau_x and tir_y == bateau_y:   
        from time import sleep

        for strx in "*Bravo! You Have WIN*":      # This loop is added just for fun and to make some animation.
            sleep(.1)                             # wait 0.1 second
            print strx,
        break
    else:
        if(plateau[tir_x ][tir_y ] == "X"):
            print "This case was already Discovered."
            print "Press a key ..."
            print raw_input("")
        else:
            print "Try again ..."
            plateau[tir_x ][tir_y ] = "X"

        if (tour == 4): print "\n" * 11 , "X X X   Game Over   X X X"

# This code was corrected to prevent ERROR when user enter string or a number not in range.
# by: Jslearn16  ,I hope its helpfull.