#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. JavaScript 继承方式的实现

    1.原型链继承 function superType(name){ this.name= 'milk'; } super.prototype.sayName=function(){ console.l ...

  2. w3school教程整理

    原文链接:http://www.flygon.net/w3school 原文链接:https://github.com/wizardforcel/w3school w3school教程整理 离线版大部 ...

  3. poj1284--原根的性质

    关于原根,在百度百科上有着详细的介绍,此题主要考查原根的两个性质 1.只有奇素数才有原根. 2.一个数的原根个数为其欧拉函数的欧拉函数. 综合以上特点,可得到,我们设输入数为n,那么输出结果就为n-1 ...

  4. 【codevs】2292图灵机游戏

    题 Shadow最近知道了图灵机是什么(Shadow:就是一行格子和一个机器头移来移去的呗!),于是他突发奇想,创造了一个新游戏——“图灵机游戏”(Shadow:好听吧?). 游戏规则如下: 在一条长 ...

  5. BZOJ 4016: [FJOI2014]最短路径树问题( 最短路 + 点分治 )

    先跑出最短路的图, 然后对于每个点按照序号从小到大访问孩子, 就可以搞出符合题目的树了. 然后就是经典的点分治做法了. 时间复杂度O(M log N + N log N) -------------- ...

  6. jquery实现点击改变背景色,点击其他恢复原来背景色,被点击的改变背景色

    实现这个功能很简单,可是之前自己不怎么熟悉jquery,感觉各种乱写.现在终于整理好了思路. html: <div class="managementPanel"> & ...

  7. 提取 ECharts 中的svg地图信息

    地图的需求还是蛮大的,全国都要自己画的话,还是需要投入比较大的人力. ECharts中有地图,那我们能不能把里面的地图文件提取出来呢,主要逻辑在map.js中. 看源代码发现,ECharts中地图信息 ...

  8. Oracle EBS-SQL (BOM-4):检查期间新增编码总数.sql

    selectFU.description                                    创建者,msi.CREATION_DATE                        ...

  9. SLC和MLC闪存芯片的区别

    许多人对闪存的SLC和MLC区分不清.就拿目前热销的MP3随身听来说,是买SLC还是MLC闪存芯片的呢?在这里先告诉大家,如果你对容量要求不高,但是对机器质量.数据的安全性.机器寿命等方面要求较高,那 ...

  10. git搭建服务器

    搭建Git服务器 在远程仓库一节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商 ...