Python程式碼 | 井字棋

IronRoc發表於2024-07-14
import random

checkerboard = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]
]
i = 0
print(f"歡迎來玩井子棋,請在輪到你落子時輸入你要下的位置,第x行,第y列,用逗號隔開,例如:1,2")


def print_board():
    for row in checkerboard:
        for col in row:
            if col == 0:
                print("| ", end="")
            if col == 1:
                print("|○", end="")
            if col == -1:
                print("|×", end="")
        print("|")
    print()


def is_over():
    for row in checkerboard:
        if sum(row) == 3:
            print("你贏了")
            return True
        if sum(row) == -3:
            print("你輸了")
            return True

    for y in range(3):
        if checkerboard[0][y] + checkerboard[1][y] + checkerboard[2][y] == 3:
            print("你贏了")
            return True
        if checkerboard[0][y] + checkerboard[1][y] + checkerboard[2][y] == -3:
            print("你輸了")
            return True

    if checkerboard[0][0] + checkerboard[1][1] + checkerboard[2][2] == 3:
        print("你贏了")
        return True
    if checkerboard[0][0] + checkerboard[1][1] + checkerboard[2][2] == -3:
        print("你輸了")
        return True
    if checkerboard[0][2] + checkerboard[1][1] + checkerboard[2][0] == 3:
        print("你贏了")
        return True
    if checkerboard[0][2] + checkerboard[1][1] + checkerboard[2][0] == -3:
        print("你輸了")
        return True
    if i == 9:
        print("平局")
        return True


# 電腦為後手,故勝利第一,防守第二,進攻第三
def computer_turn():
    # 勝利
    for x in range(3):
        if sum(checkerboard[x]) == -2:
            for y in range(3):
                if checkerboard[x][y] == 0:
                    checkerboard[x][y] = -1
                    return 0
    for y in range(3):
        if checkerboard[0][y] + checkerboard[1][y] + checkerboard[2][y] == -2:
            for x in range(3):
                if checkerboard[x][y] == 0:
                    checkerboard[x][y] = -1
                    return 0
    if checkerboard[0][0] + checkerboard[1][1] + checkerboard[2][2] == -2:
        for j in range(3):
            if checkerboard[j][j] == 0:
                checkerboard[j][j] = -1
                return 0
    if checkerboard[0][2] + checkerboard[1][1] + checkerboard[2][0] == -2:
        for j in range(3):
            if checkerboard[j][2 - j] == 0:
                checkerboard[j][2 - j] = -1
                return 0

    # 防守
    for x in range(3):
        if sum(checkerboard[x]) == 2:
            for y in range(3):
                if checkerboard[x][y] == 0:
                    checkerboard[x][y] = -1
                    return 0
    for y in range(3):
        if checkerboard[0][y] + checkerboard[1][y] + checkerboard[2][y] == 2:
            for x in range(3):
                if checkerboard[x][y] == 0:
                    checkerboard[x][y] = -1
                    return 0
    if checkerboard[0][0] + checkerboard[1][1] + checkerboard[2][2] == 2:
        for j in range(3):
            if checkerboard[j][j] == 0:
                checkerboard[j][j] = -1
                return 0
    if checkerboard[0][2] + checkerboard[1][1] + checkerboard[2][0] == 2:
        for j in range(3):
            if checkerboard[j][2 - j] == 0:
                checkerboard[j][2 - j] = -1
                return 0

    # 進攻的寫法比較隨機,是賣給玩家唯一的破綻
    vacancies = []
    for x in range(3):
        for y in range(3):
            if checkerboard[x][y] == 0:
                vacancies.append((x, y))
    x, y = vacancies[random.randint(0, len(vacancies))]
    checkerboard[x][y] = -1


print_board()
while True:
    while True:
        x, y = map(int, input(f"輪到你落子了,選擇你要下的位置:").split(","))
        if x > 3 or y > 3:
            print("超出棋盤範圍,請重新輸入")
            continue
        if checkerboard[x - 1][y - 1] != 0:
            print("該位置已有棋子,請重新輸入")
            continue
        break
    checkerboard[x - 1][y - 1] = 1
    i += 1
    print("")
    print(f"第{i}子:")
    print_board()
    if is_over():
        break

    computer_turn()
    i += 1
    print("")
    print(f"第{i}子:")
    print_board()
    if is_over():
        break

  

相關文章