Hướng dẫn how to print a tic tac toe board in python - cách in bảng tic tac toe trong python

Bạn có thể thử điều này: Tìm mã Python bên dưới cho trò chơi bảng Tic-Tac-Toe tương tác từ đầu đến cuối. Mã trông dài có thể được tối ưu hóa, nhưng nó hoạt động hoàn hảo như trò chơi bảng Tic-Tac-Toe tương tác.

#Function code to clear the output space [screen]
 
 from IPython.display import clear_output

#code to display just board-

def ttt_borad[board]:
cl = clear_output[]
print['Your Tic-Tac-Toe board now:\n']
print[board[1] + "|" + board[2] + "|" + board[3]]
print["________"]
print[board[4] + "|" + board[5] + "|" + board[6]]
print["________"]
print[board[7] + "|" + board[8] + "|" + board[9]]

#function code to accept player key choices-

def player_key[]:
player_choice = ''
play1 = ''
play2 = ''
while player_choice not in ['Y', 'N']:
    player_choice = input["Player-1 would like to go first ? Enter Y/N: "]
    player_choice = player_choice.upper[]
    if player_choice not in ['Y', 'N']:
        print["Invalid Key"]
    else:
        pass
if player_choice == 'Y':

    while play1 not in ['X', 'O']:
        play1 = input["Select your Key for Player-1 X or O: "]
        play1 = play1.upper[]
        if play1 not in ['X', 'O']:
            print["Invalid Key"]
        else:
            pass
else:
    while play2 not in ['X', 'O']:
        play2 = input["Select your Key for Player-2 X or O: "]
        play2 = play2.upper[]
        if play2 not in ['X', 'O']:
            print["Invalid Key"]
        else:
            pass

if play1 == 'X':
    play2 = 'O'
elif play1 == 'O':
    play2 = 'X'
elif play2 == 'X':
    play1 = 'O'
elif play2 == 'O':
    play1 = 'X'

print[f'Key for Player-1 is: {play1} and Key for Player-2 is: {play2}']
return play1, play2



#function code to accept key strokes to play game

def enter_key[key, bp]:
play1, play2 = key
ind = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
i = 1
while i < 10:
    j = 0
    k = 0
    print[f'Game Move: {i}']
    while j not in ind:
        j = input["Player-1: Select position [1-9] for your Move: "]
        if j not in ind:
            print["Invalid Key or Position already marked"]
        else:
            pass
    x = ind.index[j]
    ind.pop[x]
    j = int[j]
    bp[j] = play1
    ttt_borad[bp]
    i = i + 1
    tf = game_winner[key, bp]
    if tf == 1:
        print["The Winner is: Player-1 !!"]
        break
    print[f'Game Move: {i}']
    if i == 10:
        break

    while k not in ind:
        k = input["Player-2: Select position [1-9] for your Move: "]
        if k not in ind:
            print["Invalid Key or Position already marked"]
        else:
            pass
    y = ind.index[k]
    ind.pop[y]
    k = int[k]
    bp[k] = play2
    ttt_borad[bp]
    i = i + 1
    ft = game_winner[key, bp]
    if ft == 2:
        print["The Winner is: Player-2 !!"]
        break
return bp

#function code to calculate and display winner of the game-
def game_winner[key, game]:
p1, p2 = key
p = 0
if game[1] == game[2] == game[3] == p1:
    p = 1
    return p
elif game[1] == game[4] == game[7] == p1:
    p = 1
    return p
elif game[1] == game[5] == game[9] == p1:
    p = 1
    return p
elif game[2] == game[5] == game[8] == p1:
    p = 1
    return p
elif game[3] == game[6] == game[9] == p1:
    p = 1
    return p
elif game[4] == game[5] == game[6] == p1:
    p = 1
    return p
elif game[3] == game[5] == game[7] == p1:
    p = 1
    return p
elif game[1] == game[2] == game[3] == p2:
    p = 2
    return p
elif game[1] == game[4] == game[7] == p2:
    p = 2
    return p
elif game[1] == game[5] == game[9] == p2:
    p = 2
    return p
elif game[2] == game[5] == game[8] == p2:
    p = 2
    return p
elif game[3] == game[6] == game[9] == p2:
    p = 2
    return p
