用 Python 做个简单的井字游戏

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 21:55   57   0

在这个教程中,我将展示如何利用Python来做一个井字游戏。这将包括函数、列表、if语句、while循环、for循环以及错误处理等等。

  首先,我们将创建两个函数,第一个函数将会打印出井字游戏的背景模板:

1
2
3
4
5
6
7
def print_board():
for i in range(0,3):
for j in range(0,3):
print map[2-i][j],
if j != 2:
print "|",
print ""

  在这里,我们使用了两个for循环,要遍历一个名为map的列表变量。这个变量是一个二维列表,将保存每个位置的信息。

  由于我会按照小键盘的数字来进行对照位置(稍后你会看到),所以第一个值我们把它设为(2-i),然后我们想用"|"来进行分割我们的位置,所以在每个位置打印完之后,我们给他打印一个"|",我们在这里print map[2-i][j],使用了逗号,以保证他们在同一行被打印出来。

  现在,这个函数可以打印一个游戏的背景啦,它看起来是这个样子滴:

1
2
3
| |
| |
| |
1
2
3
X | X |
O | X | O
| O | X
1
2
3
X | X | X
X | X | X
X | X | X

  接下来,我们创建一个check_done()函数,它会在每轮结束之后检查游戏是否结束了,如果游戏结束,那么返回True并打印一条消息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def check_done():
for i in range(0,3):
if map[i][0] == map[i][1] == map[i][2] != " " \
or map[0][i] == map[1][i] == map[2][i] != " ":
print turn, "won!!!"
return True
if map[0][0] == map[1][1] == map[2][2] != " " \
or map[0][2] == map[1][1] == map[2][0] != " ":
print turn, "won!!!"
return True
if " " not in map[0] and " " not in map[1] and " " not in map[2]:
print "Draw"
return True
return False

  首先,我们会检查水平和垂直方向,是不是有三格是相同、并且不为空(所以他不会认为连续三个空行是符合条件的),其次,我们以相同的方式来检查对角线。

  这8行如果有一行符合条件,那么游戏结束并且打印出“Won!!!”并返回True,同时注意turn这个变量,它的作用是判断现在下棋的是那一方,最终展现出来的消息将会是“X赢了!!”或“O赢了!!”。

  接下来这个函数会判断假如没有一个位置是空的,那么就意味着没有人能够赢得比赛(前面判断过了),那么就打印出平局,并且返回True

  如果没有上述两种情况,那么游戏还没结束,返回False

  OK,现在我们有了两个函数,接下来开始我们真正的程序,首先来创建三个变量:

1
2
3
4
5
turn = "X"
map = [[" "," "," "],
[" "," "," "],
[" "," "," "]]
done = False

  我已经告诉过你这三个变量是熟么意思了,假如你忘了的话,那么看看下面:

  • turn:该谁走了
  • map:游戏的背景地图
  • done:这个游戏到底有木有结束

  接下来,这样写:

1
2
3
4
5
6
7
8
while done != True:
print_board()
print turn, "'s turn"
print
moved = False
while moved != True:

  里面有一个while循环,直到done为True为止,我们打印出该轮到谁走了。

  然后创建一个名为moved的变量,检查玩家是不是移动了,如果没有移动,则进入下一个循环。

  接下来,我们打印玩家该怎样去下:

1
2
3
4
5
print "Please select position by typing in a number between 1 and 9, see below for which number that is which position..."
print "7|8|9"
print "4|5|6"
print "1|2|3"
print

  接下来:

1
2
3
try:
pos = input("Select: ")
if pos <=9 and pos >=1:

  我们希望玩家输入一个数字,然后我们检查是不是在1~9之间,同时,我们还得增加一个错误处理,比如玩家输入”Hello”,程序不能就这么退出了。

  现在,我们需要检查他走的这一步能不能走:

1
2
3
4
5
6
7
Y = pos/3
X = pos%3
if X != 0:
X -=1
else:
X = 2
Y -=1

  hah,睁大眼睛啦,首先我们得到一个X和Y的值,然后使用他们来检查,他要下的那个位置是不是空的,接下来我会向你解释X和Y他们是肿么工作的:

  • 位置1:Y = 1/3 = 0, X = 1%3 = 1; x -= 1 = 0
  • 位置2:Y = 2/3 = 0, X = 2%3 = 2; X -= 1 = 1
  • 位置3:Y = 3/3 = 1, X = 3%3 = 0; X = 2, Y -= 1 = 0
  • ……

  下面的自己算啊,我直接上结论(靠,Hexo默认的模板不显示表格啊,我在mou上面编辑的时候比下面的漂亮多了!):

Y\X x=0 x=1 x=2
y=2 7 8 9
y=1 4 5 6
y=0 1 2 3

  aha,这个位置和我们键入的是一样的!

1
2
3
print "7|8|9"
print "4|5|6"
print "1|2|3"

  现在我们完成大部分工作了,但是还有几行代码:

1
2
3
4
5
6
7
8
9
10
11
12
map[Y][X] = turn
moved = True
done = check_done()
if done == False:
if turn == "X":
turn = "O"
else:
turn = "X"
except:
print "You need to add a numeric value"

  嗯,我们给moved变量复制为True,并检查是否结束了,木有结束的话变换角色换下一个人走。

  OK,差不多结束了,假如你只是想Ctrl+C 和 Ctrl+V的话,下面是全部的代码,希望你学到了点什么,( ^_^ )/~~拜拜。

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
def print_board():
for i in range(0,3):
for j in range(0,3):
print map[2-i][j],
if j != 2:
print "|",
print ""
def check_done():
for i in range(0,3):
if map[i][0] == map[i][1] == map[i][2] != " " \
or map[0][i] == map[1][i] == map[2][i] != " ":
print turn, "won!!!"
return True
if map[0][0] == map[1][1] == map[2][2] != " " \
or map[0][2] == map[1][1] == map[2][0] != " ":
print turn, "won!!!"
return True
if " " not in map[0] and " " not in map[1] and " " not in map[2]:
print "Draw"
return True
return False
turn = "X"
map = [[" "," "," "],
[" "," "," "],
[" "," "," "]]
done = False
while done != True:
print_board()
print turn, "'s turn"
print
moved = False
while moved != True:
print "Please select position by typing in a number between 1 and 9, see below for which number that is which position..."
print "7|8|9"
print "4|5|6"
print "1|2|3"
print
try:
pos = input("Select: ")
if pos <=9 and pos >=1:
Y = pos/3
X = pos%3
if X != 0:
X -=1
else:
X = 2
Y -=1
if map[Y][X] == " ":
map[Y][X] = turn
moved = True
done = check_done()
if done == False:
if turn == "X":
turn = "O"
else:
turn = "X"
except:
print "You need to add a numeric value"

  原文出处: Vswe

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP