Beispiel #1
0
    def listen(self):
        self._socket.listen(5)
        while True:
            channel, client_address = self._socket.accept()
            b = Board()            
            b.cls()
            print('---------------Starting the Game----------------')
            print >>sys.stderr, 'Fighting with: ', client_address
            print('Your symbol is: ' + self._symbol)
            b.printHelp()
            try:
                channel.sendall(self.initializePlayer())
                while True:
                    print('Waiting for move...')
                    data = channel.recv(self._data_size)
                    b.cls()
                    print('---------------Starting the Game----------------')
                    print >>sys.stderr, 'Fighting with: ', client_address
                    print('Your symbol is: ' + self._symbol)
                    print('Player O: ' + str(data)) 
                    b.addMove(data, 'O')
                    b.printBoard()
                    if str(data) == 'q' or not data:
                        print('---------------End of the game----------------')
                        channel.close()
                        sys.exit()
                        break
                    if data == 'h':
                        b.printHelp()
                    else:
                        while True:
                            msg = raw_input('Type number of field you want to fill (h for help, q for quit):\n')
                            if(b.checkIfEmpty(msg)):
                                b.cls()
                                print('---------------Starting the Game----------------')
                                print >>sys.stderr, 'Fighting with: ', client_address
                                print('Your symbol is: ' + self._symbol)
                                b.addMove(msg, self._symbol)
                                b.printBoard()
                                channel.send(str(msg))
                                break
                            else:
                                if msg == 'q':
                                    channel.close()
                                    break
                                if msg == 'h':
                                    b.printHelp()
                                else:
                                    print('bad move')

                    
            finally:
                print('Connection %s:%s closed' % client_address)
                break
                channel.close()            
Beispiel #2
0
    def startGame(self):
        b = Board()
        b.cls()
        print("---------------Starting the Game----------------")
        print >>sys.stderr, "Fighting with: ", self._server_address
        print("Your symbol is: " + self._symbol)
        b.printHelp()
        while True:
            while True:
                msg = raw_input("Type number of field you want to fill (h for help, q for quit):\n")
                if b.checkIfEmpty(msg):
                    b.addMove(msg, self._symbol)
                    b.printBoard()
                    self._socket.send(str(msg))
                    break
                else:
                    if msg == "q":
                        self._socket.close()
                        sys.exit()
                        break
                    if msg == "h":
                        b.printHelp()
                    else:
                        print("bad move")

            if msg == "h":
                b.printHelp()
            b.cls()
            print("---------------Starting the Game----------------")
            print >>sys.stderr, "Fighting with: ", self._server_address
            print("Your symbol is: " + self._symbol)
            b.printBoard()
            print("Waiting for move...")
            response = self._socket.recv(self._data_size)
            b.addMove(response, "X")
            b.cls()
            print("---------------Starting the Game----------------")
            print >>sys.stderr, "Fighting with: ", self._server_address
            print("Your symbol is: " + self._symbol)
            b.printBoard()
            print("Player X: " + str(response))
            if msg == "q" or response == "q":
                print("---------------End of the game----------------")
                self._socket.close()
                break