I have added more meat to it, I wanted it to be multi-player, but I can't test it right now... I've coded it but I am behind a router... and I can't forward ports because my father's girlfriend has changed the router's login and password... MEH ! Here's the code though, don't be shy and tell me if something's wrong with it.
Code:
import socket
# turnInfo is used to know wether the current application is
# player 1 (host) or player 2 (client)
turnInfo = { 'y':1, 'n':2 }
# symbols is used to display the status of the game table
symbols = { 0:' ', 1:'x', 2:'o' }
# The game table is represented by a serie of zeros (empty),
# ones (player 1) and twos (player 2)
table = [0,0,0,0,0,0,0,0,0]
wantsToPlay = 'y'
port = 1234
host = "127.0.0.1"
def displayTable(table):
for i in range(len(table)):
if (i+1)%3 is 0:
print symbols[table[i]]
else:
print symbols[table[i]], '|',
def resetTable(table):
for i in range(len(table)):
table[i] = 0
def isWinner(table, player):
if (table[0] is player and table[1] is player and table[2] is player) or \
(table[3] is player and table[4] is player and table[5] is player) or \
(table[6] is player and table[7] is player and table[8] is player) or \
(table[0] is player and table[3] is player and table[6] is player) or \
(table[1] is player and table[4] is player and table[7] is player) or \
(table[2] is player and table[5] is player and table[8] is player) or \
(table[0] is player and table[4] is player and table[8] is player) or \
(table[2] is player and table[4] is player and table[6] is player):
return 1
else:
return 0
# Initialize socket and decide who is the server
discussionSocket = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
isHost = raw_input("Is this the server side ? (y/n) ")
# If current application is host, bind socket and listen for connection
if isHost is 'y':
s.bind(("127.0.0.1", port))
s.listen(1)
discussionSocket, addr = s.accept()
print "Successfully connected to %s" % (addr)
# Otherwise, try connecting to host
else:
if s.connect_ex((host, port)) is 0:
print "Successfully connected to host"
discussionSocket = s
while(wantsToPlay is 'y'):
resetTable(table)
# We start with player one, so we need to set playerTurn as 2
playerTurn = 2
# While nobody wins
while(isWinner(table, 1) is not 1 and isWinner(table, 2) is not 1):
displayTable(table)
# Change player turn. This trick recquired playerTurn to be set as 2 previously
# in order to start the game with player 1
playerTurn = (2 - playerTurn) + 1
# If current application matches player turn (host = 1, client = 2)
if turnInfo[isHost] is playerTurn:
# Wait until the player has entered a valid input
while 1 is 1:
coord = int(raw_input("What coord do you want to play ? (0-8) "))
if table[coord] is 0:
table[coord] = playerTurn
discussionSocket.send(coord)
break
# Otherwise, wait for distant player's move
else:
answer = discussionSocket.recv(4)
table[int(answer)] = playerTurn
print "Congratulations player %d you have won !" % (playerTurn)
wantsToPlay = raw_input("Do you want to play again ? (y/n) ")
I've added comments on the previous code, by the way.