function make_move($move, $userid, $roomid)
{
    $move--;
    if (!is_numeric($move)) {
        die;
    }
    global $db, $ir;
    $game = $db->query("SELECT * FROM bs_room WHERE id={$roomid}", $c) or die("1");
    $ga = $db->fetch_row($game);
    $turn = $ga['turn'];
    if ($turn == $userid) {
        $ginfo = $db->query("SELECT * FROM bs_game WHERE bs_room={$roomid}");
        $gin = $db->fetch_row($ginfo);
        if ($gin['p1'] == $userid) {
            $oppboard = explode('|', $gin['p2b']);
            $icoppboard = explode('|', $gin['p1bo']);
            $boardfield = "p1bo";
        } else {
            if ($gin['p2'] == $userid) {
                $oppboard = explode('|', $gin['p1b']);
                $icoppboard = explode('|', $gin['p2bo']);
                $boardfield = "p2bo";
            }
        }
        if ($icoppboard[$move] == 0) {
            if ($oppboard[$move] == 0 && ctype_digit($oppboard[$move])) {
                $oppboard[$move] = -1;
            }
            $icoppboard[$move] = $oppboard[$move];
            $uicoppboard = implode("|", $icoppboard);
            $db->query("UPDATE bs_game SET {$boardfield} = '{$uicoppboard}' WHERE bs_room = {$roomid}");
            $db->query("UPDATE bs_room SET play_time=unix_timestamp() WHERE id={$roomid}");
            $winner = check_win($roomid);
            if ($winner != 0) {
                award_win($roomid, $winner);
            } else {
                toggle_turn($roomid);
            }
            //only toggle turn if no win yet
        }
    }
}
function make_move($move, $userid, $roomid)
{
    global $db, $ir;
    $game = $db->query("SELECT * FROM ttt_room WHERE id={$roomid}", $c) or die("1");
    $ga = $db->fetch_row($game);
    $turn = $ga['turn'];
    if ($turn == $userid) {
        $ginfo = $db->query("SELECT * FROM ttt_game WHERE ttt_room={$roomid}");
        $gin = $db->fetch_row($ginfo);
        $field = "b" . $move;
        if ($gin[$field] == 0) {
            $ctime = time();
            $db->query("UPDATE ttt_game SET {$field} = {$userid} WHERE ttt_room = {$roomid}");
            $db->query("UPDATE ttt_room SET play_time=unix_timestamp() WHERE id={$roomid}");
            $winner = check_win($roomid);
            if ($winner != 0) {
                award_win($roomid, $winner);
            } else {
                toggle_turn($roomid);
            }
            //only toggle turn if no win yet
        }
    }
}
function make_move($move, $userid, $roomid)
{
    global $db, $ir;
    $game = $db->query("SELECT * FROM man_room WHERE id={$roomid}", $c) or die("1");
    $ga = $db->fetch_row($game);
    $turn = $ga['turn'];
    if ($turn == $userid) {
        $ginfo = $db->query("SELECT * FROM man_game WHERE man_room={$roomid}");
        $gin = $db->fetch_row($ginfo);
        $movefrom = "b" . $move;
        //make sure square is not empty
        if ($gin["{$movefrom}"] > 0) {
            //make sure square is on their side of the board
            if ($gin['bottom'] == $userid && $move > 0 && $move < 7 or $gin['bottom'] != $userid && $move > 7 && move < 14) {
                $totstones = $gin["{$movefrom}"];
                $gin["{$movefrom}"] = 0;
                $spot = ($move + 1) % 14;
                while ($totstones > 0) {
                    if ($spot != 7 && $spot != 0 || ($spot == 7 && $gin['bottom'] == $userid || $spot == 0 && $gin['bottom'] != $userid)) {
                        $totstones--;
                        $gin["b" . $spot] += 1;
                        $finalspot = $spot;
                        $spot++;
                    }
                    if ($spot == 7 && $gin['bottom'] != $userid || $spot == 0 && $gin['bottom'] == $userid) {
                        $spot++;
                    }
                    $spot = $spot % 14;
                }
                if ($finalspot == 0 || $finalspot == 7) {
                    $notoggle = 1;
                }
                if ($gin["b" . $finalspot] == 1 && $finalspot != 0 && $finalspot != 7) {
                    $oppspot = get_opp($finalspot);
                    if ($gin["b" . $oppspot] > 0) {
                        //only if on your side of the board
                        if ($finalspot > 0 && $finalspot < 7 && $gin['bottom'] == $userid || $finalspot > 7 && $finalspot < 14 && $gin['bottom'] != $userid) {
                            $wonstones = 1 + $gin["b" . $oppspot];
                            if ($gin['bottom'] == $userid) {
                                $gin['b7'] += $wonstones;
                            }
                            if ($gin['bottom'] != $userid) {
                                $gin['b0'] += $wonstones;
                            }
                            $gin["b" . $oppspot] = 0;
                            $gin["b" . $finalspot] = 0;
                        }
                    }
                }
                $db->query("UPDATE man_game SET b0={$gin['b0']},b1={$gin['b1']},b2={$gin['b2']},b3={$gin['b3']},b4={$gin['b4']},b5={$gin['b5']},b6={$gin['b6']},b7={$gin['b7']},b8={$gin['b8']},b9={$gin['b9']},b10={$gin['b10']},b11={$gin['b11']},b12={$gin['b12']},b13={$gin['b13']} WHERE man_room={$roomid}");
                $db->query("UPDATE man_room SET play_time=unix_timestamp() WHERE id={$roomid}");
                $winner = check_win($roomid);
                if ($winner != 0) {
                    award_win($roomid, $winner);
                } else {
                    if (!$notoggle) {
                        toggle_turn($roomid);
                    }
                }
            }
        }
    }
}
function toggle_turn($roomid)
{
    global $db, $ir;
    $ui = $db->fetch_row($db->query("SELECT p1,p2,turn FROM ms_room WHERE id = {$roomid}"));
    $db->query("UPDATE ms_game SET mselected=1 WHERE ms_room={$roomid}");
    if ($ui['turn'] == $ui['p1']) {
        $db->query("UPDATE ms_room SET notifyturn=0,play_time=unix_timestamp(),turn={$ui['p2']} WHERE id={$roomid}");
    } else {
        $db->query("UPDATE ms_room SET notifyturn=0,play_time=unix_timestamp(),turn={$ui['p1']} WHERE id={$roomid}");
    }
    $winner = check_win($roomid);
    if ($winner != 0) {
        award_win($roomid, $winner);
    }
}
function make_move($move, $userid, $roomid)
{
    global $db, $ir;
    $game = $db->query("SELECT * FROM ck_room WHERE id={$roomid}", $c) or die("1");
    $ga = $db->fetch_row($game);
    $game2 = $db->query("SELECT * FROM ck_game WHERE ck_room={$roomid}", $c) or die("1");
    $gijoe = $db->fetch_row($game2);
    $turn = $ga['turn'];
    if ($turn == $userid) {
        $vnam = "b" . $move;
        if ($move == 32) {
            $db->query("UPDATE ck_game SET pselect = 0 WHERE ck_room={$roomid}");
            die;
        }
        if ($gijoe["{$vnam}"] == $userid) {
            $db->query("UPDATE ck_game SET pselect={$move} WHERE ck_room={$roomid}");
            die;
        }
        if ($gijoe["{$vnam}"] == "-" . $userid) {
            $db->query("UPDATE ck_game SET pselect={$move} WHERE ck_room={$roomid}");
            die;
        }
        $red = $gijoe['red'];
        // get red player (used for direction on board)
        if ($gijoe['red'] == $gijoe['p1']) {
            $white = $gijoe['p2'];
        } else {
            $white = $gijoe['p1'];
        }
        $pselect = $gijoe['pselect'];
        //basic move no capture no king
        $valid = valid_move($userid, $red, $move, $pselect);
        $bmove = $gijoe[b . "{$move}"];
        $lastmove = $gijoe['lastmove'];
        $t = "b" . $gijoe[pselect];
        $user = $gijoe["{$t}"];
        if ($valid > 0 && $bmove == 0 && $lastmove == 0 && $user > 0) {
            $temp = "b" . $gijoe[pselect];
            $temp2 = "b" . $move;
            $db->query("UPDATE ck_game SET {$temp}=0, {$temp2}={$userid},pselect = {$temp2},lastmove = {$move}, lastmove2 = {$move} WHERE ck_room={$roomid}");
            $gijoe["{$temp2}"] = $userid;
            $value = king_me($userid, $red, $move);
            if ($value == 1) {
                $temp3 = "-" . $gijoe["{$temp2}"];
                $db->query("UPDATE ck_game SET {$temp2}={$temp3} WHERE ck_room={$roomid}");
            }
            end_turn($userid, $roomid);
        }
        //capture a piece
        $valid2 = jump_move($userid, $red, $white, $move, $pselect);
        $jumpedspot = $gijoe[b . "{$move}"];
        $newspot = "b" . $move;
        $jumpsomeone = $gijoe[b . "{$valid2}"];
        $user = $gijoe[b . "{$pselect}"];
        if ($valid2 > 0 && $jumpedspot == 0 && (abs($jumpsomeone) > 0 && abs($jumpsomeone) != $userid) && $user > 0) {
            $temp = "b" . $gijoe[pselect];
            // was
            $temp2 = "b" . $move;
            // going
            $temp3 = "b" . $valid2;
            //spot jumped
            $db->query("UPDATE ck_game SET {$temp}=0, {$temp2}={$userid}, pselect = {$move} ,lastmove = {$move},lastmove2 = {$move}, {$temp3} = 0 WHERE ck_room={$roomid}");
            $gijoe["{$temp2}"] = $userid;
            $value = king_me($userid, $red, $move);
            if ($value == 1) {
                $temp4 = "-" . $gijoe["{$temp2}"];
                $db->query("UPDATE ck_game SET {$temp2}={$temp4} WHERE ck_room={$roomid}");
            }
            $secondjump = array();
            $secondjump = second_jump($move, $red, $userid);
            //possible spots to
            $valid1 = jump_move($userid, $red, $white, $secondjump["d1"], $move);
            $valid2 = jump_move($userid, $red, $white, $secondjump["d2"], $move);
            $start = "b" . $move;
            $spotjumped1 = $gijoe[b . "{$valid1}"];
            $spotjumped2 = $gijoe[b . "{$valid2}"];
            $temp1 = $secondjump["d1"];
            $temp2 = $secondjump["d2"];
            $finish1 = $gijoe[b . "{$temp1}"];
            $finish2 = $gijoe[b . "{$temp2}"];
            if ($valid1 > 0 && $finish1 == 0 && ($spotjumped1 != 0 && abs($spotjumped1) != $userid) || $valid2 > 0 && $finish2 == 0 && ($spotjumped2 != 0 && abs($spotjumped2) != $userid)) {
                $themissionary = 69;
                $db->query("UPDATE ck_game SET lastmove2=0 WHERE ck_room={$roomid}");
            } else {
                end_turn($userid, $roomid);
            }
        }
        //king moves normal move
        $valid = valid_king_move($userid, $red, $move, $pselect);
        $bmove = $gijoe[b . "{$move}"];
        $lastmove = $gijoe['lastmove'];
        $isaking = $gijoe[pselect];
        $kingvalue = $gijoe[b . "{$isaking}"];
        if ($valid > 0 && $bmove == 0 && $lastmove == 0 && $kingvalue < 0) {
            $temp = "b" . $gijoe[pselect];
            $temp2 = "b" . $move;
            $user = $gijoe["{$temp}"];
            $db->query("UPDATE ck_game SET {$temp}=0, {$temp2}='{$user}',pselect = {$temp2},lastmove = {$move}, lastmove2 = {$move} WHERE ck_room={$roomid}");
            $gijoe["{$temp2}"] = $userid;
            end_turn($userid, $roomid);
        }
        //king jump
        //king_second($lastmove, $red, $userid) // possible moves
        //function king_jump($userid,$red,$white,$move,$pselect)
        $valid = king_jump($userid, $red, $white, $move, $pselect);
        $jumptoo = $gijoe[b . "{$move}"];
        //value of new spot
        $newspot = "b" . $move;
        //name of new spot
        $spotjumped = $gijoe[b . "{$valid}"];
        //value of spot jumped
        $user = abs($gijoe[b . "{$pselect}"]);
        //user value of starting spot
        $isaking = $gijoe[pselect];
        $kingvalue = $gijoe[b . "{$isaking}"];
        if ($valid > 0 && $jumptoo == 0 && (abs($spotjumped) > 0 && abs($spotjumped) != $userid) && abs($user) > 0 && $kingvalue < 0) {
            $temp = "b" . $gijoe[pselect];
            // was
            $temp2 = "b" . $move;
            // going
            $temp3 = "b" . $valid;
            //spot jumped
            $neguser = "******" . $userid;
            $db->query("UPDATE ck_game SET {$temp}=0, {$temp2}={$neguser}, pselect = {$move} ,lastmove = {$move},lastmove2 = {$move}, {$temp3} = 0 WHERE ck_room={$roomid}");
            $gijoe["{$temp2}"] = "-" . $userid;
            //second jump
            $secondjump = array();
            $secondjump = king_second($move, $red, $userid);
            //possible spots to
            $valid1 = king_jump($userid, $red, $white, $secondjump["d1"], $move);
            $valid2 = king_jump($userid, $red, $white, $secondjump["d2"], $move);
            $valid3 = king_jump($userid, $red, $white, $secondjump["d3"], $move);
            $valid4 = king_jump($userid, $red, $white, $secondjump["d4"], $move);
            $start = "b" . $move;
            $spotjumped1 = $gijoe[b . "{$valid1}"];
            $spotjumped2 = $gijoe[b . "{$valid2}"];
            $spotjumped3 = $gijoe[b . "{$valid3}"];
            $spotjumped4 = $gijoe[b . "{$valid4}"];
            $temp1 = $secondjump["d1"];
            $temp2 = $secondjump["d2"];
            $temp3 = $secondjump["d3"];
            $temp4 = $secondjump["d4"];
            $finish1 = $gijoe[b . "{$temp1}"];
            $finish2 = $gijoe[b . "{$temp2}"];
            $finish3 = $gijoe[b . "{$temp3}"];
            $finish4 = $gijoe[b . "{$temp4}"];
            if ($valid1 > 0 && $finish1 == 0 && ($spotjumped1 > 0 && abs($spotjumped1) != $userid) || $valid2 > 0 && $finish2 == 0 && ($spotjumped2 > 0 && abs($spotjumped2) != $userid) || $valid3 > 0 && $finish3 == 0 && ($spotjumped3 > 0 && abs($spotjumped3) != $userid) || $valid4 > 0 && $finish4 == 0 && ($spotjumped4 > 0 && abs($spotjumped4) != $userid)) {
                $themissionary = 69;
                $db->query("UPDATE ck_game SET lastmove2=0 WHERE ck_room={$roomid}");
            } else {
                end_turn($userid, $roomid);
            }
        }
        $winner = check_win($roomid);
        if ($winner != 0) {
            award_win($roomid, $winner);
        }
    }
}
$userid = $ir['userid'];
include "s_{$gpre}func.php";
$pp = $db->query("SELECT * FROM {$gpre}game WHERE gameover=0 AND userid={$userid}");
$pany = $db->num_rows($pp);
$move = abs((int) $_POST['move']);
$select = abs((int) $_POST['select']);
print "<center>";
//This code is essentially the code that executes in the if($pany && $_GET['act']=='play') statement within s_pp_game.php
if ($pany) {
    $p = $db->fetch_row($pp);
    print "<h2>Poker Patience</h2><a href='s_pp_game.php?act=highscores'><b><font color=green>View Highscores</font></b></a> | \n\t<b>How to play:</b><br /><font size=1> Get the best 5 card hand possible on each row, column, and the two diagonals. <br />\n\tYour final score is the total points you earned from all 12 of those hands.<br />\n\tClick one of the two decks at top to select a card to play, and click an open square to play it.</font><br /><br />\n\t<div id = \"gamediv\">";
    $gameid = $p['id'];
    $boardtxt = draw_board($userid, $gameid);
    if ($move != 0 && $p['gameover'] == 0) {
        make_move($move, $userid);
        $points = check_win($userid);
        if ($points > 0) {
            $ins = $db->query("SELECT * FROM pp_scores WHERE userid={$userid} AND {$points}<score");
            $insn = $db->num_rows($ins);
            //keep old score only if there's a score in the DB that has a higher value than $points
            if (!$insn) {
                $db->query("DELETE FROM pp_scores WHERE userid={$userid}");
                $db->query("DELETE FROM pp_game WHERE userid={$userid} AND id!={$gameid}");
                $db->query("INSERT INTO pp_scores (userid, score, gameid) VALUES({$userid}, {$points}, {$gameid})");
            }
            print "Game over, you got <b>{$points} points</b>  <br /><br>";
            print "<form method='post' action='?act=replay'>\n\t\t\t<input type='submit' value='New Game?'>\n\t\t\t</form><br /><br />";
            print "<form method='post' action='?act=post'>\n\t\t\tVictory Message (optional): <input type=text name='victory'><br />\n\t\t\t<input type='submit' value='Post to Facebook?'>\n\t\t\t</form>";
        }
    }
    if ($select != 0) {
function make_move($move, $userid, $roomid)
{
    global $db, $ir;
    $game = $db->query("SELECT * FROM or_room WHERE id={$roomid}", $c) or die("1");
    $ga = $db->fetch_row($game);
    $turn = $ga['turn'];
    if ($turn != $userid) {
        die;
    }
    //not your turn
    $ginf = $db->query("SELECT * FROM or_game WHERE or_room = {$roomid}");
    $gin = $db->fetch_row($ginf);
    if ($gin["b" . $move] > 0) {
        die;
    }
    //square not empty
    //horizontal right
    $good = 0;
    $loop = 1;
    $goodmove = 0;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace + 1;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i < $endloc; $i++) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    //horizontal left
    $good = 0;
    $goodmove = 0;
    $loop = 1;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace - 1;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i > $endloc; $i--) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    //vertical down
    $good = 0;
    $loop = 1;
    $goodmove = 0;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace + 10;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i < $endloc; $i += 10) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    //vertical up
    $good = 0;
    $loop = 1;
    $goodmove = 0;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace - 10;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i > $endloc; $i -= 10) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    //diagnol down right
    $good = 0;
    $loop = 1;
    $goodmove = 0;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace + 11;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i < $endloc; $i += 11) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    //diagnol down left
    $good = 0;
    $loop = 1;
    $goodmove = 0;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace + 9;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i < $endloc; $i += 9) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    //diagnol up right
    $good = 0;
    $loop = 1;
    $goodmove = 0;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace - 9;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i > $endloc; $i -= 9) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    //diaganol up left
    $good = 0;
    $loop = 1;
    $goodmove = 0;
    $nextspace = $move;
    while ($loop) {
        $loop = 0;
        $nextspace = $nextspace - 11;
        $gin["b" . $nextspace];
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] != $userid) {
            if (!$good) {
                $good = 1;
            }
            //set flag for valid moves.
            $loop = 1;
        }
        if ($gin["b" . $nextspace] > 0 && $gin["b" . $nextspace] == $userid && $good == 1) {
            //valid move
            $goodmove = 1;
            $endloc = $nextspace;
        }
    }
    if ($goodmove == 1) {
        $begin = $move;
        $end = $endloc;
        $finishturn = 1;
        $updatearr = array();
        for ($i = $move; $i > $endloc; $i -= 11) {
            $updatearr[] = "b{$i}={$userid}";
        }
        $updatestr = implode(",", $updatearr);
        $db->query("UPDATE or_game SET {$updatestr} WHERE or_room={$roomid}");
    }
    if ($finishturn == 1) {
        $db->query("UPDATE or_game SET lastmove={$move} WHERE or_room={$roomid}");
        $winner = check_win($roomid);
        if ($winner != 0) {
            award_win($roomid, $winner);
        } else {
            toggle_turn($roomid);
        }
        //only toggle turn if no win yet
    }
}
Exemplo n.º 8
0
if (count(array_filter($game)) == 9) {
    $response['outcome'] = 'Tie game. Reload to play again.';
} else {
    if (check_win($game, 'X')) {
        $response['outcome'] = 'You win! Reload to play again.';
    } else {
        do {
            $pos = mt_rand(0, 8);
            if (empty($game[$pos])) {
                $game[$pos] = 'O';
                $response['pos'] = $pos;
                $response['move'] = 'O';
                break;
            }
        } while (true);
        if (check_win($game, 'O')) {
            $response['outcome'] = 'You lose! Reload to play again.';
        }
    }
}
echo Javelin::renderAjaxResponse($response);
function check_win($game, $player)
{
    static $wins = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 3, 6, 1, 4, 7, 2, 5, 8, 0, 4, 8, 6, 4, 2);
    foreach (array_chunk($wins, 3) as $win) {
        $is_win = true;
        foreach ($win as $cell) {
            if ($game[$cell] !== $player) {
                $is_win = false;
                break;
            }
function make_move($move, $userid, $roomid)
{
    global $db, $ir;
    $game = $db->query("SELECT * FROM bg_room WHERE id={$roomid}", $c) or die("1");
    $ga = $db->fetch_row($game);
    $turn = $ga['turn'];
    $catch = 0;
    if ($turn == $userid) {
        $ginfo = $db->query("SELECT * FROM bg_game WHERE bg_room={$roomid}");
        $gin = $db->fetch_row($ginfo);
        if ($userid == $gin['black']) {
            $movefrom = "b" . $move;
        } else {
            $movefrom = "a" . $move;
        }
        $dsel = $gin['dselected'];
        // which dice is selected
        $dval = $gin["d" . $dsel];
        // value of dice selected
        $dname = "d" . $dsel;
        if ($gin["{$movefrom}"] > 0 || $userid == $gin['black'] && $gin['bbar'] > 0 && $move == 26 || $userid != $gin['black'] && $gin['abar'] > 0 && $move == 27) {
            if ($userid == $gin['black']) {
                $nextspot = $move + $dval;
                //normal move
                if ($gin["a" . $nextspot] <= 1 && $nextspot < 25 && $gin['bbar'] == 0) {
                    $temp = "b" . $nextspot;
                    $temp2 = "a" . $nextspot;
                    if ($gin["a" . $nextspot] == 1) {
                        $db->query("UPDATE bg_game SET abar = abar + 1, {$temp2}=0 WHERE bg_room={$roomid}");
                    }
                    $db->query("UPDATE bg_game SET {$temp}={$temp}+1,{$movefrom}={$movefrom}-1,{$dname} = 0,dselected = 0 WHERE bg_room={$roomid}");
                    $db->query("UPDATE bg_room SET play_time=unix_timestamp() WHERE id={$roomid}");
                    $gin["{$dname}"] = 0;
                }
                //if they have pieces on the bar
                if ($gin['bbar'] > 0 && $move == 26) {
                    if ($gin["a" . $dval] <= 1) {
                        $temp = "b" . $dval;
                        $temp2 = "a" . $dval;
                        if ($gin["a" . $dval] == 1) {
                            $db->query("UPDATE bg_game SET abar = abar + 1, {$temp2}=0 WHERE bg_room={$roomid}");
                        }
                        $db->query("UPDATE bg_game SET {$temp}={$temp}+1,bbar=bbar-1,{$dname} = 0,dselected = 0 WHERE bg_room={$roomid}");
                        $db->query("UPDATE bg_room SET play_time=unix_timestamp() WHERE id={$roomid}");
                        $gin["{$dname}"] = 0;
                    }
                }
                //moving into home
                if ($nextspot >= 25 && $move != 26 && $move != 27 && $gin['bbar'] == 0) {
                    $temp = $gin['b19'] + $gin['b20'] + $gin['b21'] + $gin['b22'] + $gin['b23'] + $gin['b24'] + $gin['b25'];
                    if ($temp == 15) {
                        $temp = "b" . $move;
                        $db->query("UPDATE bg_game SET b25=b25+1, {$temp} = {$temp} -1,{$dname} = 0,dselected = 0 WHERE bg_room={$roomid}");
                        $db->query("UPDATE bg_room SET play_time=unix_timestamp() WHERE id={$roomid}");
                        $gin["{$dname}"] = 0;
                    }
                    $winner = check_win($roomid);
                    if ($winner != 0) {
                        $catch = 1;
                        award_win($roomid, $winner);
                    }
                }
            } else {
                $nextspot = $move - $dval;
                //normal move
                if ($gin["b" . $nextspot] <= 1 && $nextspot > 0 && $gin['abar'] == 0) {
                    $temp = "a" . $nextspot;
                    $temp2 = "b" . $nextspot;
                    if ($gin["b" . $nextspot] == 1) {
                        $db->query("UPDATE bg_game SET bbar = bbar + 1, {$temp2}=0 WHERE bg_room={$roomid}");
                    }
                    $db->query("UPDATE bg_game SET {$temp}={$temp}+1,{$movefrom}={$movefrom}-1,{$dname} = 0,dselected = 0 WHERE bg_room={$roomid}");
                    $db->query("UPDATE bg_room SET play_time=unix_timestamp() WHERE id={$roomid}");
                    $gin["{$dname}"] = 0;
                }
                //if they have pieces on the bar
                if ($gin['abar'] > 0 && $move == 27) {
                    $temp2 = 25 - $dval;
                    $temp = "a" . $temp2;
                    $temp3 = "b" . $temp2;
                    if ($gin["{$temp3}"] <= 1) {
                        if ($gin["{$temp3}"] == 1) {
                            $db->query("UPDATE bg_game SET bbar = bbar + 1, {$temp3}=0 WHERE bg_room={$roomid}");
                        }
                        $db->query("UPDATE bg_game SET {$temp}={$temp}+1,abar=abar-1,{$dname} = 0,dselected = 0 WHERE bg_room={$roomid}");
                        $db->query("UPDATE bg_room SET play_time=unix_timestamp() WHERE id={$roomid}");
                        $gin["{$dname}"] = 0;
                    }
                }
                //moving into home
                if ($nextspot <= 0 && $move != 26 && $move != 27 && $gin['abar'] == 0) {
                    $temp = $gin['a0'] + $gin['a1'] + $gin['a2'] + $gin['a3'] + $gin['a4'] + $gin['a5'] + $gin['a6'];
                    if ($temp == 15) {
                        $temp = "a" . $move;
                        $db->query("UPDATE bg_game SET a0=a0+1, {$temp} = {$temp} -1,{$dname} = 0,dselected = 0 WHERE bg_room={$roomid}");
                        $db->query("UPDATE bg_room SET play_time=unix_timestamp() WHERE id={$roomid}");
                        $gin["{$dname}"] = 0;
                    }
                    $winner = check_win($roomid);
                    if ($winner != 0) {
                        $catch = 1;
                        award_win($roomid, $winner);
                    }
                }
            }
            //check for end of turn
            if ($gin['d1'] == 0 && $gin['d2'] == 0 && $gin['d3'] == 0 && $gin['d4'] == 0) {
                if ($catch == 0) {
                    toggle_turn($roomid);
                }
                //only toggle turn if no win yet
            }
            //select last die
            if ($gin['d1'] > 0 && $gin['d2'] == 0 && $gin['d3'] == 0 && $gin['d4'] == 0) {
                $db->query("UPDATE bg_game SET dselected=1 WHERE bg_room={$roomid}");
            }
            if ($gin['d1'] == 0 && $gin['d2'] > 0 && $gin['d3'] == 0 && $gin['d4'] == 0) {
                $db->query("UPDATE bg_game SET dselected=2 WHERE bg_room={$roomid}");
            }
            if ($gin['d1'] == 0 && $gin['d2'] == 0 && $gin['d3'] > 0 && $gin['d4'] == 0) {
                $db->query("UPDATE bg_game SET dselected=3 WHERE bg_room={$roomid}");
            }
            if ($gin['d1'] == 0 && $gin['d2'] == 0 && $gin['d3'] == 0 && $gin['d4'] > 0) {
                $db->query("UPDATE bg_game SET dselected=4 WHERE bg_room={$roomid}");
            }
        }
    } else {
        //print "<a href=\"http://www.seanybob.net/fail.html\" onclick=\"window.open('http://www.seanybob.net/fail.html','popup','width=500,height=500,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false\">About</a>";
    }
}