if (sq1 + sq2 + sq3 == 0) { win0 = 1; win = 0; } else if (sq4 + sq5 + sq6 == 0) { win0 = 2; win = 0; } else if (sq7 + sq8 + sq9 == 0) { win0 = 3; win = 0; } else if (sq1 + sq4 + sq7 == 0) { win0 = 4; win = 0; } else if (sq2 + sq5 + sq8 == 0) { win0 = 5; win = 0; } else if (sq3 + sq6 + sq9 == 0) { win0 = 6; win = 0; } else if (sq1 + sq5 + sq9 == 0) { win0 = 7; win = 0; } else if (sq3 + sq5 + sq7 == 0) { win0 = 8; win = 0; } else if (sq1 + sq2 + sq3 == 3) { winx = 1; win = 1; } else if (sq4 + sq5 + sq6 == 3) { winx = 2; win = 1; } else if (sq7 + sq8 + sq9 == 3) { winx = 3; win = 1; } else if (sq1 + sq4 + sq7 == 3) { winx = 4; win = 1; } else if (sq2 + sq5 + sq8 == 3) { winx = 5; win = 1; } else if (sq3 + sq6 + sq9 == 3) { winx = 6; win = 1; } else if (sq1 + sq5 + sq9 == 3) { winx = 7; win = 1; } else if (sq3 + sq5 + sq7 == 3) { winx = 8; win = 1; } else if (sq1 + sq2 + sq3 + sq4 + sq5 + sq6 + sq7 + sq8 + sq9 == 5) { win = 2; } else { win = 99; } 使用 ActionScript 3.0优化
时间: 2024-04-21 14:25:54 浏览: 150
以下是使用 ActionScript 3.0 优化后的代码:
var wins:Array = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]
];
for (var i:int = 0; i < wins.length; i++) {
var sum:int = 0;
for (var j:int = 0; j < wins[i].length; j++) {
sum += this["sq" + wins[i][j]];
}
if (sum == 0) {
win0 = i + 1;
win = 0;
break;
} else if (sum == 3) {
winx = i + 1;
win = 1;
break;
}
}
if (win == null) {
win = (sq1 + sq2 + sq3 + sq4 + sq5 + sq6 + sq7 + sq8 + sq9 == 5) ? 2 : 99;
}
在 ActionScript 3.0 中,可以使用数组来存储所有可能的胜利组合。使用两个循环来遍历数组中的每个组合,并计算出方格的总和。如果总和为 0,则表示 O 获胜;如果总和为 3,则表示 X 获胜。使用变量 win0 和 winx 来存储胜利组合的索引,使用变量 win 来存储胜利者的标志 (0 表示 O 获胜,1 表示 X 获胜,2 表示平局,99 表示无胜者)。如果没有胜利者,则判断是否为平局。
阅读全文