Được giới thiệu về App Game Kit và khám phá sản phẩm này là gì. Bấm vào đây để xem phần Giới thiệu
Nguyên tắc
Mục đích của phần trợ giúp này là hướng dẫn bạn cách viết chương trình của riêng mình bằng ngôn ngữ lập trình CƠ BẢN [còn được gọi là Cấp 1]. Bấm vào đây để xem phần Nguyên tắc
Ngôn ngữ
Đây là hướng dẫn tham khảo dành cho nhà phát triển sử dụng BASIC [Cấp 1]. Nhấn vào đây để xem phần Ngôn ngữ
hướng dẫn
Một số hướng dẫn có sẵn bao gồm những điều cơ bản của các lĩnh vực như họa tiết, âm thanh và đầu vào. Nhấn vào đây để xem Hướng dẫn
ví dụ
App Game Kit đi kèm với một loạt các chương trình ví dụ sẽ giúp bạn khám phá cách thực hiện những việc như hiển thị một nhân vật trên màn hình và chơi vật lý. Nhấn vào đây để xem các ví dụ
lệnh
Có hàng trăm lệnh được bao gồm trong App Game Kit. Khu vực tài liệu này chứa danh sách cho tất cả các lệnh này. Bấm vào đây để xem Lệnh
tiện ích
Tìm hiểu thêm các tiện ích kèm theo App Game Kit. Bấm vào đây để xem Tiện ích
Phân bổ
Thông tin quan trọng liên quan đến việc phân phối trò chơi được tạo trong App Game Kit. Nhấn vào đây để xem phần Phân phối
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 3×3 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_______0_______7_______0_______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_______1_______5# Tic-Tac-Toe Program using
8_______0_______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_______1_______5# Tic-Tac-Toe Program using
8_______0_______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
3_______3_______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
6_______6_______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
6_______8_______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
11_______4_______0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
import
3_______3_______4 # Tic-Tac-Toe Program using
19import
5 # Tic-Tac-Toe Program using
21
random
0_______0_______06____3_______5 # Tic-Tac-Toe Program using
25
random
0_______0_______27
numpy as np
5import
4 # Tic-Tac-Toe Program using
06_______3_______5_______3_______5 # Tic-Tac-Toe Program using
08import
9
import
3_______0_______6# Tic-Tac-Toe Program using
37
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6_______0_______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
11_______4_______0 numpy as np
1numpy as np
2numpy as np
3numpy as np
4
import
3_______3_______4 # Tic-Tac-Toe Program using
67import
5 # Tic-Tac-Toe Program using
21
random
0_______0_______06____3_______5 # Tic-Tac-Toe Program using
25
random
0_______0_______27
numpy as np
5import
4 # Tic-Tac-Toe Program using
06_______3_______5_______3_______5 # Tic-Tac-Toe Program using
08import
9
import
3_______0_______6# Tic-Tac-Toe Program using
37
# Tic-Tac-Toe Program using
5# Tic-Tac-Toe Program using
6_______0_______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_______3_______5 # 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_______0_______06import
5 # 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_______0_______11_______3_______5 numpy as np
3_______8_______2# random number in Python
44 # random number in Python
45 # random number in Python
44 time
9
import
3_______3_______4 # Tic-Tac-Toe Program using
19import
5 # Tic-Tac-Toe Program using
21
random
0_______0_______06____3_______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_______1_______82# random number in Python
80
random
0# random number in Python
85
import
3_______1_______65import
5 from
9
# Tic-Tac-Toe Program using
5import
4 # random number in Python
92_______1_______93_______1_______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_______0_______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
5_______3_______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_______2_______46____3_______5 # importing all necessary libraries
48
import
3_______2_______23numpy as np
2_______2_______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