Example #1
0
 def test_finished(self):
     board = Board()
     b = board.board
     b[0][1] = Sign.x
     b[1][1] = Sign.x
     b[2][1] = Sign.x
     board.board = b
     self.assertTrue(board.finished)
Example #2
0
File: tests.py Project: jchmura/PiE
 def test_won_column(self):
     board = Board()
     b = board.board
     b[0][0] = Sign.o
     b[0][1] = Sign.o
     b[0][2] = Sign.o
     board.board = b
     self.assertTrue(board.won(Sign.o))
Example #3
0
 def test_won_x(self):
     board = Board()
     b = board.board
     b[0][1] = Sign.x
     b[1][1] = Sign.x
     b[2][1] = Sign.x
     board.board = b
     self.assertTrue(board.won(Sign.x))
Example #4
0
File: tests.py Project: jchmura/PiE
 def test_won_x(self):
     board = Board()
     b = board.board
     b[0][1] = Sign.x
     b[1][1] = Sign.x
     b[2][1] = Sign.x
     board.board = b
     self.assertTrue(board.won(Sign.x))
Example #5
0
File: tests.py Project: jchmura/PiE
 def test_finished(self):
     board = Board()
     b = board.board
     b[0][1] = Sign.x
     b[1][1] = Sign.x
     b[2][1] = Sign.x
     board.board = b
     self.assertTrue(board.finished)
Example #6
0
 def test_won_column(self):
     board = Board()
     b = board.board
     b[0][0] = Sign.o
     b[0][1] = Sign.o
     b[0][2] = Sign.o
     board.board = b
     self.assertTrue(board.won(Sign.o))
Example #7
0
    def test_not_finished(self):
        board = Board()
        self.assertFalse(board.finished)

        b = board.board
        b[0][1] = Sign.x
        b[1][1] = Sign.o
        b[2][0] = Sign.o
        board.board = b
        self.assertFalse(board.finished)
Example #8
0
File: tests.py Project: jchmura/PiE
    def test_not_finished(self):
        board = Board()
        self.assertFalse(board.finished)

        b = board.board
        b[0][1] = Sign.x
        b[1][1] = Sign.o
        b[2][0] = Sign.o
        board.board = b
        self.assertFalse(board.finished)
Example #9
0
 def __init__(self):
     self._config = Configuration()
     self._sock = None
     self._player1 = None
     self._player2 = None
     self._board = Board()
     self._new_board = False
     self.round_sign = Sign.x
Example #10
0
    def test_initial_board(self):
        board = Board()
        b = board.board

        self.assertEqual(3, len(b))
        for row in b:
            self.assertEqual(3, len(row))
            for item in row:
                self.assertEqual(Sign.empty, item)
Example #11
0
 def test_tie(self):
     board = Board()
     x, o = Sign.x, Sign.o
     board.board = [[x, x, o], [o, o, x], [x, o, o]]
     self.assertTrue(board.tie())
Example #12
0
File: tests.py Project: jchmura/PiE
 def test_tie(self):
     board = Board()
     x, o = Sign.x, Sign.o
     board.board = [[x, x, o], [o, o, x], [x, o, o]]
     self.assertTrue(board.tie())
Example #13
0
 def test_assign_wrong_sign(self):
     board = Board()
     x, o = Sign.x, Sign.o
     self.assertRaises(ValueError, setattr, board, "board",
                       [[x, x, o], [x, o, o], [o, o, 3]])
Example #14
0
 def test_put_already_filled(self):
     board = Board()
     x, y = 1, 1
     board.put(x, y, Sign.x)
     self.assertRaises(IndexError, board.put, x, y, Sign.x)
Example #15
0
 def test_assign_wrong_column_length(self):
     board = Board()
     x, o = Sign.x, Sign.o
     self.assertRaises(ValueError, setattr, board, "board", [[x, o]])
Example #16
0
 def test_put_not_sign(self):
     board = Board()
     self.assertRaises(ValueError, board.put, 1, 1, 'x')
Example #17
0
 def test_put_outside(self):
     board = Board()
     self.assertRaises(IndexError, board.put, 3, 1, Sign.x)
Example #18
0
 def test_put(self):
     board = Board()
     x, y = 2, 1
     board.put(x, y, Sign.x)
     self.assertEqual(Sign.x, board.board[x][y])
Example #19
0
 def __init__(self):
     self._config = Configuration()
     self._sock = None
     self._board = Board()
     self._display = None
     self._logger = logging.getLogger(__name__)
