Jorge1555 Posted May 11, 2013 at 09:12 PM Report #506938 Posted May 11, 2013 at 09:12 PM (edited) Olá a todos, eu sou novato na área da programação e tenho 14 anos. Gostava muito de por este jogo de batalha naval que fiz a funcionar através de uma base de dados ou qualquer coisa parecida onde se possa armazenar as variáveis na Internet, já tentei ver tutoriais mas tudo parece um pouco confuso e só para saberem tudo o que eu sei sobre python, aprendi com a Internet por isso sejam brandos comigo se eu tiver alguma coisa mal com no código. Tentei utilizar o modulo Shelve onde guarda as variáveis num ficheiro mas não funcionou muito bem e se não se importavam de tirar um pouco do vosso tempo livre para fazer um favor a um novato de substituir as partes do modulo Shelve por MySQL. Então aqui têm o meu código: ##tabuleiroX_1 = Navios ##tabuleir0X_2 = Tentativas import shelve, os, time f = shelve.open("Batalha_naval", flag="c", protocol=None, writeback=False) navios = ["+---+\n| X |\n+---+", "+---+\n| X |\n+---+", "+---+\n| X |\n+---+", "+---+---+\n| X | x |\n+---+---+", "+---+---+\n| X | x |\n+---+---+", "+---+---+---+\n| X | x | x |\n+---+---+---+", "+---+---+---+---+\n| X | x | x | x |\n+---+---+---+---+", "+---+---+---+---+\n| X | x | x | x |\n+---+---+---+---+"] f["A_pronto"] = False f["ganhou_A"] = False #Criar lista para o tabuleiro tabuleiroA_1 = [] tabuleiroA_2 = [] for x in range(10): tabuleiroA_1.append([]) tabuleiroA_2.append([]) for y in range(10): tabuleiroA_1[x].append(" ") tabuleiroA_2[x].append(" ") #função para limpar o ecrã quando iniciado na kinha de comandos do windows, eu utilizo uam função porque limpar o ecra #utilizando o PyScripter faz o software do meu rato deixar de funcionar def clear(): None##os.system("cls") #função para verificar se escreve um numero válido def check_valid(x): a = "1 2 3 4 5 6 7 8 9 0".split() for b in range(0, 10): if x == int(a[b]): return True return False #função para passar as letras das colunas para numeros def abc_to_num(x): a = "a b c d e f g h i j".split() for b in range(0, 10): if x == a[b]: return(b+1) return False #função para "imprimir" os dois tabuleiros def print_tabluleiro(): print(" "+"{:^43}".format("Os teus Navios")+ " " +"{:^43}".format("As tuas tentativas")) print(" A B C D E F G H I J A B C D E F G H I J") for x in range(10): x = x+1 if x != 10:print(" +---+---+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+" "\n" + str(x) + " | "+ tabuleiroA_1[0][x-1] +" | "+ tabuleiroA_1[1][x-1] +" | "+ tabuleiroA_1[2][x-1] +" | "+ tabuleiroA_1[3][x-1] +" | "+ tabuleiroA_1[4][x-1] +" | "+ tabuleiroA_1[5][x-1] +" | "+ tabuleiroA_1[6][x-1] +" | "+ tabuleiroA_1[7][x-1] +" | "+ tabuleiroA_1[8][x-1] +" | "+ tabuleiroA_1[9][x-1] +" |" + " " + str(x) + " | "+ tabuleiroA_2[0][x-1] +" | "+ tabuleiroA_2[1][x-1] +" | "+ tabuleiroA_2[2][x-1] +" | "+ tabuleiroA_2[3][x-1] +" | "+ tabuleiroA_2[4][x-1] +" | "+ tabuleiroA_2[5][x-1] +" | "+ tabuleiroA_2[6][x-1] +" | "+ tabuleiroA_2[7][x-1] +" | "+ tabuleiroA_2[8][x-1] +" | "+ tabuleiroA_2[9][x-1] +" |") else:print(" +---+---+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+" "\n" + str(x) + " | "+ tabuleiroA_1[0][x-1] +" | "+ tabuleiroA_1[1][x-1] +" | "+ tabuleiroA_1[2][x-1] +" | "+ tabuleiroA_1[3][x-1] +" | "+ tabuleiroA_1[4][x-1] +" | "+ tabuleiroA_1[5][x-1] +" | "+ tabuleiroA_1[6][x-1] +" | "+ tabuleiroA_1[7][x-1] +" | "+ tabuleiroA_1[8][x-1] +" | "+ tabuleiroA_1[9][x-1] +" |" + " " + str(x) + " | "+ tabuleiroA_2[0][x-1] +" | "+ tabuleiroA_2[1][x-1] +" | "+ tabuleiroA_2[2][x-1] +" | "+ tabuleiroA_2[3][x-1] +" | "+ tabuleiroA_2[4][x-1] +" | "+ tabuleiroA_2[5][x-1] +" | "+ tabuleiroA_2[6][x-1] +" | "+ tabuleiroA_2[7][x-1] +" | "+ tabuleiroA_2[8][x-1] +" | "+ tabuleiroA_2[x-1][x-1] +" |") print(" +---+---+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+") #função para verificar se o jogo acabou def check(): for x in range(10): for y in range(10): if tabuleiroA_1[x][y] == "X": if tabuleiroB_2[x][y] != "F": return False return True #Posicionamento dos navios a = 7 while True: clear() a = a+1 if a == 9: break print(" "+"{:^43}".format("Os teus Navios")) print(" A B C D E F G H I J") for x in range(10): x = x+1 if x != 10:print(" +---+---+---+---+---+---+---+---+---+---+" "\n" + str(x) + " | "+ tabuleiroA_1[0][x-1] +" | "+ tabuleiroA_1[1][x-1] +" | "+ tabuleiroA_1[2][x-1] +" | "+ tabuleiroA_1[3][x-1] +" | "+ tabuleiroA_1[4][x-1] +" | "+ tabuleiroA_1[5][x-1] +" | "+ tabuleiroA_1[6][x-1] +" | "+ tabuleiroA_1[7][x-1] +" | "+ tabuleiroA_1[8][x-1] +" | "+ tabuleiroA_1[9][x-1] +" |") else:print(" +---+---+---+---+---+---+---+---+---+---+" "\n" + str(x) + " | "+ tabuleiroA_1[0][x-1] +" | "+ tabuleiroA_1[1][x-1] +" | "+ tabuleiroA_1[2][x-1] +" | "+ tabuleiroA_1[3][x-1] +" | "+ tabuleiroA_1[4][x-1] +" | "+ tabuleiroA_1[5][x-1] +" | "+ tabuleiroA_1[6][x-1] +" | "+ tabuleiroA_1[7][x-1] +" | "+ tabuleiroA_1[8][x-1] +" | "+ tabuleiroA_1[9][x-1] +" |") print(" +---+---+---+---+---+---+---+---+---+---+") print("Escolhe aonde queres colocar esta peca (X) escrevendo assim (horizontal) (vertical) (1 para horizontal ou 2 rodado 90º no sentido dos ponteiros do relógio):") print(navios[a-1]) try: x, y, z = input().split() x = x.lower() if not abc_to_num(x): raise x = int(abc_to_num(x)-1) y = int(y) y = y-1 if not check_valid(x): raise if not check_valid(y): raise z = int(z) print(str(x) + "_" + str(y) + "_" + str(z)) print(a) if z != 1 and z != 2: raise z = int(z) except: clear() print("Escreves-te alguma coisa mal!") input() a = a-1 continue if a == 1 or a == 2 or a == 3: try: if tabuleiroA_1[x][y] == " ": tabuleiroA_1[x][y] = "X" continue else:raise except: a = a-1 print("Esse local não é válido") input() continue if a == 4 or a == 5: print(x) if z == 1: try: if tabuleiroA_1[x][y] == " " and tabuleiroA_1[x+1][y] == " ": tabuleiroA_1[x][y] = "X" tabuleiroA_1[x+1][y] = "X" continue else:raise except: a = a-1 print("Esse local não é válido") input() continue if z == 2: try: if tabuleiroA_1[x][y] == " " and tabuleiroA_1[x][y+1] == " ": tabuleiroA_1[x][y] = "X" tabuleiroA_1[x][y+1] = "X" continue else:raise except: a = a-1 print("Esse local não é válido") input() continue if a == 6: if z == 1: try: if tabuleiroA_1[x][y] == " " and tabuleiroA_1[x+1][y] == " " and tabuleiroA_1[x+2][y] == " ": tabuleiroA_1[x][y] = "X" tabuleiroA_1[x+1][y] = "X" tabuleiroA_1[x+2][y] = "X" continue else:raise except: a = a-1 print("Esse local não é válido") input() continue if z == 2: try: if tabuleiroA_1[x][y] == " " and tabuleiroA_1[x][y+1] == " " and tabuleiroA_1[x][y+2] == " ": tabuleiroA_1[x][y] = "X" tabuleiroA_1[x][y+1] = "X" tabuleiroA_1[x][y+2] = "X" continue else:raise except: a = a-1 print("Esse local não é válido") input() continue if a == 7 or a == 8: if z == 1: try: if tabuleiroA_1[x][y] == " " and tabuleiroA_1[x+1][y] == " " and tabuleiroA_1[x+2][y] == " " and tabuleiroA_1[x+3][y] == " ": tabuleiroA_1[x][y] = "X" tabuleiroA_1[x+1][y] = "X" tabuleiroA_1[x+2][y] = "X" tabuleiroA_1[x+3][y] = "X" continue else:raise except: a = a-1 print("Esse local não é válido") input() continue if z == 2: try: if tabuleiroA_1[x][y] == " " and tabuleiroA_1[x][y+1] == " " and tabuleiroA_1[x][y+2] == " " and tabuleiroA_1[x][y+3] == " ": tabuleiroA_1[x][y] = "X" tabuleiroA_1[x][y+1] = "X" tabuleiroA_1[x][y+2] = "X" tabuleiroA_1[x][y+3] = "X" continue else:raise except: a = a-1 print("Esse local não é válido") input() continue f["A_pronto"] = True #Esperar pelo jogador B posicionar os seus navios while True: clear() print("{:#^50}".format("")) print("{:^50}".format("Esperando pelo jogador B")) print("{:#^50}".format("")) try: print(f["B_pronto"]) if f["B_pronto"] == True: tabuleiroA_1 = f["tabuleiroA_1"] tabuleiroA_2 = f["tabuleiroA_2"] clear() break time.sleep(1) except:None f["A_pronto"] = False f["vez"] = "A" #O JOGO while True: if f["ganhou_B"] == False: if check(): f["ganhou_A"] = True while True: print("{:#^50}".format("")) print("{:^50}".format("Ganhas-te")) print("{:#^50}".format("")) input() while True: clear() if f["vez"] == "A": print_tabuleiro() try: x, y = input().split() x = x.lower() if not abc_to_num(x): raise x = int(abc_to_num(x)-1) y = int(y) y = y-1 if not check_valid(x): raise if not check_valid(y): raise print(str(x) + "_" + str(y) + "_" + str(z)) print(a) except: clear() print("Escreves-te alguma coisa mal!") input() continue tabuleiroB_1 = f["tabuleiroB_1"] tabuleiroB_2 = f["tabuleiroB_2"] #Se existe uma parte do navio e se nao fez essa jogada então FOGO if tabuleiroB_1[x][y] == "X" and tabuleiroA_2[x][y] == " ": print("{:#^50}".format("")) print("{:^50}".format("FOGO")) print("{:#^50}".format("")) input() tabuleiroA_2[x][y] = "F" f["vez"] = "B" f["tabuleiroA_1"] = tabuleiroA_1 f["tabuleiroA_2"] = tabuleiroA_2 break #Se não existe uma parte do navio e se nao fez essa jogada então ÀGUA elif tabuleiroB_1[x][y] == " " and tabuleiroA_2[x][y] == " ": print("{:#^50}".format("")) print("{:^50}".format("ÁGUA")) print("{:#^50}".format("")) input() tabuleiroA_2[x][y] = "A" f["vez"] = "B" f["tabuleiroA_1"] = tabuleiroA_1 f["tabuleiroA_2"] = tabuleiroA_2 break else: if tabuleiroA_2[x][y] != " ": print("{:#^50}".format("")) print("{:^50}".format("Já Fizes-te essa jogada")) print("{:#^50}".format("")) input() clear() continue else: print("{:#^50}".format("")) print("{:^50}".format("É a vez do jogador B!")) print("{:#^50}".format("")) time.sleep(1) break else: while True: print("{:#^50}".format("")) print("{:^50}".format("Perdes-te")) print("{:#^50}".format("")) input() f["tabuleiroA_1"] = tabuleiroA_1 f["tabuleiroA_2"] = tabuleiroA_2 time.sleep(1) E supostamente o código do jogador B é só trocar A por B nas variáveis e vice-versa. Eu ficarei bastante agradecido se alguém conseguir fazer o que peço e até pode ser que eu aprenda mais alguma coisa 😁 . Edited May 12, 2013 at 01:45 PM by thoga31 GeSHi
pedrotuga Posted May 11, 2013 at 10:05 PM Report #506941 Posted May 11, 2013 at 10:05 PM O que estás a pedir é esticado. Tenta tu, assim que te encontrares encalhado, afixa aqui a dúvida.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now