#Tic-Tac-Toe
#机器人和人类下井字棋 #全局变量
import random
X = "X"
O = "O"
EMPTY = " " #表示棋盘上的空空格
TIE = "TIE" #表示平局
NUM_SQUARES = 9 #井字棋棋盘上的方格数 #显示游戏说明
def display_instruct():
"""Display game instrcutin."""
print(
"""Welcome to the gratest intellectual challenge of all time:Tic-Tac-Toe.
This will be a showdown between your human brain and my silicon proessor.
you will make your move known by entering a nmber,0 - 8. The number will
correspond to the board position as illustrated:
0 | 1 | 2
---------
3 | 4 | 5
---------
6 | 7 | 8
Prepare yourself,human .The ultimate battle is about aobegin.\n"""
) #询问一个“是或否”的问题。接受一个问题,返回y或n
def ask_yes_no(quesion):
response = None
while response not in ("y","n"):
response = input(quesion.lower())
return response #求情指定范围内的一个数字
def ask_number(question,low,high):
response = None
while response not in range(low,high):
response = int(input(question))
return response #询问玩家是否希望先行棋
def pieces():
go_first = ask_yes_no("Do you require the first move ? (y/n):")
if go_first =="y":
print "\nThen take the first move.You wil need it."
human = X
computer = O
else:
print "\nYour bravery will be your undoing... I will go first.."
human = O
computer = X
return computer, human #创建新的q棋盘
def new_board():
board = []
for square in range(NUM_SQUARES):
board.append(EMPTY)
return board #显示棋盘
def display_board(board):
print "\n\t",board[0],"|",board[1],"|",board[2]
print "\t","--------"
print "\n\t",board[3],"|",board[4],"|",board[5]
print "\t","--------"
print "\n\t",board[6],"|",board[7],"|",board[8] #接受一个棋盘,返回一个合法的行棋步骤
def legal_moves(board):
moves = []
for square in range(NUM_SQUARES):
if board[square] == EMPTY:
moves.append(square)
return moves #判断输赢
def winner(board):
WAYS_TO_WIN = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))
winner = ""
for row in WAYS_TO_WIN:
if board[row[0]] == board[row[1]] == board[row[2]]!=EMPTY:
winner = board[row[0]]
return winner
if winner=="":
if EMPTY not in board:
return TIE
else:
return None #用户行棋
def human_move(board,human):
legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number("Where you will move ?(0-8):",0,NUM_SQUARES)
if move not in legal:
print ("\nThat square is already occupied,foolish human.Choose another.\n")
print "Fine..."
return move #机器人行棋
def computer_move(board,computer,human):
board = board[:]
BEST_MOVES =(4,0,2,6,8,1,3,5,7) #如果机器人能赢,就走那个位置
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print move
return move
#技术当前行棋方案的测试,并取消之
board[move] =EMPTY
#如果玩家能赢,就堵住那个位置
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print move
return move
#技术当前行棋方案的测试,并取消之
board[move] =EMPTY #由于本轮谁也赢不了,所以叫挑选最佳的空位来走
for move in BEST_MOVES:
if move in legal_moves(board):
print move
return move #返回下一个行棋方
def next_turn(turn):
if turn == X:
return O
else:
return X #接受游戏的赢家
def congract_winner(the_winner,computer,humna):
if the_winner !=TIE:
print the_winner,"won"
else:
print "tie!" if the_winner==computer:
print "computer win"
elif the_winner ==humna:
print "human win!"
elif the_winner==TIE:
print "tie" #
def main():
display_instruct()
computer,human = pieces()
turn = X
board = new_board()
display_board(board) while not winner(board):
if turn == human:
move = human_move(board,human)
board[move] = human
else:
move = computer_move(board,computer,human)
board[move] = computer
display_board(board)
turn = next_turn(turn)
the_winner = winner(board)
congract_winner(the_winner,computer,human) main()

执行过程:

Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Welcome to the gratest intellectual challenge of all time:Tic-Tac-Toe.
This will be a showdown between your human brain and my silicon proessor.
you will make your move known by entering a nmber,0 - 8. The number will
correspond to the board position as illustrated:
0 | 1 | 2
---------
3 | 4 | 5
---------
6 | 7 | 8
Prepare yourself,human .The ultimate battle is about aobegin. do you require the first move ? (y/n):"y" Then take the first move.You wil need it. | |
-------- | |
-------- | |
Where you will move ?(0-8):4
Fine... | |
-------- | X |
-------- | |
0 O | |
-------- | X |
-------- | |
Where you will move ?(0-8):2
Fine... O | | X
-------- | X |
-------- | |
6 O | | X
-------- | X |
-------- O | |
Where you will move ?(0-8):3
Fine... O | | X
-------- X | X |
-------- O | |
5 O | | X
-------- X | X | O
-------- O | |
Where you will move ?(0-8):1
Fine... O | X | X
-------- X | X | O
-------- O | |
7 O | X | X
-------- X | X | O
-------- O | O |
Where you will move ?(0-8):8
Fine... O | X | X
-------- X | X | O
-------- O | O | X
tie!
tie
>>>

Tic-Tac-Toe游戏的更多相关文章

  1. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  2. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  3. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  4. 【leetcode】1275. Find Winner on a Tic Tac Toe Game

    题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...

  5. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  7. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

  8. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  9. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  10. [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

随机推荐

  1. Java基础笔记-异常

    Java中的异常机制: Throwable类是 Java 语言中所有错误或异常的超类.主要包括两个子类: Error和Exception. 一般中要处理的异常是Exception. Java中最常见的 ...

  2. 前端入门HTML5扩展知识(一)

    一. 请描述一个网页从开始请求到最终显示的完整过程? 1.  在浏览器中输入网址: 2.  发送至 DNS 服务器并获得域名对应的 WEB 服务器的 IP 地址: 3.  与 WEB 服务器建立 TC ...

  3. Php开源项目大全

    WordPress  [PHP开源 博客Blog] WordPress是最热门的开源个人信息发布系统(Blog)之一,基于PHP+MySQL构建.WordPress提供的功能包括: 1.文章发布.分类 ...

  4. 利用VS自带的命令行工具查看和生产PublicKeyToken (转)

    使用VS2013(或其他版本)命令行工具,键入:SN -T C:\*****.dll 就会显示出该dll具体的PublicKeyToken数值. 如果该程序集没有强命 名,则不会有PublicKeyT ...

  5. iOS技术

    iOS技术 OC:分类(好处,和延展的区别) 分类: 在不修改原有的类的基础上增加新的方法  一个庞大的类可以分模块开发 一个庞大的类可以由多个人来编写,更有利于团队合作 分类是对原有类的一种扩展,在 ...

  6. 利用oxygen编辑并生成xml文件,并使用JAVA的JAXB技术完成xml的解析

    首先下载oxygen软件(Oxygen XML Editor),目前使用的是试用版(可以安装好软件以后get trial licence,获得免费使用30天的权限,当然这里鼓励大家用正版软件!!!) ...

  7. npm 安装参数中的-save和 -save-dev

    当你为你的模块安装一个依赖模块时,正常情况下你得先安装他们(在模块根目录下npm install module-name),然后连同版本号手动将他们添加到模块配置文件package.json中的依赖里 ...

  8. Attempted to lock an already-locked dir的解决方法

    第一,在当前目录使用“清理”功能,如果不行,到上一级目录,再执行“清理”. 第二,如果看到某个包里面的文件夹没有SVN的标志,直接用“Ctrl+Delete”手工删除,然后“清理”.

  9. Hive进阶(上)

    Hive进阶(上) Hive进阶(上) 执行数据导入 使用Load语句 语法: 1.LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE ...

  10. FPC Trace Pattern Layout Design Notices (軟板線路設計注意事項)

    整理了一些軟板(FPCB/Flex Cable)製造廠關於線路設計的要求 (Design Guide)以避免應用上的品質問題. 1.Relationship between Through Hole, ...