Python小游戏 井字棋(人机对战,玩家对战)

简介:

# -*- coding:utf-8 -*-

import time

import random

#井字棋 人机对战

def drawBoard(board):

blank_board = '|     '*3+'|'

edge_board = '+-----'*3+'+'

def drawLine(board_line):

insert_sym = '|'

print blank_board

print "|%3s%3s%3s%3s%3s  |" % (board_line[0],insert_sym,board_line[1],insert_sym,board_line[2])

print blank_board

print edge_board

print edge_board

drawLine(board[7:10])

drawLine(board[4:7])

drawLine(board[1:4])

def inputPlayerLetter():

letter = ''

while not (letter == 'X' or letter =='O'):

print 'you want use X or O?'

letter = raw_input().upper()

if letter =='X':

return ['X','O']

else:

return ['O','X']

def playerMove(board,letter):

move=' '

while move not in '1 2 3 4 5 6 7 8 9'.split():

print 'you next chioes(1-9)'

move = raw_input()

try:

if not isSpaceFree(board,int(move)):

print 'There is already a pawn on this board'

move = ''

continue

except:

print 'Enter a non-compliant rule, enter a valid number (1-9)'

continue

board[int(move)] = letter

return isWinner(board,letter)


def computerMove(board,letter):

print 'wait......'

while True:

move = random.randint(1,9)

if not isSpaceFree(board,move):

move = 0

continue

else:

break

board[int(move)] = letter

return isWinner(board,letter)

def playAgain():

#startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False

print 'Do you want play again? (yes or no)'

return raw_input().lower().startswith('y')

def isWinner(bo,le):

#有连成线的返回True

return ((bo[7]==le and bo[8]==le and bo[9]==le)or

(bo[4]==le and bo[5]==le and bo[6]==le)or

(bo[1]==le and bo[2]==le and bo[3]==le)or

(bo[7]==le and bo[4]==le and bo[1]==le)or

(bo[8]==le and bo[5]==le and bo[2]==le)or

(bo[9]==le and bo[6]==le and bo[3]==le)or

(bo[7]==le and bo[5]==le and bo[3]==le)or

(bo[9]==le and bo[5]==le and bo[1]==le))

def isSpaceFree(board,move):

#传入的board[move]的值不是'X','O'返回True

return board[move] == ' ' or board[move] in '1 2 3 4 5 6 7 8 9'.split()

def isBoardFull(board):

#board 中的的值有不是'X','O'的 返回True

for i in range(1,10):

if isSpaceFree(board,i):

return False

return True


if __name__=='__main__':

print 'welcome to this game'

while True:

theBoard = '0 1 2 3 4 5 6 7 8 9'.split()

playerLetter,computerLetter = inputPlayerLetter()

letter = (playerLetter,computerLetter)

turn = 0

gameIsPlaying = True

while gameIsPlaying:

drawBoard(theBoard)

print 'go'

if playerMove(theBoard,playerLetter):

drawBoard(theBoard)

print ' you are winner'

break

else:

if isBoardFull(theBoard):

drawBoard(theBoard)

print 'Game over '

break

drawBoard(theBoard)

if computerMove(theBoard,computerLetter):

drawBoard(theBoard)

print ' computer is winner'

break

else:

if isBoardFull(theBoard):

drawBoard(theBoard)

print 'Game over '

break

if not playAgain():

break



# -*- coding:utf-8 -*-


#井字棋  玩家对战

def drawBoard(board):

blank_board = '|     '*3+'|'

edge_board = '+-----'*3+'+'

def drawLine(board_line):

insert_sym = '|'

print blank_board

print "|%3s%3s%3s%3s%3s  |" % (board_line[0],insert_sym,board_line[1],insert_sym,board_line[2])

print blank_board

print edge_board

print edge_board

drawLine(board[7:10])

drawLine(board[4:7])

drawLine(board[1:4])

def inputPlayerLetter():

letter = ''

while not (letter == 'X' or letter =='O'):

