#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. CentOS6.4下搭建hadoop2.2(64bit)注意事项

    注:本文针对64位机器,32bit课直接tar -zxvf hadoop-2.2.0.tar.gz 解压配置即可. Step1:安装jdk(6以上版本) Step2:下载hadoop--->ht ...

  2. UIPasteboard 粘贴板

    UIPasteboard *pasteboard = pasteboard.string = self.label.text;

  3. Editplus配置java运行环境

    Editplus配置java运行环境 下载及安装: editplus官网下载地址:https://www.editplus.com/ 安装方法和安装普通exe应用程序一样,选在安装路径,下一步下一步, ...

  4. python heapq

    这个模块(build-in)实现了一个堆的数据结构,完美的解决了Top-K问题,以后解决Top-K问题的时候,直接把这个模块拿来用就可以了 注意,默认的heap是一个小顶堆! heapq模块提供了如下 ...

  5. chrome误删书签恢复。

    由于手残本来想添加网页到书签文件夹的,结果点了删除. 但是整个人就炸了,里面有我好多链接. 于是立马Google了一下,发现不少朋友和我一样,都是误删了书签或者书签文件夹. 但是chrome并没有书签 ...

  6. C 中注意的小问题

    输入:char ch[100],gets(ch); scanf("%d",&in); char ch,ch=getchar(); VC:  所有变量声明放在所有操作前面: ...

  7. 复习-C语言内嵌汇编-初级(1)

    打印hello world并改变变量i的值 # include <stdio.h> int main() { ; __asm__( "mov %0, #4\n" :&q ...

  8. Activity四种launchMode

    更多内容在这里查看 https://ahangchen.gitbooks.io/windy-afternoon/content/ 总共有四篇关于Activity,task,launchMode的文章, ...

  9. Surface Pro 4 和 Surface Book 使用名为 Surface UEFI(统一可扩展固件接口)的新固件接口

    Surface Pro 4 和 Surface Book 使用名为 Surface UEFI(统一可扩展固件接口)的新固件接口.Surface UEFI 提供新功能,如启动更快速.安全性更高.可替换 ...

  10. android中获取root权限的方法以及原理(转)

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...