fork download
  1. def counter(string):
  2. x=string.count('X')
  3. o=string.count('O')
  4. d=string.count('.')
  5. t=string.count('T')
  6. return (x,o,t,d)
  7.  
  8. def win(tupa):
  9. if((tupa[0]==3 and tupa[2]==1) or tupa[0]==4):
  10. print 'Case #'+str(i)+': X won'
  11. return True
  12. elif((tupa[1]==3 and tupa[2]==1)or tupa[1] ==4):
  13. print 'Case #'+str(i)+': O won'
  14. return True
  15. return False
  16.  
  17. t=input()
  18. for i in range(1,t+1):
  19. a=[[],[],[],[]]
  20. status=True
  21. flag=False
  22. a[0]=raw_input()
  23. if(len(a[0])==0):
  24. a[0]=raw_input()
  25. a[1]=raw_input()
  26. a[2]=raw_input()
  27. a[3]=raw_input()
  28.  
  29. for p in range(0,4): #horizontal case
  30. tup=counter(a[p])
  31. if(tup[3]>0):
  32. flag=True
  33. if(win(tup)):
  34. status=False
  35. break
  36. if(not status):
  37. continue
  38.  
  39. for p in range(0,4):
  40. tup=counter(a[0][p]+a[1][p]+a[2][p]+a[3][p])
  41. if(tup[3]>0):
  42. flag=True
  43. if( win(counter(a[0][p]+a[1][p]+a[2][p]+a[3][p]))):
  44. status=False
  45. break
  46. if(not status):
  47. continue
  48.  
  49. if(win(counter(a[0][0]+a[1][1]+a[2][2]+a[3][3]))): #diagonal
  50. status=False
  51. continue
  52. if(win(counter(a[3][0]+a[2][1]+a[1][2]+a[0][3]))):
  53. status=False
  54. continue
  55.  
  56. if(status and flag):
  57. print 'Case #'+str(i)+': Game not completed'
  58. continue
  59. print 'Case #'+str(i)+': Draw'
Success #stdin #stdout 0.08s 8840KB
stdin
6
X X X T
. . . .
O O . .
. . . .

X O X T
X X O O
O X O X
X X O O

X O X .
O X . .
. . . .
. . . .

O O X X
O X X X
O X . T
O . . O

X X X O
. . O .
. O . .
T . . .

O X X X
X O . .
. . O .
. . . O
stdout
Case #1: X won
Case #2: Draw
Case #3: Game not completed
Case #4: O won
Case #5: Game not completed
Case #6: Game not completed