Python 实现劳拉游戏的实例代码(四连环、重力四子棋)
这篇文章主要介绍了Python 实现劳拉游戏的实例代码(四连环、重力四子棋),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
游戏规则:双方轮流选择棋盘的列号放进自己的棋子,
若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来,
则使用该型号棋子的玩家就赢了!
程序实现游戏,并将每局的数据保存到本地的文件中
首先我们要创建一个空白的棋盘
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 实现劳拉游戏的实例代码(四连环、重力四子棋)的文章就介绍到这了