0

Below is the Python code for a simple game i had to create now i am having difficulty seperating the code to play the game over a client and server using sockets. any help would be appreciated

import random
rannum = random.randrange(0,10,1)
print rannum
counter = 0
a=0
while(a==0):
 guess = int(raw_input("What is your guess?: "))
 counter = counter + 1
 diff = abs(guess - rannum)
 if(diff >5):
 print "Way Off!"
 elif(diff >2):
 print "Close!"
 elif (diff>1):
 print "Closer!"
 elif(diff >0):
 print "Even Closer!"
 elif (diff==0):
 print "Correct"
 name = str(raw_input("What is your name?: "))
 print name + " Your score is: "
 print counter
 f = open('high_scores.txt', 'a')
 score = str(counter)
 f.write(name + " " + score + "\n")
 f.close()
 break
Xavier
1,5822 gold badges17 silver badges29 bronze badges
asked Nov 7, 2012 at 9:04
2
  • 1
    What's your question? You just pasted your code and basiclly said "Help me!". Commented Nov 7, 2012 at 9:10
  • because i am unsure how to seperate user interface and game logic and the specific code to ensure it works over a client server architecture Commented Nov 7, 2012 at 9:30

2 Answers 2

1

The client interface in your code is just the raw_input function and print statements. Effectively, all you need to do is turn those into recv and send calls on a socket, and you've got a server. Or, more simply, wrap the socket in makefile and just write and read lines:

import random
import socket
rannum = random.randrange(0,10,1)
print rannum
counter = 0
a=0
sock = socket.socket()
sock.bind(('127.0.0.1', 12345))
sock.listen(5)
clientsock, addr = sock.accept()
client = clientsock.makefile('r+')
while(a==0):
client.write('What is your guess?: ')
guess = int(client.readline())
counter = counter + 1
diff = abs(guess - rannum)
if(diff >5):
 client.write("Way Off!\n")
...

You can test this without even writing a client, just using nc localhost 12345. (If you're on Windows, you don't have nc built-in; google "netcat Windows" and get yourself a copy.) Then just look at the docs for socket.connect and you should be able to write the client side.

Now, you may want to reorganize your interface a bit. Maybe the client should provide the prompts and format the results (so, e.g., you can have separate English and Spanish clients with the same server—or, even better, a nice GUI client). To do that, you need to design some kind of protocol for the client and server to use for communication. (In fact, even the trivial example above has an implicit protocol: every prompt, response, etc. is separated by newlines. I "cheated" by using makefile and readline to handle that automatically. But figuring out how to separate messages—newlines, length-prefixing, etc.—is just the first step.)

You also may want to consider whether you really want to design a "raw" protocol that sits on top of TCP, or use something that takes care of the low-level stuff for you (a web service, JSON-RPC over sockets, etc.) so you only need to write the application-specific stuff yourself.

Finally, this is a very, very bad server. It only accepts one client, and it does everything synchronously, so if the client screws up the server can't recover. A real server needs what's called a "reactor" or a "proactor" (you can built your own with select.select, or use asyncore or something third-party like twisted), or needs to spawn a new thread or process for each accept.

answered Nov 7, 2012 at 9:20
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot just using IDLE and only requires one client to work with it. think you've pretty much covered all bases
OK, but a real server runs forever, and accepts as many clients as want to connect (even multiple clients at the same time), so understand that learning how to write code like this is only the first step to writing a real server...
0

The question is not really clear, but you should first try to define what goes in the client and what goes on the server. In your case, it should be something like:

  • user interaction (prompts and getting values) on the client
  • random value generation, verification of user input and management of high scores on the server

Then you have to think about how the client and server communicates: how do you start a new game, what needs to go in the pipe (guess value in one direction, quality of guess in the other)...

answered Nov 7, 2012 at 9:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.