Skip to content

Instantly share code, notes, and snippets.

@yetimdasturchi
Created June 8, 2022 19:44
Show Gist options
  • Save yetimdasturchi/ba3fd95da1f5eadd108060e79fef381a to your computer and use it in GitHub Desktop.
Save yetimdasturchi/ba3fd95da1f5eadd108060e79fef381a to your computer and use it in GitHub Desktop.

PHP dasturlash tilida Tic tac toe o'yini

o'yinni ishga tushirish uchun buyruqlar satrida

$: php ticTac.php
<?php
function translateKeypress( $string ) {
switch ($string) {
// Yuqoriga harakatlantirish tugmasi
case "\033[A":
return "UP";
// Pastga harakatlantirish tugmasi
case "\033[B":
return "DOWN";
// O'ngga harakatlantirish tugmasi
case "\033[C":
return "RIGHT";
// Chapga harakatlantirish tugmasi
case "\033[D":
return "LEFT";
// ENTER (kiritish) tugmasi
case "\n":
return "ENTER";
// Bo'sh joy tugmasi
case " ":
return "SPACE";
// Satrdan belgini o'chirish tugmasi
case "\010":
case "\177":
return "BACKSPACE";
// Tabulator tugmasi
case "\t":
return "TAB";
// Bekor qilish tugmasi
case "\e":
return "ESC";
}
return $string;
}
function move( $stdin, &$state, &$activeCell, &$player ) {
$key = fgets( $stdin );
if ( $key ) {
$key = translateKeypress( $key );
switch ($key) {
case "UP":
if ( $activeCell[0] >= 1 ) $activeCell[0]--;
break;
case "DOWN":
if ( $activeCell[0] < 2 ) $activeCell[0]++;
break;
case "RIGHT":
if ( $activeCell[1] < 2 ) $activeCell[1]++;
break;
case "LEFT":
if ( $activeCell[1] >= 1 ) $activeCell[1]--;
break;
case "ENTER":
case "SPACE":
if ( $state[ $activeCell[ 0 ] ][ $activeCell[ 1 ] ] == '' ) {
$state[ $activeCell[ 0 ] ][ $activeCell[ 1 ] ] = $player;
$player = ( $player == 'X' ) ? 'O' : 'X';
}
break;
}
}
}
function renderGame( $state, $activeCell, $player ) {
$output = '';
$output .= 'O\'yinchi: ' . $player . PHP_EOL;
foreach ( $state as $x => $line ) {
$output .= '|';
foreach ( $line as $y => $item ) {
switch ( $item ) {
case '';
$cell = ' ';
break;
case 'X';
$cell = 'X';
break;
case 'O';
$cell = 'O';
break;
}
$cell = ( $activeCell[ 0 ] == $x && $activeCell[ 1 ] == $y ) ? '-'. $cell . '-' : ' ' . $cell . ' ';
$output .= $cell . '|';
}
$output .= PHP_EOL;
}
return $output;
}
function isWinState( $state ) {
foreach ( [ 'X', 'O' ] as $player ) {
foreach ($state as $x => $line) {
// Gorizontal qator mavjud.
if ( $state[ $x ][ 0 ] == $player && $state[ $x ][ 1 ] == $player && $state[ $x ][ 2 ] == $player ) exit( $player . ' yutdi' . PHP_EOL );
foreach ( $line as $y => $item ) {
// Vertikal qator mavjud.
if ( $state[ 0 ][ $y ] == $player && $state[ 1 ][ $y ] == $player && $state[ 2 ][ $y ] == $player ) exit( $player . ' yutdi' . PHP_EOL );
}
}
// Diagonal chiziq yuqoridan, chapdan, pastdan o'ngga shaklda topildi.
if ( $state[ 0 ][ 0 ] == $player && $state[ 1 ][ 1 ] == $player && $state[ 2 ][ 2 ] == $player ) exit( $player . ' yutdi' . PHP_EOL );
// Diagonal chiziq pastdan, chapdan, yuqoriga o'ngga shaklda topildi.
if ( $state[ 2 ][ 0 ] == $player && $state[ 1 ][ 1 ] == $player && $state[ 0 ][ 2 ] == $player ) exit( $player . ' yutdi' . PHP_EOL );
}
// Durang holati
$blankQuares = 0;
foreach ( $state as $x => $line ) {
foreach ( $line as $y => $item ) {
if ( $state[ $x ][ $y ] == '' ) $blankQuares++;
}
}
// Agar bo'sh kataklar qolmasa va yuqoridagi shartlar bajarilmasa durang deb e'lon qilish.
if ( $blankQuares == 0 ) exit( 'Durang natija!' . PHP_EOL );
}
$stdin = fopen( 'php://stdin', 'r' );
stream_set_blocking( $stdin, 0 );
system( 'stty cbreak -echo' );
$state = [
[ '', '', '' ],
[ '', '', '' ],
[ '', '', '' ],
];
$player = 'X';
$activeCell = [ 0 => 0, 1 => 0 ];
while( 1 ) {
system( 'clear');
move( $stdin, $state, $activeCell, $player );
echo renderGame( $state, $activeCell, $player );
isWinState( $state );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment