simple turn-based multiplayer game via TCP server/client

2009-01-03 Thread greyw...@gmail.com
Hi everyone,

I'm learning python to get a multiplayer roleplaying game up and
running.

I didn't see any simple examples of multiplayer games on the web so I
thought I'd post mine here.  I choose Rock, Paper, Scissors as a first
game to experiment with as the game logic/options are easy to
implement and understand.

Initially, I tried to get the socketserver.TCPServer example in the
Python Docs to work, but couldn't get my game variables into the
handle method of the class MyTCPHandler.  And I didn't know how else
to do it.  I ended up creating my own server  client out of simple
sockets based on the simple echo server  client examples in the
Python Docs.

I also wanted to send chat messages OR game variables back  forth,
but I couldn't figure out how to do the OR, so the basic idea in this
implementation is to send the dictionary 'game' back and forth.  game
dict contains all the gaming variables as well as any chat messages.
The program processes results depending on whether the game is
starting, there's a chat message, or there's a game move.

Finally, in testing, I ran the server in IDLE, but I had to load a
command prompt and switch to c:\python30; then type 'python
rpsmulti.py' for the client every time.  Anyone know how to test
server/client code strictly in IDLE?

Anyway, try it out and let me know how you would improve it.

John R.

# NAME: rpsmulti.py
# DESCRIPTION: rock, paper, scissors game multiplayer game
# AUTHOR: John Robinson
# DATE: 1/3/09
# VERSION: Python 3.0
# TO DO:
#   .server_address instead of HOST, PORT?

import socket
from random import choice
from pickle import dumps, loads
from pprint import pprint

HOST, PORT = localhost,   # defined for now
BUFFSIZE = 1024 # for socket.send  recv commands
BACKLOG = 2 # number of clients supported by
server
SECONDS = 3 # seconds until socket.timeout (not
implemented)
# moves dict to translate 'rps' choice
MOVES = {'r':'Rock',
 'p':'Paper',
 's':'Scissors'}
# outcome dict stores result for all possible scenarios
OUTCOME = {('p','r'): 'win',
   ('r','s'): 'win',
   ('s','p'): 'win',
   ('p','p'): 'draw',
   ('r','r'): 'draw',
   ('s','s'): 'draw',
   ('r','p'): 'lose',
   ('s','r'): 'lose',
   ('p','s'): 'lose'}

def main_menu(game):
 initialize game dict variables  opening screen of the game

game['total'] = 0   # total number of games played
game['won'] = 0 # total games won by player
game['lost'] = 0# total games lost by player
game['drew'] = 0# total games drew by player
game['move'] = ''   # player's move (rps)
game['message'] = ''# player's chat message
game['sentmessage'] = ''# player's previous message
game['start'] = True# setting up the game boolean

print(\tROCK PAPER SCISSORS\n)
if game['name']=='':# if returning to menu don't display
the following
game['name'] = get_name()
print(Welcome +game['name']+. Remember...)
print(Rock smashes scissors! Paper covers Rock! Scissors cuts
paper!\n)
print(1. Play single player game)
print(2. Start two player game)
print(3. Join two player game)
print(4. Quit)
print()
c = safe_input(Your choice? , 1234)
c = int(c)
if c==1:
one_player(game)
elif c==2:
start_two_player(game)
elif c==3:
two_player_join(game)
else:
print('Play again soon.')

def safe_input(prompt, values='abcdefghijklmnopqrstuvwxyz'):
 gives prompt, checks first char of input, assures it meets
given values
default is anything goes 
while True:
i = input(prompt)
try:
c = i[0].lower()
except IndexError:  # the only possible error?!
if c=='':
print(Try again.)
else:
if c not in values: # some other character
print(Try again.)
else:   # looks good. continue.
break
return i

def get_name():
 returns input as name 
while True:
name = input(What is your name? )
check = input(name + . Correct (y/n)? )
if check[0] in 'yY':
break
return name

def get_result(player, opponent):
 reports opponent's choice;