print 'you want use X or O?'

letter = raw_input().upper()

if letter =='X':

return ['X','O']

else:

return ['O','X']

def playerMove(board,letter):

move=' '

while move not in '1 2 3 4 5 6 7 8 9'.split():

print 'you next chioes(1-9)'

move = raw_input()

try:

if not isSpaceFree(board,int(move)):

print 'There is already a pawn on this board'

move = ''

continue

except:

print 'Enter a non-compliant rule, enter a valid number (1-9)'

continue

board[int(move)] = letter

return isWinner(board,letter)

def playAgain():

print 'Do you want play again? (yes or no)'

return raw_input().lower().startswith('y')

def isWinner(bo,le):

return ((bo[7]==le and bo[8]==le and bo[9]==le)or

(bo[4]==le and bo[5]==le and bo[6]==le)or

(bo[1]==le and bo[2]==le and bo[3]==le)or

(bo[7]==le and bo[4]==le and bo[1]==le)or

(bo[8]==le and bo[5]==le and bo[2]==le)or

(bo[9]==le and bo[6]==le and bo[3]==le)or

(bo[7]==le and bo[5]==le and bo[3]==le)or

(bo[9]==le and bo[5]==le and bo[1]==le))

def isSpaceFree(board,move):

return board[move] == ' ' or board[move] in '1 2 3 4 5 6 7 8 9'.split()

def isBoardFull(board):

for i in range(1,10):

if isSpaceFree(board,i):

return False

return True


if __name__=='__main__':

print 'welcome to this game'

while True:

theBoard = '0 1 2 3 4 5 6 7 8 9'.split()

player1Letter,player2Letter = inputPlayerLetter()

letter = (player1Letter,player2Letter)

print ' '+letter[0]+' to go first'

turn = 0

gameIsPlaying = True

while gameIsPlaying:

drawBoard(theBoard)

current_letter = letter[turn]

print ' '+current_letter+' to go'

if playerMove(theBoard,current_letter):

drawBoard(theBoard)

print ' '+current_letter+' is winner'

gameIsPlaying = False

else:

if isBoardFull(theBoard):

drawBoard(theBoard)

print 'Game over '

break

else:

turn = (turn+1)%2

if not playAgain():

break










本文转自 Xuenqlve 51CTO博客,原文链接:http://blog.51cto.com/13558754/2057906,如需转载请自行联系原作者
目录
相关文章
|
19天前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
19天前
|
Python
python小游戏7
python小游戏7
|
19天前
|
Python
python小游戏6
python小游戏6
|
19天前
|
Python
python小游戏5
python小游戏5
|
19天前
|
Python
python小游戏4
python小游戏4
|
19天前
|
Python
python小游戏1
python小游戏1
|
19天前
|
安全 C++ Python
小游戏实战-Python实现石头剪刀布+扫雷小游戏
小游戏实战-Python实现石头剪刀布+扫雷小游戏
42 0
|
19天前
|
数据可视化 安全 数据安全/隐私保护
使用Python做个可视化的“剪刀石头布”小游戏
使用Python做个可视化的“剪刀石头布”小游戏
38 0
|
19天前
|
Python
python小游戏3
python小游戏3
|
19天前
|
算法 编译器 C语言
C语言猜数字小游戏(也包含python实现的用法)
本文基于VS2022、pycharm和前面的知识,写一个凭借分支与循环的小游戏,比如: 写一个猜数字游戏 游戏要求: 电脑自动生成1~100的随机数 玩家猜数字,猜数的过程中,根据猜测数据的大小给出大了或小了的反馈,直到猜对,游戏结束 在pyhton中生成随机数是比较简单的,可以直接导入random的包,直接生成随机数,导致写猜数字小游戏变成了判读语句和循环语句嵌套就能写出来,所以我不做过多的介绍了,可以直接看后面的代码展示,想了解更多的python可看python的基础知识,这里面有我在学习python的过程中写的笔记
36 0