Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question: I'm trying to set up a TCP Server/Client System to run a game; however, I need help with the foll... I'm trying to set
Question: I'm trying to set up a TCP Server/Client System to run a game; however, I need help with the foll...
I'm trying to set up a TCP Server/Client System to run a game; however, I need help with the following: 1) Making sure that when the client tries to move that they can not move outside of the walls that are indicated by #. Below is the program I have so far, any help in getting the above mentioned issues setup is appreciated. I am using Python, but I am not firmilar with Python. TCP GameServer: from socket import * from threading import Thread from client import client def server(): serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind(['', serverPort]) serverSocket.listen(1) print("The server is ready to receive") def collision(x, y): return maze[x][y] == '#' def coins(): coins = 0 def checkForCoins(x, y): if maze[x][y] == '@': coins += 1 maze[x][y] = ' ' def a (): a = ['#', '#', '#', '#', '#', '#', '#', '#'] while True: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024).decode() capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence.encode()) connectionSocket.close() maze = ( ('#', '#', '#', '#', '#', '#', '#', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', ' ', ' ', ' ', ' ', ' ', ' ', '#'), ('#', '#', '#', '#', '#', '#', '#', '#') ) for x in range(len(a)): print (a[x],) if CMD_W: if not collision(x, y + 1): y += 1 checkForCoins(x, y) playerPositionY+=1 elif CMD_A: if not collision(x - 1, y): x -= 1 checkForCoins(x, y) playerPositionX-=1 elif CMD_S: if not collision(x, y - 1): y -= 1 checkForCoins(x, y) playerPositionY-=1 else: if not collision(x + 1, y): x += 1 checkForCoins(x, y) playerPositionX+=1 serverThread = Thread(target=server) serverThread.start()
TCP GameClient:
import string from socket import * def client(): serverName = "127.0.0.1" serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input("") clientSocket.send(sentence.encode()) modifiedSentence = clientSocket.receive(1024) commands = "!help displays help commands " \ "CMD_W: Moves the player up one space " \ "CMD_A: Moves the player left one space " \ "CMD_S: Moves the player down one space " \ "CMD_D: Moves the plater right one space " \ "SCORE: Prints the players score" \ "END: Ends Coin Hunter" timer = 0 print('Type end to close connection to server') message = " " while ('!close' != message): message = input(':') if message == "!help": print(commands) else: print("0") if CMD_W: if not collision(x, y + 1): y += 1 checkForCoins(x, y) playerPositionY+=1 elif CMD_A: if not collision(x - 1, y): playerPositionX-=1 checkForCoins(x, y) playerPositionX-=1 elif CMD_S: if not collision(x, y - 1): y -= 1 checkForCoins(x, y) playerPositionY-=1 elif CMD_D: if not collision(x + 1, y): x += 1 checkForCoins(x, y) playerPositionX+=1 else: clientSocket.close()
Actual Assignement:
A. Create a server that will hold the following information for each player in a list a. Player ID (Will be the index of the player array) b. Position X c. Position Y B. C. D. d. Coins The server will have a 2D array of characters spaces for open ground and "#" for walls. The server will assign an empty place for the player If the server receives a player input you will try to change the Position X and Position Y variablesa a. Remember with arrays it is Column, Row notation. Therefore the "Y value would go second. Also remember 0,0 is the top left index. b. E. The Server will also have various places in the maze with a lower-case o. This will represent a coih F. If a player moves onto this position then their Coin variable increases by one. G. The coin will be a space when the player moves off it. H The server will accept "CMD_"with a "W"', "A", "S", or D" at the end, representing the direction the plaver wants to move. L The player cannot move out of the maze or onto a wall. J. When the server receives a command it will return the maze with each player at the correct position, marked by their player ID K. If the Client sends "SCORE", the server will return a list of player IDs and their scores. Client A. The client will accept W,A,S or D from the client and then send the command message B. It will immediately print the maze it receives from the server C. IF the client types SCORE, it will requests the score from the server and then print the results. A. Create a server that will hold the following information for each player in a list a. Player ID (Will be the index of the player array) b. Position X c. Position Y B. C. D. d. Coins The server will have a 2D array of characters spaces for open ground and "#" for walls. The server will assign an empty place for the player If the server receives a player input you will try to change the Position X and Position Y variablesa a. Remember with arrays it is Column, Row notation. Therefore the "Y value would go second. Also remember 0,0 is the top left index. b. E. The Server will also have various places in the maze with a lower-case o. This will represent a coih F. If a player moves onto this position then their Coin variable increases by one. G. The coin will be a space when the player moves off it. H The server will accept "CMD_"with a "W"', "A", "S", or D" at the end, representing the direction the plaver wants to move. L The player cannot move out of the maze or onto a wall. J. When the server receives a command it will return the maze with each player at the correct position, marked by their player ID K. If the Client sends "SCORE", the server will return a list of player IDs and their scores. Client A. The client will accept W,A,S or D from the client and then send the command message B. It will immediately print the maze it receives from the server C. IF the client types SCORE, it will requests the score from the server and then print the resultsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started