Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # http://stackoverflow.com/questions/40956421/i-am-implementing-a-server-client-based-tictactoe-but-i-am-getting-errors
- # http://pastebin.com/dexntYKx
- #
- import socket
- import threading
- from tkinter import *
- import Pmw
- class TicTacToeClient(Frame, threading.Thread ):
- """Client that plays a game of Tic-Tac-Toe"""
- def __init__(self):
- """Create GUI and play game"""
- threading.Thread.__init__( self )
- # initialize GUI
- Frame.__init__( self )
- Pmw.initialise()
- self.pack( expand = YES, fill = BOTH )
- self.master.title( "Tic-Tac-Toe Client" )
- self.master.geometry( "250x325" )
- self.id = Label( self, anchor = W )
- self.id.grid( columnspan = 3, sticky = W+E+N+S )
- self.board = []
- # create and add all buttons to the board
- for i in range(9):
- newButton = Button( self, font = "Courier 20 bold",
- height = 1, width = 1, relief = GROOVE,
- name = str( i ) )
- newButton.bind( "<Button-1>", self.sendClickedSquare )
- self.board.append( newButton )
- current = 0
- # display buttons in 3x3 grid beginning with grid's row one
- for i in range( 1, 4 ):
- for j in range( 3 ):
- self.board[ current ].grid( row = i, column = j,
- sticky = W+E+N+S )
- current += 1
- # area for server messages
- self.display = Pmw.ScrolledText( self, text_height = 10,
- text_width = 35, vscrollmode = "static" )
- self.display.grid( row = 4, columnspan = 3 )
- self.start() # run thread
- def run( self ):
- """Control thread to allow continuous updated display"""
- # setup connection to server
- HOST = "127.0.0.1"
- PORT = 5000
- self.connection = socket.socket( socket.AF_INET,
- socket.SOCK_STREAM )
- self.connection.connect( ( HOST, PORT ) )
- self.myMark = self.connection.recv( 1 ).decode('ascii')
- self.id.config( text = 'You are player "%s"' % self.myMark )
- self.myTurn = 0
- # receive messages sent to client
- while 1:
- #message = self.connection.recv( 34 ).decode('ascii')
- length = int(self.connection.recv(2).decode('ascii'))
- message = self.connection.recv(length).decode('ascii')
- if not message:
- break
- self.processMessage( message )
- self.connection.close()
- self.display.insert( END, "Game over.\n" )
- self.display.insert( END, "Connection closed.\n" )
- self.display.yview( END )
- def processMessage( self, message ):
- """Interpret server message to perform necessary actions"""
- # valid move occurred
- if message == "Valid move.":
- self.display.insert( END, "Valid move, please wait.\n" )
- self.display.yview( END )
- # set mark
- self.board[ self.currentSquare ].config(
- text = self.myMark, bg = "white" )
- # invalid move occurred
- elif message == "Invalid move, try again.":
- self.display.insert( END, message + "\n" )
- self.display.yview( END )
- self.myTurn = 1
- # opponent moved
- elif message == "Opponent moved.":
- # get move location
- location = int( self.connection.recv( 1 ).decode('ascii') )
- # update board
- if self.myMark == "X":
- self.board[ location ].config( text = "O",
- bg = "gray" )
- else:
- self.board[ location ].config( text = "X",
- bg = "gray" )
- self.display.insert( END, message + " Your turn.\n" )
- self.display.yview( END )
- self.myTurn = 1
- # other player's turn
- elif message == "Other player connected. Your move.":
- self.display.insert( END, message + "\n" )
- self.display.yview( END )
- self.myTurn = 1
- # simply display message
- else:
- self.display.insert( END, message + "\n" )
- self.display.yview( END )
- def sendClickedSquare( self, event ):
- """Send attempted move to server"""
- if self.myTurn:
- name = event.widget.winfo_name()
- self.currentSquare = int( name )
- print(name, type(name))
- # send location to server
- self.connection.send( name.encode('ascii') )
- self.myTurn = 0
- def main():
- TicTacToeClient().mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement