bol_in 2022-01-10 21:43 采纳率: 64.6%
浏览 86
已结题

Python-OX遊戲



Sample Output 12OK
0 1 2
1 0 0
2 1 0
Undecided
5

Sample Input 132
5 2 1 4

Sample Output 13OK
2 1 0
1 2 0
0 0 0
Undecided
9

Sample Input 141
7 5 8

Sample Output 14OK
0 0 0
0 2 0
1 1 0
Undecided
9

Sample Input 152
5 3 7 9

Sample Output 15OK
0 0 1
0 2 0
2 0 1
Undecided
6

Sample Input 161
3 4 7

Sample Output 16OK
0 0 1
2 0 0
1 0 0
Undecided
5

Sample Input 172
2 5 4 9

Sample Output 17OK
0 2 0
0 1 2
0 0 1
Undecided
1


  • 写回答

5条回答 默认 最新

  • soar3033 2022-01-11 09:47
    关注

    img

     
    class test():
        def __init__(self):
            self.m=0
            self.s=[]
            self.err=0
            self.t=[0 for i in range(9)]
            self.win=0
            self.best=0
        def read(self):
            t=int(input(""))
            self.m=t
            t=input("")
            self.s=t.split()
            self.s=[int(i) for i in self.s]
        def cal(self):
            for i in range(len(self.s)):
                if self.t[self.s[i]-1]==0:
                    if self.m==1:
                        self.t[self.s[i]-1]=i%2+1
                    else:
                        self.t[self.s[i]-1]=2-i%2
                else:
                    self.err=1
                    break
            self.win=self.judge()
            flag=0
            for i in range(9):
                if self.t[i]==0:
                    flag=1
            if flag==1 and self.win==0:
                print(1)
                self.win=3
                for i in range(9):
                    if self.t[i]==0:
                        self.t[i]=2
                        if(self.judge()==2):
                            self.best=(i+1)
                        self.t[i]=0
                if self.best==0:
                    self.best=1
        def judge(self):
            win=0
            for i in range(3):
                if self.t[i*3]==1 and self.t[i*3+1]==1 and self.t[i*3+2]==1:
                    win=1
                if self.t[i]==1 and self.t[3+i]==1 and self.t[6+i]==1:
                    win=1
            for i in range(3):
                if self.t[i*3]==2 and self.t[i*3+1]==2 and self.t[i*3+2]==2:
                    win=2
                if self.t[i]==2 and self.t[3+i]==2 and self.t[6+i]==2:
                    win=2
            if self.t[0]==2 and self.t[4]==2 and self.t[8]==2:
                win=2
            if self.t[2]==2 and self.t[4]==2 and self.t[6]==2:
                win=2
            if self.t[0]==1 and self.t[4]==1 and self.t[8]==1:
                win=1
            if self.t[2]==1 and self.t[4]==1 and self.t[6]==1:
                win=1
            return win
        def draw(self):
            if self.err==0:
                print('OK')
            else:
                print('Error')
            for i in range(3):
                print(self.t[i*3],self.t[i*3+1],self.t[i*3+2])
                    
            if self.win==1:
                print('Player win')
            if self.win==2:
                print('Computer win')
            if self.win==0:
                print('Tie')
            if self.win==3:
                print('Undecided')
                print(self.best)
    a=test()
    a.read()
    a.cal()
    a.draw()
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 1月25日
  • 已采纳回答 1月17日
  • 修改了问题 1月17日
  • 创建了问题 1月10日

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络