#4o: CODDING IN PYTHON {4X4 BATTLESHIPS}

import random
ass1=[]
ass2=[]
com=['b1','b2','b3','b4','a1','a2','a3','a4','c1','c2','c3','c4','d1','d2','d3','d4']
pi=str(input('Enter your name: '))
ap=['a1','a2','a3','a4']
bp=['b1','b2','b3','b4']
cp=['c1','c2','c3','c4']
dp=['d1','d2','d3','d4']

apq=['a1','a2','a3','a4']
bpq=['b1','b2','b3','b4']
cpq=['c1','c2','c3','c4']
dpq=['d1','d2','d3','d4']

ap1=['a1','a2','a3','a4']
bp1=['b1','b2','b3','b4']
cp1=['c1','c2','c3','c4']
dp1=['d1','d2','d3','d4']

ap2=['a1','a2','a3','a4']
bp2=['b1','b2','b3','b4']
cp2=['c1','c2','c3','c4']
dp2=['d1','d2','d3','d4']
totty=[ap,bp,cp,dp]
totty1=[ap1,bp1,cp1,dp1]
totty2=[ap2,bp2,cp2,dp2]

def cool(x):
    for ww in (x):
        for et in (ww):
            print(et,' ',end="")
        print()
cool(totty)
for ty in range(10):
    nu=int(input('Enter number of ships: '))
    if nu>0 and nu<16:
        for yyy in range(1):
            if nu>0 and nu<16:
                for tt in range(1,nu+1):
                    for eee in range(10):
                        
                        sh='Ships location from above, for ship',tt,': '
                        nt=str(input(sh))
                        if nt in (com):
                            if nt in (ass1):
                                print(pi,' location already having one ship,please retry.')
                            else:
                                for az in (totty):
                                    if nt in (az):
                                                ass1.append(nt)
                                                ooo=az.index(nt)
                                                az.remove(nt)
                                                az.insert(ooo,'##')
                                break
        
                            
                        else:
                            print(pi,' location not avaialable,please retry.')
                            
                            
                xxx=ass1.copy()
                cool(totty)

                
                for to in range(1,nu+1):
                    
                    nt1=random.choice(com)
                    
                    for az1 in (totty1):
                        if nt1 in (az1):
                            ass2.append(nt1)
                            ooo=az1.index(nt1)
                            az1.remove(nt1)
                            az1.insert(ooo,'##')
                    com.remove(nt1)
                xxx1=ass2.copy()
                zz1=[]
                zz2=[]
                comy=['b1','b2','b3','b4','a1','a2','a3','a4','c1','c2','c3','c4','d1','d2','d3','d4']
                comy1=['b1','b2','b3','b4','a1','a2','a3','a4','c1','c2','c3','c4','d1','d2','d3','d4']
                for ee in range(1,17):
                    for it in range(10):
                        nan='Where to hit now ',pi
                        
                        qw=str(input(nan))
                        if qw in (comy):
                            if qw in (zz1):
                                print(pi,' you have already hitted it, please retry.')
                                break
                            
                            if it==9:
                                break
                        
                        
                            else:
                                
                                zz1.append(qw)
                                                
                                for az in (totty2):
                                        if qw in (az):
                                                
                                                ooo=az.index(qw)
                                                az.remove(qw)
                                                az.insert(ooo,'**')
                                                if qw in(ass2):
                                                        ass2.remove(qw)
                                print('H   I   T   S')
                                cool(totty2)
                                print(pi,' you have already hitted: ',zz1)
                                break
                        else:
                            print('Unknown location')
                    if it>8:
                        
                        break
                    
                    else:   
                        print()
                        qe=random.choice(comy1)
                                
                        if qe in (ass1):
                            print(pi,' your ship at ',qe,' got hit;')
                            ass1.remove(qe)
                            print(pi,' your available ships are at: ',ass1)
                        comy1.remove(qe)
                        if len(ass1)==0:
                            print('Computer defeated you. ',pi)
                            print('Better luck next time.')
                            print('Compter left ',ass2)
                            
                            print('Computers ship were at ',xxx1)
                            print(pi,' your ships were at ',xxx)
                            break
                        if len(ass2)==0:
                            print(pi,'You won')
                            print('Thank you for playing.')
                            print('Computers ship were at ',xxx1)
                            
                            
                            print(pi,' your ships were at ',xxx)
                            break
        break
    else:
        print('Either wrong input or out of range (1-16),please retry.')

                
Download CODES here.         
    

Comments