This repository has been archived by the owner on Jan 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
battleships.py
167 lines (146 loc) · 5.51 KB
/
battleships.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
import random
import time
import fixpath
import sys
from colorama import Fore, Back, Style
from subprocess import call
import colorama
colorama.init()
firstShip = []
secondShip = []
thirdShip = []
mycoordshit = []
myguesses = []
botguesses = []
botships = []
chatbox=[]
maxplace = 9
message="none"
submsg=""
def board():
place = 1
call(["cls"], shell=True)
print(Style.BRIGHT + Fore.WHITE + "\n Battleships")
print("\n\n | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | " + message)
print("\n\n |----|----|----|----|----|----|----|----| ")
for row in range(0,8):
for i in range(0,8):
if i == 0:
sys.stdout.write(" |")
if (place in firstShip) or (place in secondShip) or (place in thirdShip): #One of my ships
if place in mycoordshit: #My ships that have been hit
user = Style.BRIGHT + Fore.RED + "XX" + Fore.WHITE #HIT SHIP
else:
user = Style.BRIGHT + Fore.GREEN + "XX" + Fore.WHITE #Not hit
elif place in myguesses:
if place in botships:
user = Style.BRIGHT + Fore.BLUE + "YH" + Fore.WHITE #I have hit here!
else:
user = Style.BRIGHT + Fore.YELLOW + "G " + Fore.WHITE #I have guessed here
else:
if len(str(abs(place))) > 1:
user = Fore.WHITE + str(place)
else:
user = Fore.WHITE + str(place) + " "
if i <> 8:
if place == 8:
sys.stdout.write(" " + user + " | " + submsg)
else:
sys.stdout.write(" " + user + " |")
else:
sys.stdout.write(user)
place = place + 1
if i != 3:
sys.stdout.write("\n")
print(" |----|----|----|----|----|----|----|----|")
print("")
def drawchatbox():
for i in chatbox:
print(" " + i + Fore.WHITE)
if len(chatbox) > 8:
chatbox.pop(0)
def botShips():
for i in range(0,6):
coord = random.randint(1,36)
botships.append(coord)
chatbox.append(Fore.MAGENTA + "Bot: " + Fore.YELLOW + "Has chosen his ships!")
def myShips():
#For loops for ever
shipLoc11 = raw_input(Fore.MAGENTA + "Me: " + Fore.YELLOW + "Enter first coordinate for ship 1: ")
shipLoc12 = raw_input(Fore.MAGENTA + "Me: " + Fore.YELLOW + "Enter second coordinate for ship 1: ")
shipLoc21 = raw_input(Fore.MAGENTA + "Me: " + Fore.YELLOW + "Enter first coordinate for ship 2: ")
shipLoc22 = raw_input(Fore.MAGENTA + "Me: " + Fore.YELLOW + "Enter second coordinate for ship 2: ")
shipLoc31 = raw_input(Fore.MAGENTA + "Me: " + Fore.YELLOW + "Enter first coordinate for ship 3: ")
shipLoc32 = raw_input(Fore.MAGENTA + "Me: " + Fore.YELLOW + "Enter second coordinate for ship 3: ")
firstShip.append(int(shipLoc11))
firstShip.append(int(shipLoc12))
secondShip.append(int(shipLoc21))
secondShip.append(int(shipLoc22))
thirdShip.append(int(shipLoc31))
thirdShip.append(int(shipLoc32))
submsg = Fore.YELLOW + "Your ships have been deployed!" + Fore.WHITE
board()
def AIchoose():
validChoice = False
while validChoice == False:
placeChosen = random.randint(1,36)
if placeChosen not in botguesses:
validChoice = True
botguesses.append(placeChosen)
mycoordshit.append(placeChosen)
def choose():
placeChosen = raw_input(Fore.MAGENTA + " Me: " + Fore.YELLOW + ": Enter place number to strike: " + Fore.WHITE)
#if (int(placeChosen) in Xcoords) or (int(placeChosen) in Ocoords) :
# chatbox.append(Fore.RED + "This place is already taken on the game board")
#else: #Untidy, especially with name colors but does its job at the moment
# if cuser==Fore.RED + "Crosses":
# Xcoords.append(int(placeChosen))
# else:
# Ocoords.append(int(placeChosen))
myguesses.append(int(placeChosen))
if int(placeChosen) in botships:
chatbox.append(Fore.MAGENTA + "Me" + Fore.YELLOW + " You have hit Player 2's ship!")
else:
chatbox.append(Fore.MAGENTA + "Me" + Fore.YELLOW + " Has chosen coordinate: " + placeChosen)
def gameloop():
board()
drawchatbox()
choose()
AIchoose()
time.sleep(0.1)
def resetGame():
print "Resetting messages.."
chatbox = []
time.sleep(1)
print "Configuring the game board."
print "Resetting player scores..."
time.sleep(0.5)
print "Setting up the game board..."
print "Preparing the game..."
resetGame()
message = Fore.CYAN + "Welcome to Battleships!" + Fore.WHITE
submsg = Fore.YELLOW + "Please choose your ships!" + Fore.WHITE
time.sleep(0.1)
board()
botShips()
myShips()
board()
while True:
scomb = []
for ship in firstShip:
scomb.append(ship)
for ship in secondShip:
scomb.append(ship)
for ship in thirdShip:
scomb.append(ship)
i = 0
for ship in scomb:
if ship in botguesses:
submsg = Fore.RED + "Your ship has been hit!" + Fore.WHITE
i = i + 1
if i == 6:
submsg = Fore.RED + "All your ships have been sunk!" + Fore.WHITE
if i != 6:
gameloop()
else:
chatbox.append(Fore.RED + "All your ships are now sink!")