游戏规则:双方轮流选择棋盘的列号放进自己的棋子,
若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来,
则使用该型号棋子的玩家就赢了!
程序实现游戏,并将每局的数据保存到本地的文件中
首先我们要创建一个空白的棋盘
1
2
3
4
5
6
|
def into(): #初始空白棋盘 for i in range ( 6 ): list_width = [] for j in range ( 8 ): list_width.append( ' ' + '|' ) screen.append(list_width) |
然后呢 我们再写一个输赢判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
def eeferee(): #判断输赢 #判断行 for i in range ( 6 ): for j in range ( 8 - 3 ): if screen[i][j][ 0 ] = = screen[i][j + 1 ][ 0 ] = = screen[i][j + 2 ][ 0 ] = = screen[i][j + 3 ][ 0 ] and screen[i][j][ 0 ]! = ' ' : return false #判断列 for i in range ( 6 - 3 ): for j in range ( 8 ): if screen[i][j][ 0 ] = = screen[i + 1 ][j][ 0 ] = = screen[i + 2 ][j][ 0 ] = = screen[i + 3 ][j][ 0 ] and screen[i][j][ 0 ]! = ' ' : return false #判断斜线 for i in range ( 6 - 3 ): for j in range ( 8 - 3 ): if screen[i][j][ 0 ] = = screen[i + 1 ][j + 1 ][ 0 ] = = screen[i + 2 ][j + 2 ][ 0 ] = = screen[i + 3 ][j + 3 ][ 0 ] and screen[i][j][ 0 ]! = ' ' : return false if j> = 3 : if screen[i][j][ 0 ] = = screen[i + 1 ][j - 1 ][ 0 ] = = screen[i + 2 ][j - 2 ][ 0 ] = = screen[i + 3 ][j - 3 ][ 0 ] and screen[i][j][ 0 ] ! = ' ' : return false return true |
下完每步棋,我们要显示一下棋盘,下面写一下棋盘的显示
1
2
3
4
5
6
7
8
9
10
11
12
13
|
def screen_print(): #打印棋盘 print (' ',1,2,3,4,5,6,7,8,sep=' ') print (' ', 1, 2, 3, 4, 5, 6, 7, 8, sep=' ', file = file , flush = true) for i in range ( 6 ): print ( '|' ,end = '') print ( '|' , end = '', file = file , flush = true) for j in range ( 8 ): print (screen[i][j],end = '') print (screen[i][j], end = '', file = file , flush = true) print ('') print ('', file = file , flush = true) print ( '——' * ( 9 )) print ( '——' * ( 9 ), file = file , flush = true) |
下面是劳拉的自动下棋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def lara(): # 劳拉 global screen while true: coordinate = random.randint( 0 , 7 ) flag = true high = 0 for i in range ( 5 , - 1 , - 1 ): if screen[i][coordinate][ 0 ] = = ' ' : high = i break if i = = 0 and screen[i][coordinate][ 0 ] ! = ' ' : flag = false if flag: print ( '>>>轮到我了,我把o棋子放在第%d列...' % (coordinate + 1 )) print ( '>>>轮到我了,我把o棋子放在第%d列...' % (coordinate + 1 ), file = file , flush = true) screen[high][coordinate] = 'o' + '|' break screen_print() |
下棋中 我们还要判断棋盘是否被下满了
1
2
3
4
5
6
|
def full(): for i in screen: for j in i: if j[ 0 ] = = ' ' : return true return false |
最后 我们完成一下玩家的下棋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
def user(): global screen while true: print ( ">>>轮到你了,你放x棋子,请选择列号(1-8): " ,end = '') print ( ">>>轮到你了,你放x棋子,请选择列号(1-8): " , end = '', file = file , flush = true) coordinate = int ( input ()) - 1 if coordinate not in range ( 7 ): print ( '输入错误的列号,请重新输入' ) print ( '输入错误的列号,请重新输入' , file = file , flush = true) continue flag = true high = 0 for i in range ( 5 , - 1 , - 1 ): if screen[i][coordinate][ 0 ] = = ' ' : high = i break if i = = 0 and screen[i][coordinate][ 0 ] ! = ' ' : flag = false print ( '你输入的地方已经有棋子了,请重新输入' ) print ( '你输入的地方已经有棋子了,请重新输入' , file = file , flush = true) if flag: screen[high][coordinate] = 'x' + '|' break screen_print() |
完整代码如下:
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
import random screen = [] #棋盘列表 def into(): #初始空白棋盘 for i in range ( 6 ): list_width = [] for j in range ( 8 ): list_width.append( ' ' + '|' ) screen.append(list_width) def screen_print(): #打印棋盘 print (' ',1,2,3,4,5,6,7,8,sep=' ') print (' ', 1, 2, 3, 4, 5, 6, 7, 8, sep=' ', file = file , flush = true) for i in range ( 6 ): print ( '|' ,end = '') print ( '|' , end = '', file = file , flush = true) for j in range ( 8 ): print (screen[i][j],end = '') print (screen[i][j], end = '', file = file , flush = true) print ('') print ('', file = file , flush = true) print ( '——' * ( 9 )) print ( '——' * ( 9 ), file = file , flush = true) def eeferee(): #判断输赢 #判断行 for i in range ( 6 ): for j in range ( 8 - 3 ): if screen[i][j][ 0 ] = = screen[i][j + 1 ][ 0 ] = = screen[i][j + 2 ][ 0 ] = = screen[i][j + 3 ][ 0 ] and screen[i][j][ 0 ]! = ' ' : return false #判断列 for i in range ( 6 - 3 ): for j in range ( 8 ): if screen[i][j][ 0 ] = = screen[i + 1 ][j][ 0 ] = = screen[i + 2 ][j][ 0 ] = = screen[i + 3 ][j][ 0 ] and screen[i][j][ 0 ]! = ' ' : return false #判断斜线 for i in range ( 6 - 3 ): for j in range ( 8 - 3 ): if screen[i][j][ 0 ] = = screen[i + 1 ][j + 1 ][ 0 ] = = screen[i + 2 ][j + 2 ][ 0 ] = = screen[i + 3 ][j + 3 ][ 0 ] and screen[i][j][ 0 ]! = ' ' : return false if j> = 3 : if screen[i][j][ 0 ] = = screen[i + 1 ][j - 1 ][ 0 ] = = screen[i + 2 ][j - 2 ][ 0 ] = = screen[i + 3 ][j - 3 ][ 0 ] and screen[i][j][ 0 ] ! = ' ' : return false return true def full(): for i in screen: for j in i: if j[ 0 ] = = ' ' : return true return false def lara(): # 劳拉 global screen while true: coordinate = random.randint( 0 , 7 ) flag = true high = 0 for i in range ( 5 , - 1 , - 1 ): if screen[i][coordinate][ 0 ] = = ' ' : high = i break if i = = 0 and screen[i][coordinate][ 0 ] ! = ' ' : flag = false if flag: print ( '>>>轮到我了,我把o棋子放在第%d列...' % (coordinate + 1 )) print ( '>>>轮到我了,我把o棋子放在第%d列...' % (coordinate + 1 ), file = file , flush = true) screen[high][coordinate] = 'o' + '|' break screen_print() def user(): global screen while true: print ( ">>>轮到你了,你放x棋子,请选择列号(1-8): " ,end = '') print ( ">>>轮到你了,你放x棋子,请选择列号(1-8): " , end = '', file = file , flush = true) coordinate = int ( input ()) - 1 if coordinate not in range ( 7 ): print ( '输入错误的列号,请重新输入' ) print ( '输入错误的列号,请重新输入' , file = file , flush = true) continue flag = true high = 0 for i in range ( 5 , - 1 , - 1 ): if screen[i][coordinate][ 0 ] = = ' ' : high = i break if i = = 0 and screen[i][coordinate][ 0 ] ! = ' ' : flag = false print ( '你输入的地方已经有棋子了,请重新输入' ) print ( '你输入的地方已经有棋子了,请重新输入' , file = file , flush = true) if flag: screen[high][coordinate] = 'x' + '|' break screen_print() if __name__ = = '__main__' : file = open ( '四连环log-%d.txt' % random.randint( 10000 , 99999 ), 'w' ,encoding = 'utf-8' ) print ( """hi,我是劳拉,我们来玩一局四连环。我用o型棋子,你用x型棋子。 游戏规则:双方轮流选择棋盘的列号放进自己的棋子, 若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来, 则使用该型号棋子的玩家就赢了!""" ) print ( """hi,我是劳拉,我们来玩一局四连环。我用o型棋子,你用x型棋子。 游戏规则:双方轮流选择棋盘的列号放进自己的棋子, 若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来, 则使用该型号棋子的玩家就赢了!""" , file = file , flush = true) into() print ( '开始了!这是棋盘的初始状态:' ) print ( '开始了!这是棋盘的初始状态:' , file = file , flush = true) screen_print() flag = true while eeferee() and full(): lara() if not eeferee() and full(): flag = false break user() if full(): print ( '******* 难分胜负!@_@' ) print ( '******* 难分胜负!@_@' , file = file , flush = true) if flag: print ( '******* 好吧,你赢了!^_^' ) print ( '******* 好吧,你赢了!^_^' , file = file , flush = true) else : print ( '******* 耶,我赢了!^_^' ) print ( '******* 耶,我赢了!^_^' , file = file , flush = true) |
效果图:
到此这篇关于python 实现劳拉游戏的实例代码(四连环、重力四子棋)的文章就介绍到这了,更多相关python 实现劳拉游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Miku_wx/article/details/111811663