Last active
July 6, 2021 04:15
-
-
Save m1ch3lp3r3z/8d5b9ffa19157f36ea9ce2bedc6088f0 to your computer and use it in GitHub Desktop.
Tic Tac Toe Attempt 1 h 5 minutes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TicTacToe | |
{ | |
const VALUE_X = "x"; | |
const VALUE_O = "o"; | |
protected $size; | |
protected $board; | |
protected $winner; | |
public function __construct($size) | |
{ | |
$this->size = $size; | |
for ($i = 0; $i < $this->size; $i++) { | |
$this->board[$i] = array_fill(0, $this->size, null); | |
} | |
} | |
protected function checkColumn($i) | |
{ | |
$value = $this->board[$i][0]; | |
if (!is_null($value)) { | |
for ($j = 1; $j < $this->size; $j++) { | |
if ($value !== $this->board[$i][$j]) { | |
return null; | |
} | |
} | |
} | |
return $value; | |
} | |
protected function checkRow($j) | |
{ | |
$value = $this->board[0][$j]; | |
if (!is_null($value)) { | |
for ($i = 1; $i < $this->size; $i++) { | |
if ($value !== $this->board[$i][$j]) { | |
return null; | |
} | |
} | |
} | |
return $value; | |
} | |
protected function checkDiagonal() | |
{ | |
$value = $this->board[0][0]; | |
if (!is_null($value)) { | |
for ($i = 1; $i < $this->size; $i++) { | |
if ($value !== $this->board[$i][$i]) { | |
return null; | |
} | |
} | |
} | |
return $value; | |
} | |
protected function checkBackDiagonal() | |
{ | |
$value = $this->board[$this->size - 1][0]; | |
if (!is_null($value)) { | |
for ($i = $this->size - 1; $i <= 0; $i--) { | |
if ($value !== $this->board[$i][$this->size - $i]) { | |
return null; | |
} | |
} | |
} | |
return $value; | |
} | |
public function check() | |
{ | |
// columns | |
for ($i = 0; $i < $this->size; $i++) { | |
if ($value = $this->checkColumn($i)) { | |
return $value; | |
} | |
} | |
// rows | |
for ($j = 0; $j < $this->size; $j++) { | |
if ($value = $this->checkRow($j)) { | |
return $value; | |
} | |
} | |
// diagonals | |
if ($value = $this->checkDiagonal()) { | |
return $value; | |
} | |
if ($value = $this->checkBackDiagonal()) { | |
return $value; | |
} | |
return null; | |
} | |
protected function won($value) | |
{ | |
$this->winner = $value; | |
print "Winner is {$this->winner} \n"; | |
} | |
public function play($i, $j, $value) | |
{ | |
if ($this->winner) { | |
print "{$this->winner} already won \n"; | |
return; | |
} | |
$this->board[$i][$j] = $value; | |
$this->printBoard(); | |
$value = $this->check(); | |
if (!is_null($value)) { | |
$this->won($value); | |
} | |
} | |
protected function printBoard() | |
{ | |
for ($j = 0; $j < $this->size; $j++) { | |
// print("--- \n"); | |
for ($i = 0; $i < $this->size; $i++) { | |
$value = $this->board[$i][$j] ?? " "; | |
print(" $value |"); | |
} | |
print("\n"); | |
} | |
print(" ------------ \n"); | |
} | |
} | |
$game = new TicTacToe(3); | |
$game->play(0, 0, TicTacToe::VALUE_O); | |
$game->play(0, 1, TicTacToe::VALUE_X); | |
$game->play(1, 0, TicTacToe::VALUE_O); | |
$game->play(1, 1, TicTacToe::VALUE_X); | |
$game->play(2, 0, TicTacToe::VALUE_O); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment