Example #1
0
File: TTTS.py Project: suen/ttts
 def new_game_init(self):
     print "New Game initializing"
     self.game = TicTacToe()
     #self.player1 = AI("artificial agent", "X", self)
     self.player2 = AI("AI agent", "O", self)
     self.game.setPlayer1(self.player1)
     self.game.setPlayer2(self.player2)
     self.game.setFirstMover(self.player1)
     self.startGame = True
     self.playerHasMoved = False
     self.playerMove = None
     print "New Game started"
Example #2
0
from Game import TicTacToe

welcome = """
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Welcome to tic tac toe. I hate to break it
to you, but you're going to lose. But lets
play anyways! To choose a cell, simply
specify the corresponding number...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"""

print welcome
game = TicTacToe('o')
game.drawBoard()
#game.minimalMode()
game.play()
Example #3
0
from Game import TicTacToe, Humanplayer, Randomplayer
from QLearning import  Qlearning

game = TicTacToe() #game instance
player1=Humanplayer() #human player
player2=Qlearning()  #agent
game.startGame(player1,player2)#player1 is X, player2 is 0
game.reset() #reset
game.render() # render display
Example #4
0
from Game import TicTacToe
from QLearning import  Qlearning
import sys

ites = []
assert len(sys.argv)-1==3
for i in range(1,4):
    ites.append(int(sys.argv[i]))
game = TicTacToe(True) #game instance, True means training
player1= Qlearning() #player1 learning agent
player2 =Qlearning() #player2 learning agent
game.startTraining(player1,player2)
game.train(ites[2],ites)
game.saveStates() 
from Game import TicTacToe, Humanplayer, Randomplayer
from QLearning import Qlearning

game = TicTacToe()  #init game
player1 = Humanplayer()  #human
player2 = Qlearning()  #agent

game.startGame(player1, player2)  #player1: X, player2: 0

game.reset()  #reset
game.render()  # render display
from Game import TicTacToe
from QLearning import Qlearning

game = TicTacToe(True)  #init game, true implies training
agent1 = Qlearning()  #learning agent 1
agent2 = Qlearning()  #learning agent 2

game.startTraining(agent1, agent2)  #train agents
game.train(250000)  #250,000 iterations

game.saveStates()  #save Qtable
Example #7
0
File: TTTS.py Project: suen/ttts
class Main:
    def __init__(self):
        config = ConfigParser.RawConfigParser()
        config.read("config.ini")
           
        self.username = config.get('user', 'username');
        self.broadcastAddress = config.get('network', 'broadcastAddress')
        self.UDPPort = int(config.get('network', 'UDPPort'))
        self.TCPPort = int(config.get('network', 'TCPPort'))
        self.WebSocketPort = int(config.get('network', 'WebSocketPort'))

        try:
            self.privateKeyPath = config.get('user', 'privateKey')
            if os.path.isfile(self.privateKeyPath):
                self.rsaKey = RSA.importKey((open(self.privateKeyPath, "r")).read())
                if self.rsaKey is not None :
                    print "RSA Key loaded successfully"
            else:
                self.rsaKey = None
                print "no private found, generate one for distributed score system"
        except ConfigParser.NoOptionError:
            pass
        self.config = config
        
        self.breceiver = BroadcastListener(self)
        
        self.net = Network.Instance()
        self.net.setBroadcastAddress(self.broadcastAddress)
        self.net.setUDPPort(self.UDPPort)
        self.net.setTCPPort(self.TCPPort)
        self.net.setWebSocketPort(self.WebSocketPort)
        self.net.setMain(self)

        self.webClient = None;
        self.startGame = False
        self.board_sent_to_player = False
        self.user = AsyncUser(self)

        self.broadcastReceived = []
        self.lastBroadcastedMsg = ""
            
        signal.signal(signal.SIGINT, self.signal_handler)

        self.die = False
        #self.gameLoopThread = Thread(target=self.run)
        #self.gameLoopThread.start()
        
        Thread(target=self.startWebclient).start()
        
        self.startNetwork()

    def new_game_init(self):
        print "New Game initializing"
        self.game = TicTacToe()
        #self.player1 = AI("artificial agent", "X", self)
        self.player2 = AI("AI agent", "O", self)
        self.game.setPlayer1(self.player1)
        self.game.setPlayer2(self.player2)
        self.game.setFirstMover(self.player1)
        self.startGame = True
        self.playerHasMoved = False
        self.playerMove = None
        print "New Game started"

    def startNetwork(self):
        self.net.startNetwork()
        self.die = True
    
    def startWebclient(self):
        os.system("/usr/bin/google-chrome ../web/index.html")
        return
    
    def connectPeer(self, address, port):
        self.net.connectPeer(address, port);

    def setWebClient(self, client):
        print "new webclient"
        self.user = AsyncUser(self)
        self.user.setWebClient(client);
