-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamemaster.py
229 lines (204 loc) · 5.54 KB
/
gamemaster.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import battleship as p1
import battleship2 as p2
from pprint import pprint
import pygame,sys
from pygame.locals import *
FPS = 1000
windowwidth = 1280
windowheight = 720
revealspeed = 8
boxsize = 40
gapsize = 5
boardgap = 20
boardwidth = 10
boardheight = 10
assert (boardwidth*boardheight)%2==0, "we need even no. of boxes"
xmargin = int((windowwidth-(boardgap+(boardwidth*(boxsize+gapsize)*2)))/2)
ymargin = int((windowheight-(boardheight*(boxsize+gapsize)))/2)
gray = (100,100,100)
navyblue = (60,60,100)
white = (255,255,255)
red = (255,0,0)
green = (0,120,0)
blue = (0,0,255)
yellow = (255,255,0)
orange = (255,128,0)
purple = (255,0,255)
cyan = (0,255,255)
black = (15,15,15)
bgcolor = navyblue
lightbgcolor = gray
boxcolor = white
highlightcolor = blue
BASICFONTSIZE = 24
TEXTCOLOR = white
names=['aircraft carrier','battleship','destroyer','cruiser','patrol boat']
hit=7
miss=1
sink=9
unguessed=0
counter=0
matches = 1000
score=0
def main():
global counter,score
p1.boardgenerator()
p2.boardgenerator()
global FPSCLOCK,DISPLAYSURF
pygame.init()
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
#score = counter
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF=pygame.display.set_mode((windowwidth,windowheight))
pygame.display.set_caption('BATTLESHIP')
DISPLAYSURF.fill(bgcolor)
abc=0
noofmoves=0
while True:
noofmoves+=1
DISPLAYSURF.fill(bgcolor)
drawRadar(p1.radar,p2.radar,BASICFONT)
if abc>=matches:
print "player 2 won %d out of %d matches" %(score,matches)
pygame.quit()
sys.exit()
for event in pygame.event.get():
if event.type == QUIT or (event.type==KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
x,y = p1.nextmove()
move = p2.hitat(x,y)
p1.updateradar(x,y,move)
if len(p1.ships)==0:
#print "victory in %d moves" %(c)
#pprint(p1.statscore)
p1.resetplayer(p1)
p2.resetplayer(p2)
score+=1
counter+=1
# p1.board = []
# p1.radar = []
# p1.probscore = []
# p1.neighbourscore = []
# p1.bestprob = []
# p1.hitfound = []
# p1.shipnames = {'aircraft carrier':[],
# 'battleship':[],
# 'destroyer':[],
# 'cruiser':[],
# 'patrol boat':[]}
# p1.ships = [5,4,3,3,2]
# p1.oppships = [5,4,3,3,2]
# p1.scoreadd = 100
# p1.maxprob=0
# p2.board = []
# p2.radar = []
# p2.probscore = []
# p2.neighbourscore = []
# p2.bestprob = []
# p2.hitfound = []
# p2.shipnames = {'aircraft carrier':[],
# 'battleship':[],
# 'destroyer':[],
# 'cruiser':[],
# 'patrol boat':[]}
# p2.ships = [5,4,3,3,2]
# p2.oppships = [5,4,3,3,2]
# p2.maxprob=0
if abc>=matches:
print "player 1 won %d out of %d matches" %(counter,matches)
pygame.quit()
sys.exit()
else:
abc+=1
print noofmoves
noofmoves=0
p1.boardgenerator()
p2.boardgenerator()
x1,y1 = p2.nextmove()
move1 = p1.hitat(x1,y1)
p2.updateradar(x1,y1,move1)
if len(p2.ships)==0:
#print "victory in %d moves" %(c)
#pprint(p1.statscore)
p1.resetplayer(p1)
p2.resetplayer(p2)
counter+=1
# p1.board = []
# p1.radar = []
# p1.probscore = []
# p1.neighbourscore = []
# p1.bestprob = []
# p1.hitfound = []
# p1.shipnames = {'aircraft carrier':[],
# 'battleship':[],
# 'destroyer':[],
# 'cruiser':[],
# 'patrol boat':[]}
# p1.ships = [5,4,3,3,2]
# p1.oppships = [5,4,3,3,2]
# p1.maxprob=0
# p1.scoreadd = 100
# p2.board = []
# p2.radar = []
# p2.probscore = []
# p2.neighbourscore = []
# p2.bestprob = []
# p2.hitfound = []
# p2.shipnames = {'aircraft carrier':[],
# 'battleship':[],
# 'destroyer':[],
# 'cruiser':[],
# 'patrol boat':[]}
# p2.ships = [5,4,3,3,2]
# p2.oppships = [5,4,3,3,2]
# p2.maxprob=0
if abc>=matches:
print "player 1 won %d out of %d matches" %(counter,matches)
pygame.quit()
sys.exit()
else:
abc+=1
print noofmoves
noofmoves=0
p1.boardgenerator()
p2.boardgenerator()
pygame.display.update()
FPSCLOCK.tick(FPS)
def leftTopCoordsOfBox(boxx,boxy):
left = boxx*(boxsize+gapsize) + xmargin
top = boxy*(boxsize+gapsize) + ymargin
return (left,top)
def drawRadar(radar1,radar2,BASICFONT):
for x in range(boardwidth):
for y in range(boardheight):
left,top = leftTopCoordsOfBox(x,y)
if radar1[y][x] == unguessed:
pygame.draw.rect(DISPLAYSURF,white,(left,top,boxsize,boxsize))
elif radar1[y][x] == hit:
pygame.draw.rect(DISPLAYSURF,red,(left,top,boxsize,boxsize))
elif radar1[y][x] == miss:
pygame.draw.rect(DISPLAYSURF,blue,(left,top,boxsize,boxsize))
elif radar1[y][x] == sink:
pygame.draw.rect(DISPLAYSURF,black,(left,top,boxsize,boxsize))
left = left + boardwidth*(boxsize+gapsize) + boardgap
if radar2[y][x] == unguessed:
pygame.draw.rect(DISPLAYSURF,white,(left,top,boxsize,boxsize))
elif radar2[y][x] == hit:
pygame.draw.rect(DISPLAYSURF,red,(left,top,boxsize,boxsize))
elif radar2[y][x] == miss:
pygame.draw.rect(DISPLAYSURF,blue,(left,top,boxsize,boxsize))
elif radar2[y][x] == sink:
pygame.draw.rect(DISPLAYSURF,black,(left,top,boxsize,boxsize))
pygame.draw.rect(DISPLAYSURF,orange,(360,50,90,60))
pygame.draw.rect(DISPLAYSURF,orange,(830,50,90,60))
textSurf1 = BASICFONT.render(str(counter-score), True, TEXTCOLOR)
textSurf2 = BASICFONT.render(str(score), True, TEXTCOLOR)
textRect1 = textSurf1.get_rect()
textRect2 = textSurf2.get_rect()
textRect1.center = 405,80
textRect2.center = 875,80
DISPLAYSURF.blit(textSurf1, textRect1)
DISPLAYSURF.blit(textSurf2, textRect2)
if __name__ == '__main__':
main()