我在玩刽子手游戏。我试图发送到服务器,如果lose==7,那么loseGame=true。而在客户端,如果loseGame是真的,就打印出游戏已经输了。我匹配了send和recv,但它不起作用,一直在请求输入以猜测字母。你知道我做错了什么吗?在
我把问题放在我认为是问题的地方。在
谢谢你!在
服务器:
import sys
# Import socket library
from socket import *
if sys.argv.__len__() != 2:
serverPort = 5895
# Get port number from command line
else:
serverPort = int(sys.argv[1])
# Choose SOCK_STREAM, which is TCP
# This is a welcome socket
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# Start listening on specified port
serverSocket.bind(('', serverPort))
# Listener begins listening
serverSocket.listen(1)
print("The server is ready to receive")
#Set secret word
word = 'arkansas'
linesForString = ''
#Prints out number of letters
for x in word:
linesForString += '_'
newWord = 'arkansas'
# Wait for connection and create a new socket
# It blocks here waiting for connection
connectionSocket, addr = serverSocket.accept()
win = ' '
#Sends lines of words
linesInBytes = linesForString.encode('utf-8')
connectionSocket.send(linesInBytes)
lose = 0
while 1:
l = list(word)
list2 = list(linesForString)
win = False
while 1:
while win == False:
losee = 0
# Receives Letter
letter = connectionSocket.recv(1024)
letterString = letter.decode('utf-8')
for x in range(len(list2)):
if(list2[x] == '_'):
if(letterString == l[x]):
list2[x] = letterString
for x in range(len(word)):
if(letterString == word[x]):
losee = -1
if (losee != -1):
lose += 1
print(lose)
newWord = "".join(list2)
#Sends newWord
newWordInBytes = newWord.encode('utf-8')
connectionSocket.send(newWordInBytes, lose)
if(newWord == 'arkansas'):
win = True
winGame = 'You have won the game'
winGameInBytes = winGame.encode('utf-8')
connectionSocket.send(winGameInBytes)
connectionSocket.close()
if(lose == 7):
loseGame = 'true'
connectionSocket.close()
else:
loseGame = 'false'
#THIS IS WHERE THE PROBLEM IS
loseGameInBytes = loseGame.encode('utf-8')
connectionSocket.send(loseGameInBytes)
# Close connection to client but do not close welcome socket
connectionSocket.close()
客户:
^{pr2}$
在我遇到一些问题之前: 1注意,当你接受输入时使用原始输入,否则我会崩溃。 2.当你认为服务器有问题的时候,确实有问题。你不小心关闭了连接,然后在游戏失败后发送 三。在客户机中,}
4我将print语句改为
loseGame = loseGame.encode('utf-8')
而不是{print(str(x) + " "),
,因为你的语句给了我一些问题这是我修好后的代码。请注意,我更改了服务器,并添加了不必要的中断,它们将导致服务器在一个游戏结束时关闭。在
服务器代码:
客户代码:
^{pr2}$相关问题 更多 >
编程相关推荐