Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 12, 2020 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/abd9aad53d6d18b133b549297f57ded7 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/abd9aad53d6d18b133b549297f57ded7 to your computer and use it in GitHub Desktop.
Just PHP FUN 101.
<?php
# https://www.codewars.com/kata/525caa5c1bf619d28c000335 Tic-Tac-Toe Checker.
function is_solved(array $board): int {
$x = implode(array_map('implode', $board));
$row = '/^((1{3}.{6})|(.{3}1{3}.{3})|(.{6}1{3}))$/';
$column = '/^((1..){3}|(.1.){3}|(..1){3})$/';
$diagonal = '/^((1..)(.1.)(..1))|((..1)(.1.)(1..))$/';
if(preg_match($row,$x)||preg_match($column,$x)||preg_match($diagonal,$x)) return 1;
list($row,$column,$diagonal) = preg_replace('/1/','2',[$row,$column,$diagonal]);
if(preg_match($row,$x)||preg_match($column,$x)||preg_match($diagonal,$x)) return 2;
if(preg_match('/0/',$x)) return -1;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment