function nextMove($n, $x, $y, $grid)
{
    foreach ($grid as $y => $fila) {
        foreach (str_split($fila) as $x => $celda) {
            if ($celda != '-') {
                $t[$celda] = array($x, $y);
            }
        }
    }
    $move_x = $t['m'][0] - $t['p'][0];
    $move_y = $t['m'][1] - $t['p'][1];
    if (abs($move_x) > 0) {
        echo $move_x > 0 ? 'LEFT' : 'RIGHT';
    } else {
        if (abs($move_y) > 0) {
            echo $move_y > 0 ? 'UP' : 'DOWN';
        }
    }
}
///// HackerRank:
$fp = fopen("php://stdin", "r");
fscanf($fp, "%d", $n);
$pos = fgets($fp);
$pos = split(' ', $pos);
$grid = array();
for ($i = 0; $i < $n; $i++) {
    fscanf($fp, "%s", $grid[$i]);
}
nextMove($n, $pos[0], $pos[1], $grid);
        return '1 1';
    }
    if ($turn == 3 and $g['11'] == $player) {
        if ($g['00'] and $g['22'] or $g['02'] and $g['20']) {
            return '0 1';
        }
    }
    // Capture Corners
    foreach (array('22', '00', '20', '02') as $xy) {
        if (!$g[$xy]) {
            $xy = str_split($xy);
            return $xy[0] . ' ' . $xy[1];
        }
    }
    // Capture Lateral
    foreach (array('21', '10', '12', '01') as $xy) {
        if (!$g[$xy]) {
            $xy = str_split($xy);
            return $xy[0] . ' ' . $xy[1];
        }
    }
}
///// HackerRank:
$fp = fopen("php://stdin", "r");
fscanf($fp, "%s", $player);
$board = array();
for ($i = 0; $i < 3; $i++) {
    fscanf($fp, "%s", $board[$i]);
}
echo nextMove($player, $board);
                    if ($_x > $x) {
                        $a = 'LEFT';
                    } else {
                        if ($_y < $y) {
                            $a = 'DOWN';
                        } else {
                            if ($_y > $y) {
                                $a = 'UP';
                            } else {
                                $a = 'CLEAN';
                            }
                        }
                    }
                }
                $dirt[abs($x - $_x) + abs($y - $_y)] = $a;
            }
        }
    }
    ksort($dirt);
    echo current($dirt);
}
///// HackerRank:
$fp = fopen("php://stdin", "r");
$temp = fgets($fp);
$position = split(' ', $temp);
$board = array();
for ($i = 0; $i < 5; $i++) {
    fscanf($fp, "%s", $board[$i]);
}
nextMove($position[0], $position[1], $board);
예제 #4
0
        if ($board[1][1] == $player && $board[2][0] == $player) {
            return true;
        }
    } elseif ($board[1][0] == $player) {
        if ($board[1][1] == $player && $board[1][2] == $player) {
            return true;
        }
    } elseif ($board[2][0] == $player) {
        if ($board[2][1] == $player && $board[2][2] == $player) {
            return true;
        }
    }
    return false;
}
function nextMove($board, $player, $move = 1)
{
    var_dump(isWin($board, $player));
}
$fp = fopen("php://stdin", "r");
//If player is X, I'm the first player.
//If player is O, I'm the second player.
fscanf($fp, "%s", $player);
//Read the board now. The board is a 3x3 array filled with X, O or _.
$board = array();
for ($i = 0; $i < 3; $i++) {
    fscanf($fp, "%s", $board[$i]);
    $board[$i] = str_split($board[$i]);
}
$move = witchMove($board);
nextMove($board, $player, $move);