您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python 实现劳拉游戏的实例代码(四连环、重力四子棋)

51自学网 2021-10-30 22:53:37
  python
这篇教程Python 实现劳拉游戏的实例代码(四连环、重力四子棋)写得很实用,希望能帮到您。

游戏规则:双方轮流选择棋盘的列号放进自己的棋子,
若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来,
则使用该型号棋子的玩家就赢了!

程序实现游戏,并将每局的数据保存到本地的文件中

首先我们要创建一个空白的棋盘

def into():#初始空白棋盘  for i in range(6):    list_width=[]    for j in range(8):      list_width.append(' '+'|')    screen.append(list_width)

然后呢 我们再写一个输赢判断

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 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 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 full():  for i in screen:    for j in i:      if j[0] == ' ':        return True  return False

最后 我们完成一下玩家的下棋

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()

完整代码如下:

import randomscreen = [] #棋盘列表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 Truedef full():  for i in screen:    for j in i:      if j[0] == ' ':        return True  return Falsedef 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 实现劳拉游戏内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!


对pytorch中x = x.view(x.size(0), -1) 的理解说明
基于PyTorch中view的用法说明
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。