本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下
#五子棋
‘''
矩阵做棋盘 16*16 “+”
打印棋盘 for for
游戏是否结束
开始下棋 while 游戏是否结束:
黑白交替 player=0 p%2==0 ==1 p+=1
下棋动作一样 但是棋子不一样
‘''
代码
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
|
#创建棋盘的程序 def initboard(): global board #调用全局的board board = [none] * 16 for i in range ( len (board)): board[i] = [ "+ " ] * 16 #打印棋盘的程序 def printboard(): global board for i in range ( len (board)): for j in range ( len (board[i])): print (board[i][j],end = " " ) print ("") #开始下棋的程序 def startgame(): global board player = 0 while isgamecontinue(): if player % 2 = = 0 : #黑方下棋 print ( "==>黑方下棋" ) if not playchess( "●" ): continue else : #白方下棋 print ( "==>白方下棋" ) if not playchess( "○" ): continue player + = 1 def playchess(chess): #获取位置 x = int ( input ( "==> x=" )) - 1 y = int ( input ( "==> y=" )) - 1 if board[x][y] = = "+ " : board[x][y] = chess printboard() return true #落子成功 else : print ( "==> 已有棋子 请重新落子\a" ) printboard() return false #落子失败 def isgamecontinue(): for i in range ( len (board)): for j in range ( len (board[i])): if board[i][j]! = "+ " : #横向 if j< = 11 : if board[i][j] = = board[i][j + 1 ] = = board[i][j + 2 ] = = board[i][j + 3 ] = = board[i][j + 4 ]: whowin(i,j) return false #竖向 if i< = 11 : if board[i][j] = = board[i + 1 ][j] = = board[i + 2 ][j] = = board[i + 3 ][j] = = board[i + 4 ][j]: whowin(i,j) return false #反斜 if i< = 11 and j< = 11 : if board[i][j] = = board[i + 1 ][j + 1 ] = = board[i + 2 ][j + 2 ] = = board[i + 3 ][j + 3 ] = = board[i + 4 ][j + 4 ]: whowin(i,j) return false #正斜 if i> = 4 and j< = 11 : if board[i][j] = = board[i - 1 ][j + 1 ] = = board[i - 2 ][j + 2 ] = = board[i - 3 ][j + 3 ] = = board[i - 4 ][j + 4 ]: whowin(i,j) return false return true def whowin(i,j): if board[i][j] = = "●" : print ( "黑方胜!" ) else : print ( "白方胜!" ) for i in range ( 10 ): print ( "\a" ) board = [] initboard() printboard() startgame() |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_42197548/article/details/85073198