我有一个关联数组,我按升序排序,现在我想对这些值做出一些决定。我不明白我该怎么办。
让我解释一下我想要实现的目标:
假设:我有以下原始数组:
Array
(
[0] => Array
(
[cell] => b2
[sign] => ✘
)
[1] => Array
(
[cell] => a1
[sign] => ◯
)
)
然后我把它分类为:
Array
(
[0] => Array
(
[cell] => a1
[sign] => ◯
)
[1] => Array
(
[cell] => b2
[sign] => ✘
)
)
现在我想做出决定,例如细胞a1 = b1 = c1
或a1 = b2 = c3
等的值。现在我该怎么做?
答案 0 :(得分:1)
猜测你的方格只有 3x3 ,我想说最简单的方法是将获胜的匹配存储在一个数组中,然后使用array_intersect()
比较两个数组来比较它们并返回数组中的重复值。要获得重复数量,只需打包count()
函数并比较玩家的一个或两个值是否与$winArr
匹配。显然,通过停止输入数组的重复值仍然可以做到,在每次输入后循环foreach()
等等......但至少你的逻辑是正确的。
$winner; //Winner --> Used when game finished
$winArr = [
['c1','c2','c3'],
['b1','b2','b3'], // --> Vertical Win
['a1','a2','a3'],
['a1','b2','c3'],
['c1','b2','c3'], // --> Diagonal Win
['a1','b1','c1'],
['a2','b2','c2'], // --> Horizontal Win
['a3','b3','c3'],
];
$gameArr = [ // --> `Input Values Array = Dimension for P1 & P2 for ease
'playerOne' => ['a1', 'b1', 'c2', 'c1'],
'playerTwo' => ['a3', 'b3', 'a2'],
];
foreach ($winArr as $k=>$v) {
if (count(array_intersect($v, $gameArr['playerOne'])) === 3) {
$winner = 'Player One!';
break;
} elseif(count(array_intersect($v, $gameArr['playerTwo'])) === 3) {
$winner = 'Player Two!';
break;
}
}
if (isset($winner)) {
echo $winner;
}
输出
Player One!
请参阅: http://www.php.net/manual/en/function.array-intersect.php
答案 1 :(得分:1)
我建议用零基索引来指代细胞的位置。它可以很容易地使用数学技巧,如模数或整数除法来选择行或列。否则,您必须使用大量偏移来避免一个一个错误。
IE,董事会可以贴上标签:
0 1 2
3 4 5
6 7 8
游戏状态可以用字符数组表示,字符' - '表示空,x和o表示x和o在那里播放。
这是一个人类游戏视图,游戏存储在一个字符串数组中:
x
o x o
o x
var state = "-x-oxoox-";
要检查游戏是否已获胜,请发挥功能。
如果没有人赢,该函数返回' - ',如果赢了则返回'x',如果赢了则返回'o'。
我将启动一个硬编码每个路径进行检查的实现。然后我将展示如何生成要检查的路径。
function WhoIsWinningHardCode(state) {
var rows = IsWin(state, [[0,1,2],[3,4,5],[6,7,8]]);
var cols = IsWin(state, [[0,3,6],[1,4,7],[2,5,8]]);
var diagUp = IsWin(state, [[6,4,2]]);
var diagDown = IsWin(state, [[0,4,8]]);
if (rows !== '-') return rows;
if (cols !== '-') return cols;
if (diagUp !== '-') return diagUp;
return diagDown;
}
function WhoIsWinningGeneratePaths(state) {
var rows = IsWin(state, GenPaths(3, 0, 1, 3));
var cols = IsWin(state, GenPaths(3, 0, 3, 1));
var diagUp = IsWin(state, GenPaths(1, 2, 2, 0));
var diagDown = IsWin(state, GenPaths(1, 0, 4, 0));
if (rows !== '-') return rows;
if (cols !== '-') return cols;
if (diagUp !== '-') return diagUp;
return diagDown;
}
function GenPaths(count, start, incrementA, incrementB) {
var paths = [];
for (var i = 0; i < count; i++) {
var path = [];
for (var j = 0; j < 3; j++) {
path.push(start + i*incrementB + j*incrementA)
}
paths.push(path);
}
return paths;
}
function IsWin(state, paths) {
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
var a = state[path[0]];
var b = state[path[1]];
var c = state[path[2]];
if (a !== '-')
if (a == b)
if (a == c)
return a;
}
return '-';
}
答案 2 :(得分:1)
这是Javascript中更通用的实现。它仅在2x2和3x3游戏状态下进行测试,可能需要针对其他游戏尺寸进行调试:
// xxx
// oo-
// o--
alert("game a: " + WhoIsWinningGeneratePaths('xxxoo-o--'));
// x-x
// oo-
// o--
alert("game b: " + WhoIsWinningGeneratePaths('x-xoo-o--'));
// x-x
// ooo
// ---
alert("game c: " + WhoIsWinningGeneratePaths('x-xooo---'));
// x-
// oo
alert("game d: " + WhoIsWinningGeneratePaths('x-oo'));
// x-
// ox
alert("game e: " + WhoIsWinningGeneratePaths('x-ox'));
// x-
// o-
alert("game f: " + WhoIsWinningGeneratePaths('x-o-'));
function WhoIsWinningGeneratePaths(state) {
var n = Math.sqrt(state.length);
var rows = IsWin(state, GenPaths(n, 0, 1, n, n));
var cols = IsWin(state, GenPaths(n, 0, n, 1, n));
var diUp = IsWin(state, GenPaths(1, n-1, n-1, 0, n));
var diDn = IsWin(state, GenPaths(1, 0, n+1, 0, n));
if (rows !== '-') return rows;
if (cols !== '-') return cols;
if (diUp !== '-') return diUp;
return diDn;
}
function GenPaths(count, start, incrementA, incrementB, lengthToWin) {
var paths = [];
for (var i = 0; i < count; i++) {
var path = [];
for (var j = 0; j < lengthToWin; j++) {
path.push(start + i * incrementB + j * incrementA)
}
paths.push(path);
}
return paths;
}
function IsWin(state, paths) {
for (var i = 0; i < paths.length; i++) {
var currentPathResult = IsPathWin(state, paths[i]);
if (currentPathResult != '-')
return currentPathResult;
}
return '-';
}
function IsPathWin(state, path) {
var first = state[path[0]];
for (var j = 1; j < path.length; j++)
{
var compareToFirst = state[path[j]];
if (compareToFirst !== first)
return '-';
}
return first;
}
&#13;