Author Topic: [Python]Battleships game  (Read 3257 times)

0 Members and 1 Guest are viewing this topic.

Offline DeXtreme

  • Peasant
  • *
  • Posts: 95
  • Cookies: 8
  • I was there and you never knew.
    • View Profile
    • My Designs
[Python]Battleships game
« on: September 09, 2012, 04:32:25 pm »
I have been learning and praticing python all weekend and after programming a simple Tic-Tac-Toe game(I posted the code in the "Scripting Languages Forum") with a fairly good AI, i got a great idea for another table based-game,Battleships. I'm not done with the coding yet but i will be paste the code soon.  ;D

Offline DeXtreme

  • Peasant
  • *
  • Posts: 95
  • Cookies: 8
  • I was there and you never knew.
    • View Profile
    • My Designs
Re: [Python]Battleships game
« Reply #1 on: September 11, 2012, 09:14:27 pm »
Ok.It's all done.Actually, i was done about hour after my first post but due to some internet issues i couldn't post the code... ;D
 
The code is pretty self--explanatory
Code: [Select]
import string
import random

compboard=[]
playboard=[]
game=False
print "********************************DeXtreme**************************************"
print "    ////////// //////////  ///////////// ///////////// ///        ///////// "
print "   ///    /// ///    ///       ///           ///      ///        ///        "
print "  ////////// //////////       ///           ///      ///        //////////  "
print " ///    /// ///    ///       ///           ///      ///        ///          "
print "////////// ///    ///       ///           ///      ////////// ///////////   "
print "********************************DeXtreme**************************************"
print "To set the pieces,enter the row letter and column number followed by \"h\" to place"
print "place them horizontally or \"v\" to place them vertically...eg A1h"
print "To play,enter the row letter and column number..eg B6"
print "Aircraft Carrier-5 pieces,Battleship-4 pieces,Submarine & Cruiser-3 pieces"
print "Destroyer-2 pieces"




def makeboards():
    for i in range(0,10):
        compboard.append([])
        playboard.append([])
        for j in range(0,10):
            compboard[i].append(" ")
            playboard[i].append(" ")

def printboard():
    print "   0   1   2   3   4   5   6   7   8   9  "
    print "------------------------------------------"
    print "A| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[0][0],playboard[0][1],playboard[0][2],playboard[0][3],playboard[0][4],playboard[0][5],playboard[0][6],playboard[0][7],playboard[0][8],playboard[0][9])
    print "------------------------------------------"
    print "B| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[1][0],playboard[1][1],playboard[1][2],playboard[1][3],playboard[1][4],playboard[1][5],playboard[1][6],playboard[1][7],playboard[1][8],playboard[1][9])
    print "------------------------------------------"
    print "C| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[2][0],playboard[2][1],playboard[2][2],playboard[2][3],playboard[2][4],playboard[2][5],playboard[2][6],playboard[2][7],playboard[2][8],playboard[2][9])
    print "------------------------------------------"
    print "D| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[3][0],playboard[3][1],playboard[3][2],playboard[3][3],playboard[3][4],playboard[3][5],playboard[3][6],playboard[3][7],playboard[3][8],playboard[3][9])
    print "------------------------------------------"
    print "E| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[4][0],playboard[4][1],playboard[4][2],playboard[4][3],playboard[4][4],playboard[4][5],playboard[4][6],playboard[4][7],playboard[4][8],playboard[4][9])
    print "------------------------------------------"
    print "F| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[5][0],playboard[5][1],playboard[5][2],playboard[5][3],playboard[5][4],playboard[5][5],playboard[5][6],playboard[5][7],playboard[5][8],playboard[5][9])
    print "------------------------------------------"
    print "G| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[6][0],playboard[6][1],playboard[6][2],playboard[6][3],playboard[6][4],playboard[6][5],playboard[6][6],playboard[6][7],playboard[6][8],playboard[6][9])
    print "------------------------------------------"
    print "H| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[7][0],playboard[7][1],playboard[7][2],playboard[7][3],playboard[7][4],playboard[7][5],playboard[7][6],playboard[7][7],playboard[7][8],playboard[7][9])
    print "------------------------------------------"
    print "I| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[8][0],playboard[8][1],playboard[8][2],playboard[8][3],playboard[8][4],playboard[8][5],playboard[8][6],playboard[8][7],playboard[8][8],playboard[8][9])
    print "------------------------------------------"
    print "J| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(playboard[9][0],playboard[9][1],playboard[9][2],playboard[9][3],playboard[9][4],playboard[9][5],playboard[9][6],playboard[9][7],playboard[9][8],playboard[9][9])
    print "------------------------------------------"




def printboardc():
    print "   0   1   2   3   4   5   6   7   8   9  "
    print "------------------------------------------"
    print "A| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[0][0],compboard[0][1],compboard[0][2],compboard[0][3],compboard[0][4],compboard[0][5],compboard[0][6],compboard[0][7],compboard[0][8],compboard[0][9])
    print "------------------------------------------"
    print "B| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[1][0],compboard[1][1],compboard[1][2],compboard[1][3],compboard[1][4],compboard[1][5],compboard[1][6],compboard[1][7],compboard[1][8],compboard[1][9])
    print "------------------------------------------"
    print "C| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[2][0],compboard[2][1],compboard[2][2],compboard[2][3],compboard[2][4],compboard[2][5],compboard[2][6],compboard[2][7],compboard[2][8],compboard[2][9])
    print "------------------------------------------"
    print "D| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[3][0],compboard[3][1],compboard[3][2],compboard[3][3],compboard[3][4],compboard[3][5],compboard[3][6],compboard[3][7],compboard[3][8],compboard[3][9])
    print "------------------------------------------"
    print "E| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[4][0],compboard[4][1],compboard[4][2],compboard[4][3],compboard[4][4],compboard[4][5],compboard[4][6],compboard[4][7],compboard[4][8],compboard[4][9])
    print "------------------------------------------"
    print "F| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[5][0],compboard[5][1],compboard[5][2],compboard[5][3],compboard[5][4],compboard[5][5],compboard[5][6],compboard[5][7],compboard[5][8],compboard[5][9])
    print "------------------------------------------"
    print "G| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[6][0],compboard[6][1],compboard[6][2],compboard[6][3],compboard[6][4],compboard[6][5],compboard[6][6],compboard[6][7],compboard[6][8],compboard[6][9])
    print "------------------------------------------"
    print "H| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[7][0],compboard[7][1],compboard[7][2],compboard[7][3],compboard[7][4],compboard[7][5],compboard[7][6],compboard[7][7],compboard[7][8],compboard[7][9])
    print "------------------------------------------"
    print "I| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[8][0],compboard[8][1],compboard[8][2],compboard[8][3],compboard[8][4],compboard[8][5],compboard[8][6],compboard[8][7],compboard[8][8],compboard[8][9])
    print "------------------------------------------"
    print "J| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |" %(compboard[9][0],compboard[9][1],compboard[9][2],compboard[9][3],compboard[9][4],compboard[9][5],compboard[9][6],compboard[9][7],compboard[9][8],playboard[9][9])
    print "------------------------------------------"


def setpieces(board,piece,row,col,oren):
    if piece=="a":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" " and board[row][col+3]==" " and board[row][col+4]==" ":
                    for i in range(0,5):
                        board[row][col+i]="A"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
        if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " and board[row+3][col]==" " and board[row+4][col]==" ":
                    for i in range(0,5):
                        board[row+i][col]="A"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
    elif piece=="b":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" " and board[row][col+3]==" " :
                    for i in range(0,4):
                        board[row][col+i]="B"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
        if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " and board[row+3][col]==" " :
                    for i in range(0,4):
                        board[row+i][col]="B"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
    elif piece=="s":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" ":
                     for i in range(0,3):
                         board[row][col+i]="S"
                     return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
        if oren=="v":
             try:
                 if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " :
                    for i in range(0,3):
                        board[row+i][col]="S"
                    return
                 else:
                    print "Cannot play there"
                    ask(piece)
             except:
                print "Cannot play there"
                ask(piece)
    elif piece=="c":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" ":
                    for i in range(0,3):
                         board[row][col+i]="C"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
        if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " :
                    for i in range(0,3):
                        board[row+i][col]="C"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
    elif piece=="d":
         if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " :
                    for i in range(0,2):
                        board[row][col+i]="D"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)
         if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " :
                    for i in range(0,2):
                        board[row+i][col]="D"
                    return
                else:
                    print "Cannot play there"
                    ask(piece)
            except:
                print "Cannot play there"
                ask(piece)



