Tic-tac-toe là một trò chơi rất phổ biến, vì vậy hãy triển khai trò chơi Tic-tac-toe tự động bằng Python. Trò chơi được chương trình tự động chơi và do đó, không cần người dùng nhập liệu. Tuy nhiên, phát triển một trò chơi tự động sẽ rất thú vị. Hãy xem làm thế nào để làm điều này. NumPy và các thư viện Python ngẫu nhiên được sử dụng để xây dựng trò chơi này. Thay vì yêu cầu người dùng đánh dấu lên bảng, mã chọn ngẫu nhiên một vị trí trên bảng và đánh dấu. Nó sẽ hiển thị bảng sau mỗi lượt trừ khi người chơi thắng. Nếu trò chơi được hòa, thì nó trả về -1.
Giải trình. play_game[] là chức năng chính, thực hiện các tác vụ sau
- Gọi create_board[] để tạo một bảng 9×9 và khởi tạo bằng 0
- Đối với mỗi người chơi [1 hoặc 2], hãy gọi hàm random_place[] để chọn ngẫu nhiên một vị trí trên tàu và đánh dấu vị trí đó bằng số của người chơi, cách khác
- In bảng sau mỗi lần di chuyển
- Đánh giá bàn cờ sau mỗi nước đi để kiểm tra xem một hàng, cột hoặc đường chéo có cùng số người chơi hay không. Nếu vậy, hiển thị tên của người chiến thắng. Nếu sau 9 nước đi mà không có quân thắng thì hiển thị -1
Dưới đây là mã cho trò chơi trên.
Python3
# Tic-Tac-Toe Program using
# random number in Python
# importing all necessary libraries
import
numpy as np
import
random
from
time
import
# Tic-Tac-Toe Program using
1
# Tic-Tac-Toe Program using
2
# Tic-Tac-Toe Program using
3 # Tic-Tac-Toe Program using
4
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6# Tic-Tac-Toe Program using
7# Tic-Tac-Toe Program using
8# Tic-Tac-Toe Program using
9# Tic-Tac-Toe Program using
8# Tic-Tac-Toe Program using
9# Tic-Tac-Toe Program using
8# random number in Python
3
# random number in Python
4# random number in Python
5# Tic-Tac-Toe Program using
8# Tic-Tac-Toe Program using
9# Tic-Tac-Toe Program using
8# Tic-Tac-Toe Program using
9# Tic-Tac-Toe Program using
8# random number in Python
3
# random number in Python
4# random number in Python
5# Tic-Tac-Toe Program using
8# Tic-Tac-Toe Program using
9# Tic-Tac-Toe Program using
8# Tic-Tac-Toe Program using
9# Tic-Tac-Toe Program using
8# importing all necessary libraries
9
import
0
# Tic-Tac-Toe Program using
3 import
2
# Tic-Tac-Toe Program using
5import
4import
5 import
6
# Tic-Tac-Toe Program using
5import
8 import
9numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
numpy as np
5import
8 numpy as np
7numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
import
3import
4 import
5import
5import
5 # Tic-Tac-Toe Program using
8import
9
random
0random
1
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6random
4
random
5
# Tic-Tac-Toe Program using
3 random
7
# Tic-Tac-Toe Program using
5random
9import
5 from
1
# Tic-Tac-Toe Program using
5from
3import
5 from
5
# Tic-Tac-Toe Program using
5from
7import
5 from
9
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6time
2
time
3
time
4
# Tic-Tac-Toe Program using
3 time
6
# Tic-Tac-Toe Program using
5import
8 time
9numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
numpy as np
5# Tic-Tac-Toe Program using
06import
5 # Tic-Tac-Toe Program using
08
numpy as np
5import
8 # Tic-Tac-Toe Program using
11numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
import
3import
4 # Tic-Tac-Toe Program using
19import
5 # Tic-Tac-Toe Program using
21
random
0# Tic-Tac-Toe Program using
06import
5 # Tic-Tac-Toe Program using
25
random
0# Tic-Tac-Toe Program using
27
numpy as np
5import
4 # Tic-Tac-Toe Program using
06import
5import
5 # Tic-Tac-Toe Program using
08import
9
import
3____06____037
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6# Tic-Tac-Toe Program using
37
time
3
# Tic-Tac-Toe Program using
42
# Tic-Tac-Toe Program using
3 # Tic-Tac-Toe Program using
44
# Tic-Tac-Toe Program using
5import
8 time
9numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
numpy as np
5# Tic-Tac-Toe Program using
06import
5 # Tic-Tac-Toe Program using
08
numpy as np
5import
8 # Tic-Tac-Toe Program using
11numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
import
3____34 # Tic-Tac-Toe Program using
67____35 # Tic-Tac-Toe Program using
21
random
0# Tic-Tac-Toe Program using
06import
5 # Tic-Tac-Toe Program using
25
random
0# Tic-Tac-Toe Program using
27
numpy as np
5import
4 # Tic-Tac-Toe Program using
06import
5import
5 # Tic-Tac-Toe Program using
08import
9
import
3____06____037
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6# Tic-Tac-Toe Program using
37
time
3
# Tic-Tac-Toe Program using
90
# Tic-Tac-Toe Program using
3 # Tic-Tac-Toe Program using
92
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
06import
5 # Tic-Tac-Toe Program using
08
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
11____35 # Tic-Tac-Toe Program using
8
# Tic-Tac-Toe Program using
5import
8 time
9numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
numpy as np
5import
4 # random number in Python
11import
5 # Tic-Tac-Toe Program using
21
import
3____006____35 # Tic-Tac-Toe Program using
25
# Tic-Tac-Toe Program using
5import
4 # random number in Python
20
numpy as np
5# Tic-Tac-Toe Program using
6 # Tic-Tac-Toe Program using
06
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
06import
5 # Tic-Tac-Toe Program using
08
# Tic-Tac-Toe Program using
5import
4 # random number in Python
20
numpy as np
5import
8 time
9numpy as np
0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
import
3______011import
5 numpy as np
3____82# random number in Python
44 # random number in Python
45 # random number in Python
44 time
9
import
3import
4 # Tic-Tac-Toe Program using
19import
5 # Tic-Tac-Toe Program using
21
random
0# Tic-Tac-Toe Program using
06import
5 # Tic-Tac-Toe Program using
25
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6 # Tic-Tac-Toe Program using
06
# random number in Python
60
# random number in Python
61
# Tic-Tac-Toe Program using
3 # random number in Python
63
# Tic-Tac-Toe Program using
5# random number in Python
65import
5 # Tic-Tac-Toe Program using
8
# Tic-Tac-Toe Program using
5import
8 from
9numpy as np
0 # random number in Python
5# random number in Python
45# Tic-Tac-Toe Program using
9# random number in Python
75# random number in Python
76
numpy as np
5import
4 # random number in Python
79# random number in Python
80
random
0# random number in Python
82____180
random
0# random number in Python
85
import
3______165import
5 from
9
# Tic-Tac-Toe Program using
5import
4 # random number in Python
92# random number in Python
93# random number in Python
94import
5 # Tic-Tac-Toe Program using
8# random number in Python
97# random number in Python
98 # random number in Python
65import
5import
5 # Tic-Tac-Toe Program using
8import
9
numpy as np
5# random number in Python
65import
5 # random number in Python
44# random number in Python
45
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6 # random number in Python
65
# importing all necessary libraries
12
# Tic-Tac-Toe Program using
3 # importing all necessary libraries
14
# Tic-Tac-Toe Program using
5# importing all necessary libraries
16import
5 # importing all necessary libraries
18# Tic-Tac-Toe Program using
8# Tic-Tac-Toe Program using
9# random number in Python
45
# Tic-Tac-Toe Program using
5# importing all necessary libraries
23time
2
# Tic-Tac-Toe Program using
5# importing all necessary libraries
26# random number in Python
75# random number in Python
97
# Tic-Tac-Toe Program using
5# importing all necessary libraries
30 # random number in Python
65import
5import
5 # Tic-Tac-Toe Program using
8import
9
numpy as np
5import
8 from
9numpy as np
0 # random number in Python
5# random number in Python
45# Tic-Tac-Toe Program using
9# random number in Python
75# random number in Python
76
import
3____246____35 # importing all necessary libraries
48
import
3______223____42# importing all necessary libraries
52 # importing all necessary libraries
53 # importing all necessary libraries
54# importing all necessary libraries
55# importing all necessary libraries
53 # importing all necessary libraries
57# random number in Python
97