我的是简单游戏的井字游戏

用 Python 做个简单的井字游戏 - 文章 - 伯乐在线
& 用 Python 做个简单的井字游戏
【本文来自译者
的投稿。如果其他朋友也有不错的原创或译文,可以尝试。】
在这个教程中,我将展示如何利用Python来做一个井字游戏。这将包括函数、列表、if语句、while循环、for循环以及错误处理等等。
首先,我们将创建两个函数,第一个函数将会打印出井字游戏的背景模板:
def print_board():
for i in range(0,3):
for j in range(0,3):
print map[2-i][j],
if j != 2:
print &|&,
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],使用了逗号,以保证他们在同一行被打印出来。
现在,这个函数可以打印一个游戏的背景啦,它看起来是这个样子滴:
&&|&& |&& &&|&& |&& &&|&& |
X | X |&& O | X | O &&| O | X
X | X | X X | X | X X | X | X
接下来,我们创建一个check_done()函数,它会在每轮结束之后检查游戏是否结束了,如果游戏结束,那么返回True并打印一条消息。
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
1234567891011121314151617
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,现在我们有了两个函数,接下来开始我们真正的程序,首先来创建三个变量:
turn = &X&
map = [[& &,& &,& &],
[& &,& &,& &],
[& &,& &,& &]]
done = False
turn = "X"map = [[" "," "," "],&&&&&& [" "," "," "],&&&&&& [" "," "," "]]done = False
我已经告诉过你这三个变量是熟么意思了,假如你忘了的话,那么看看下面:
turn:该谁走了
map:游戏的背景地图
done:这个游戏到底有木有结束
接下来,这样写:
while done != True:
print_board()
print turn, &'s turn&
moved = False
while moved != True:
while done != True:&&&&print_board()&&&&&print turn, "'s turn&&&&&print&&&&&moved = False&&&&while moved != True:
里面有一个while循环,直到done为True为止,我们打印出该轮到谁走了。
然后创建一个名为moved的变量,检查玩家是不是移动了,如果没有移动,则进入下一个循环。
接下来,我们打印玩家该怎样去下:
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 "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
pos = input(&Select: &)
if pos &=9 and pos &=1:
try:&&&&pos = input("Select: ")&&&&if pos <=9 and pos >=1:
我们希望玩家输入一个数字,然后我们检查是不是在1~9之间,同时,我们还得增加一个错误处理,比如玩家输入”Hello”,程序不能就这么退出了。
现在,我们需要检查他走的这一步能不能走:
if X != 0:
Y = pos/3X = pos%3if X != 0:&&&&X -=1else:&&&&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上面编辑的时候比下面的漂亮多了!):
aha,这个位置和我们键入的是一样的!
print &7|8|9&
print &4|5|6&
print &1|2|3&
print "7|8|9"print "4|5|6"print "1|2|3"
现在我们完成大部分工作了,但是还有几行代码:
map[Y][X] = turn
moved = True
done = check_done()
if done == False:
if turn == &X&:
turn = &O&
turn = &X&
print &You need to add a numeric value&
123456789101112
map[Y][X] = turnmoved = Truedone = 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的话,下面是全部的代码,希望你学到了点什么,( ^_^ )/~~拜拜。
def print_board():
for i in range(0,3):
for j in range(0,3):
print map[2-i][j],
if j != 2:
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&
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&
pos = input(&Select: &)
if pos &=9 and pos &=1:
if X != 0:
if map[Y][X] == & &:
map[Y][X] = turn
moved = True
done = check_done()
if done == False:
if turn == &X&:
turn = &O&
turn = &X&
print &You need to add a numeric value&
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
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"
可能感兴趣的话题
wow,在这里能让更多的朋友看到,那是极好的。
关于伯乐在线博客
在这个信息爆炸的时代,人们已然被大量、快速并且简短的信息所包围。然而,我们相信:过多“快餐”式的阅读只会令人“虚胖”,缺乏实质的内涵。伯乐在线内容团队正试图以我们微薄的力量,把优秀的原创文章和译文分享给读者,为“快餐”添加一些“营养”元素。
新浪微博:
推荐微信号
(加好友请注明来意)
– 好的话题、有启发的回复、值得信赖的圈子
– 分享和发现有价值的内容与观点
– 为IT单身男女服务的征婚传播平台
– 优秀的工具资源导航
– 翻译传播优秀的外文文章
– 国内外的精选文章
– UI,网页,交互和用户体验
– 专注iOS技术分享
– 专注Android技术分享
– JavaScript, HTML5, CSS
– 专注Java技术分享
– 专注Python技术分享
& 2018 伯乐在线我的井字游戏
我的井字游戏
综合评分:
我的宠物井字是一个简单的象棋游戏,但是很有趣,有漂亮的卡通!如果你喜欢玩井字与朋友和家人,你一定会喜欢这个程序!
其他帐号登录:
为此应用打分:
请填写评论内容
(通过iTunes下载)(下载到电脑)
应用相关文章
大家都在玩
热门苹果应用
天天撸一撸
水果连连看
奇怪的追逐者
我的世界(Minecr
孤胆车神:维加斯
植物大战僵尸2(国
都市建筑师(Mega
最近浏览过的应用
我的井字游戏我的井字小游戏 1.0.0.0
投诉建议:
软件大小: 324KB
商: tw.dotblogs
软件语言: 简体中文
软件授权: 免费
软件评级:
更新时间:
我的井字小游戏1.0.0.0应用截图
这是一个有趣的井字游戏,本游戏突破传统的OX 概念,使用其他图样,并以「三点一连线」的想法来完成生活有趣的井字游戏,换句话说,只要3个图样一样且连续成一直线,即代表赢得本次(局)游戏。是个老少皆宜的好玩游戏。 [点评]:这个比较考验智力,小小井字考智商哦~
下载该软件的用户还下载了
同类软件下载排行
WP热门应用推荐
热门关键词}

我要回帖

更多关于 井字游戏 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信