这个游戏原本是学习的这本书上的小游戏,但是文章最后留了一个地址,然后我点进去了,然后就看到了这个小游戏

觉得很有趣,一下子没看懂,所以就把它注释了一番,帮助自己理解

原文源码地址(需要翻墙)

# Tic Tac Toeimport random# 打印方法
def drawBoard(board):# This function prints out the board that it was passed.# "board" is a list of 10 strings representing the board (ignore index 0)print('   |   |')print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])print('   |   |')print('-----------')print('   |   |')print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])print('   |   |')print('-----------')print('   |   |')print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])print('   |   |')def inputPlayerLetter():# Lets the player type which letter they want to be.# Returns a list with the player’s letter as the first item, and the computer's letter as the second.letter = ''while not (letter == 'X' or letter == 'O'):print('Do you want to be X or O?')letter = input().upper()# the first element in the list is the player’s letter, the second is the computer's letter.if letter == 'X':return ['X', 'O']else:return ['O', 'X']def whoGoesFirst():# Randomly choose the player who goes first.if random.randint(0, 1) == 0:return 'computer'else:return 'player'def playAgain():# This function returns True if the player wants to play again, otherwise it returns False.print('Do you want to play again? (yes or no)')return input().lower().startswith('y')# 下子
def makeMove(board, letter, move):board[move] = letter# 判断游戏是否结束
def isWinner(bo, le):# Given a board and a player’s letter, this function returns True if that player has won.# We use bo instead of board and le instead of letter so we don’t have to type as much.return ((bo[7] == le and bo[8] == le and bo[9] == le) or  # across the top(bo[4] == le and bo[5] == le and bo[6] == le) or  # across the middle(bo[1] == le and bo[2] == le and bo[3] == le) or  # across the bottom(bo[7] == le and bo[4] == le and bo[1] == le) or  # down the left side(bo[8] == le and bo[5] == le and bo[2] == le) or  # down the middle(bo[9] == le and bo[6] == le and bo[3] == le) or  # down the right side(bo[7] == le and bo[5] == le and bo[3] == le) or  # diagonal(bo[9] == le and bo[5] == le and bo[1] == le))  # diagonaldef getBoardCopy(board):# Make a duplicate of the board list and return it the duplicate.dupeBoard = []for i in board:dupeBoard.append(i)return dupeBoard# 验证输入的list值是否为空
def isSpaceFree(board, move):# Return true if the passed move is free on the passed board.return board[move] == ' '# 返回下子位置
def getPlayerMove(board):# Let the player type in their move.move = ' 'while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):print('What is your next move? (1-9)')move = input()return int(move)# 从这些列表里面随机下
def chooseRandomMoveFromList(board, movesList):# Returns a valid move from the passed list on the passed board.# Returns None if there is no valid move.possibleMoves = []# 获取空子位置listfor i in movesList:if isSpaceFree(board, i):possibleMoves.append(i)# list不为空,随机选一个if len(possibleMoves) != 0:return random.choice(possibleMoves)else:return None# 电脑获取下子位置
def getComputerMove(board, computerLetter):# Given a board and the computer's letter, determine where to move and return that move.if computerLetter == 'X':playerLetter = 'O'else:playerLetter = 'X'# 这个是机器下子的算法# Here is our algorithm for our Tic Tac Toe AI:# 首先检测我们下一步是否能赢# First, check if we can win in the next movefor i in range(1, 10):# copy一份目前的下子画板copy = getBoardCopy(board)# 如果备份的画板中内容不为空if isSpaceFree(copy, i):# 下子makeMove(copy, computerLetter, i)# 如果下这个位置赢就将这个位置返回if isWinner(copy, computerLetter):return i# 检测对手下一步是否会赢,会赢的话就堵它# Check if the player could win on their next move, and block them.for i in range(1, 10):copy = getBoardCopy(board)if isSpaceFree(copy, i):makeMove(copy, playerLetter, i)if isWinner(copy, playerLetter):return i# 优先下这些位置(优先占据角落)# Try to take one of the corners, if they are free.move = chooseRandomMoveFromList(board, [1, 3, 7, 9])if move != None:return move# 夺取中心点# Try to take the center, if it is free.if isSpaceFree(board, 5):return 5# 在最后的列表中下子# Move on one of the sides.return chooseRandomMoveFromList(board, [2, 4, 6, 8])def isBoardFull(board):# Return True if every space on the board has been taken. Otherwise return False.for i in range(1, 10):if isSpaceFree(board, i):return Falsereturn Trueprint('Welcome to Tic Tac Toe!')# 死循环,没有
while True:# Reset the board# 重置输出板theBoard = [' '] * 10# 选棋子playerLetter, computerLetter = inputPlayerLetter()# 随机产生谁先下turn = whoGoesFirst()# 打印是谁先下print('The ' + turn + ' will go first.')# 游戏开始gameIsPlaying = Truewhile gameIsPlaying:# 人先下if turn == 'player':# Player’s turn.# 打印画板drawBoard(theBoard)# 获取下子位置move = getPlayerMove(theBoard)# 下子makeMove(theBoard, playerLetter, move)# 判断游戏是否结束if isWinner(theBoard, playerLetter):drawBoard(theBoard)print('Hooray! You have won the game!')# 结束游戏gameIsPlaying = Falseelse:# 验证画板是否画满if isBoardFull(theBoard):drawBoard(theBoard)print('The game is a tie!')breakelse:turn = 'computer'else:# Computer’s turn.# 机器获取下子位置move = getComputerMove(theBoard, computerLetter)makeMove(theBoard, computerLetter, move)if isWinner(theBoard, computerLetter):drawBoard(theBoard)print('The computer has beaten you! You lose.')gameIsPlaying = Falseelse:if isBoardFull(theBoard):drawBoard(theBoard)print('The game is a tie!')breakelse:turn = 'player'# 如果不想玩了就跳出循环if not playAgain():break

