0

Hello,

Im having a spot of bother with the below function.Its been a year or two since I looked at any php so I thought I would try and make a noughts and crosses game to refreash my memory while I was off over Christmas "just for fun" and Ive hit a block in the road.

The array holds the either a 0 or a x so I am testing for three in a line. I thought maybe it was because I was testing to arrays against each other but I tested $array_cont_val[1][1] == $x as well with no joy.

Any help would be greatly appriated

function who_won($array_cont_val){

$x = 23;

    if($array_cont_val[1][1] == $array_cont_val[1][2] && $array_cont_val[1][3] == $array_cont_val[1][2]){
        $x = "wins";
            } elseif ($array_cont_val[2][1] == $array_cont_val[2][2] && $array_cont_val[2][3]) == $array_cont_val[2][2] {
            $x = "wins";
            }elseif($array_cont_val[3][1] == $array_cont_val[3][2] && $array_cont_val[3][3] == $array_cont_val[3][2]){
            $x = "wins";
                }elseif($array_cont_val[1][1] == $array_cont_val[2][1] && $array_cont_val[3][1] == $array_cont_val[2][1]){
                $x = "wins";
                    }elseif($array_cont_val[1][2] == $array_cont_val[2][2] && $array_cont_val[3][2]) == $array_cont_val[2][2] {
                    $x = "wins";
                        }elseif{($array_cont_val[1][3] == $array_cont_val[2][3] && $array_cont_val[3][3] == $array_cont_val[2][3]){
                        $x = "wins";
                            }elseif{($array_cont_val[1][1] == $array_cont_val[2][2] && $array_cont_val[3][3] == $array_cont_val[2][2]){
                            $x = "wins";
                        }
}
                }
            }
2
Contributors
2
Replies
11
Views
2 Years
Discussion Span
Last Post by diafol
0

You could try this:

function array_check($array, $start=0)
{
    $doubles = array();
    $stop = $start + count($array) - 1;
    for($x = $start;$x<=$stop;$x++)
    {
        $doubles[] = $array[$x][$x];
        $firststatic = array();
        $secstatic = array();
        for($y = $start;$y<=$stop;$y++)
        {
            $firststatic[] = $array[$x][$y];
            $secstatic[] = $array[$y][$x];
        }
        if(count(array_unique($firststatic)) == 1 || count(array_unique($secstatic)) == 1) return "win";
    }
    if(count(array_unique($doubles)) == 1) return "win";
    return "lose";
}

$array = array(1=>array(1=>3,6,8),2=>array(1=>4,1,1),3=>array(1=>1,7,8));

echo array_check($array,1);

Should be scalable for 2d array with same number of elements e.g. array[6][6]

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.