Example #20
0
class Client:
    def __init__(self):
        self._config = Configuration()
        self._sock = None
        self._board = Board()
        self._display = None
        self._logger = logging.getLogger(__name__)

    def connect(self):
        self._sock = SocketWrapper(socket.socket())
        self._sock.socket.connect(
            (self._config.server_ip, self._config.server_port))

    def run(self):
        sign = self.receive().payload()
        self._display = Display(sign, self._board, self)
        self._display.display_empty_grid()
        self._display.display_title('You are {}'.format(sign))
        self._logger = logging.getLogger(__name__ + str(sign))
        self._logger.debug('Connected to server')

        ready = self.receive().payload()
        if not ready:
            self._display.display_message('Waiting for opponent...')
            self._logger.debug('Waiting for opponent')

        ready = self.receive().payload()
        if not ready:
            self._logger.warn(
                'Now the server should be ready, something bad happened')
            return
        self._display.clear_message()
        self._logger.info('Everyone is connected, the game can start')

        while True:
            message = self.receive()
            if isinstance(message, BoardMessage):
                board = message.payload()
                self._logger.debug('Received new board')
                self._board.board = board
                self._display.fill_grid()
                self._logger.debug('New board displayed on the screen')
            elif isinstance(message, RoundMessage):
                round = message.payload()
                if round == sign:
                    self._logger.debug('It is my round')
                    self._display.display_message('This is your round')
                    self._display.input()
                    self._logger.info(
                        'Move is made, sending new board to the server\n{}'.
                        format(self._board))
                    self.send(BoardMessage(self._board.board))
                else:
                    self._logger.debug("It is my opponent's round")
                    self._display.display_message(
                        "This is your opponent's round")
            elif isinstance(message, EndMessage):
                self._logger.info('The game has ended')
                tie = message.payload()
                if tie:
                    self._display.display_message("Is's a tie")
                    self._logger.debug("It's a tie")
                else:
                    winner = self._board.won(sign)
                    if winner:
                        self._display.display_message('You won!')
                    else:
                        self._display.display_message('You lost')
                    self._logger.debug('Have I won? {}'.format(winner))
                self._display.quit()
                break
            else:
                self._logger.warn('Unknown message: {}'.format(message))

    def receive(self):
        return self._sock.receive()

    def send(self, message):
        self._sock.send(message)

    def disconnect(self):
        if self._sock is not None:
            self._sock.socket.close()
            self._sock = None
            self._logger.info('Socket closed')
        else:
            self._logger.debug('Socket is not open, no need to close it')

        if self._display is not None:
            self._display.exit()
Example #21
0
File: tests.py Project: jchmura/PiE
 def test_put(self):
     board = Board()
     x, y = 2, 1
     board.put(x, y, Sign.x)
     self.assertEqual(Sign.x, board.board[x][y])
Example #22
0
File: tests.py Project: jchmura/PiE
 def test_put_already_filled(self):
     board = Board()
     x, y = 1, 1
     board.put(x, y, Sign.x)
     self.assertRaises(IndexError, board.put, x, y, Sign.x)
Example #23
0
 def __init__(self):
     self._config = Configuration()
     self._sock = None
     self._board = Board()
     self._display = None
     self._logger = logging.getLogger(__name__)
Example #24
0
class Client:
    def __init__(self):
        self._config = Configuration()
        self._sock = None
        self._board = Board()
        self._display = None
        self._logger = logging.getLogger(__name__)

    def connect(self):
        self._sock = SocketWrapper(socket.socket())
        self._sock.socket.connect((self._config.server_ip, self._config.server_port))

    def run(self):
        sign = self.receive().payload()
        self._display = Display(sign, self._board, self)
        self._display.display_empty_grid()
        self._display.display_title('You are {}'.format(sign))
        self._logger = logging.getLogger(__name__ + str(sign))
        self._logger.debug('Connected to server')

        ready = self.receive().payload()
        if not ready:
            self._display.display_message('Waiting for opponent...')
            self._logger.debug('Waiting for opponent')

        ready = self.receive().payload()
        if not ready:
            self._logger.warn('Now the server should be ready, something bad happened')
            return
        self._display.clear_message()
        self._logger.info('Everyone is connected, the game can start')

        while True:
            message = self.receive()
            if isinstance(message, BoardMessage):
                board = message.payload()
                self._logger.debug('Received new board')
                self._board.board = board
                self._display.fill_grid()
                self._logger.debug('New board displayed on the screen')
            elif isinstance(message, RoundMessage):
                round = message.payload()
                if round == sign:
                    self._logger.debug('It is my round')
                    self._display.display_message('This is your round')
                    self._display.input()
                    self._logger.info('Move is made, sending new board to the server\n{}'.format(self._board))
                    self.send(BoardMessage(self._board.board))
                else:
                    self._logger.debug("It is my opponent's round")
                    self._display.display_message("This is your opponent's round")
            elif isinstance(message, EndMessage):
                self._logger.info('The game has ended')
                tie = message.payload()
                if tie:
                    self._display.display_message("Is's a tie")
                    self._logger.debug("It's a tie")
                else:
                    winner = self._board.won(sign)
                    if winner:
                        self._display.display_message('You won!')
                    else:
                        self._display.display_message('You lost')
                    self._logger.debug('Have I won? {}'.format(winner))
                self._display.quit()
                break
            else:
                self._logger.warn('Unknown message: {}'.format(message))

    def receive(self):
        return self._sock.receive()

    def send(self, message):
        self._sock.send(message)

    def disconnect(self):
        if self._sock is not None:
            self._sock.socket.close()
            self._sock = None
            self._logger.info('Socket closed')
        else:
            self._logger.debug('Socket is not open, no need to close it')

        if self._display is not None:
            self._display.exit()