bingo小游戏(圈圈叉叉小游戏)python相关推荐

  1. Py之tkinter:python最简单的猜字小游戏带你进入python的GUI世界

    Py之tkinter:python最简单的猜字小游戏带你进入python的GUI世界 目录 输出结果 设计思路 输出结果 设计思路 from tkinter import * import tkint ...

  2. python小游戏开发,使用python实现英语打字游戏

    需求分析 英文打字小游戏,要有多界面交互,界面整洁.美观,可调节游戏等级难度,可配置游戏信息. 要有游戏分数,游戏时间,动画特效,背景音乐,不同游戏等级的历史最高分记录. 拼写成功的英文单词显示中文意 ...

  3. python制作图形化小游戏_创意编程|Python的GUI简易界面设计测测你的反应力

    Python的GUI简易界面设计案例 测测你的反应力      作为初次接触代码编程的你,是不是觉得Python程序除了"码"就是"字"即使是有趣的程序除了烧烧 ...

  4. python编的俄罗斯方块游戏_手把手制作Python小游戏:俄罗斯方块(一)

    手把手制作Python小游戏:俄罗斯方块1 大家好,新手第一次写文章,请多多指教 A.准备工作: 这里我们运用的是Pygame库,因为Python没有内置,所以需要下载 如果没有pygame,可以到官 ...

  5. python做游戏代码_利用Python基础代码语句,实现2G时代文字小游戏,世界如此简单!...

    相信许多80,90后都玩过2G时代的文字小游戏,它是来自QQ家园的专属回忆.偷菜,美味小镇,大乐斗,还有精武堂等等,虽然只是文字的输出,但是留给我们这一代的人的印象却是最深刻的.曾经流量很少,响应很快 ...

  6. python爬取小游戏_如何用Python爬取小游戏网站,把喜欢的游戏收藏起来(附源码)...

    简介: Python 是一门简单易学且功能强大的编程语言,无需繁琐的配置,掌握基本语法,了解基本库函数,就可以通过调用海量的现有工具包编写自己的程序,轻松实现批量自动化操作,可以极大提高办公和学习效率 ...

  7. python小游戏——跑酷小恐龙代码开源

    ♥️作者:小刘在这里 ♥️每天分享云计算网络运维课堂笔记,努力不一定有收获,但一定会有收获加油!一起努力,共赴美好人生! ♥️夕阳下,是最美的,绽放,愿所有的美好,再疫情结束后如约而至. 目录 一.效 ...

  8. python小项目——2048小游戏(详解)

    2048游戏 原版游戏地址 第一部分 导入所需要的库 第二部分 确认游戏键位设置,并和对应的操作关联 第三部分 获取用户输入的值,并直到有效键位 第四部分 对矩阵的应用,减少代码量 第五部分 创建棋盘 ...

  9. python简单小游戏代码-零基础python教程-用Python设计你的第一个小游戏

    学以致用,今天给大家分享零基础Python设计你的第一个小游戏,既然要学习Python就要让它来实现我们想做的东西,这次咱就用Python来做个简单小游戏,在实践中不断成长.刚学习Python的小伙伴 ...

  10. python小游戏代码大全打枪-python实现微信小游戏打飞机代码

    以前版本的微信小游戏有一个打飞机的游戏,学完python之后我试着写了下程序去基本实现打飞机的功能,下面是小游戏打飞机的python代码 注:python中部分代码转自crossin编程教室 impo ...

最新文章

  1. NS2网络模拟(3)-吞吐率
  2. Silverlight 解谜游戏 之十四 音效
  3. 单源最短路径算法---Dijkstra
  4. 【转】通过CountDownLatch提升请求处理速度
  5. java时间往后一天_往后余生,不能再陪你了
  6. VLAN介绍、工作原理以及配置
  7. ExtJS中的renderTo何applyTo的差别
  8. C++虚函数指针虚函数表
  9. 视频教程-小学生c++趣味编程入门视频教程 少儿C十十信息学奥赛竞赛网课-C/C++
  10. 每周分享第 37 期
  11. 8个免费高清图片素材网站,再也不用担心侵权了。
  12. Windows下使用Grub4dos无损(无需格式化)制作Windows/Linux双引导U盘并引导ISO镜像
  13. 内容推荐场景下多模态语义召回的若干实践
  14. Matlab获取tif各格点经纬度
  15. Acrobat Reader XI启动后自动关闭的分析
  16. Java后端对接微信支付(微信小程序、APP、PC端扫码)非常全,包含查单、退款
  17. 七麦数据爬虫 analysis值加密
  18. 谷歌 百度 搜狗的音乐搜索比较
  19. 开源物联网应用开发平台列表
  20. GA 遗传算法-含代码-多图

热门文章

  1. Unity射线和游戏暂停切换场景的方法
  2. 因为计算机安装了更新i,电脑安装iTunes时提示这台电脑已安装了更高版本的解决方法图文教程...
  3. 1988汉城奥运会歌曲《HIn Hand》铃声 1988汉城奥运会歌曲《HI...
  4. 论文阅读笔记--Clustered Federated Learning:Model-Agnostic Distributed Multitask Optimization Under Privacy
  5. 【论文代码复现】Clustered Sampling: Low-Variance and Improved Representativity for Clients Selection in Fede
  6. 突破防盗链Referrer
  7. 神奇校车桥梁书版和图画书版到底哪个更难?
  8. Spring5学习笔记二
  9. DQN(Deep Q Network)论文笔记
  10. 如何获取由MATLAB神经网络工具箱训练得到的神经网络的隐含层输出