这是一个简单的用python编写并使用sockets的hangman游戏。你知道吗
好的,这是我的客户程序。你知道吗
#!/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()
这是我的服务器程序。你知道吗
#!/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()
问题是,当我发送一个长度正确但猜错的单词时,它就退出了,它应该发送一个侮辱和∗并等待另一个消息,但它没有。 我已经从这个代码中删除了一些列表,或者它太长了。 任何帮助都将不胜感激。 谢谢
我只是做了一些事情,可能不会让我得到100%的分数为这个任务,但它的工作哈哈!你知道吗
我刚刚更改了它将作为单个消息接受并作为单个消息发送的字节数(>;:)
相关问题 更多 >
编程相关推荐