Python mini-game Tic Tac-Chess (man-machine vs. Player vs. players)

Source: Internet
Author: User

#-*-Coding:utf-8-*-

Import time

Import Random

#井字棋 Man-Machine battle

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 ' 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 isn't 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 ():

The #startswith () method is used to check whether the string starts with the specified substring, or True if it is, otherwise 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):

The value of #传入的board [move] is not ' X ', ' O ' returns True

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

def isboardfull (board):

The value in the #board has not ' X ', ' O ' returns 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're 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-*-


#井字棋 Player vs.

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 ' 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 isn't 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


Python mini-game Tic Tac-Chess (man-machine vs. Player vs. players)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.