#        self.player1 = AsyncPlayer("Web Player", "X", self)
#        self.player1.setWebSocket(self.webClient)

    def getPeerList(self):
        return self.net.getPeers()
  
    def getPeerById(self, peerId):
        return self.net.getPeerById(peerId)
    
    def getPeerByIP(self, peerIp):
        return self.net.getPeerByIP(peerIp)
    
    def onPeerListChange(self):
        print "calling self.user for peer change"
        self.user.onPeerListChange()
    
    def sendMessage(self, peerId, message):
        for (id, p, ip) in self.getPeerList():
            if id == peerId:
                p.sendLine(message)
    
    
    def onPeerMsgReceived(self, peerIdentity, msg):
        self.user.onPeerMsgReceived(peerIdentity, msg)
    
    def onBroadcastReceived(self, msgTuple):
        datagram,peerIdentity = msgTuple;
        
        broadcastingPeer = self.net.getPeerByIP(peerIdentity[0])
        
        if broadcastingPeer is None:
            self.connectPeer(peerIdentity[0], peerIdentity[1]);
            return
        
        if msgTuple not in self.broadcastReceived:
            #self.broadcastReceived.append(msgTuple)
            self.user.onNewBroadcastReceived((broadcastingPeer.peerName, datagram))              
            
    def setOpponentPlayer(self, opponent):
        self.player2 = opponent
        
    def playerNextMove(self, player, move):
        self.playerMove = move
        self.playerHasMoved = True
        return;
    
    def createNewRoom(self, roomName):
        self.gameRoom = GameRoom(roomName)
        print "Creating a new room " + self.gameRoom.getName()


    def reannounceRoom(self):
        print "Multicasting created room " + self.gameRoom.getName()
        self.net.sendMulticast("NEW_ROOM " + self.gameRoom.getName())
    
    def getGameRoom(self):
        return self.gameRoom

    def sendMulticast(self, msg):
        self.net.sendMulticast(msg)
        
    def broadcast(self):
        self.net.broadcast();

    def getUsername(self):
        return self.username
 
    def generateKey(self):
        self.rsaKey = RSA.generate(2048, os.urandom);
    
        fw = open("private.key", "w")
        fw.write(self.rsaKey.exportKey());
        fw.close()
    
        self.config.set("user", "privateKey", "private.key")
        with open("config.ini", "w") as configfile:
            self.config.write(configfile)
        print "RSA Key generated and written to key and included to config.ini"

    def getPublicKey(self):
        return self.rsaKey.publickey()

    def getPublicKeyString(self):
        return self.rsaKey.publickey().exportKey()
 
    def run(self):

        while (self.net is None or 
                self.webClient is None or 
                not self.startGame ):
            print "No game, GameLoopThread sleeping"
            if self.die:
                return
            time.sleep(10)
        
        #self.net.sendDataWebPeers("GAME BOARDSTATE " + str(self.game.board.board))    
        
        if self.game.gameover():
            winner = self.game.getWinner();
            if winner == "":
                #self.net.sendDataWebPeers("GAME GAME_OVER NO_WINNER")
                win_code1 = 0;
                win_code2 = 0;
            else:
                win_code1 = 1 if winner == self.player1.symbol else 2
                win_code2 = 1 if winner == self.player2.symbol else 2

            self.player1.gameover(self.game.board.board, win_code1)
            self.player2.gameover(self.game.board.board, win_code2)

            self.startGame = False
        
        if not self.playerHasMoved:
            if not self.board_sent_to_player: 
                self.game.next_player.makeMove(self.game.board.makeCopy())
                print "Asking for player input"
                self.board_sent_to_player = True
        else:
            coord = self.playerMove
            self.game.makeMove(self.game.next_player, coord)
            self.playerHasMoved = False
            self.playerMove = None
            self.board_sent_to_player = False
        
        time.sleep(1)
        self.run()

    def signal_handler(self, signal, frame):
        print 'KeyBoard Interrupt',signal
        print 'Graceful Exit'
        self.die = True
        reactor.callFromThread(reactor.stop)
Example #8
0
import sys
from PyQt5.QtWidgets import QApplication
from Game import TicTacToe

if __name__ == '__main__':
    app = QApplication(sys.argv)
    tictactoe = TicTacToe()
    sys.exit(app.exec_())
Example #9
0
from Game import TicTacToe
from QLearning import  Qlearning

game = TicTacToe(True) #game instance, True means training
player1= Qlearning() #player1 learning agent
player2 =Qlearning() #player2 learning agent
game.startTraining(player1,player2) #start training
game.train(200000) #train for 200,000 iterations
game.saveStates()  #save Qtable