Es ist ein einfaches Hangman-Spiel, geschrieben in Python und mit Sockets.Server und Client funktionieren nicht so wie sie sollten, wenn sie mit einer Nachricht länger als 1 Zeichen umgehen
Ok, das ist mein Client-Programm.
#!/usr/bin/env python3
import socket
import time
import sys
por=(sys.argv[2])
Zaphod_Beeblerox = int(por)
print ("To exit from the game at any time please type and send QUIT")
def Main():
host = sys.argv[1]
port = Zaphod_Beeblerox
s = socket.socket()
s.connect((host, port))
message = input("Would you like a GAME or QUIT?")
while message != 'QUIT':
s.send("NEW_GAME".encode('utf-8'))
dat = s.recv(1024).decode('utf-8')
print(dat)
msg = "a"
data = 'nowt'
while msg != 'QUIT':
while "score" not in (data):
if (msg) in (lowercase):
msg = input("->")
s.send(msg.encode('utf-8'))
data = s.recv(1024).decode('utf-8')
if "_" not in (data):
print (data)
s.send(msg.encode('utf-8'))
data = s.recv(1024).decode('utf-8')
print (data)
break
else:
print (data)
else:
break
break
break
print("Exiting")
s.close()
if __name__ == '__main__':
Main()
Dies ist mein Serverprogramm.
#!/usr/bin/env python3
import sys
import socket
import random
import time
por=(sys.argv[1])
Zaphod_Beeblerox = int(por)
def Main():
#setting up the connection
host = ''
port = Zaphod_Beeblerox
s = socket.socket()
s.bind((host,port))
# setting the beginning parameters
secret_word = random.choice(word_list)
guesses=0
letters_guessed = []
word = []
for x in range(len(secret_word)):
word.append('_ ')
#listening for a connection and then announcing it.
s.listen(5)
c, addr = s.accept()
print("Connection from: " + str(addr))
#while testing i used this but it is cheating
# print (secret_word)
while True:
guess = c.recv(1024).decode('utf-8')
print (guess)
if guess == 'NEW_GAME':
c.send(("\n %s"%''.join(word)).encode('utf-8'))
#if the guess is a single lowercase letter
elif len(guess) == 1:
if (guess) in (letters_guessed): #if already guessed
ins = random.choice(insult)
c.send(("\n %s"%''.join(word) + "\n" + "\n" + ins).encode('utf-8'))
elif (guess) in (lowercase):
#if it is the final letter
if (guess).join(word) == secret_word:
score = guesses/2
scr = 10.5 - scor
scrr = str(scr)
print("Score =", + scr)
Quote = random.choice(Hitch_Quote)
c.send((("\n Your score is: ") + (scrr) + "\n" + "\n" (Quote) + "\n" + "\n" (secret_word)).encode('utf-8'))
break
# else:
# c.send(("\n %s"%''.join(word)).encode('utf-8'))
elif (guess) in (secret_word):
for index, letter in enumerate(secret_word):
if letter == guess:
word[index] = guess
letters_guessed.append(guess)
Quote = random.choice(Hitch_Quote)
c.send(("\n %s"%''.join(word) + "\n" + "\n" + Quote).encode('utf-8'))
else:
letters_guessed.append(guess)
guesses = guesses + 1
ins = random.choice(insult)
c.send(("\n %s"%''.join(word) + "\n" + "\n" + ins).encode('utf-8'))
if ''.join(word) == secret_word:
score = guesses/2
scr = 10.5 - score
scrr = str(scr)
print("Score =", + scr)
Quote = random.choice(Hitch_Quote)
c.send((("\n \n \nYour score is: ") + (scrr) + "\n" + "\n" + (Quote) + "\n" + "\n").encode('utf-8'))
break
else:# if isnt in lowercase
break
elif len(guess) == len(secret_word): #word guesses
while guess != secret_word:
letters_guessed.append(guess)
guesses = guesses + 1
ins = random.choice(insult)
c.send(("\n %s"%''.join(word) + "\n" + "\n" + ins).encode('utf-8'))
split = list(guess)
if set(split).issubset(lowercase) == True:
if guess == secret_word:
print(secret_word)
score = guesses/2
scr = 10.5 - score
scrr = str(scr)
print("Score =", + scr)
Quote = random.choice(Hitch_Quote)
c.send((secret_word).encode('utf-8'))
c.send((("\n \n \nYour score is: ") + (scrr) + "\n" + "\n" + (Quote) + "\n" + "\n").encode('utf-8'))
time.sleep(3)
break
else:
break
else:
break
if not guess:
break
print("GAME OVER")
c.close()
if __name__ == '__main__':
Main()
Das Problem ist, dass, wenn ich ein Wort erraten die richtige Länge wird aber falsch es beendet wird, sollte es eine Beleidigung und der _ und warten auf eine andere Nachricht senden, aber es funktioniert nicht. Ich habe einige Listen aus diesem Code gelöscht oder es wird zu lang. Jede Hilfe würde sehr geschätzt werden. Danke
Von einem Blick auf dem Code an: Sie gehen davon aus einem 'send()' entspricht einen 'reveice() '. Bei Sockets tut es das nicht. Sie benötigen ein Anwendungsprotokoll, um den Nachrichtenrahmen bereitzustellen. – CodeCaster
Das klingt kompliziert. – Bridget