def run(self): # Create and send update(-1,-1) to first player to start game. updatepdu = UpdatePDU() updatepdu.setX(-1) updatepdu.setY(-1) updatepdu.setSendersays("Game started! Make first move!") self.fgpsession.send(updatepdu, 0) # first player's host and port are in position 0 of sendersqueue. self.setState(WF_P1_MOVE()) while self.listening: self.fgpsession.listen()
def move(self, ctx): if ctx.isMoveLegal(ctx.clientMsg.getX(), ctx.clientMsg.getY()): logging.debug("P1 legal move at " + str(ctx.clientMsg.getX()) + ", " + str(ctx.clientMsg.getY())) ctx.getBoard().addPiece(Piece("white"), ctx.clientMsg.getX(), ctx.clientMsg.getY()) # Count score score = ctx.countScore() minus = score.find("-") ws = score[0:minus] bs = score[minus+1:] if ctx.getBoard().isBoardFull(): gameoverpdu = GameOverPDU() gameoverpdu.setX(ctx.clientMsg.getX()) gameoverpdu.setY(ctx.clientMsg.getY()) gameoverpdu.setWhiteScore(int(ws)) gameoverpdu.setBlackScore(int(bs)) gameoverpdu.setSendersays("Game Over!") #gameoverpdu.setSendersays("Score: " + score + ", Game Over!") ctx.getFGPSession().send(gameoverpdu, 0) # Broadcast ctx.getFGPSession().send(gameoverpdu, 1) # Kill Session ctx.stopListening() else: updatepdu = UpdatePDU() updatepdu.setX(ctx.clientMsg.getX()) updatepdu.setY(ctx.clientMsg.getY()) updatepdu.setWhiteScore(int(ws)) updatepdu.setBlackScore(int(bs)) updatepdu.setSendersays("It is black's turn!") #updatepdu.setSendersays("Score: " + score + ", Opponent played at (" + str(ctx.clientMsg.getX()) + ", " + str(ctx.clientMsg.getY()) + ")." + " Make a move!") ctx.getFGPSession().send(updatepdu, 0) ctx.getFGPSession().send(updatepdu, 1) ctx.setState(WF_P2_MOVE()) else: logging.debug("P1 illegal move") # Count score score = ctx.countScore() minus = score.find("-") ws = score[0:minus] bs = score[minus+1:] updatepdu = UpdatePDU() updatepdu.setX(-1) updatepdu.setY(-1) updatepdu.setWhiteScore(int(ws)) updatepdu.setBlackScore(int(bs)) updatepdu.setSendersays("Dude, that was illegal! Make a new move!") #updatepdu.setSendersays("Score: " + score + ", Dude, that was illegal! Make a new move!") ctx.getFGPSession().send(updatepdu, 0) # Send it to player who made the illegal move. ctx.setState(WF_P1_MOVE())