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


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()
 

能发简体麽

能发中文简体吗?!

尽量自己先学习一下,然后再发问题

插眼,学习