def comp():
    d1=0
    d2=0
    c1=0
    c2=0
    su1=0
    su2=0
    b1=0
    a1=0
    while c1==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        c1=csetpieces(compboard,"c",row,col,oren)
    while c2==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        c2=csetpieces(compboard,"c",row,col,oren)
    while d1==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        d1=csetpieces(compboard,"d",row,col,oren)
    while d2==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        d2=csetpieces(compboard,"d",row,col,oren)
    while su1==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        su1=csetpieces(compboard,"s",row,col,oren)
    while su2==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        su2=csetpieces(compboard,"s",row,col,oren)
    while b1==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        b1=csetpieces(compboard,"b",row,col,oren)
    while a1==0:
        row=random.choice(range(0,10))
        col=random.choice(range(0,10))
        oren=random.choice(["h","v"])
        a1=csetpieces(compboard,"a",row,col,oren)


       
def csetpieces(board,piece,row,col,oren):
    if piece=="a":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" " and board[row][col+3]==" " and board[row][col+4]==" ":
                    for i in range(0,5):
                        board[row][col+i]="A"
                    return 1
                else:
                    return 0
            except:
                return 0
        if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " and board[row+3][col]==" " and board[row+4][col]==" ":
                    for i in range(0,5):
                        board[row+i][col]="A"
                    return 1
                else:
                    return 0
            except:
                return 0
    elif piece=="b":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" " and board[row][col+3]==" " :
                    for i in range(0,4):
                        board[row][col+i]="B"
                    return 1
                else:
                    return 0
            except:
                return 0
        if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " and board[row+3][col]==" " :
                    for i in range(0,4):
                        board[row+i][col]="B"
                    return 1
                else:
                    return 0
            except:
                return 0
    elif piece=="s":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" ":
                     for i in range(0,3):
                         board[row][col+i]="S"
                     return 1
                else:
                    return 0
            except:
                return 0
        if oren=="v":
             try:
                 if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " :
                    for i in range(0,3):
                        board[row+i][col]="S"
                    return 1
                 else:
                    return 0
             except:
                return 0
    elif piece=="c":
        if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " and board[row][col+2]==" ":
                    for i in range(0,3):
                         board[row][col+i]="C"
                    return 1
                else:
                    return 0
            except:
                return 0
        if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " and board[row+2][col]==" " :
                    for i in range(0,3):
                        board[row+i][col]="C"
                    return 1
                else:
                    return 0
            except:
                return 0
    elif piece=="d":
         if oren=="h":
            try:
                if board[row][col]==" " and board[row][col+1]==" " :
                    for i in range(0,2):
                        board[row][col+i]="D"
                    return 1
                else:
                    return 0
            except:
                return 0
         if oren=="v":
            try:
                if board[row][col]==" " and board[row+1][col]==" " :
                    for i in range(0,2):
                        board[row+i][col]="D"
                    return 1
                else:
                    return 0
            except:
                return 0



def checkboard(board,who):
    global game
    check=0
    for i in range(0,10):
        for j in range(0,10):
            if board[i][j]==" ":
                check=check+1
    if check==100:
        if who=="c":
            print "Computer Has Destroyed Your Entire Fleet"
            print "Computer Wins"
            game=False
            retry()
        if who=="u":
            print "You Have Leveled Your Enemy's Forces..You Win!!!"
            game=False
            retry()




def retry():
    anw=raw_input("Play Again??(Y/N): ")
    if anw=="Y":
        start()
    elif anw=="N":
        pass
    else:
        retry()


       
