Tic-Tac-Toe游戏
#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游戏的更多相关文章
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 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 ...
- 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 ...
- 【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 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- ACM-Team Tic Tac Toe
我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...
- [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 ...
随机推荐
- jQuery源码笔记——二
jQuery选择这样返回对象 var jQuery = function( selector, context ) { return new jQuery.fn.init( selector, con ...
- 文件和文件夹权限-Win7公共盘中出现大量临时文件
公司中有一个文件服务器,给不同部门和员工设置了不同的权限,最近有员工(没有修改权限,有读取及执行,读取,写入)反映在公共盘上修改文件的时候会产生大量的临时文件,添加上修改权限之后就可以了,然后被同事问 ...
- 浏览器检测JS代码(兼容目前各大主流浏览器)
var BrowserMatch = { init: function () { this.browser = this.getBrowser().browser || "An Unknow ...
- VBA基础知识———常用语句
语句一:if判断语句 Sub 判断1() '单条件判断 If Range("a1").Value > 0 Then Range("b1") = " ...
- leetcode Sudoku Solver python
#the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...
- 图片压缩上传 Android
图片压缩的话 想保持 图像清晰度,但是又想保持图片的大小在100k左右. 同时的话又不想自己写那些压缩的代码的话.那你就找对地方了. 提供一个思路. 先读取你的文件,然后读到bitmap里面进行尺寸裁 ...
- Docker镜像与仓库(二)Dockerfile
Docker镜像文件与仓库(二) Docker镜像文件与仓库(二) Dockerfile指令 Dockerfile格式: 1.#Comment注释2.INSTRUCTION大写的指令名 argumen ...
- hadoop笔记之MapReduce原理
MapReduce原理 MapReduce原理 简单来说就是,一个大任务分成多个小的子任务(map),并行执行后,合并结果(reduce). 例子: 100GB的网站访问日志文件,找出访问次数最多的I ...
- mini-httpd源码分析-version.h
/* version.h - version defines for mini_httpd */ #ifndef _VERSION_H_ #define _VERSION_H_ #define SER ...
- Httpwatch 工具介绍
一 概述: HttpWatch强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理.缓存管理.消息头发送/接受.字符查询.POST 数据和目录管理功 ...