Improved the calculation of winning condition