def decoderow(a):
    if a=="A":
        return 0
    elif a=="B":
        return 1
    elif a=="C":
        return 2
    elif a=="D":
        return 3
    elif a=="E":
        return 4
    elif a=="F":
        return 5
    elif a=="G":
        return 6
    elif a=="H":
        return 7
    elif a=="I":
        return 8
    elif a=="J":
        return 9


def encoderow(a):
    if a==0:
        return "A"
    elif a==1:
        return "B"
    elif a==2:
        return "C"
    elif a==3:
        return "D"
    elif a==4:
        return "E"
    elif a==5:
        return "F"
    elif a==6:
        return "G"
    elif a==7:
        return "H"
    elif a==8:
        return "I"
    elif a==9:
        return "J"

           

def ask(piece):
    if piece=="a":
        inpt=list(raw_input("Set Aircaft Carrier At: "))
        try:
            row=decoderow(inpt[0].upper())
            col=string.atoi(inpt[1])
            oren=inpt[2]
            setpieces(playboard,"a",row,col,oren)
            printboard()
        except:
            print "Can't play there"
            ask(piece)
    if piece=="b":
        inpt=list(raw_input("Set Battleship At: "))
        try:
            row=decoderow(inpt[0].upper())
            col=string.atoi(inpt[1])
            oren=inpt[2]
            setpieces(playboard,"b",row,col,oren)
            printboard()
        except:
            print "Can't play there"
            ask(piece)
    if piece=="s":
        inpt=list(raw_input("Set Submarine At: "))
        try:
            row=decoderow(inpt[0].upper())
            col=string.atoi(inpt[1])
            oren=inpt[2]
            setpieces(playboard,"s",row,col,oren)
            printboard()
        except:
            print "Can't play there"
            ask(piece)
    if piece=="c":
        inpt=list(raw_input("Set Cruiser At: "))
        try:
            row=decoderow(inpt[0].upper())
            col=string.atoi(inpt[1])
            oren=inpt[2]
            setpieces(playboard,"c",row,col,oren)
            printboard()
        except:
            print "Can't play there"
            ask(piece)
    if piece=="d":
        inpt=list(raw_input("Set Destroyer At: "))
        try:
            row=decoderow(inpt[0].upper())
            col=string.atoi(inpt[1])
            oren=inpt[2]
            setpieces(playboard,"d",row,col,oren)
            printboard()
        except:
            print "Can't play there"
            ask(piece)


def complay():
    row=random.choice(range(0,10))
    col=random.choice(range(0,10))
    try:
        if playboard[row][col] != " ":
            playboard[row][col] = " "
            print "Computer: %s%d" %(encoderow(row),col)
            print "You:Hit"
            printboard()
            checkboard(compboard,"u")
        else:
            print "Computer: %s%d" %(encoderow(row),col)
            print "You:Miss"
            printboard()
    except:
        complay()



def start():
    global compboard
    global playboard
    compboard=[]
    playboard=[]
    makeboards()
    comp()
    printboard()
    ask("c")
    ask("c")
    ask("d")
    ask("d")
    ask("s")
    ask("s")
    ask("b")
    ask("a")
    global game
    game=True


start()   

while game==True:
    try:
        play=list(raw_input("Play Where: "))
        row=decoderow(play[0].upper())
        col=string.atoi(play[1])
        if compboard[row][col] !=" ":
            compboard[row][col]=" "
            print "Computer:Hit"
            checkboard(compboard,"u")
            complay()
        else:
            print "Computer:Miss"
            complay()
    except:
        print "Can't Play That"
       
       
 

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: [Python]Battleships game
« Reply #2 on: September 17, 2012, 06:58:19 am »
thanks man!!! this code game looks really cool, I just didn't look at this board for some reason, I'm def going to look at this code and play a little bit ;) sorry for the late reply though.
« Last Edit: September 17, 2012, 06:59:14 am by Polyphony »
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline DeXtreme

  • Peasant
  • *
  • Posts: 95
  • Cookies: 8
  • I was there and you never knew.
    • View Profile
    • My Designs
Re: [Python]Battleships game
« Reply #3 on: September 18, 2012, 12:17:42 am »
You're welcome.Have fun and see if you can improve the AI. ;D
« Last Edit: September 18, 2012, 12:17:57 am by DeXtreme »