服务器之家

服务器之家 > 正文

python实现简单的井字棋

时间:2021-11-13 10:47     来源/作者:哈尔的心

本文实例为大家分享了python实现简单的井字棋的具体代码,供大家参考,具体内容如下

使用python实现井字棋游戏,没有具体算法,只是用随机下棋简单实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import random
board = [['+','+','+'],['+','+','+'],['+','+','+']]
def ma(board):
    if isempty(board):
        a = random.randint(0, 2)
        b = random.randint(0, 2)
        if board[a][b] != 'X' and board[a][b] != 'O':
            print("机器走:")
            board[a][b] = 'O'
            oput(board)
        else:
            ma(board)
    else:
        print("平局")
def oput(board):
    print("  0 1 2")
    for i in range(3):
        print(i, end=' ')
        for j in range(3):
            print(board[i][j], end=" ")
        print("")
def winput(i,j):
    if board[i][j] == 'X':
        print("human win")
    else:
        print("machine win")
    return 1
 
def test(board):
    for i in range(3):
        for j in range(3):
            if board[i][j] != '+':
                if j == 0:
                    if board[i][j] == board[i][j + 1] == board[i][j + 2]:
                        return winput(i,j)
                if i == 0:
                    if board[i][j] == board[i + 1][j] == board[i + 2][j]:
                        return winput(i,j)
                if i == 0 and j == 0:
                    if board[i][j] == board[i + 1][j + 1] == board[i + 2][j + 2]:
                        return winput(i,j)
                if i == 2 and j == 0:
                    if board[i][j] == board[i - 1][j + 1] == board[i - 2][j + 2]:
                        return winput(i,j)
 
def isempty(board):
    for i in range(3):
        for j in range(3):
            if board[i][j] == '+':
                return True
    return False
def main():
    print("初始棋盘:")
    oput(board)
    flag = 0
    t = input("human first? Y/N  human for X, machine for O\n")
    if t == 'Y':
        while isempty(board):
            print("人走:  ")
            a, b = map(int, input("输入落子纵横坐标: a,b \n").split(','))
            if board[a][b] == '+':
               board[a][b] = 'X'
               oput(board)
               flag = test(board)
               if flag == 1:
                  break
            else:
                print("落子位置不对")
                continue
            ma(board)
            flag = test(board)
            if flag == 1:
                break
            if isempty(board) == 0 and flag == 0:
                print("平局")
                break
    elif t == 'N':
        while isempty(board):
            ma(board)
            flag = test(board)
            if isempty(board) == 0 and flag == 0:
                print("平局")
                break
            if flag == 1:
                break
            print("人走:  ")
            a, b = map(int, input("输入落子纵横坐标: a,b \n").split(','))
            if board[a][b] == '+':
                board[a][b] = 'X'
                oput(board)
                flag = test(board)
                if flag == 1:
                    break
            else:
                print("落子位置不对")
                continue
 
if __name__ == "__main__":
    main()

结果:

python实现简单的井字棋

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43871677/article/details/117234869

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部