def first_attack(self,s,view_list,ship_life_list,CL): while True: os.system(CL) ShWr_map(view_list).show_enemy_map() for i in ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() attack_top = input('请选择要打击的坐标\n格式为(数字和字母):') try: attack_num = int(attack_top[0]) attack_word = attack_top[-1] except: print('格式错误!请重新输入!') time.sleep(1) continue if (attack_num not in list_num) or (attack_word not in list_word_s): print('输入有误!请注意数字的值以及区分大小写!') time.sleep(1) continue view_list[attack_num][dict_word_s[attack_word]] = ' X ' os.system(CL) ShWr_map(view_list).show_enemy_map() print('打击坐标已在地图中以\'X\'标识!\n') time.sleep(1) s.send(attack_top.encode()) msg = s.recv(1024).decode() print(msg) input('回车确认,结束回合') s.send(b'over') return
def put_boom(self): # if self.boom < 1: # self.boom += 1 # return self.boom = 0 while True: os.system(self.CL) ShWr_map(self.view_list).show_enemy_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() boom_top = input('输入坐标放置鱼雷 格式(数字和字母)\n按\'r\'键阅读\'关于鱼雷\':') if boom_top == '': continue if boom_top[0] == 'r': os.system(self.CL) ShWr_map(self.view_list).show_enemy_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() print('关于鱼雷:\n每回合补充一个鱼雷,玩家可以在对方地图放置\n鱼雷会被永久放置,直到被对方船只触碰到') input('回车确认') continue try: boom_num = int(boom_top[0]) boom_word = boom_top[-1] if (boom_num not in list_num) or (boom_word not in list_word_s): print('请填写合理范围内的坐标!') continue break except: print('输入参数有误!请重新输入!') time.sleep(1) continue msg = 'putboom ' + str(boom_num) + ' ' + boom_word self.s.send(msg.encode()) time.sleep(0.5) msg = self.s.recv(1024) if '鱼雷击中了船只' in msg.decode(): self.view_list[boom_num][dict_word_s[boom_word]] = '( )' else: self.view_list[boom_num][dict_word_s[boom_word]] = ' O ' os.system(self.CL) ShWr_map(self.view_list).show_enemy_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() print(msg.decode()) input('回车确认')
def first_wait(self,s,my_list,all_life_list,CL) : os.system(CL) ShWr_map(my_list).show_self_map() for i in all_life_list: print(i[0],'号舰血量:[',i[-1],']',end='--') print() print('<<===对方回合,请等待===>>') print() attack_top = s.recv(1024).decode() attack_num = int(attack_top[0]) attack_word = attack_top[-1] flag = 0 if my_list[attack_num][dict_word_s[attack_word]] != ' ': flag = 1 if my_list[attack_num][dict_word_s[attack_word]] == ' ^ ': hurt_num = my_list[attack_num+1][dict_word_s[attack_word]][1] elif my_list[attack_num][dict_word_s[attack_word]] == ' v ': hurt_num = my_list[attack_num-1][dict_word_s[attack_word]][1] elif my_list[attack_num][dict_word_s[attack_word]] == ' < ': hurt_num = my_list[attack_num][dict_word_s[attack_word]+1][1] elif my_list[attack_num][dict_word_s[attack_word]] == ' > ': hurt_num = my_list[attack_num][dict_word_s[attack_word]-1][1] else: hurt_num = my_list[attack_num][dict_word_s[attack_word]][1] all_life_list[int(hurt_num)-1][1] -=1 station_str = my_list[attack_num][dict_word_s[attack_word]] my_list[attack_num][dict_word_s[attack_word]] = ' x ' else: station_str = ' ' my_list[attack_num][dict_word_s[attack_word]] = ' X ' os.system(CL) ShWr_map(my_list).show_enemy_map() for i in all_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<===对方回合,请等待===>>') print() if flag == 1: print('实时战报:\n对方击中了我方船只!\n已在地图中以\'x\'标识!\n') msg = '击中目标!对方船只行踪暴露!' s.send(msg.encode()) else: print('实时战报:\n所幸对方没有击中我方船只!\n已在地图中以\'X\'标识!\n') msg = '没有击中目标!此地标暂时无对方行踪!' s.send(msg.encode()) while True: msg = s.recv(1024) if msg == b'over': time.sleep(1) print('实时战报:\n对方回合结束!') input('按回车键开始回合') return station_str
def attack(self): while True: os.system(self.CL) ShWr_map(self.view_list).show_enemy_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() boom_top = input('输入坐标进行打击 格式(数字和字母):') try: boom_num = int(boom_top[0]) boom_word = boom_top[-1] if (boom_num not in list_num) or (boom_word not in list_word_s): print('请填写合理范围内的坐标!') continue except: print('输入参数有误!请重新输入!') time.sleep(1) continue if self.view_list[boom_num][dict_word_s[boom_word]] == ' O ': print('这里是您布置好的鱼雷...') time.sleep(1) print('您不能这么愚钝...') time.sleep(1) print('我就当做什么都没看见,您重新选择地标吧...') time.sleep(1.5) continue msg = 'attack ' + str(boom_num) + ' ' + boom_word self.s.send(msg.encode()) time.sleep(0.5) msg = self.s.recv(1024) for i in range(10): for j in range(12): if self.view_list[i][j] == ' X ': self.view_list[i][j] = ' ' if self.view_list[i][j][0] == '(': self.view_list[i][j] = ' ' self.view_list[boom_num][dict_word_s[boom_word]] = ' X ' os.system(self.CL) ShWr_map(self.view_list).show_enemy_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>\n') print(msg.decode()) time.sleep(0.5) input('\n回车确认,结束回合') self.s.send(b'over') return
def leng_ship(self,list,CL,num): """ 纵向船只摆放 :return: 刷新列表 """ while True: os.system(CL) ShWr_map(list).show_self_map() ship_show_leng(self.__count) pos = input('请输入船只放置坐标(格式为: 数字和小写字母)\n按r键重新调整方向:') if pos == 'r': return list,num if len(pos) > 2: print('格式错误,请重新输入') time.sleep(1.5) continue try: pos_num = int(pos[0]) pos_word = pos[-1] if (pos_num not in list_num) or (pos_word not in list_word_s): print('参数错误,请重新输入') time.sleep(1.5) continue if pos_num == 0 or pos_num == 9: print('船只长度溢出地图,请重新输入') time.sleep(1.5) continue if (list[pos_num][dict_word_s[pos_word]] != ' ') or (list[pos_num+1][dict_word_s[pos_word]] != ' ') or (list[pos_num-1][dict_word_s[pos_word]] != ' '): print('船只重叠!请重新输入') time.sleep(1.5) continue list[pos_num][dict_word_s[pos_word]] = '[%d]'%num list[pos_num-1][dict_word_s[pos_word]] = ' ^ ' list[pos_num+1][dict_word_s[pos_word]] = ' v ' return list,num+1 except: print('格式错误,请重新输入') time.sleep(1.5) continue
def wait(self): msg_s = '<<===对方回合,请等待===>>\n' while True: os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print(msg_s) r_msg = self.s.recv(1024) msg_list_before = r_msg.decode() msg = msg_list_before.split(' ') if msg[0] == 'win': print('游戏结束,您获胜了!') time.sleep(3) self.s.close() break msg_s += '\n实时战报:' if msg[0] == 'getboom': self.view_list[int(msg[1])][int(msg[2])] = '( )' os.system(self.CL) ShWr_map(self.view_list).show_enemy_map() # for i in self.ship_life_list: # print(i[0], '号舰血量:[', i[-1], ']', end='--') msg_s += '\n对方触碰了鱼雷!' print(msg_s) time.sleep(2.5) msg_s += '\n下回合将在[敌方地图]上以\'( )\'显示\n' continue if msg[0] == 'died': msg_s += '\n对方牺牲了一条船只!' continue if msg[0] == 'putboom': boom_num = int(msg[1]) boom_word = msg[2] msg_s += '\n对方放置了鱼雷!' sendmsg = '鱼雷已被放置\n' if self.my_list[boom_num][dict_word_s[boom_word]] == ' ': msg_s+='\n已在地图中以\'O\'标识\n' self.my_list[boom_num][dict_word_s[boom_word]] = ' O ' else: sendmsg += '鱼雷击中了船只!\n' msg_s += '鱼雷击中船只!船只行踪暴露!\n' station_str = '' for i in range(3): if i == 0: station_str += '(' elif i == 1: station_str += self.my_list[boom_num][dict_word_s[boom_word]][i] else: station_str += ')' self.station_str = self.my_list[boom_num][dict_word_s[boom_word]] self.my_list[boom_num][dict_word_s[boom_word]] = station_str hurt_num = 0 hurt_word = 0 if self.my_list[boom_num][dict_word_s[boom_word]][1] == '<': self.choose_ship = self.my_list[boom_num][dict_word_s[boom_word] + 1][1] hurt_num = boom_num hurt_word = dict_word_s[boom_word] + 1 elif self.my_list[boom_num][dict_word_s[boom_word]][1] == '>': self.choose_ship = self.my_list[boom_num][dict_word_s[boom_word] - 1][1] hurt_num = boom_num hurt_word = dict_word_s[boom_word] - 1 elif self.my_list[boom_num][dict_word_s[boom_word]][1] == '^': self.choose_ship = self.my_list[boom_num + 1][dict_word_s[boom_word]][1] hurt_num = boom_num+1 hurt_word = dict_word_s[boom_word] elif self.my_list[boom_num][dict_word_s[boom_word]][1] == 'v': self.choose_ship = self.my_list[boom_num - 1][dict_word_s[boom_word]][1] hurt_num = boom_num-1 hurt_word = dict_word_s[boom_word] else: self.choose_ship = self.my_list[boom_num][dict_word_s[boom_word]][1] hurt_num = boom_num hurt_word = dict_word_s[boom_word] for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_ship: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: del self.ship_life_list[i] self.my_list[hurt_num][hurt_word] = ' ' if self.my_list[hurt_num - 1][hurt_word] == ' ^ ': self.my_list[hurt_num - 1][hurt_word] = ' ' self.my_list[hurt_num + 1][hurt_word] = ' ' else: self.my_list[hurt_num][hurt_word - 1] = ' ' self.my_list[hurt_num][hurt_word + 1] = ' ' msg_s += '船只牺牲!\n' sendmsg += '对方损失一艘船只!' self.s.send(sendmsg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 break sendmsg += '已在图中标识!\n' self.s.send(sendmsg.encode()) time.sleep(0.1) continue if msg[0] == 'attack': boom_num = int(msg[1]) boom_word = msg[-1] msg_s += '\n对方进行了地标轰炸,已在图中以\'X\'标识!' sendmsg = '\n轰炸完成!\n' # print(boom_num,type(boom_num),boom_word,type(boom_word))# # # if self.my_list[boom_num][dict_word_s[boom_word]] == ' ': msg_s += '\n所幸没有伤亡...' sendmsg += '没有击中目标!此地标暂时无对方行踪!\n' self.station_str = ' ' self.my_list[boom_num][dict_word_s[boom_word]] = ' X ' else: self.station_str = self.my_list[boom_num][dict_word_s[boom_word]] sendmsg += '轰炸击中了船只!\n' msg_s += '轰炸击中船只!船只行踪暴露!\n' small_flag = 0 hurt_num = 0 hurt_word = 0 if self.my_list[boom_num][dict_word_s[boom_word]][1] == '<': self.choose_ship = self.my_list[boom_num][dict_word_s[boom_word] + 1][1] hurt_num = boom_num hurt_word = dict_word_s[boom_word] + 1 elif self.my_list[boom_num][dict_word_s[boom_word]][1] == '>': self.choose_ship = self.my_list[boom_num][dict_word_s[boom_word] - 1][1] hurt_num = boom_num hurt_word = dict_word_s[boom_word] - 1 elif self.my_list[boom_num][dict_word_s[boom_word]][1] == '^': self.choose_ship = self.my_list[boom_num + 1][dict_word_s[boom_word]][1] hurt_num = boom_num + 1 hurt_word = dict_word_s[boom_word] elif self.my_list[boom_num][dict_word_s[boom_word]][1] == 'v': self.choose_ship = self.my_list[boom_num - 1][dict_word_s[boom_word]][1] hurt_num = boom_num - 1 hurt_word = dict_word_s[boom_word] else: self.choose_ship = self.my_list[boom_num][dict_word_s[boom_word]][1] hurt_num = boom_num hurt_word = dict_word_s[boom_word] for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_ship: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: del self.ship_life_list[i] self.my_list[hurt_num][hurt_word] = ' ' if self.my_list[hurt_num - 1][hurt_word] == ' ^ ': self.my_list[hurt_num - 1][hurt_word] = ' ' self.my_list[hurt_num + 1][hurt_word] = ' ' else: self.my_list[hurt_num][hurt_word - 1] = ' ' self.my_list[hurt_num][hurt_word + 1] = ' ' msg_s += '船只牺牲!\n' sendmsg += '对方损失一艘船只!' self.my_list[boom_num][dict_word_s[boom_word]] = ' X ' small_flag = 1 if len(self.ship_life_list) == 0: return 1 break self.station_str = self.my_list[boom_num][dict_word_s[boom_word]] if small_flag == 0: self.my_list[boom_num][dict_word_s[boom_word]] = ' x ' self.s.send(sendmsg.encode()) time.sleep(0.1) data = self.s.recv(1024) if data == b'over': os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print(msg_s) print() input('对方回合结束,回车确认:') return if msg[0] == 'bye': print('游戏结束!您获胜了') return 1
def round_ship(self): for i in range(10): for j in range(12): if self.my_list[i][j] == ' x ': self.my_list[i][j] = self.station_str if self.my_list[i][j][0] == '(': if self.my_list[i][j][1] in('<','>','^','v') : str_code = '' for num in range(3): if num == 1: str_code += self.my_list[i][j][1] else: str_code += ' ' self.my_list[i][j] = str_code else: str_code = '' for num in range(3): if num == 0: str_code += '[' if num == 1: str_code += self.my_list[i][j][1] if num == 2: str_code += ']' self.my_list[i][j] = str_code while True: os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() if (self.pos_num_list[0] in (0, 9)) or (self.pos_num_list[1] in (0, 11)): input('该船只无法转向,按[回车]载入打击界面') break inp = input('是否转向?\ny键确认 n键跳过:') if inp == 'n': break elif inp == 'y': if self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] == ' < ': flag = 0 if self.my_list[self.pos_num_list[0]+1][self.pos_num_list[1]] not in (' ',' X ',' O ','( )') or self.my_list[self.pos_num_list[0]-1][self.pos_num_list[1]] not in (' ',' X ',' O ','( )'): os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() input('转向遭到船只阻碍,按[回车]载入攻击模块') break if self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0] - 1) msg += ' ' msg += str(self.pos_num_list[1]) self.s.send(msg.encode()) os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() print('触碰到鱼雷!船只踪迹暴露!\n') time.sleep(1.5) for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: print('船只牺牲!\n') time.sleep(1) del self.ship_life_list[i] flag = 1 self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' ' msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 time.sleep(0.5) if self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0] + 1) msg += ' ' msg += str(self.pos_num_list[1]) self.s.send(msg.encode()) print('触碰到鱼雷!船只踪迹暴露!') for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: print('船只牺牲!\n') time.sleep(1) del self.ship_life_list[i] flag = 1 self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' ' msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 time.sleep(0.5) if flag == 0: self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ^ ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' v ' os.system(self.CL) ShWr_map(self.my_list).show_self_map() input('\n转向完成!按[回车]载入攻击模块') break if self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] == ' ^ ': flag = 0 if self.my_list[self.pos_num_list[0]][self.pos_num_list[1]-1] not in (' ',' X ',' O ','( )') or self.my_list[self.pos_num_list[0]][self.pos_num_list[1]+1] not in (' ',' X ',' O ','( )'): input('该船只无法在地图边缘转向\n按[回车]载入攻击模块') break if self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0]) msg += ' ' msg += str(self.pos_num_list[1] - 1) self.s.send(msg.encode()) print('\n触碰到鱼雷!船只踪迹暴露!') for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: print('船只牺牲!\n') time.sleep(1) del self.ship_life_list[i] flag = 1 self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' ' msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 time.sleep(0.5) if self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0]) msg += ' ' msg += str(self.pos_num_list[1] + 1) self.s.send(msg.encode()) print('触碰到鱼雷!船只踪迹暴露!') for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: print('船只牺牲!\n') time.sleep(1) del self.ship_life_list[i] flag = 1 self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' ' msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 time.sleep(0.5) if flag == 0: self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' < ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' > ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' ' os.system(self.CL) ShWr_map(self.my_list).show_self_map() input('\n转向完成!按[回车]载入攻击模块') break else: continue for i in range(10): for j in range(12): if self.my_list[i][j] == ' X ': self.my_list[i][j] = ' ' if self.my_list[i][j] == ' x ': self.my_list[i][j] = ' '
def move(self): while True: flag = 0 while flag == 0: choose_ship_str = [] for i in self.ship_life_list: choose_ship_str.append(i[0]) os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() self.choose_str = input('请输入[船只编号]选择要移动的船只:\n按r键阅读\'关于移动\':') if not self.choose_str: continue if self.choose_str[0] == 'r': os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() print('关于移动:\n每回合,玩家只能且必须选择一艘船只进行移动\n注意!!!\n横向船只只能水平移动,纵向船只只能上下移动\n您可以在移动结束后选择是否转向一次') input('回车确认') continue for i in choose_ship_str: if self.choose_str == i: flag = 1 break if flag == 0: print('没有这艘船只!') time.sleep(1) continue self.pos_num_list = [] for i in range(10): for j in range(12): if self.my_list[i][j][1] == self.choose_str: self.pos_num_list.append(i) self.pos_num_list.append(j) if len(self.pos_num_list) < 2: for i in range(10): for j in range(12): if self.my_list[i][j] == ' x ': self.pos_num_list.append(i) self.pos_num_list.append(j) while True: os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print('') print('确定移动[%s]号船只??' % self.choose_str) s = input('r键重新选择--回车确认') if s == '': s = ' ' if s == 'r': break if (self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] == ' < ') or ( self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] == ' > '): os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() choose = input('注意:\n该船只为[横向]放置,请选择左右移动\n\'a\'键为左移,\'d\'键为右移\n\'其他\'键撤销:') if choose == 'a': flag = 0 if self.pos_num_list[1] == 1: print('该船只不能行驶到地图外!请重新选择!') time.sleep(1) continue if self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 2] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0]) msg += ' ' msg += str(self.pos_num_list[1] - 2) self.s.send(msg.encode()) time.sleep(0.5) print('触碰到鱼雷!船只踪迹暴露!\n') for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: flag = 1 del self.ship_life_list[i] self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 2] = ' ' self.my_list[self.pos_num_list[0]][ self.pos_num_list[1] - 1] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' ' print('船只牺牲!\n') msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 break input('回车确认') elif self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 2] != ' X ' and self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 2] != ' ': input('船只位置重叠!按[回车]重新选择!') continue if flag == 0: self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 2] = ' < ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = '[%s]' % self.choose_str self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' > ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = ' ' self.pos_num_list[1] -= 1 return if choose == 'd': flag = 0 if self.pos_num_list[1] == 10: os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() print('该船只不能行驶到地图外!请重新选择!') time.sleep(1) continue if self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 2] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0]) msg += ' ' msg += str(self.pos_num_list[1] + 2) self.s.send(msg.encode()) time.sleep(0.5) print('触碰到鱼雷!船只踪迹暴露!\n') for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: flag = 1 del self.ship_life_list[i] self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 2] = ' ' self.my_list[self.pos_num_list[0]][ self.pos_num_list[1] + 1] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' ' print('船只牺牲!\n') msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 break time.sleep(1) elif self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 2] != ' X ' and self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 2] != ' ': os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() print('船只位置重叠!请重新选择!') time.sleep(1.5) continue if flag == 0: self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 2] = ' > ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] + 1] = '[%s]' % self.choose_str self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' < ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1] - 1] = ' ' self.pos_num_list[1] += 1 return else: continue else: os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() choose = input('注意:\n该船只为[纵向]放置,请选择上下移动\n\'w\'键为上移,\'s\'键为下移\n\'其他\'键撤销:') if choose == 'w': flag = 0 if self.pos_num_list[0] == 1: os.system(self.CL) ShWr_map(self.my_list).show_self_map() for i in self.ship_life_list: print(i[0], '号舰血量:[', i[-1], ']', end='--') print() print('<<=====我方回合=====>>') print() print('该船只不能行驶到地图外!请重新选择!') time.sleep(1) continue if self.my_list[self.pos_num_list[0] - 2][self.pos_num_list[1]] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0] - 2) msg += ' ' msg += str(self.pos_num_list[1]) self.s.send(msg.encode()) time.sleep(0.5) print('触碰到鱼雷!船只踪迹暴露!\n') for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: flag = 1 del self.ship_life_list[i] self.my_list[self.pos_num_list[0] - 2][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] - 1][ self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' ' print('船只牺牲!\n') msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 break time.sleep(1) elif self.my_list[self.pos_num_list[0] - 2][self.pos_num_list[1]] != ' X ' and self.my_list[self.pos_num_list[0] - 2][self.pos_num_list[1]] != ' ': print('船只位置重叠!请重新选择!') continue if flag == 0: self.my_list[self.pos_num_list[0] - 2][self.pos_num_list[1]] = ' ^ ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = '[%s]' % self.choose_str self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' v ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = ' ' self.pos_num_list[0] -= 1 return if choose == 's': flag = 0 if self.pos_num_list[0] == 8: print('该船只不能行驶到地图外!请重新选择!') time.sleep(1) continue if self.my_list[self.pos_num_list[0] + 2][self.pos_num_list[1]] == ' O ': msg = 'getboom ' msg += str(self.pos_num_list[0] + 2) msg += ' ' msg += str(self.pos_num_list[1]) self.s.send(msg.encode()) time.sleep(0.5) print('触碰到鱼雷!船只踪迹暴露!\n') for i in range(len(self.ship_life_list)): if self.ship_life_list[i][0] == self.choose_str: self.ship_life_list[i][1] -= 1 if self.ship_life_list[i][1] == 0: flag = 1 del self.ship_life_list[i] self.my_list[self.pos_num_list[0] + 2][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] + 1][ self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ' print('船只牺牲!\n') msg = 'died ' self.s.send(msg.encode()) time.sleep(0.5) if len(self.ship_life_list) == 0: return 1 break time.sleep(1) elif self.my_list[self.pos_num_list[0] + 2][self.pos_num_list[1]] != ' X ' and self.my_list[self.pos_num_list[0] + 2][self.pos_num_list[1]] != ' ': print('船只位置重叠!请重新选择!') continue if flag == 0: self.my_list[self.pos_num_list[0] + 2][self.pos_num_list[1]] = ' v ' self.my_list[self.pos_num_list[0] + 1][self.pos_num_list[1]] = '[%s]' % self.choose_str self.my_list[self.pos_num_list[0]][self.pos_num_list[1]] = ' ^ ' self.my_list[self.pos_num_list[0] - 1][self.pos_num_list[1]] = ' ' self.pos_num_list[0] += 1 return else: continue
def place_ship(self,CL): """ 船只初次摆放流程 :param CL: 清屏指令 :return: 地图列表 """ my_list = [ [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, ] self.__count = 1 while self.__count <6: if self.__count !=5: os.system(CL) ShWr_map(my_list).show_self_map() ship_show(self.__count) else: os.system(CL) ShWr_map(my_list).show_self_map() if self.__count == 1: print("请选择横向放置或者纵向放置:\n1.横向放置\n2.纵向放置") elif self.__count <5: print("请选择横向放置或者纵向放置:\n1.横向放置\n2.纵向放置\n3.修改上一艘船") elif self.__count ==5: print("1.修改上一艘船\n2.确认全部放置") str_pos = input(">>>:") if str_pos == '1': for i in range(10): for j in range(12): if my_list[i][j] == '[%d]' % (self.__count - 1): my_list[i][j] = ' ' if my_list[i][j - 1] == ' < ': my_list[i][j - 1] = ' ' my_list[i][j + 1] = ' ' elif my_list[i - 1][j] == ' ^ ': my_list[i - 1][j] = ' ' my_list[i + 1][j] = ' ' break self.__count -= 1 continue elif str_pos == '2' or str_pos == '': return my_list str_pos = input(">>>:") if str_pos == '2' and self.__count !=5: my_list,self.__count = self.leng_ship(my_list,CL,self.__count) elif str_pos == '1'and self.__count !=5: my_list,self.__count = self.line_ship(my_list,CL,self.__count) elif str_pos == '3': for i in range(10): for j in range(12): if my_list[i][j] == '[%d]'%(self.__count-1): my_list[i][j] = ' ' if my_list[i][j-1] == ' < ': my_list[i][j - 1] = ' ' my_list[i][j + 1] = ' ' elif my_list[i-1][j] == ' ^ ': my_list[i-1][j] = ' ' my_list[i+1][j] = ' ' break self.__count -= 1 else: print("输入有误!注意格式(数字和小写字母)") time.sleep(1)
def join_room(self,user_name,CL,IPGOT): os.system(CL) print(LOGO) s = socket() join_ip=input("请输入要加入的房间ip\n按r键返回:") if join_ip == 'r': return s.settimeout(1) count = 0 while True: count +=1 try: os.system(CL) print(LOGO) print('正在链接房间', '.' * (count % 3 + 1)) s.connect((join_ip,55055)) break except: if count == 10: os.system(CL) print(LOGO) print('链接失败!\n请检查ip是否正确且房间存在') print('您输入的ip:[%s]'%join_ip) input('按[回车]返回') return continue s.settimeout(2000) s.send(user_name.encode()) data = s.recv(1024) os.system(CL) print(LOGO) print('链接成功!准备进入[%s]舰长的房间'%(data.decode())) time.sleep(2) print('正在载入...') time.sleep(3) for i in range(3): print() my_list = self.place_ship(CL) os.system(CL) ShWr_map(my_list).show_self_map() print("<<====版图完成,请等待对方就绪====>>") time.sleep(3) s.send(b'ok') msg = s.recv(1024) if msg == b'ok': print('对方已就绪!') time.sleep(1.5) all_life_list = [['1', 3], ['2', 3], ['3', 3], ['4', 3]] view_list = [ [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, ] msg = s.recv(1024) if msg == b'my': print('>>==我方获得先手机会==<<') time.sleep(1.5) self.first_attack(s, view_list, all_life_list,CL) game_combo = Game_Combo(s, all_life_list, my_list,view_list, CL,1) while True: flag = 0 while True: result = game_combo.wait() if result == 1: flag = 1 s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return result = game_combo.move() if result == 123: continue elif result == 1: flag = 1 s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return else: break result = game_combo.round_ship() if result == 1: flag = 1 s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return game_combo.put_boom() game_combo.attack() if flag == 1: break s.close() else: print('>>==对方获得先手机会==<<') time.sleep(1.5) station_str = self.first_wait(s,my_list,all_life_list,CL) game_combo = Game_Combo(s, all_life_list, my_list,view_list, CL,2,station_str) while True: flag = 0 while True: result = game_combo.move() if result == 123: continue elif result == 1: flag = 1 s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return else: break result = game_combo.round_ship() if result == 1: flag = 1 print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) s.close() return game_combo.put_boom() game_combo.attack() result = game_combo.wait() if result == 1: flag = 1 s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return if flag == 1: break s.close()
def create_room(self,user_name,CL,IPGOT): ship_life_list=[['1号舰',3],['2号舰',3],['3号舰',3],['4号舰',3]] os.system(CL) for i in range(3): print('获取本机ip中', '.' * (i + 1)) time.sleep(1) os.system(CL) while True: os.system(CL) os.system(IPGOT) print('**备注:ip号请以INET为主') INEIP = input('请输入本机ip:') if len(INEIP)<=7 : print('格式不对!请重新确认!') time.sleep(1) continue try: s = socket() s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) s.settimeout(1.1) s.bind((INEIP,55055)) s.listen(5) break except: print('ip设置有误!请检查是否与INET一致') input('回车返回') # fork_num = os.fork() # if fork_num == 0: # self.wait_gamer(s) # self.send_father(1) # os._exit(0) # # else: # self.send_father(INEIP,0) count = 0 while True: # if count == 30: # break count +=1 os.system(CL) print(LOGO) print("报告[%s]舰长,您的房间已经创建完毕.\nip号为:"%user_name, INEIP,' ',40-count) print('等待玩家链接','.'*(count%3+1)) try: connfd, addr = s.accept() break except: if count==40: os.system(CL) print(LOGO) print('连接超时!请重新创建链接!') time.sleep(2) return continue s.settimeout(2000) print('链接成功!') msg = connfd.recv(1024) enemy_name = msg.decode() os.system(CL) print(LOGO) print('[%s]舰长成功与你建立桥接' % (enemy_name)) msg = user_name.encode() connfd.send(msg) time.sleep(3) my_list = self.place_ship(CL) os.system(CL) ShWr_map(my_list).show_self_map() print("<<====版图完成,请等待对方就绪====>>") msg = connfd.recv(1024) if msg == b'ok': connfd.send(b'ok') print('对方已就绪!') time.sleep(1.5) all_life_list = [['1', 3], ['2', 3], ['3', 3], ['4', 3]] view_list = [ [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, [' '] * 12, ] combo_number = random.randint(1,2) if combo_number == 1: connfd.send(b'enemy') print('>>==我方获得先手机会==<<') time.sleep(1.5) self.first_attack(connfd,view_list,all_life_list,CL) game_combo = Game_Combo(connfd, all_life_list, my_list,view_list, CL,1) while True: flag = 0 while True: result = game_combo.wait() if result == 1: flag = 1 connfd.close() s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return result = game_combo.move() if result == 123: continue elif result == 1: flag = 1 connfd.close() s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return else: break result = game_combo.round_ship() if result == 1: flag = 1 connfd.close() s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return game_combo.put_boom() result = game_combo.attack() if flag == 1: break connfd.close() s.close() else: connfd.send(b'my') print('>>==对方获得先手机会==<<') time.sleep(1.5) station_str = self.first_wait(connfd,my_list,all_life_list,CL) game_combo = Game_Combo(connfd, all_life_list, my_list,view_list, CL,2,station_str) while True: flag = 0 while True: result = game_combo.move() if result == 123: continue elif result == 1: flag = 1 connfd.close() s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return else: break result = game_combo.round_ship() if result == 1: flag = 1 connfd.close() s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return game_combo.put_boom() game_combo.attack() result = game_combo.wait() if result == 1: flag = 1 connfd.close() s.close() print(' 您输了,正在退出游戏!') s.send(b'win ') time.sleep(3) return if flag == 1: break
def place_bigship(self): pos_round = [] print('地图初始化完成----开始摆放战舰') time.sleep(1) self.show_map() print('****1号母舰位置选择****') Show_ship.show_bigship() while True: print("请选择横向放置或者纵向放置:\n1.横向放置\n2.纵向放置") str_pos = input(">>>:") if str_pos == '1': pos_round = [2, 1] elif str_pos == '2': pos_round = [1, 2] else: print("请输入正确选择!") time.sleep(2) continue str = input("输入放置中心坐标(格式:'数字,小写字母'):") map = ShWr_map() demo_pos = str.split(',') list_pos = [] try: list_pos.append((int(demo_pos[0]))) list_pos.append(demo_pos[-1]) if list_pos[0] not in map.list_num[ pos_round[-1]:len(map.list_num) - pos_round[-1]]: print("船只纵向边缘超出地图范围!") continue if list_pos[-1] not in map.list_word_s[ pos_round[0]:len(map.list_word_s) - pos_round[0]]: print("船只横向边缘超出地图范围!") continue if str_pos == '1': # 横向画法 self.list[list_pos[0]][map.dict_word_s[ list_pos[-1]]] = '[1]' print(list_pos[0], map.dict_word_s[list_pos[-1]]) self.list[list_pos[0]][map.dict_word_s[list_pos[-1]] - 1] = '***' print(list_pos[0], map.dict_word_s[list_pos[-1]] - 1) self.list[list_pos[0]][map.dict_word_s[list_pos[-1]] + 1] = '***' self.list[list_pos[0]][map.dict_word_s[list_pos[-1]] - 2] = ' | ' self.list[list_pos[0]][map.dict_word_s[list_pos[-1]] + 2] = ' | ' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]]] = ' - ' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]] - 1] = ' - ' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]] + 1] = ' - ' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]] - 2] = ' / ' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]] + 2] = ' \\ ' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]]] = ' - ' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]] - 1] = ' - ' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]] + 1] = ' - ' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]] - 2] = ' \\ ' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]] + 2] = ' / ' elif str_pos == '2': # 纵向画法 self.list[list_pos[0]][map.dict_word_s[ list_pos[-1]]] = '[1]' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]]] = '***' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]]] = '***' self.list[list_pos[0] - 2][map.dict_word_s[list_pos[-1]]] = ' - ' self.list[list_pos[0] + 2][map.dict_word_s[list_pos[-1]]] = ' - ' self.list[list_pos[0]][map.dict_word_s[list_pos[-1]] - 1] = ' | ' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]] - 1] = ' | ' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]] - 1] = ' | ' self.list[list_pos[0]][map.dict_word_s[list_pos[-1]] + 1] = ' | ' self.list[list_pos[0] - 1][map.dict_word_s[list_pos[-1]] + 1] = ' | ' self.list[list_pos[0] + 1][map.dict_word_s[list_pos[-1]] + 1] = ' | ' self.list[list_pos[0] - 2][map.dict_word_s[list_pos[-1]] - 1] = ' / ' self.list[list_pos[0] - 2][map.dict_word_s[list_pos[-1]] + 1] = ' \\ ' self.list[list_pos[0] + 2][map.dict_word_s[list_pos[-1]] + 1] = ' / ' self.list[list_pos[0] + 2][map.dict_word_s[list_pos[-1]] - 1] = ' \\ ' self.show_map() if input("按r键重新摆放,回车键确认摆放") == 'r': self.list = [ [' '] * 15, [' '] * 15, [' '] * 15, [' '] * 15, [' '] * 15, [' '] * 15, [' '] * 15, [' '] * 15, [' '] * 15, [' '] * 15, ] continue break except: print("格式不对,请核对!") continue
def show_map(self): map = ShWr_map(self.list) map.show_self_map()