In this project, you will implement a simple game of hangman (https://en.wikiped
ID: 3721274 • Letter: I
Question
In this project, you will implement a simple game of hangman (https://en.wikipedia.org/wiki/Hangman (game) using socket programming in Python or Any other language you choose to send and receive messages between a client program and a server program. Hangman is a paper and pencil guessing game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses. (via Wikipedia) You're going to build a set of server and client such that server will generate words and determine whether the players win or lose the game, and the players will play the game on the client side that send its guess to the server. The protocol for this program will be described in this document. This project is adopted from CSE 422@MSÙ (https://www.cse.msu.edu/%7Eden nisp se422).Explanation / Answer
Hangman-game-Python-
server and client based Hangman game (Python)
steps to run:
run hangman_server.py first--> python hangman_server.py
run hangman_client.py ---> python hangman_client.py
enter the word to be played in client terminal
now guess the word entering only one letter at a time. (Basic hangman rules apply) HERE SERVER is the player and CLIENT is another player who gives the word to guess. PS: sorry for the names confusion
------------------------------
client file: client.py
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
inpt = input()
clientsocket.send(bytes(inpt, 'UTF-8'))
print("the message has been sent")
--------------------------------------
server file: server.py
import socket
import sys
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 8089))
serversocket.listen(1) # become a server socket, maximum 5 connections
while True:
connection, address = serversocket.accept()
buf = connection.recv(64)
print(str(buf, "utf-8"))
word = str(buf, "utf-8")
break
print(word)
HANGMANPICS = ['''
+-----+
| |
|
|
|
|
======= ''','''
+-----+
| |
| 0
|
|
|
======= ''','''
+-----+
| |
| 0
| |
|
|
======= ''','''
+-----+
| |
| 0
| /|
|
|
======= ''','''
+-----+
| |
| 0
| /|
|
|
======= ''','''
+-----+
| |
| 0
| /|
| /
|
======= ''','''
+-----+
| |
| 0
| /|
| /
|
======= ''' ]
def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
print(HANGMANPICS[len(missedLetters)])
print()
print('Missed letters:', end=' ')
for letter in missedLetters:
print(letter, end=' ')
print()
blanks = '_' * len(secretWord)
for i in range(len(secretWord)): # replace blanks with correctly guessed letters
if secretWord[i] in correctLetters:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
for letter in blanks: # show the secret word with spaces in between each letter
print(letter, end=' ')
print()
def getGuess(alreadyGuessed):
while True:
print('Guess a letter.')
guess = input()
guess = guess.lower()
if len(guess) != 1:
print('Please enter a single letter.')
elif guess in alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please enter a LETTER.')
else:
return guess
print('H A N G M A N')
missedLetters = ''
correctLetters = ''
secretWord = word
gameIsDone = False
while True:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
guess = getGuess(missedLetters + correctLetters)
if guess in secretWord:
correctLetters = correctLetters + guess
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print('Yes! The secret word is "' + secretWord + '"! You have won!')
clientsocket.send(bytes('You lost opponent won', 'UTF-8'))
gameIsDone = True
break
else:
missedLetters = missedLetters + guess
if len(missedLetters) == len(HANGMANPICS) - 1:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
print('You have run out of guesses! After ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
gameIsDone = True
break
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.