日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行

發(fā)布時(shí)間:2023/12/4 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)不到“玩家1”的原因是你的支票中缺少一個(gè)空格。你也沒有正確地檢查一個(gè)玩家何時(shí)獲勝,這就是為什么你會(huì)有這種奇怪的行為。你需要檢查每個(gè)位置,而不僅僅是最后一個(gè)。我還添加了對(duì)用戶輸入的檢查,以確保用戶輸入的是一個(gè)數(shù)字,如果不是,它不會(huì)崩潰。

我完全不明白為什么你基本上把你的板打印顛倒,但我留下了原來的。

試試這個(gè):

import random

def display_board(board): # Board setup

print('\n'*100)

print(board[7] + '|' + board[8] + '|' + board[9])

print(board[4] + '|' + board[5] + '|' + board[6])

print(board[1] + '|' + board[2] + '|' + board[3])

def player_input(): # Player assignment - use while loop to keep asking until a valid character is entered

marker = ''

while marker != 'X' and marker != 'O':

marker = input('Player1: Choose X or O: ').upper()

if marker == 'X':

return ('X','O')

else:

return ('O','X')

def place_marker(board, marker, position): # Allows player to place their marker at specific board index

board[position] = marker

def win_check(board, mark): # Check all rows, columns, diagonals for sharing the same marker to check for a winner

return ((board[1] == mark and board[2] == mark and board[3] == mark) or # Rows

(board[4] == mark and board[5] == mark and board[6] == mark) or

(board[7] == mark and board[8] == mark and board[9] == mark) or

(board[1] == mark and board[4] == mark and board[7] == mark) or # Columns

(board[2] == mark and board[5] == mark and board[8] == mark) or

(board[3] == mark and board[6] == mark and board[9] == mark) or

(board[1] == mark and board[5] == mark and board[9] == mark) or # Diagonals

(board[3] == mark and board[5] == mark and board[7] == mark))

def choose_first(): # Randomize who goes first

flip = random.randint(0,1)

if flip == 0:

return 'Player 1'

else:

return 'Player 2'

def space_check(board, position): # Check to see if a space on the board is still available

return board[position] == ' '

def full_board_check(board): # Checks to see if board is full resulting in a draw

for i in range(1,10):

if space_check(board,i): # If there is free space, board is NOT full

return False

return True

def player_choice(board): # Asks for the player's next move choice

position = 0

while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):

try:

position = int(input('Choose a position: (1-9): '))

except:

print("Please enter a valid number.")

return position

def replay(): # Asks if the players want to play again

choice = input("Play again? Enter Yes or No: ")

return choice == 'Yes'

# Logic to run the game

# While loop needed to keep running the game

# Need to break out of the while loop on replay()

print ('Welcome to TIC TAC TOE')

while True:

# Game Setup (Board, Players, Player turns)

the_board = [' ']*10

player1_marker,player2_marker = player_input()

turn = choose_first()

print(turn + ' will go first')

play_game = input('Ready to play? y or n: ')

if play_game == 'y':

game_on = True

else: game_on = False

# Game Play

while game_on:

if turn == 'Player 1':

# Show the board

display_board(the_board)

# Choose a place to move

position = player_choice(the_board)

# Place the marker on the position

place_marker(the_board,player1_marker,position)

# Check if they won

if win_check(the_board,player1_marker):

display_board(the_board)

print('PLAYER 1 HAS WON!')

game_on = False

else:

if full_board_check(the_board): # Check for tie

display_board(the_board)

print("TIE GAME!")

game_on = False

else:

turn = 'Player 2'

else:

# Show the board

display_board(the_board)

# Choose a place to move

position = player_choice(the_board)

# Place the marker on the position

place_marker(the_board,player2_marker,position)

# Check if they won

if win_check(the_board,player2_marker):

display_board(the_board)

print('PLAYER 2 HAS WON!')

game_on = False

else:

if full_board_check(the_board):

display_board(the_board)

print("TIE GAME!")

game_on = False

else:

turn = 'Player 1'

if not replay():

break

總結(jié)

以上是生活随笔為你收集整理的python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。