Python - TaTeTi

 
Vista:
sin imagen de perfil
Val: 7
Ha disminuido su posición en 16 puestos en Python (en relación al último mes)
Gráfica de Python

TaTeTi

Publicado por Python2.0 (1 intervención) el 04/11/2020 13:10:41
Buenas, estaba tratando de realizar un tateti a partir de unos tests, tengo estos dos archivos.
el test_tateti.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import unittest
from unittest.mock import patch
from tateti import TaTeTi
from parameterized import parameterized
 
 
class Test(unittest.TestCase):
    def test_tablero_inicial(self):
        game = TaTeTi()
        expected = '1.1|1.2|1.3\n---+---+---\n2.1|2.2|2.3\n'
        expected += '---+---+---\n3.1|3.2|3.3'
        self.assertEqual(game.__str__(), expected)
 
    def test_tablero_diccionario(self):
        game = TaTeTi()
        positions = ['1.1', '1.2', '1.3',
                     '2.1', '2.2', '2.3',
                     '3.1', '3.2', '3.3']
        board = {value: value for value in positions}
        self.assertEqual(game.board, board)
 
    def test_input_position_1(self):
        game = TaTeTi()
        valid = ['1.1', '1.2', '1.3',
                 '2.1', '2.3',
                 '3.1', '3.2', '3.3']
        with patch('builtins.input', side_effect=['4.4', '2.2']):
            game.input_position()
            self.assertEqual(game.valid, valid)
 
    def test_input_position_2(self):
        game = TaTeTi()
        with patch('builtins.input', side_effect=['6.1', '7.7', '1.1']):
            self.assertEqual(game.input_position(), '1.1')
 
    @parameterized.expand([({'1.1': ' x ', '1.2': '1.2', '1.3': ' o ',
                             '2.1': ' o ', '2.2': ' x ', '2.3': '2.3',
                             '3.1': '3.1', '3.2': '3.2', '3.3': ' x '},
                            True),
                           ({'1.1': ' x ', '1.2': '1.2', '1.3': '1.3',
                             '2.1': ' x ', '2.2': '2.2', '2.3': '2.3',
                             '3.1': ' x ', '3.2': '3.2', '3.3': '3.3'},
                            True),
                           ({'1.1': '1.1', '1.2': ' o ', '1.3': '1.3',
                             '2.1': '2.1', '2.2': ' o ', '2.3': '2.3',
                             '3.1': '3.1', '3.2': ' o ', '3.3': '3.3'},
                            True),
                           ({'1.1': '1.1', '1.2': '1.2', '1.3': ' x ',
                             '2.1': '2.1', '2.2': ' x ', '2.3': '2.3',
                             '3.1': ' x ', '3.2': '3.2', '3.3': '3.3'},
                            True),
                           ({'1.1': ' o ', '1.2': ' o ', '1.3': ' o ',
                             '2.1': '2.1', '2.2': '2.2', '2.3': '2.3',
                             '3.1': '3.1', '3.2': '3.2', '3.3': '3.3'},
                            True),
                           ({'1.1': '1.1', '1.2': '1.2', '1.3': '1.3',
                             '2.1': ' x ', '2.2': ' x ', '2.3': ' x ',
                             '3.1': '3.1', '3.2': '3.2', '3.3': '3.3'},
                            True),
                           ({'1.1': '1.1', '1.2': '1.2', '1.3': ' x ',
                             '2.1': '2.1', '2.2': ' o ', '2.3': '2.3',
                             '3.1': ' x ', '3.2': '3.2', '3.3': '3.3'},
                            False),
                           ({'1.1': ' x ', '1.2': ' x ', '1.3': ' o ',
                             '2.1': ' o ', '2.2': ' o ', '2.3': ' x ',
                             '3.1': ' x ', '3.2': ' x ', '3.3': ' o '},
                            False),
                           ({'1.1': ' x ', '1.2': '1.2', '1.3': ' o ',
                             '2.1': ' x ', '2.2': ' o ', '2.3': '2.3',
                             '3.1': '3.1', '3.2': '3.2', '3.3': ' x '},
                            False)])
    def test_win(self, board, result):
        game = TaTeTi()
        game.board = board
        self.assertEqual(game.win(), result)
 
    def test_game(self):
        game = TaTeTi()
        game.board = {'1.1': ' o ', '1.2': ' o ', '1.3': ' x ',
                      '2.1': ' x ', '2.2': ' x ', '2.3': ' o ',
                      '3.1': ' o ', '3.2': ' x ', '3.3': '3.3'}
        game.valid = ['3.3']
        with patch('builtins.input', side_effect=['1.1', '2.2', '3.3']):
            self.assertEqual(game.game(), 'Ninguno')
 
 
if __name__ == '__main__':
    unittest.main()


y el tateti.py



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class TaTeTi():
 
    def game(self):
        print(self)
        while not self.win() and len(self.valid) > 0:
            self.board[self.input_position()] = ' ' + self.piece + ' '
            print(self)
            winner = self.piece
            self.piece = 'o' if self.piece == 'x' else 'x'
        if len(self.valid) == 0:
            winner = 'Ninguno'
        return winner
 
 
if __name__ == '__main__':
    game = TaTeTi()
 
    print('Ganó ' + game.game())
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder