Guest User

Untitled

a guest
Dec 4th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.11 KB | None | 0 0
  1. # Fig. 20.7: fig20_07.py
  2. # Client for Tic-Tac-Toe program.
  3.  
  4. import socket
  5. import threading
  6. from tkinter import *
  7. import Pmw
  8.  
  9. class TicTacToeClient(Frame, threading.Thread ):
  10.    """Client that plays a game of Tic-Tac-Toe"""
  11.  
  12.    def __init__( self ):
  13.       """Create GUI and play game"""
  14.  
  15.       threading.Thread.__init__( self )
  16.  
  17.       # initialize GUI
  18.       Frame.__init__( self )
  19.       Pmw.initialise()
  20.       self.pack( expand = YES, fill = BOTH )
  21.       self.master.title( "Tic-Tac-Toe Client" )
  22.       self.master.geometry( "250x325" )
  23.  
  24.       self.id = Label( self, anchor = W )
  25.       self.id.grid( columnspan = 3, sticky = W+E+N+S )
  26.  
  27.       self.board = []
  28.  
  29.       # create and add all buttons to the board
  30.       for i in range( 9 ):
  31.          newButton = Button( self, font = "Courier 20 bold",
  32.             height = 1, width = 1, relief = GROOVE,
  33.             name = str( i ) )
  34.          newButton.bind( "<Button-1>", self.sendClickedSquare )
  35.          self.board.append( newButton )
  36.  
  37.       current = 0
  38.  
  39.       # display buttons in 3x3 grid beginning with grid's row one
  40.       for i in range( 1, 4 ):
  41.  
  42.          for j in range( 3 ):
  43.             self.board[ current ].grid( row = i, column = j,
  44.                sticky = W+E+N+S )
  45.             current += 1
  46.  
  47.       # area for server messages
  48.       self.display = Pmw.ScrolledText( self, text_height = 10,
  49.          text_width = 35, vscrollmode = "static" )
  50.       self.display.grid( row = 4, columnspan = 3 )
  51.  
  52.       self.start()   # run thread
  53.  
  54.    def run( self ):
  55.       """Control thread to allow continuous updated display"""
  56.  
  57.       # setup connection to server
  58.       HOST = "127.0.0.1"
  59.       PORT = 5000
  60.       self.connection = socket.socket( socket.AF_INET,
  61.          socket.SOCK_STREAM )
  62.       self.connection.connect( ( HOST, PORT ) )
  63.       self.myMark = self.connection.recv( 1 )
  64.       self.id.config( text = 'You are player "%s"' % self.myMark )
  65.  
  66.       self.myTurn = 0
  67.  
  68.       # receive messages sent to client
  69.       while 1:
  70.          message = self.connection.recv( 34 )
  71.  
  72.          if not message:
  73.             break
  74.  
  75.          self.processMessage( message )
  76.  
  77.       self.connection.close()
  78.       self.display.insert( END, "Game over.\n" )
  79.       self.display.insert( END, "Connection closed.\n" )
  80.       self.display.yview( END )
  81.  
  82.    def processMessage( self, message ):
  83.       """Interpret server message to perform necessary actions"""
  84.  
  85.       # valid move occurred
  86.       if message == "Valid move.":
  87.          self.display.insert( END, "Valid move, please wait.\n" )
  88.          self.display.yview( END )
  89.  
  90.          # set mark
  91.          self.board[ self.currentSquare ].config(
  92.             text = self.myMark, bg = "white" )
  93.  
  94.       # invalid move occurred
  95.       elif message == "Invalid move, try again.":
  96.          self.display.insert( END, message + "\n" )
  97.          self.display.yview( END )
  98.          self.myTurn = 1
  99.  
  100.       # opponent moved
  101.       elif message == "Opponent moved.":
  102.  
  103.          # get move location
  104.          location = int( self.connection.recv( 1 ) )
  105.  
  106.          # update board
  107.          if self.myMark == "X":
  108.             self.board[ location ].config( text = "O",
  109.                bg = "gray" )
  110.          else:
  111.             self.board[ location ].config( text = "X",
  112.                bg = "gray" )
  113.  
  114.          self.display.insert( END, message + " Your turn.\n" )
  115.          self.display.yview( END )
  116.          self.myTurn = 1
  117.  
  118.       # other player's turn
  119.       elif message == "Other player connected. Your move.":
  120.          self.display.insert( END, message + "\n" )
  121.          self.display.yview( END )
  122.          self.myTurn = 1
  123.  
  124.       # simply display message
  125.       else:
  126.          self.display.insert( END, message + "\n" )
  127.          self.display.yview( END )
  128.  
  129.    def sendClickedSquare( self, event ):
  130.       """Send attempted move to server"""
  131.  
  132.       if self.myTurn:
  133.          name = event.widget.winfo_name()
  134.          self.currentSquare = int( name )
  135.  
  136.          # send location to server
  137.          self.connection.send( name )
  138.          self.myTurn = 0
  139.  
  140. def main():
  141.    TicTacToeClient().mainloop()
  142.  
  143. if __name__ == "__main__":
  144.    main()
Add Comment
Please, Sign In to add comment