Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import socket
- import sys
- HOST = ''
- PORT = 1337
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- def Game(conn):
- while True:
- conn.send("Wait while player1 is setting his name...\n")
- player1 = raw_input("Player1 Name: ")
- conn.send("You're playing with %s\n" %player1)
- conn.send("Player2 Name: ")
- print "Wait while player2 is setting his name..."
- player2 = conn.recv(1024)
- print "You're playing with %s" %player2
- options = ["Rock", "Paper", "Scissors"]
- print "\n1-Rock"
- print "2-Paper"
- print "3-Scissors\n\n"
- conn.send("\n\n1-Rock\n2-Paper\n3-Scissors\n\n\n")
- conn.send("It's %s time...\n" %player1)
- play1 = int(input("It's your time, %s: " %player1))
- print "It's %s time..." %player2
- conn.send(("It's your time, %s: " %player2))
- play2 = int(conn.recv(1024))
- if (play1 == 1 and play2 == 3) or (play1 == 2 and play2 == 1) or (play1 == 3 and play2 == 2):
- print "%s beats %s" %(options[play1-1],options[play2-1])
- print "The winner is %s, congratulations!\n" %player1
- conn.send("%s beats %s\n" %(options[play1-1],options[play2-1]))
- conn.send("The winner is %s, congratulations!\n\n" %player1)
- elif (play2 == 1 and play1 == 3) or (play2 == 2 and play1 == 1) or (play2 == 3 and play1 == 2):
- print "%s beats %s" %(options[play2-1],options[play1-1])
- print "The winner is %s, congratulations!\n" %player2
- conn.send("%s beats %s\n" %(options[play2-1],options[play1-1]))
- conn.send("The winner is %s, congratulations!\n\n" %player2)
- else:
- print "Wow, you have a draw!\n"
- conn.send("Wow, you have a draw!\n\n")
- again = raw_input("Wanna play again? [Y\\n]: ")
- print "Waiting for the player2..."
- conn.send("Wanna play again? [Y\\n]: ")
- recv = conn.recv(1024)
- if again.lower() == 'n' and recv.lower() == 'n':
- print "GAME OVER"
- conn.send("GAME OVER\n")
- break
- try:
- s.bind((HOST,PORT))
- except socket.error as msg:
- print 'Error in the hosting step:',msg
- sys.exit()
- s.listen(10)
- os.system('clear')
- print "Server listening..."
- while True:
- conn, addr = s.accept()
- print "Connected with "+addr[0]+":"+str(addr[1])
- Game(conn)
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement