2
I have a work in the course of programming in networks and we were asked to use a game code and make a client and server using sockets
, but my teacher’s explanation was very vague and he does not give any support to students.
I’ve put together everything that is right and never the connection between client and server, I don’t know exactly how to build client and server.
Could you give me a hand? I’ve searched everywhere and nothing has an explanation of how to divide a code between client and server and use the socket
along.
#
# Jogo da Velha
#
# O tabuleiro
velha=""" Jogadas Posições jogáveis
| | 7 | 8 | 9
---+---+--- ---+---+---
| | 4 | 5 | 6
---+---+--- ---+---+---
| | 1 | 2 | 3
"""
# Listagem de posicoes (horizontal e vertical) para as posicoes do jogo.
# Numeração das posicoes ira facilitar o entendimento para jogabilidade.
posicoes = [
None, # Indice
(5, 1), # 1
(5, 5), # 2
(5, 9), # 3
(3, 1), # 4
(3, 5), # 5
(3, 9), # 6
(1, 1), # 7
(1, 5), # 8
(1, 9), # 9
]
# Descrição das posicoes que ganham o jogo fazendo uma linha, um coluna, linha ou diagonal == win
# Os números representam as posicoes ganhadoras
win = [
[ 1, 2, 3], #linha
[ 4, 5, 6],
[ 7, 8, 9],
[ 7, 4, 1], #coluna
[ 8, 5, 2],
[ 9, 6, 3],
[ 7, 5, 3], #diag
[ 1, 5, 9]
]
# Tabuleiro é construido usndo string e gera lista
tabuleiro = []
for linha in velha.splitlines():
tabuleiro.append(list(linha))
jogador = "X" # Começa jogando com X
jogando = True
jogada = 0 # Contador de jogadas
while True:
for t in tabuleiro: # Mostra o tabuleiro
print("".join(t))
if not jogando: # Termina após mostrar o último tabuleiro
break
if jogada == 9: # Se 9 jogadas, todas as posicoes já foram preenchidas
print("Deu velha! Ninguém ganhou.")
break
jogada = int(input("Digite a posição a jogar 1-9 (jogador %s):" % jogador))
if jogada<1 or jogada>9:
print("Posição inválida")
continue
# Verifica posição livre
if tabuleiro[posicoes[jogada][0]][posicoes[jogada][1]] != " ":
print("Posição ja utilizada ocupada.");
continue
# Marca a jogada p/ o jogador
tabuleiro[posicoes[jogada][0]][posicoes[jogada][1]] = jogador
# Verfica se ganhou
for p in win:
for x in p:
if tabuleiro[posicoes[x][0]][posicoes[x][1]] != jogador:
break
else: # Se o for terminar sem break, todas as posicoes de p pertencem ao mesmo jogador
print("O jogador %s ganhou (%s): "%(jogador, p))
jogando = False
break
jogador = "X" if jogador == "O" else "O" # Alterna os jogador
jogada +=1 # Contador de jogadas
Do you have to do the client and the server? or just the client?
– Don't Panic
type the idea is to make both client and server, I just have no idea how to divide this code and where to fit the socket.
– Cristian Pereira
The way your question stands is too broad. I believe you can divide your problem into several parts and, if necessary, ask a question relating to each part. Start by studying the package
socket
from Python and try to create communication between two computers. If you have difficulty with this, ask this subject only.– Woss
You have read the Python documentation --> 1) https://docs.python.org/3/howto/sockets.html 2) https://pymotw.com/2/socket/tcp.html
– weltonvaz
Ja li. I understood how the socket works, I just couldn’t understand how I would make the client and server mount the distribution of the code between the two joining the socket. ( my logic is very weak I have a very serious difficulty to understand)
– Cristian Pereira
tip: read first about functions, and rewrite your prototypical sweating functions. it won’t be impossible until you start.
– jsbueno