Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#CREATED BY hackers
import random
import replit #to use the clear function - handy!

#Build blank template boards
def setboard():
  board=[]
  for x in range(8):
    board.append(["~"]*8)
  return board

#Print formatted boards
def bprint(boardname):
  for y in boardname:
    print(" ".join(y))

fin=setboard()

#AUTO ship placer
def autoship(s):
  trow=random.randint(0,len(fin)-1)
  tcol=random.randint(0,len(fin)-1)
  autorot = random.randint(0,1)
  if s==1:
    if trow<5 and autorot==1:
      fin[trow][tcol]=fin[trow+1][tcol]=fin[trow+2][tcol]=fin[trow+3][tcol]="X"
    elif tcol<5 and autorot==0:
      fin[trow][tcol]=fin[trow][tcol+1]=fin[trow][tcol+2]=fin[trow][tcol+3]="X"
    else:
      autoship(1)
  elif s==2:
    if autorot==1 and trow<6 and fin[trow][tcol]==fin[trow+1][tcol]==fin[trow+2][tcol]=="~":
      fin[trow][tcol]=fin[trow+1][tcol]=fin[trow+2][tcol]="X"
    elif autorot==0 and tcol<6 and fin[trow][tcol]==fin[trow][tcol+1]==fin[trow][tcol+2]=="~":
      fin[trow][tcol]=fin[trow][tcol+1]=fin[trow][tcol+2]="X"
    else:
      autoship(2)
  elif s==3:
    if autorot==1 and trow<7 and fin[trow][tcol]==fin[trow+1][tcol]=="~":
      fin[trow][tcol]=fin[trow+1][tcol]="X"
    elif autorot==0 and tcol<7 and fin[trow][tcol]==fin[trow][tcol+1]=="~":
      fin[trow][tcol]=fin[trow][tcol+1]="X"
    else:
      autoship(3)
  elif s==4:
    if fin[trow][tcol]=="~":
      fin[trow][tcol]="X"
    else:
      autoship(4)

#Ship mover calculations, works alongside MANUAL ship placer function.
def vmover(r,c,s,rot):
  m=input("Move (l)eft/(r)ight/(u)p/(d)own/rotate(x) OR (p)lace\n")
  if m not in "lrudpx" or m=="":
    print("\nInvalid Input.")
  elif m in "lrudx":
    if m=="x" and c<s+4 and r<s+4:
      if rot==0:
        rot=1
      else:
        rot=0
    elif m=="l" and c>0:
      c-=1
    elif m=="u" and r>0:
      r-=1
    elif m=="r":
      if (rot==1 and c<7) or (rot==0 and c<s+3):
        c+=1
    elif m=="d":
      if (rot==1 and r<s+3) or (rot==0 and r<7):
        r+=1
  return r,c,m,rot

#MANUAL ship table combiner, merges all into one board
def temp(cb,ob):
  for x in range(8):
    for y in range(8):
      if cb[x][y]!="X":
        cb[x][y]=ob[x][y]
  return cb

#MANUAL ship placer
def ship(snum):
  row,col=2,3
  rot=1
  while True:
    replit.clear()
    b=setboard()
    if snum==1:
      if rot==1:
        b[row][col]=b[row+1][col]=b[row+2][col]=b[row+3][col]="X"
      else:
        b[row][col]=b[row][col+1]=b[row][col+2]=b[row][col+3]="X"
    elif snum==2:
      if rot==1:
        b[row][col]=b[row+1][col]=b[row+2][col]="X"
      else:
        b[row][col]=b[row][col+1]=b[row][col+2]="X"
      b=temp(b,b1)
    elif snum==3:
      if rot==1:
        b[row][col]=b[row+1][col]="X"
      else:
        b[row][col]=b[row][col+1]="X"
      b=temp(b,b2)
    elif snum==4:
      b[row][col]="X"
      b=temp(b,b3)
    bprint(b)
    row,col,mov,rot=vmover(row,col,snum,rot)
    if mov=="p":
      return b

#select gamemode
while True:
  game=input("Would you like to play singleplayer(1) or multiplayer(2)?")
  if game=="2":
    b1=ship(1)
    b2=ship(2)
    b3=ship(3)
    b4=ship(4)
    for z in [b1,b2,b3,b4]:
      temp(fin,z)
    break
  elif game=="1":
    autoship(1)
    autoship(2)
    autoship(3)
    autoship(4)
    break

#bprint(fin)  #MASTERBOARD - UNCOMMENT FOR SOLUTION
replit.clear()
print("Welcome to battleships. Guess the row and column of ship locations.")
blboard=setboard()
bprint(blboard)

nums=["1","2","3","4","5","6","7","8"]
hits=0
hist=[]
tries=50

play = True
while play:
  if tries==0:
    print("\n\nYou have run out of missiles. GAME OVER.\n\nShip locations:")
    bprint(fin)
    play=False
  else:
    print("\nMissiles Remaining: %d\n" % tries)
    g1 = input("Row:")
    g2 = input("Column:")
    guess=[g1,g2]
    if g1=="exit" or g2=="exit":
      play = False
    elif len(g1)!=1 or len(g2)!=1 or g1 not in nums or g2 not in nums or g1=="" or g2=="":
      print("INVALID INPUT.")
    elif guess in hist:
      print("\n=====\nAlready fired here!\n=====")
    else:
      tries-=1
      g1,g2=int(g1)-1,int(g2)-1
      if fin[g1][g2]=="X":
        replit.clear()
        print("\n===HIT===")
        blboard[g1][g2]="X"
        hits+=1
        bprint(blboard)
      else:
        replit.clear()
        print("\n===MISS===")
        blboard[g1][g2]="O"
        bprint(blboard)
      hist.append(guess)
    if hits==10:
      print("\n\nYou sunk all the ships! You had %d missiles remaining." % tries)
      play = False