Python implements the game, and python implements the game.

Source: Internet
Author: User
Tags define function

Python implements the game, and python implements the game.

This article provides an example of how python implements a game of the game. The details are as follows:

Python 3.4.0 is compiled and run in Windows 7. It is not compatible with Linux because it is called by cmd.
The game is a game, where the numbers on the keyboard correspond to the Board position.

# Compile and debug the game python3.4.0 and only run it in windows. Import randomimport subprocessimport time # define 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_board [1] +' | \ n' +' -------') def input_pla Yer_letter (): letter = ''while not (letter = 'X' or letter = 'O'): print ('select X or O as the pawn :', end = '') letter = input (). upper () if letter = 'X': return ['x', 'O'] else: return ['O', 'x'] def who_first (): if 1 = random. randint (1, 2): return 'computer 'else: return 'player' def is_again (): print ('again? (Yes or No) ') return input (). lower (). startswith ('y') def is_space_free (the_board, move): return the_board [move] = ''def trim (the_board, move_from_list): possible_moves = [] for I in move_from_list: if is_space_free (the_board, I): possible_moves.append (I) if len (possible_moves )! = 0: return random. choice (possible_moves) else: return Nonedef make_move (the_board, the_letter, the_move): the_board [the_move] = the_letterdef get_board_copy (the_board): outputs = [] for I board: returns (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_move not in list (range (1, 9) or not is_space_free (the_board, the_move): print ('Enter the step :', 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 and the_board [3] = the_letter) or (the_board [4] = the_letter and the_board [5] = the_letter and the_board [6] = the_letter) or (th E_board [7] = the_letter and the_board [8] = the_letter and the_board [9] = the_letter) or (the_board [1] = the_letter 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_board [5] = the_letter and the_board [8] = the_letter) or (the_board [3] = the_letter and the_board [6] = the_letter and the_board [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_lette R = 'O' computer_letter = 'X' # check whether you can win for I in range (): 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 # Check whether the opponent can win for I in range ): if is_space_free (board, I): copy = get_board_copy (board) 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 the game! ') Time. sleep (1) print please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: when there are too many threads, there are too many threads, too many threads Please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: please refer to the following link for more information: when there are too many other users, there are too many other users, too many other users. Please refer to the following link for more information: when there are too many other users, please wait until there are too many other users ''') time. sleep (2) subprocess. call ("cls", shell = True) while True: board = [''] * 10 player_letter, computer_letter = input_player_letter () turn = who_first () print (turn + 'stopped') time. sleep (1) game_is_playing = True while game_is_playing: if turn = 'player': draw_board move = get_player_move (board) make_move (board, player_letter, move) If is_winner (board, 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 = keyboard (board, computer_letter) make_move (board, computer_letter, move) if is_winner (board, computer_letter): draw_board (board) print ('computer victory, you're down! ') 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, hoping to help you learn.

Articles you may be interested in:
  • Python version of The wenquxing guess digital game code
  • The small ball developed by python is fully elastic and collision game code
  • Python basic tutorial-stone scissors cloth game example
  • Python3.3 use tkinter to develop a guess digital game example
  • Example of using python to guess digital games (with no repeated numbers)
  • Example of a snake game written in Python
  • Core code sharing (including complete game code) for generating random maze algorithms implemented by python)
  • Python-based mine clearance game instance code
  • Learn Python with old Qi.

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.