程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java 應用JDOM解析xml文件

java 應用JDOM解析xml文件

編輯:關於JAVA

java 應用JDOM解析xml文件。本站提示廣大學習愛好者:(java 應用JDOM解析xml文件)文章只能為提供參考,不一定能成為您想要的結果。以下是java 應用JDOM解析xml文件正文


本文實例引見了python完成井字棋游戲的辦法,分享給年夜家,詳細內容以下

windows7下python3.4.0編譯運轉經由過程。因為采取了cmd挪用,所以與Linux不兼容,沒法在Linux下運轉。
游戲就是井字棋,小鍵盤上的數字地位對應棋盤地位。

#本游戲python3.4.0下編寫調試,只能在windows下運轉。
import random
import subprocess
import time
#界說函數
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_player_letter():
 letter = ' '
 while not (letter == 'X' or letter == 'O'):
  print('請選擇X或O作棋子:', 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('再一次?(Yes or No)')
 return input().lower().startswith('y')
def is_space_free(the_board, move):
 return the_board[move] == ' '
def choose_random_from_list(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 None
def make_move(the_board, the_letter, the_move):
 the_board[the_move] = the_letter
def get_board_copy(the_board):
 duplicated_board = []
 for i in board:
  duplicated_board.append(i)
 return duplicated_board
def is_board_full(the_board):
 for i in range(1, 9):
  if is_space_free(the_board, i):
   return False
 else:
  return True
def 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('請輸出走步:', end = '')
  the_move = int(input())
 return the_move
def 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 (the_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_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(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('迎接玩 井字棋 游戲!')
time.sleep(1)
print('''▆▅▅▅▆▅▅▅▅▅▅▅▂▅▅▅▆▆▅▅▃▂▆▅▅▅▅▅▅▅▅
▆▆▆▃▂▆▆▅▃▄▆▅▂▅▆▇▇▆▆▆▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▅ ▁▅▂▃▅▆▅▂▆▆▇▆▅▆▇▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▆▅  ▃▆▅▆▅▂▆▇▆▅▅▆▇▅▂▆▆▆▆▆▆▆▆▅
▆▆▆▆▆▃ ▁▅▆▆▄▂▇▇▆▅▅▆▇▅▁▆▆▆▆▆▆▆▆▅
▆▅▆▆▃▂▃▁▁▅▆▄▂▇▇▆▅▆▇▇▅▂▆▆▆▅▅▅▅▅▅
▆▅▆▃▁▅▆▃▁▁▅▅▂▆▇▆▆▇▆▆▄▂▆▅▅▅▅▅▆▆▅
▆▅▆▄▅▆▆▆▄▂▂▃▃▆▆▇▇▆▆▆▅▂▆▆▆▆▆▆▆▆▆
▆▅▄▄▄▄▄▄▄▄▃ ▂▅▄▄▃▄▄▄▃▂▅▄▄▅▅▅▅▅▅
▆▅▂▂▂▂▃▃▃▃▃▂ ▁▃▂▃▃▃▃▂ ▂▃▂▃▃▃▃▃▅
▆▅▆▆▆▇▇▇▇▆▆▅▂▁▄▆▆▆▄▅▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▅▆▇▆▆▆▆▆▄▄▄ ▃▆▂▂▅▄▂▆▅▅▆▅▅▆▆▅
▆▅▅▆▆▇▆▅▆▇▆▄▃▆▂ ▂▃▅▆▄▂▆▅▅▅▅▅▅▆▅
▆▅▅▆▇▆▅▅▆▇▇▄▃▆▅▂ ▃▆▅▄▂▆▅▅▅▅▅▆▆▅
▆▅▅▆▇▆▅▆▆▇▆▃▂▆▄▂▂▁▃▆▅▂▆▅▅▆▆▆▆▆▅
▆▅▆▆▇▆▆▇▇▆▆▄▂▄▁▄▅▂▁▂▅▂▆▅▆▆▆▆▆▆▅
▆▅▅▆▆▆▇▆▆▆▆▄▁▃▄▆▆▄▂▁▁▂▆▅▅▆▆▆▆▆▅
▆▅▂▂▂▂▃▂▂▂▂▂▁▃▃▃▃▂▁▁  ▂▂▂▂▂▂▃▄▅
▆▆▆▆▆▅▅▅▅▅▅▄▁▅▅▅▅▄▅▅▄ ▁▅▆▅▅▅▅▆▆
▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▃▂▆▆▆▆▅▅▆
▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▆▂▁▅▆▃▃▆▆
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▂▆▅▁▁▃▂▅▆▆
▆▆▆▆▆▆▆▆▆▆▆▄▃▆▆▆▆▆▆▆▄▃▆▆▄▁ ▅▇▆▅
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▆▄▁▁▁▅▆▅
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▄▂▄▃▁ ▅▆
▆▆▆▆▆▆▆▆▆▆▆▅▃▆▆▆▆▆▆▆▅▃▅▁▄▆▆▃▁ ▄
▆▆▆▆▆▆▆▆▆▆▆▅▄▆▆▆▆▆▆▆▄▃▆▅▆▆▆▆▄▃▂''')
time.sleep(2)
subprocess.call("cls", shell = True)
while True:
 board = [' '] * 10
 player_letter, computer_letter = input_player_letter()
 turn = who_first()
 print(turn + '先走')
 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(board, player_letter):
    draw_board(board)
    print('祝賀!你贏了。')
    game_is_playinig = False
   else:
    if is_board_full(board):
     draw_board(board)
     print('平手!')
     break
    else:
     turn = 'computer'
  else:
   move = get_computer_move(board, computer_letter)
   make_move(board, computer_letter, move)
   if is_winner(board, computer_letter):
    draw_board(board)
    print('電腦成功,你掛了!')
    game_is_playing = False
   else:
    if is_board_full(board):
     draw_board(board)
     print('平手!')
     break
    else:
     turn = 'player'
 if not is_again():
   break

以上就是本文的具體內容,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved