<?php
// Author's Notes
// Connect Four PHP script written by
services@rcadble.net
// 1 represents user move, 2 represents computer move
// MySQL Settings
$mysql_server = 'localhost'; // MySQL server IP
$mysql_user = 'root'; // MySQL username with appropriate permissions
$mysql_password = ''; // MySQL username's password
$mysql_database = 'connectfour'; // Database where the data is be stored.
$conn = mysql_connect($mysql_server, $mysql_user, $mysql_password) or dieError(mysql_error());
$db = mysql_select_db($mysql_database, $conn) or dieError(mysql_error());
// Session Settings
session_start();
// Functions
function setPiece($boardString, $column, $value) {
$values = explode(", ", $boardString);
$row = 5 - colFillCount($boardString, $column);
$pieceindex = ($row * 7) + $column;
$values[$pieceindex] = $value;
return implode(", ", $values);
}
function colFillCount($boardString, $column) {
$values = explode(", ", $boardString);
$counter = 0;
$fillcount = 0;
foreach ($values AS $value) {
if ($counter==$column) {
if ($value!=0) {
$fillcount++;
}
}
$counter++;
if ($counter==7) {
$counter = 0;
}
}
return $fillcount;
}
function colFilled($boardString, $column) {
$values = explode(", ", $boardString);
$counter = 0;
foreach ($values AS $value) {
if ($counter==$column) {
if ($value==0) {
return false;
}
}
$counter++;
if ($counter==7) {
$counter = 0;
}
}
return true;
}
function drawBoard($boardString) {
$values = explode(", ", $boardString);
$return_html = '<table width="700" border="3" cellspacing="0" cellpadding="0" bordercolor="#0000FF">';
$counter = 0;
for ($i = 0; $i<=6; $i++) {
$filled[$i] = colFilled($boardString, $i);
}
foreach ($values AS $value) {
if ($counter==0) {
$return_html .= '<tr>';
}
if ($filled[$counter]==true) {
if ($value==0) {
$return_html .= '<td height="100" width="100"><img src="images/empty.png"></td>';
} elseif ($value==1) {
$return_html .= '<td height="100" width="100" background="images/empty.png"><img src="images/player.png"></td>';
} elseif ($value==2) {
$return_html .= '<td height="100" width="100" background="images/empty.png"><img src="images/computer.png"></td>';
}
} else {
if ($value==0) {
$return_html .= '<td height="100" width="100"><a href="?col=' . $counter . '"><img src="images/empty.png" style="border-style: none"></a></td>';
} elseif ($value==1) {
$return_html .= '<td height="100" width="100" background="images/empty.png"><a href="?col=' . $counter . '"><img src="images/player.png" style="border-style: none"></a></td>';
} elseif ($value==2) {
$return_html .= '<td height="100" width="100" background="images/empty.png"><a href="?col=' . $counter . '"><img src="images/computer.png" style="border-style: none"></a></td>';
}
}
$counter++;
if ($counter==7) {
$return_html .= '</tr>';
$counter = 0;
}
}
$return_html .= '</table>';
return $return_html;
}
function dieError($mysql_error) {
die("<b>A MySQL error has occurred!</b><br>" . $mysql_error);
}
// Script Actions
$result = mysql_query("SELECT * FROM `games` WHERE `session_id` = '" . session_id() . "' AND `winner` = '0' LIMIT 1;", $conn) or dieError(mysql_error());
if (mysql_num_rows($result)==1) {
$row = mysql_fetch_array($result);
$_SESSION['game_ID'] = $row['id'];
}
$result = mysql_query("SELECT * FROM `games` WHERE `session_id` = '" . session_id() . "' AND `id` = '" . $_SESSION['game_ID'] . "' LIMIT 1;", $conn) or dieError(mysql_error());
if (mysql_num_rows($result)!=1) {
$_SESSION['game_ID'] = '';
}
if ($_SESSION['game_ID']==''&&$_POST['act']!='newgame') {
$content = '<b>Connect Four</b> by
services@rcadble.net.<br>You are not currently in a game.<br><br><form action="" method="POST"><input type="hidden" name="act" value="newgame"><input type="submit" value="Start a New Game!"></form>';
} elseif ($_SESSION['game_ID']==''&&$_POST['act']=='newgame') {
$emptyboard = '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0';
$result = mysql_query("INSERT INTO `games` (`id`, `winner`, `moves`, `session_id`, `umcount`) VALUES (NULL, '0', '$emptyboard', '" . session_id() . "', '0');", $conn) or dieError(mysql_error());
$result = mysql_query("SELECT * FROM `games` WHERE `session_id` = '" . session_id() . "' LIMIT 1;", $conn) or dieError(mysql_error());
$row = mysql_fetch_array($result);
$_SESSION['game_ID'] = $row['id'];
$content = drawBoard($emptyboard);
} else {
$result = mysql_query("SELECT * FROM `games` WHERE `session_id` = '" . session_id() . "' AND `id` = '" . $_SESSION['game_ID'] . "' LIMIT 1;", $conn) or dieError(mysql_error());
$row = mysql_fetch_array($result);
$column = $_GET['col'];
if ($column<0||$column>6) {
$content = drawBoard($row['moves']);
} else {
$board = setPiece($row['moves'], $column, 1);
$umcount = $row['umcount'] + 1;
if ($umcount==1) {
$board = setPiece($board, 3, 2);
} elseif ($umcount==2) {
$board = setPiece($board, 3, 2);
} elseif ($umcount==3) {
$colsafe = checkVertThree($board, 1);
if($colsafe!=-1) {
$board = setPiece($board, $colsafe, 2);
} else {
$board = setPiece($board, 3, 2);
}
} else {
$weight[0] =
$weight[1] =
$weight[2] =
$weight[3] =
$weight[4] =
$weight[5] =
$weight[6] =
}
$result = mysql_query("UPDATE `games` SET `moves` = '" . $board . "' WHERE `id` = " . $row['id'] . " LIMIT 1", $conn) or dieError(mysql_error());
$result = mysql_query("UPDATE `games` SET `umcount` = '" . $umcount . "' WHERE `id` = " . $row['id'] . " LIMIT 1", $conn) or dieError(mysql_error());
$content = drawBoard($board);
}
}
//Script Output
echo '<center><br>' . $content . '</center>';
?>