我想以编程方式刷新我的Tic Tac Toe游戏的页面。 AI为游戏选择了一个输入,我需要刷新页面(好像用户按下了提交按钮),因此可以应用AI的输入。
目前,我正在进行echo "<script>document.forms['tic_form'].submit();</script>";
,但每次发生这种情况时,页面都会被带回游戏模式选择屏幕。
代码:
<html>
<head>
<title>Tic Tac Toe</title>
</head>
<body>
<h1><font color="blue"><strong>Tic Tac Toe</strong></font></h1>
<?php
if (!session_id())
session_start();
//MAIN HEADER COMMENT Original Declaration of Variables
$_SESSION['game_win_detect'] = false;
$_SESSION['error'] = false;
if (!ISSET($_POST['submitter'])) {
$_SESSION['ul_truefalse'] = true;
$_SESSION['um_truefalse'] = true;
$_SESSION['ur_truefalse'] = true;
$_SESSION['ml_truefalse'] = true;
$_SESSION['mm_truefalse'] = true;
$_SESSION['mr_truefalse'] = true;
$_SESSION['bl_truefalse'] = true;
$_SESSION['bm_truefalse'] = true;
$_SESSION['br_truefalse'] = true;
$_SESSION['turn'] = 1;
$_SESSION['move_counter_display_p1'] = 1;
$_SESSION['move_counter_display_p2'] = 0;
$_POST['input'] = "";
$_SESSION['game_start'] = false;
$_SESSION['single_player_mode'] = false;
$_SESSION['remote_refresh'] = false;
}
//Detect a stalemate
if ($_SESSION['ul_display'] != "<font color='blue'>UL</font>" and $_SESSION['um_display'] != "<font color='blue'>UM</font>" and $_SESSION['ur_display'] != "<font color='blue'>UR</font>" and $_SESSION['ml_display'] != "<font color='blue'>ML</font>" and $_SESSION['mm_display'] != "<font color='blue'>MM</font>" and $_SESSION['mr_display'] != "<font color='blue'>MR</font>" and $_SESSION['bl_display'] != "<font color='blue'>BL</font>" and $_SESSION['bm_display'] != "<font color='blue'>BM</font>" and $_SESSION['br_display'] != "<font color='blue'>BR</font>") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Stalemate!</strong></font>";
}
//AI Controls
if ($_SESSION['single_player_mode'] == true and $_SESSION['game_win_detect'] == false) {
$_SESSION['ai_move'] = true;
while ($_SESSION['ai_move'] == true and $_SESSION['turn'] == 2) {
$random_input = array("ul","um","ur","ml","mm","mr","bl","bm","br");
$_POST['input'] = $random_input[array_rand($random_input)];
$_SESSION['ai_move'] = false;
}
$_SESSION['remote_refresh'] = true;
}
//MAIN HEADER COMMENT Gamemode select
if (ISSET($_POST['two_player'])) {
$_SESSION['game_start'] = true;
}
if (ISSET($_POST['single_player'])) {
$_SESSION['game_start'] = true;
$_SESSION['single_player_mode'] = true;
}
if ($_SESSION['game_start'] == false) {
?>
<h2>Please select a gamemode.</h2><br>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="two_player" value="Two Player">
<br>
<input type="submit" name="single_player" value="Single Player">
</form>
<?php
}
//MAIN HEADER COMMENT Display - Determinations
//Top Left
if ($_SESSION['ul_truefalse'] == true) {
$_SESSION['ul_display'] = "<font color='blue'>UL</font>";
if ($_POST['input'] == "ul" and $_SESSION['turn'] == 1) {
$_SESSION['ul_display'] = "X";
$_SESSION['ul_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "ul" and $_SESSION['turn'] == 2) {
$_SESSION['ul_display'] = "O";
$_SESSION['ul_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "ul") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>UL is taken. Please choose another spot.</strong></font>";
}
//Top Middle
if ($_SESSION['um_truefalse'] == true) {
$_SESSION['um_display'] = "<font color='blue'>UM</font>";
if ($_POST['input'] == "um" and $_SESSION['turn'] == 1) {
$_SESSION['um_display'] = "X";
$_SESSION['um_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "um" and $_SESSION['turn'] == 2) {
$_SESSION['um_display'] = "O";
$_SESSION['um_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "um") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>UM is taken. Please choose another spot.</strong></font>";
}
//Top Right
if ($_SESSION['ur_truefalse'] == true) {
$_SESSION['ur_display'] = "<font color='blue'>UR</font>";
if ($_POST['input'] == "ur" and $_SESSION['turn'] == 1) {
$_SESSION['ur_display'] = "X";
$_SESSION['ur_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "ur" and $_SESSION['turn'] == 2) {
$_SESSION['ur_display'] = "O";
$_SESSION['ur_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "ur") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>UR is taken. Please choose another spot.</strong></font>";
}
//Middle Left
if ($_SESSION['ml_truefalse'] == true) {
$_SESSION['ml_display'] = "<font color='blue'>ML</font>";
if ($_POST['input'] == "ml" and $_SESSION['turn'] == 1) {
$_SESSION['ml_display'] = "X";
$_SESSION['ml_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "ml" and $_SESSION['turn'] == 2) {
$_SESSION['ml_display'] = "O";
$_SESSION['ml_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "ml") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>ML is taken. Please choose another spot.</strong></font>";
}
//Middle Middle
if ($_SESSION['mm_truefalse'] == true) {
$_SESSION['mm_display'] = "<font color='blue'>MM</font>";
if ($_POST['input'] == "mm" and $_SESSION['turn'] == 1) {
$_SESSION['mm_display'] = "X";
$_SESSION['mm_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "mm" and $_SESSION['turn'] == 2) {
$_SESSION['mm_display'] = "O";
$_SESSION['mm_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "mm") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>MM is taken. Please choose another spot.</strong></font>";
}
//Middle Right
if ($_SESSION['mr_truefalse'] == true) {
$_SESSION['mr_display'] = "<font color='blue'>MR</font>";
if ($_POST['input'] == "mr" and $_SESSION['turn'] == 1) {
$_SESSION['mr_display'] = "X";
$_SESSION['mr_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "mr" and $_SESSION['turn'] == 2) {
$_SESSION['mr_display'] = "O";
$_SESSION['mr_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "mr") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>MR is taken. Please choose another spot.</strong></font>";
}
//Bottom Left
if ($_SESSION['bl_truefalse'] == true) {
$_SESSION['bl_display'] = "<font color='blue'>BL</font>";
if ($_POST['input'] == "bl" and $_SESSION['turn'] == 1) {
$_SESSION['bl_display'] = "X";
$_SESSION['bl_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "bl" and $_SESSION['turn'] == 2) {
$_SESSION['bl_display'] = "O";
$_SESSION['bl_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "bl") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>BL is taken. Please choose another spot.</strong></font>";
}
//Bottom Middle
if ($_SESSION['bm_truefalse'] == true) {
$_SESSION['bm_display'] = "<font color='blue'>BM</font>";
if ($_POST['input'] == "bm" and $_SESSION['turn'] == 1) {
$_SESSION['bm_display'] = "X";
$_SESSION['bm_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "bm" and $_SESSION['turn'] == 2) {
$_SESSION['bm_display'] = "O";
$_SESSION['bm_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "bm") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>BM is taken. Please choose another spot.</strong></font>";
}
//Bottom Right
if ($_SESSION['br_truefalse'] == true) {
$_SESSION['br_display'] = "<font color='blue'>BR</font>";
if ($_POST['input'] == "br" and $_SESSION['turn'] == 1) {
$_SESSION['br_display'] = "X";
$_SESSION['br_truefalse'] = false;
$_SESSION['change_player'] = true;
}
if ($_POST['input'] == "br" and $_SESSION['turn'] == 2) {
$_SESSION['br_display'] = "O";
$_SESSION['br_truefalse'] = false;
$_SESSION['change_player'] = true;
}
} elseif ($_POST['input'] == "br") {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>BR is taken. Please choose another spot.</strong></font>";
}
//Error when nothing is inputted
if ($_POST['input'] == "" and ISSET($_POST['submitter'])) {
$_SESSION['error'] = true;
$_SESSION['error_message'] = "<font color='red'><strong>Please enter your move.</strong></font>";
}
//Toggle Player Number (1 and 2), Count number of turns
if (ISSET($_POST['submitter']) and $_SESSION['change_player'] == true) {
$_SESSION['turn'] = $_SESSION['turn'] + 1;
$_SESSION['change_player'] = false;
if ($_SESSION['turn'] == 3) {
$_SESSION['turn'] = 1;
}
if ($_SESSION['turn'] == 1) {
$_SESSION['move_counter_display_p1'] = $_SESSION['move_counter_display_p1'] + 1;
}
if ($_SESSION['turn'] == 2) {
$_SESSION['move_counter_display_p2'] = $_SESSION['move_counter_display_p2'] + 1;
}
}
//Player Move Type Display (X or O)
if ($_SESSION['turn'] == 1) {
$placement = "X";
}
if ($_SESSION['turn'] == 2) {
$placement = "O";
}
if ($_POST['input'] == "cat") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player " . $_SESSION['turn'] . " said cat. They win.</strong></font><br><img src='http://4.bp.blogspot.com/-fYJrkNWec08/T9EYOmNGPNI/AAAAAAAAC04/UtdRRM8a3hc/s640/cat-fat-dancing-cat-gif.gif'>";
}
//Detect if game has been won
if ($_SESSION['ul_display'] == "X" and $_SESSION['um_display'] == "X" and $_SESSION['ur_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['ul_display'] == "O" and $_SESSION['um_display'] == "O" and $_SESSION['ur_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['ml_display'] == "X" and $_SESSION['mm_display'] == "X" and $_SESSION['mr_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['ml_display'] == "O" and $_SESSION['mm_display'] == "O" and $_SESSION['mr_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['bl_display'] == "X" and $_SESSION['bm_display'] == "X" and $_SESSION['br_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['bl_display'] == "O" and $_SESSION['bm_display'] == "O" and $_SESSION['br_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['ul_display'] == "X" and $_SESSION['ml_display'] == "X" and $_SESSION['bl_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['ul_display'] == "O" and $_SESSION['ml_display'] == "O" and $_SESSION['bl_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['um_display'] == "X" and $_SESSION['mm_display'] == "X" and $_SESSION['bm_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['um_display'] == "O" and $_SESSION['mm_display'] == "O" and $_SESSION['bm_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['ur_display'] == "X" and $_SESSION['mr_display'] == "X" and $_SESSION['br_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['ur_display'] == "O" and $_SESSION['mr_display'] == "O" and $_SESSION['br_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['ul_display'] == "X" and $_SESSION['mm_display'] == "X" and $_SESSION['br_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['ul_display'] == "O" and $_SESSION['mm_display'] == "O" and $_SESSION['br_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['ur_display'] == "X" and $_SESSION['mm_display'] == "X" and $_SESSION['bl_display'] == "X") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 1 wins!</strong></font>";
}
if ($_SESSION['ur_display'] == "O" and $_SESSION['mm_display'] == "O" and $_SESSION['bl_display'] == "O") {
$_SESSION['game_win_detect'] = true;
$_SESSION['win_message'] = "<font color='green'><strong>Player 2 wins!</strong></font>";
}
if ($_SESSION['game_start'] == true) {
?>
<!--Table Output-->
<table border="1" width="100px" height="100px">
<tr>
<td><?php echo $_SESSION['ul_display'] ?></td>
<td><?php echo $_SESSION['um_display'] ?></td>
<td><?php echo $_SESSION['ur_display'] ?></td>
</tr>
<tr>
<td><?php echo $_SESSION['ml_display'] ?></td>
<td><?php echo $_SESSION['mm_display'] ?></td>
<td><?php echo $_SESSION['mr_display'] ?></td>
</tr>
<tr>
<td><?php echo $_SESSION['bl_display'] ?></td>
<td><?php echo $_SESSION['bm_display'] ?></td>
<td><?php echo $_SESSION['br_display'] ?></td>
</tr>
</table>
<br>
<?php
if ($_SESSION['game_win_detect'] == false) {
echo "Player " . $_SESSION['turn'] . ", it's your turn! Where would you like to place your " . $placement . "?"
. "<br> Player " . $_SESSION['turn'] . "'s move number: ";
if ($_SESSION['turn'] == 1) {
echo $_SESSION['move_counter_display_p1'];
} else {
echo $_SESSION['move_counter_display_p2'];
}
?>
<form id="tic_form" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="input">
<input type="submit" name="submitter">
</form>
<?php
} else {
echo $_SESSION['win_message'];
$_SESSION['error_message'] = false;
}
echo "<br>";
if ($_SESSION['game_win_detect'] == true) {
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="reset" value="Play again">
</form>
<?php
}
}
if ($_SESSION['error'] == true) {
echo $_SESSION['error_message'];
}
if(ISSET($_POST['reset'])) {
unset($_SESSION);
}
if ($_SESSION['remote_refresh'] == true) {
$_SESSION['remote_refresh'] = false;
echo "<script>document.forms['tic_form'].submit();</script>";
}
?>
</body>
</html>
答案 0 :(得分:1)
session_start()
必须位于您希望使用会话的页面顶部:
<?php
session_start();
?>
<html>
<head>
<title>Tic Tac Toe</title>
</head>
<body>
<h1><font color="blue"><strong>Tic Tac Toe</strong></font></h1>