elif game[4] == game[5] == game[6] == p2:
    p = 2
    return p
elif game[3] == game[5] == game[7] == p2:
    p = 2
    return p
else:
    p = 3
    return p

#Function code to call all functions in order to start and play game-

def game_play[]:
clear_output[]
entry = ['M', '1', '2', '3', '4', '5', '6', '7', '8', '9']
ttt_borad[entry]
plk = player_key[]
new_board = enter_key[plk, entry]
tie = game_winner[plk, new_board]
if tie == 3:
    print["Game Tie !!! :-[ "]

print['Would you like to play again? ']
pa = input["Enter Y to continue OR Enter any other key to exit game: "]
pa = pa.upper[]
if pa == 'Y':
    game_play[]
else:
    pass

game_play[]

#Try toàn bộ mã này trong bất kỳ trình soạn thảo Python3 nào và cho tôi biết phản hồi của bạn. Tôi đã đính kèm bảng mẫu cách mã hiển thị. Mample tic-tac-toe board bằng mã

Cảm ơn

# Pam qian 2016 Fall CS 112 Python Midterm Project II # tic tack toe defMain []: # functionIntruction chính = intro [] board = created_grid [] đẹp = printpretty [board] Symar , Symbol_1, Symbol_2] # Hàm bắt đầu trò chơi cũng có ở đây.DefIntro []: # Hàm này giới thiệu các quy tắc của trò chơi tic tac toeprint ["Xin chào! \ n "] in [" Quy tắc: Người chơi 1 và Người chơi 2, được biểu thị bằng X và O, thay phiên nhau "" Đánh dấu các khoảng trắng trong lưới 3*3. Người chơi thành công trong việc đặt "ba điểm của chúng theo chiều ngang , hàng dọc hoặc đường chéo thắng. "] In [" \ n "] đầu vào [" Nhấn Enter để tiếp tục. "] Playboard: "] board = [[" "," "," "], [" "," "," "], [" "," "," "]] 'Symbolssymbol_1 = Input ["Người chơi 1, bạn có muốn trở thành X hay O không?"] ifsymbol_1 == "x": symbol_2 = "o" print ["player 2, bạn là o."] ". = 1: Player = Symbol_2Print ["Player"+Player+", đến lượt bạn. "] hàng = int [input [" chọn một hàng: "" [hàng trên: nhập 0, hàng giữa: nhập 1, hàng dưới cùng: nhập 2]: "]] cột = int [input [" chọn một cột: " "[Cột trái: Nhập 0, Cột giữa: Nhập 1, Cột bên phải Nhập 2]"]] # Tic Tack Toe def main[]: # The main function introduction = intro[] board = create_grid[] pretty = printPretty[board] symbol_1, symbol_2 = sym[] full = isFull[board, symbol_1, symbol_2] # The function that starts the game is also in here. def intro[]: # This function introduces the rules of the game Tic Tac Toe print["Hello! Welcome to Pam's Tic Tac Toe game!"] print["\n"] print["Rules: Player 1 and player 2, represented by X and O, take turns " "marking the spaces in a 3*3 grid. The player who succeeds in placing " "three of their marks in a horizontal, vertical, or diagonal row wins."] print["\n"] input["Press enter to continue."] print["\n"] def create_grid[]: # This function creates a blank playboard print["Here is the playboard: "] board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]] return board def sym[]: # This function decides the players' symbols symbol_1 = input["Player 1, do you want to be X or O? "] if symbol_1 == "X": symbol_2 = "O" print["Player 2, you are O. "] else: symbol_2 = "X" print["Player 2, you are X. "] input["Press enter to continue."] print["\n"] return [symbol_1, symbol_2] def startGamming[board, symbol_1, symbol_2, count]: # This function starts the game. # Decides the turn if count % 2 == 0: player = symbol_1 elif count % 2 == 1: player = symbol_2 print["Player "+ player + ", it is your turn. "] row = int[input["Pick a row:" "[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:"]] column = int[input["Pick a column:" "[left column: enter 0, middle column: enter 1, right column enter 2]"]] # Check if players' selection is out of range while [row > 2 or row 2 or column

Bài Viết Liên Quan

Chủ Đề