#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 可视化分析工具 第12节

    JAVA 可视化分析工具  第12节 经过前几章对堆内存以及垃圾收集机制的学习,相信小伙伴们已经建立了一套比较完整的理论体系!那么这章我们就根据已有的理论知识,通过可视化工具来实践一番. 我们今天要讲 ...

  2. 斐波那契数列 的两种实现方式(Java)

    import java.util.Scanner; /* 斐波那契数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 如果设F(n)为该数列的第n ...

  3. ecmall 点滴记录

    /* 取得列表数据 */ $model_wish =& m('wish'); $wish= $model_wish->find(array( 'conditions' => 'us ...

  4. CCF计算机认证——字符串匹配问题(运行都正确,为什么提交后只给50分?)

    我的程序: #include<iostream> #include<cctype> #include<string> #include<vector> ...

  5. InterviewProblems

    package com.xiaoysec; /** * 下面是面试趣医网技术面的时候出现的一个简单的题目 题目的要求是将一个数组中的奇数和偶数进行分离 以奇数在前一部分为例进行解题 * 算法的主要思想 ...

  6. CFILE追加写入文件

    CFile file; file.Open(strName, CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate); ) { file.S ...

  7. 使用chrome调试xpath

    使用chrome调试xpath 相信玩过爬虫的都知道一些库,如lxml(python),可以使用xpath方便地对HTML进行提取,但当真正用的时候,问题就来了,想找到一个元素往往要调试好几遍,而且得 ...

  8. jquery ajax返回json数据进行前后台交互实例

    jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...

  9. eclipse 快捷键汇总

    1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索对话框:Ctrl+H快速O ...

  10. SQL Server MySQL 中的 in 与 null

    例子: create table t(x int,y int); insert into t(x,y) values(1,1),(2,2),(null,null); 查询一: select x,y f ...