Python implements Tic-Tac-Chess game

Source: Internet
Author: User
This article introduced the Python implementation of Tic-Tac-Chess game method, share to everyone, the specific content is as follows

Windows7 under python3.4.0 compile run pass. Because of the use of CMD calls, it is incompatible with Linux and cannot be run under Linux.
The game is tic-tac-White, and the digital position on the keypad corresponds to the board position.

#本游戏python3.4.0 to write debugging, you can only run under Windows. Import randomimport subprocessimport time# definition function def draw_board (the_board): Subprocess.call ("cls", Shell = True) Print ('--  -----\ n ' + ' | ' + the_board[9] + ' | ' + the_board[8] + ' | ' + the_board[7] + ' |\n ' + '-------\ n ' + ' | ' + the_board[6] + ' | ' + the_board[5] + ' | ' + the_board[4] + ' |\n ' + '-------\ n ' + ' | ' + the_board[3] + ' | ' + the_board[2 ' + ' | ' + The_B OARD[1] + ' |\n ' + '-------') def input_player_letter (): letter = "While not" (letter = = ' X ' or letter = = ' O '): Print (' Please select X or O as pawn: ', end = ') Letter = input (). Upper () if letter = = ' x ': Return [' X ', ' O '] else:return [' O ', ' x ']def who_firs T (): if 1 = = Random.randint (1, 2): Return ' computer ' Else:return ' Player ' def is_again (): Print (' Once again? (Yes or No) ') return input (). lower (). StartsWith (' y ') def is_space_free (The_board, move): return the_board[move] = = "Def c Hoose_random_from_list (The_board, move_from_list): Possible_moves = [] for i in Move_from_list:if Is_space_free (The_boa   Rd, I):Possible_moves.append (i) If Len (possible_moves)! = 0:return Random.choice (possible_moves) Else:return Nonedef Make_mov E (The_board, The_letter, The_move): the_board[the_move] = the_letterdef get_board_copy (the_board): Duplicated_board = [  ] for I in Board:duplicated_board.append (i) return duplicated_boarddef is_board_full (the_board): For I in range (1, 9): If Is_space_free (The_board, i): return False else:return truedef get_player_move (the_board): The_move = 0 while The_mo  ve not in list (range (1, 9)) or not Is_space_free (The_board, The_move): Print (' Please enter walking: ', end = ') the_move = Int (input ()) Return the_movedef Is_winner (The_board, The_letter): return (the_board[1] = = The_letter and the_board[2] = = The_letter an D the_board[3] = = The_letter) or (the_board[4] = = The_letter and the_board[5] = = The_letter and the_board[6] = = The_letter ) or (the_board[7] = = The_letter and the_board[8] = = The_letter and the_board[9] = = The_letter) or (the_board[1] = = The_le Tter and the_board[5] = = thE_letter and the_board[9] = = The_letter) or (the_board[2] = = The_letter and the_board[5] = = The_letter and the_board[8] = = The_letter) or (the_board[3] = = The_letter and the_board[5] = = The_letter and the_board[7] = = The_letter) or (the_board[1 ] = = The_letter and the_board[4] = = The_letter and the_board[7] = = The_letter) or (the_board[2] = = The_letter and The_boar D[5] = = The_letter and the_board[8] = = The_letter) or (the_board[3] = = The_letter and the_board[6] = = The_letter and The_B OARD[9] = = The_letter) def get_computer_move (The_board, Computer_letter): Global player_letter Global Move if Player_ Letter = = ' x ': computer_letter = ' o ' else:player_letter = ' o ' computer_letter = ' x ' #虚拟棋盘查看是否自己可一步得胜 for I in range (1, 9): Copy = get_board_copy (board) if Is_space_free (board, I): Make_move (copy, Computer_letter, i) if Is_winner (copy, Computer_letter): Return i #虚拟棋盘查看是否对手可一步得胜 for I in Range (1,9): If Is_space_free (board, i): copy = Get_board_copy (b Oard) make_move (copy, player_letter, I) if Is_winner (copy, player_letter): return I move = choose_random_from_list (board, [1, 3, 7, 9]) if Move! = 0:return Move if Is_space_free (board, 5): Return 5 return choose_random_from_list (board, [2, 4, 6, 8, 7]) print (' Welcome to play well ' Word Chess game! ') time.sleep (1) print ("' ▆▅▅▅▆▅▅▅▅▅▅▅▂▅▅▅▆▆▅▅▃▂▆▅▅▅▅▅▅▅▅▆▆▆▃▂▆▆▅▃▄▆▅▂▅▆▇▇▆▆▆▄▂▆▆▆▆▆▆▆▆▅▆▅▆▅ ▁▅▂▃▅▆▅▂▆▆▇▆▅▆▇▄▂▆▆▆▆▆▆▆▆▅▆▅▆▆▅▃▆▅▆▅▂▆▇▆▅▅▆▇▅▂▆▆▆▆▆▆▆▆▅▆▆▆▆▆▃ ▁▅▆▆▄▂▇▇▆▅▅▆▇▅▁▆▆▆▆▆▆▆▆▅▆▅▆▆▃▂▃▁▁▅▆▄▂▇▇▆▅▆▇▇▅▂▆▆▆▅▅▅▅▅▅▆▅▆▃▁▅▆▃▁▁▅▅▂▆▇▆▆▇▆▆▄▂▆▅▅▅▅▅▆▆▅▆▅▆▄▅▆▆▆▄▂▂▃▃▆▆▇▇▆▆▆▅▂▆▆▆▆▆▆▆▆▆▆▅▄▄ ▄▄▄▄▄▄▃▂▅▄▄▃▄▄▄▃▂▅▄▄▅▅▅▅▅▅▆▅▂▂▂▂▃▃▃▃▃▂▁▃▂▃▃▃▃▂▂▃▂▃▃▃▃▃▅▆▅▆▆▆▇▇▇▇▆▆▅▂▁▄▆▆▆▄▅▄▂▆▆▆▆▆▆▆▆▅▆▅▆▅▆▇▆▆▆▆▆▄▄▄ ▃▆▂▂▅▄▂▆▅▅▆▅▅▆▆▅▆▅▅▆▆▇▆▅▆▇▆▄▃▆▂▂▃▅▆▄▂▆▅▅▅▅▅▅▆▅▆▅▅▆▇▆▅▅▆▇▇▄▃▆▅▂ ▃▆▅▄▂▆▅▅▅▅▅▆▆▅▆▅▅▆▇▆▅▆▆▇▆▃▂▆▄▂▂▁▃▆▅▂▆▅▅▆▆▆▆▆▅▆▅▆▆▇▆▆▇▇▆▆▄▂▄▁▄▅▂▁▂▅▂▆▅▆▆▆▆▆▆▅▆▅▅▆▆▆▇▆▆▆▆▄▁▃▄▆▆▄▂▁▁▂▆▅▅▆▆▆▆▆▅▆▅▂▂▂▂▃▂▂▂▂▂▁▃ ▃▃▃▂▁▁▂▂▂▂▂▂▃▄▅▆▆▆▆▆▅▅▅▅▅▅▄▁▅▅▅▅▄▅▅▄ ▁▅▆▅▅▅▅▆▆▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▃▂▆▆▆▆▅▅▆▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▆▂▁▅▆▃▃▆▆▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▂▆▅▁▁▃▂▅▆▆▆▆▆▆▆▆▆▆▆▆▆▄▃▆▆▆▆▆▆ ▆▄▃▆▆▄▁▅▇▆▅▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▆▄▁▁▁▅▆▅▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▄▂▄▃▁▅▆▆▆▆▆▆▆▆▆▆▆▆▅▃▆▆▆▆▆▆▆▅▃▅▁▄▆▆▃▁▄▆▆▆▆▆▆▆▆▆▆▆▅▄▆▆▆▆▆▆▆▄▃▆▅▆▆▆▆▄▃▂ ' ") Time.sleep (2) subprocess.call (" cls ", Shell = True) while true:board = ["] * player_letter, computer_letter = Input_  Player_letter () turn = Who_first () print (Turn + ' go first ') time.sleep (1) game_is_playing = True while game_is_playing:if turn = = ' Player ': draw_board (board) move = get_player_move (board) Make_move (board, Player_letter, move) if Is_winner (b Oard, Player_letter): Draw_board (board) print (' Congratulations! You win. ') Game_is_playinig = False else:if is_board_full (board): Draw_board (board) print (' Draw! ') Break Else:turn = ' computer ' Else:move = get_computer_move (board, Computer_letter) Make_move (board, C Omputer_letter, move) if Is_winner (board, Computer_letter): Draw_board (board) print (' Computer wins, you're dead!     ') game_is_playing = False else:if is_board_full (board): Draw_board (board) print (' Draw! ') Break Else:turn =' Player ' if not is_again (): Break 

The above is the detailed content of this article, I hope that everyone's study has helped.

  • 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.