checks player and opponent dicts ['move'] against OUTCOME
dict;
reports result
returns player dict with updated values 
print(opponent['name'], 'chose %s.' % (MOVES[opponent['move']]))
# check lookout dict (OUTCOME dictionary)
result = OUTCOME[(player['move'], opponent['move'])]
# update game variables
player['total'] += 1
if result=='win':
print('%s beats %s. You win.' % (MOVES[player['move']], MOVES
[opponent['move']]))
player['won'] += 1
elif result=='draw':
print('%s - %s: no one wins. You draw.' % (MOVES[player

Re: simple turn-based multiplayer game via TCP server/client

2009-01-03 Thread alex goretoy
Since we are on the subject of Rock, Paper, Scissors. I've recntly watched
this one video on how to make a .deb out of .py files and the tutor was
using a rock, paper scissors game.

Not sure how this may come of use to you but, I'll post it anyway for you to
look at. May help somehow.

http://www.google.com/search?hl=enq=filetype%3Apy+%22rock+paper+scissors%22btnG=Search

http://www.showmedo.com/videos/video?name=linuxJensMakingDebfromSeriesID=37


Hope this help you in your journey for multiplayer game somehow

On Sun, Jan 4, 2009 at 3:58 AM, greyw...@gmail.com greyw...@gmail.comwrote:

 Hi everyone,

 I'm learning python to get a multiplayer roleplaying game up and
 running.

 I didn't see any simple examples of multiplayer games on the web so I
 thought I'd post mine here.  I choose Rock, Paper, Scissors as a first
 game to experiment with as the game logic/options are easy to
 implement and understand.

 Initially, I tried to get the socketserver.TCPServer example in the
 Python Docs to work, but couldn't get my game variables into the
 handle method of the class MyTCPHandler.  And I didn't know how else
 to do it.  I ended up creating my own server  client out of simple
 sockets based on the simple echo server  client examples in the
 Python Docs.

 I also wanted to send chat messages OR game variables back  forth,
 but I couldn't figure out how to do the OR, so the basic idea in this
 implementation is to send the dictionary 'game' back and forth.  game
 dict contains all the gaming variables as well as any chat messages.
 The program processes results depending on whether the game is
 starting, there's a chat message, or there's a game move.

 Finally, in testing, I ran the server in IDLE, but I had to load a
 command prompt and switch to c:\python30; then type 'python
 rpsmulti.py' for the client every time.  Anyone know how to test
 server/client code strictly in IDLE?

 Anyway, try it out and let me know how you would improve it.

 John R.

 # NAME: rpsmulti.py
 # DESCRIPTION: rock, paper, scissors game multiplayer game
 # AUTHOR: John Robinson
 # DATE: 1/3/09
 # VERSION: Python 3.0
 # TO DO:
 #   .server_address instead of HOST, PORT?

 import socket
 from random import choice
 from pickle import dumps, loads
 from pprint import pprint

 HOST, PORT = localhost,   # defined for now
 BUFFSIZE = 1024 # for socket.send  recv commands
 BACKLOG = 2 # number of clients supported by
 server
 SECONDS = 3 # seconds until socket.timeout (not
 implemented)
 # moves dict to translate 'rps' choice
 MOVES = {'r':'Rock',
 'p':'Paper',
 's':'Scissors'}
 # outcome dict stores result for all possible scenarios
 OUTCOME = {('p','r'): 'win',
   ('r','s'): 'win',
   ('s','p'): 'win',
   ('p','p'): 'draw',
   ('r','r'): 'draw',
   ('s','s'): 'draw',
   ('r','p'): 'lose',
   ('s','r'): 'lose',
   ('p','s'): 'lose'}

 def main_menu(game):
 initialize game dict variables  opening screen of the game
 
game['total'] = 0   # total number of games played
game['won'] = 0 # total games won by player
game['lost'] = 0# total games lost by player
game['drew'] = 0# total games drew by player
game['move'] = ''   # player's move (rps)
game['message'] = ''# player's chat message
game['sentmessage'] = ''# player's previous message
game['start'] = True# setting up the game boolean

print(\tROCK PAPER SCISSORS\n)
if game['name']=='':# if returning to menu don't display
 the following
game['name'] = get_name()
print(Welcome +game['name']+. Remember...)
print(Rock smashes scissors! Paper covers Rock! Scissors cuts
 paper!\n)
print(1. Play single player game)
print(2. Start two player game)
print(3. Join two player game)
print(4. Quit)
print()
c = safe_input(Your choice? , 1234)
c = int(c)
if c==1:
one_player(game)
elif c==2:
start_two_player(game)
elif c==3:
two_player_join(game)
else:
print('Play again soon.')

 def safe_input(prompt, values='abcdefghijklmnopqrstuvwxyz'):
 gives prompt, checks first char of input, assures it meets
 given values
default is anything goes 
while True:
i = input(prompt)
try:
c = i[0].lower()
except IndexError:  # the only possible error?!
if c=='':
print(Try again.)
else:
if c not in values: # some other character
print(Try again.)
else:   # looks good. continue.
break
return i

 def get_name():
 returns input as name 
while True:
name = input(What is your name? )
check = input(name + . Correct (y/n)? )
if check[0] in 'yY':
break
return name

 def