function check_win($roomid)
{
    global $db, $ir;
    $winner = 0;
    $ginf = $db->query("SELECT * FROM ck_game WHERE ck_room = {$roomid}");
    $gin = $db->fetch_row($ginf);
    $ui = $db->fetch_row($db->query("SELECT p1,p2,turn FROM ck_room WHERE id = {$roomid}"));
    $turn = $ui['turn'];
    $red = $gin['red'];
    if ($turn == $ui['p1']) {
        $user = $ui['p1'];
        $playersturn = $ui['p2'];
    } else {
        $user = $ui['p2'];
        $playersturn = $ui['p1'];
    }
    if ($red == $ui['p1']) {
        $white == $ui['p1'];
    } else {
        $white == $ui['p2'];
    }
    $hasamove = 0;
    for ($i = 0; $i <= 31; $i++) {
        if ($i == 0) {
            $i = 33;
        }
        $piece = $gin["b" . $i];
        if (abs($piece) == $user) {
            if ($piece > 0) {
                // function all_basic($move, $red, $userid)  	 	- gives all possibilities for a basic piece non-jump
                // function king_basic($move, $red, $userid) 		- gives all possibilities for a king piece non-jump
                // function second_jump($lastmove, $red, $userid)	- gives all possibilities for a basic piece jump
                // function king_second($lastmove, $red, $userid) 	- gives all possibilities for a king jump
                // function valid_move($userid,$red,$move,$pselect) - valid normal move
                // jump_move($userid,$red,$white,$move,$pselect)	- valid jump move
                // king_jump($userid,$red,$white,$move,$pselect)   	- valid king jump
                //does player have any normal moves
                $possible_moves = all_basic($i, $red, $turn);
                $valid1 = valid_move($turn, $red, $possible_moves["d1"], $i);
                $valid2 = valid_move($turn, $red, $possible_moves["d2"], $i);
                $temp1 = $possible_moves["d1"];
                $temp2 = $possible_moves["d2"];
                $openspot1 = $gin[b . "{$temp1}"];
                $openspot2 = $gin[b . "{$temp2}"];
                /// 1 -- 0 -- --
                if ($valid1 > 0 && $openspot1 == 0 || $valid2 > 0 && $openspot2 == 0) {
                    $hasamove = 1;
                }
                //does player have any normal jumps
                $possible_moves = second_jump($i, $red, $turn);
                // gives all possibilities for a basic piece jump
                $valid1 = jump_move($turn, $red, $white, $possible_moves["d1"], $i);
                // spot jumped
                $valid2 = jump_move($turn, $red, $white, $possible_moves["d2"], $i);
                //spot jumped
                $spotjumped1 = $gin[b . "{$valid1}"];
                //value of spot jumped
                $spotjumped2 = $gin[b . "{$valid2}"];
                //value of spot jumped
                $temp1 = $possible_moves["d1"];
                $temp2 = $possible_moves["d2"];
                $finish1 = $gin[b . "{$temp1}"];
                // spot landing
                $finish2 = $gin[b . "{$temp2}"];
                // spot landing
                if ($valid1 > 0 && $finish1 == 0 && ($spotjumped1 > 0 && abs($spotjumped1) != $turn) || $valid2 > 0 && $finish2 == 0 && ($spotjumped2 > 0 && abs($spotjumped2) != $turn)) {
                    $hasamove = 1;
                }
            } else {
                //does player have any king moves
                $possible_moves = king_basic($i, $red, $turn);
                $valid1 = valid_king_move($turn, $red, $possible_moves["d1"], $i);
                $valid2 = valid_king_move($turn, $red, $possible_moves["d2"], $i);
                $valid3 = valid_king_move($turn, $red, $possible_moves["d3"], $i);
                $valid4 = valid_king_move($turn, $red, $possible_moves["d4"], $i);
                $temp1 = $possible_moves["d1"];
                $temp2 = $possible_moves["d2"];
                $temp3 = $possible_moves["d3"];
                $temp4 = $possible_moves["d4"];
                $openspot1 = $gin["b" . $temp1];
                $openspot2 = $gin["b" . $temp2];
                $openspot3 = $gin["b" . $temp3];
                $openspot4 = $gin["b" . $temp4];
                if ($valid1 > 0 && $openspot1 == 0 || $valid2 > 0 && $openspot2 == 0 || $valid3 > 0 && $openspot3 == 0 || $valid4 > 0 && $openspot4 == 0) {
                    $hasamove = 1;
                }
                //does player have any king jumps
                $possible_moves = king_second($i, $red, $turn);
                // gives all possibilities for a basic piece jump
                $valid1 = king_jump($turn, $red, $white, $possible_moves["d1"], $i);
                // spot jumped
                $valid2 = king_jump($turn, $red, $white, $possible_moves["d2"], $i);
                //spot jumped
                $valid3 = king_jump($turn, $red, $white, $possible_moves["d3"], $i);
                // spot jumped
                $valid4 = king_jump($turn, $red, $white, $possible_moves["d4"], $i);
                //spot jumped
                $spotjumped1 = $gin[b . "{$valid1}"];
                //value of spot jumped
                $spotjumped2 = $gin[b . "{$valid2}"];
                //value of spot jumped
                $spotjumped3 = $gin[b . "{$valid3}"];
                //value of spot jumped
                $spotjumped4 = $gin[b . "{$valid4}"];
                //value of spot jumped
                $temp1 = $possible_moves["d1"];
                $temp2 = $possible_moves["d2"];
                $temp3 = $possible_moves["d3"];
                $temp4 = $possible_moves["d4"];
                $finish1 = $gin[b . "{$temp1}"];
                // spot landing
                $finish2 = $gin[b . "{$temp2}"];
                // spot landing
                $finish3 = $gin[b . "{$temp3}"];
                // spot landing
                $finish4 = $gin[b . "{$temp4}"];
                // spot landing
                if ($valid1 > 0 && $finish1 == 0 && ($spotjumped1 != 0 && abs($spotjumped1) != $turn) || $valid2 > 0 && $finish2 == 0 && ($spotjumped2 != 0 && abs($spotjumped2) != $turn) || $valid3 > 0 && $finish3 == 0 && ($spotjumped3 != 0 && abs($spotjumped3) != $turn) || $valid4 > 0 && $finish4 == 0 && ($spotjumped4 != 0 && abs($spotjumped4) != $turn)) {
                    $hasamove = 1;
                }
            }
        }
        if ($i == 33) {
            $i = 0;
        }
    }
    //Return the winner's userid
    if ($hasamove == 0) {
        $winner = $playersturn;
    }
    return $winner;
}
function make_move($move, $userid, $roomid)
{
    global $db, $ir;
    $game = $db->query("SELECT * FROM st_room WHERE id={$roomid}", $c) or die("1");
    $ga = $db->fetch_row($game);
    $game2 = $db->query("SELECT * FROM st_game WHERE st_room={$roomid}", $c) or die("1");
    $gijoe = $db->fetch_row($game2);
    $turn = $ga['turn'];
    $red = $gijoe['red'];
    if ($gijoe['p1'] == $red) {
        $blue = $gijoe['p2'];
    } else {
        $blue = $gijoe['p1'];
    }
    $pselect = $gijoe['pselected'];
    $uname = $db->fetch_row($db->query("SELECT username FROM users WHERE userid={$userid}"));
    $attackersusername = $uname['username'];
    if ($userid == $red) {
        $uname = $db->fetch_row($db->query("SELECT username FROM users WHERE userid={$blue}"));
    } else {
        $uname = $db->fetch_row($db->query("SELECT username FROM users WHERE userid={$red}"));
    }
    $defendersusername = $uname['username'];
    if ($turn == $userid) {
        if ($userid == $red) {
            $loc = "a";
            $antiloc = "b";
        } else {
            $loc = "b";
            $antiloc = "a";
        }
        if ($pselect == $move) {
            $db->query("UPDATE st_game SET pselected = 0 WHERE st_room={$roomid}");
            die;
        }
        //deselect their piece
        if ($gijoe[$loc . $move] != 0) {
            $db->query("UPDATE st_game SET pselected = {$move} WHERE st_room={$roomid}");
            die;
        }
        //basic moves
        //pselect working and deselect working basic moves so normal pieces can move.
        $piecetomove = $gijoe[$loc . $pselect];
        //out of bounds
        if ($move == 56 || $move == 46 || $move == 53 || $move == 43) {
            die;
        }
        if ($move < 11 || $move > 88 || $move == 19 || $move == 29 || $move == 39 || $move == 49 || $move == 59 || $move == 69 || $move == 79) {
            die;
        }
        //movement of linear pieces
        if (abs($piecetomove) > 0 && abs($piecetomove) < 7 || abs($piecetomove) == 8) {
            if ($gijoe[$antiloc . $move] != 0) {
                //if the oppent has a piece there ATTACK
                if (valid_move($pselect, $move) == 1) {
                    $piece1 = $gijoe[$loc . $pselect];
                    //attacker
                    $piece2 = $gijoe[$antiloc . $move];
                    //defender
                    if (attack_piece(abs($piece1), abs($piece2)) == 2) {
                        $startingspot = $loc . $pselect;
                        //start spot
                        $finishingspot = $loc . $move;
                        //finishing spot
                        $revealedpiece = "-" . abs($gijoe[$startingspot]);
                        // reveal the piece
                        $opponentsspot = $antiloc . $move;
                        // losers spot
                        $Attackname = number_to_name(abs($piece1));
                        // name of attackers piece
                        $Defendname = number_to_name(abs($piece2));
                        // name of defenders piece
                        $text = $attackersusername . "\\'s " . $Attackname . " (" . abs($piece1) . ") killed " . $defendersusername . "\\'s " . $Defendname . " (" . abs($piece2) . ")";
                        $piecelost = "p" . $loc . abs($piece2);
                        //piece captured
                        $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                        $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$piecelost} = {$piecelost} +1,{$startingspot} = 0, {$finishingspot} = {$revealedpiece}, {$opponentsspot} = 0, lastmove = {$move}, battletxt = '{$text}' WHERE st_room={$roomid}");
                        toggle_turn($roomid);
                    } else {
                        if (attack_piece(abs($piece1), abs($piece2)) == 1) {
                            $startingspot = $loc . $pselect;
                            $finishingspot = $antiloc . $move;
                            $piecesname = number_to_name(abs($piece1));
                            // name of the two pieces
                            $text = "Both " . $piecesname . "\\'s (" . abs($piece1) . ") were killed";
                            $piecelost = "p" . $loc . abs($piece2);
                            // piece captured
                            $piecelost2 = "p" . $antiloc . abs($piece1);
                            //piece lost
                            $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                            $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$piecelost} = {$piecelost} +1,{$piecelost2} = {$piecelost2} +1,{$startingspot} = 0, {$finishingspot} = 0, lastmove = {$move}, battletxt = '{$text}' WHERE st_room={$roomid}");
                            toggle_turn($roomid);
                        } else {
                            if (attack_piece(abs($piece1), abs($piece2)) == 3) {
                                $winningpiece = number_to_name(abs($piece1));
                                // name of the two pieces
                                $text = $attackersusername . "\\'s " . $winnpiece . " (" . abs($piece1) . ") captured the flag";
                                $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                                $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', battletxt = '{$text}' WHERE st_room={$roomid}");
                                award_win($roomid, $userid);
                            } else {
                                $Attackname = number_to_name(abs($piece1));
                                // name of attackers piece
                                $Defendname = number_to_name(abs($piece2));
                                // name of defenders piece
                                $text = $attackersusername . "\\'s " . $Attackname . " (" . abs($piece1) . ") lost to " . $defendersusername . "\\'s " . $Defendname . " (" . abs($piece2) . ")";
                                $startingspot = $loc . $pselect;
                                $finishingspot = $antiloc . $move;
                                $revealedpiece = "-" . abs($gijoe[$finishingspot]);
                                $piecelost = "p" . $antiloc . abs($piece1);
                                $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                                $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$piecelost} = {$piecelost} +1,{$startingspot} = 0, {$finishingspot} = {$revealedpiece}, lastmove = {$move}, battletxt = '{$text}' WHERE st_room={$roomid}");
                                toggle_turn($roomid);
                            }
                        }
                    }
                }
            } else {
                if ($gijoe[$loc . $move] == 0) {
                    if (valid_move($pselect, $move) == 1) {
                        $startingspot = $loc . $pselect;
                        $finishingspot = $loc . $move;
                        $piecetomove = $gijoe[$startingspot];
                        $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                        $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$startingspot} = 0, {$finishingspot} = {$piecetomove}, pselected = 0, lastmove = {$move}, battletxt = '' WHERE st_room={$roomid}");
                        toggle_turn($roomid);
                    }
                }
            }
        }
        //movement of scout
        if (abs($piecetomove) == 7) {
            if ($gijoe[$antiloc . $move] != 0) {
                //if the oppent has a piece there ATTACK
                $scoutresults = array();
                $scoutresults = valid_scout($pselect, $move);
                $differenceofspots = $scoutresults['distance'];
                $direction = $scoutresults['direction'];
                if ($differenceofspots > 0) {
                    $openspots = 1;
                    for ($i = 1; $i < $differenceofspots; $i++) {
                        if ($direction == 4) {
                            $checklake = $pselect + $i;
                            if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                $openspots = 0;
                            }
                            $temp1 = $loc . ($pselect + $i);
                            $temp2 = $antiloc . ($pselect + $i);
                            $openplayer = $gijoe[$temp1];
                            $openopponent = $gijoe[$temp2];
                            if ($openplayer != 0 || $openopponent != 0) {
                                $openspots = 0;
                            }
                        }
                        if ($direction == 3) {
                            $checklake = $pselect - $i;
                            if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                $openspots = 0;
                            }
                            $temp1 = $loc . ($pselect - $i);
                            $temp2 = $antiloc . ($pselect - $i);
                            $openplayer = $gijoe[$temp1];
                            $openopponent = $gijoe[$temp2];
                            if ($openplayer != 0 || $openopponent != 0) {
                                $openspots = 0;
                            }
                        }
                        if ($direction == 2) {
                            $movevalue = $i . "0";
                            $checklake = $pselect - $movevalue;
                            if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                $openspots = 0;
                            }
                            $temp1 = $loc . ($pselect - $movevalue);
                            $temp2 = $antiloc . ($pselect - $movevalue);
                            $openplayer = $gijoe[$temp1];
                            $openopponent = $gijoe[$temp2];
                            if ($openplayer != 0 || $openopponent != 0) {
                                $openspots = 0;
                            }
                        }
                        if ($direction == 1) {
                            $movevalue = $i . "0";
                            $checklake = $pselect + $movevalue;
                            if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                $openspots = 0;
                            }
                            $temp1 = $loc . ($pselect + $movevalue);
                            $temp2 = $antiloc . ($pselect + $movevalue);
                            $openplayer = $gijoe[$temp1];
                            $openopponent = $gijoe[$temp2];
                            if ($openplayer != 0 || $openopponent != 0) {
                                $openspots = 0;
                            }
                        }
                    }
                    if ($openspots == 1) {
                        $piece1 = $gijoe[$loc . $pselect];
                        //attacker
                        $piece2 = $gijoe[$antiloc . $move];
                        //defender
                        if (attack_piece(abs($piece1), abs($piece2)) == 2) {
                            $startingspot = $loc . $pselect;
                            //start spot
                            $finishingspot = $loc . $move;
                            //finishing spot
                            $revealedpiece = "-" . abs($gijoe[$startingspot]);
                            // reveal the piece
                            $opponentsspot = $antiloc . $move;
                            // losers spot
                            $Attackname = number_to_name(abs($piece1));
                            // name of attackers piece
                            $Defendname = number_to_name(abs($piece2));
                            // name of defenders piece
                            $text = $attackersusername . "\\'s " . $Attackname . " (" . abs($piece1) . ") killed " . $defendersusername . "\\'s " . $Defendname . " (" . abs($piece2) . ")";
                            $piecelost = "p" . $loc . abs($piece2);
                            //piece captured
                            $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                            $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$piecelost} = {$piecelost} +1,{$startingspot} = 0, {$finishingspot} = {$revealedpiece}, {$opponentsspot} = 0, lastmove = {$move}, battletxt = '{$text}' WHERE st_room={$roomid}");
                            toggle_turn($roomid);
                        } else {
                            if (attack_piece(abs($piece1), abs($piece2)) == 1) {
                                $startingspot = $loc . $pselect;
                                $finishingspot = $antiloc . $move;
                                $piecesname = number_to_name(abs($piece1));
                                // name of the two pieces
                                $text = "Both " . $piecesname . "\\'s (" . abs($piece1) . ") were killed";
                                $piecelost = "p" . $loc . abs($piece2);
                                // piece captured
                                $piecelost2 = "p" . $antiloc . abs($piece1);
                                //piece lost
                                $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                                $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$piecelost} = {$piecelost} +1,{$piecelost2} = {$piecelost2} +1,{$startingspot} = 0, {$finishingspot} = 0, lastmove = {$move}, battletxt = '{$text}' WHERE st_room={$roomid}");
                                toggle_turn($roomid);
                            } else {
                                if (attack_piece(abs($piece1), abs($piece2)) == 3) {
                                    $winningpiece = number_to_name(abs($piece1));
                                    // name of the two pieces
                                    $text = $attackersusername . "\\'s " . $Attackname . "captured the flag";
                                    $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                                    $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', battletxt = '{$text}' WHERE st_room={$roomid}");
                                    award_win($roomid, $userid);
                                } else {
                                    $Attackname = number_to_name(abs($piece1));
                                    // name of attackers piece
                                    $Defendname = number_to_name(abs($piece2));
                                    // name of defenders piece
                                    $text = $attackersusername . "\\'s " . $Attackname . " (" . abs($piece1) . ") lost to " . $defendersusername . "\\'s " . $Defendname . " (" . abs($piece2) . ")";
                                    $startingspot = $loc . $pselect;
                                    $finishingspot = $antiloc . $move;
                                    $revealedpiece = "-" . abs($gijoe[$finishingspot]);
                                    $piecelost = "p" . $antiloc . abs($piece1);
                                    $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                                    $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$piecelost} = {$piecelost} +1,{$startingspot} = 0, {$finishingspot} = {$revealedpiece}, lastmove = {$move}, battletxt = '{$text}' WHERE st_room={$roomid}");
                                    toggle_turn($roomid);
                                }
                            }
                        }
                    }
                }
            } else {
                if ($gijoe[$loc . $move] == 0) {
                    $scoutresults = array();
                    $scoutresults = valid_scout($pselect, $move);
                    $differenceofspots = $scoutresults['distance'];
                    $direction = $scoutresults['direction'];
                    if ($differenceofspots > 0) {
                        $openspots = 1;
                        for ($i = 1; $i < $differenceofspots; $i++) {
                            if ($direction == 4) {
                                $checklake = $pselect + $i;
                                if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                    $openspots = 0;
                                }
                                $temp1 = $loc . ($pselect + $i);
                                $temp2 = $antiloc . ($pselect + $i);
                                $openplayer = $gijoe[$temp1];
                                $openopponent = $gijoe[$temp2];
                                if ($openplayer != 0 || $openopponent != 0) {
                                    $openspots = 0;
                                }
                            }
                            if ($direction == 3) {
                                $checklake = $pselect - $i;
                                if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                    $openspots = 0;
                                }
                                $temp1 = $loc . ($pselect - $i);
                                $temp2 = $antiloc . ($pselect - $i);
                                $openplayer = $gijoe[$temp1];
                                $openopponent = $gijoe[$temp2];
                                if ($openplayer != 0 || $openopponent != 0) {
                                    $openspots = 0;
                                }
                            }
                            if ($direction == 2) {
                                $movevalue = $i . "0";
                                $checklake = $pselect - $movevalue;
                                if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                    $openspots = 0;
                                }
                                $temp1 = $loc . ($pselect - $movevalue);
                                $temp2 = $antiloc . ($pselect - $movevalue);
                                $openplayer = $gijoe[$temp1];
                                $openopponent = $gijoe[$temp2];
                                if ($openplayer != 0 || $openopponent != 0) {
                                    $openspots = 0;
                                }
                            }
                            if ($direction == 1) {
                                $movevalue = $i . "0";
                                $checklake = $pselect - $movevalue;
                                if ($checklake == 56 || $checklake == 46 || $checklake == 53 || $checklake == 43) {
                                    $openspots = 0;
                                }
                                $temp1 = $loc . ($pselect + $movevalue);
                                $temp2 = $antiloc . ($pselect + $movevalue);
                                $openplayer = $gijoe[$temp1];
                                $openopponent = $gijoe[$temp2];
                                if ($openplayer != 0 || $openopponent != 0) {
                                    $openspots = 0;
                                }
                            }
                        }
                        if ($openspots == 1) {
                            $startingspot = $loc . $pselect;
                            $finishingspot = $loc . $move;
                            $piecetomove = $gijoe[$startingspot];
                            $text = "";
                            $newmovelist = move_list($pselect, $move, $gijoe['listofmoves'], $gijoe['posa'], $attackersusername, $defendersusername);
                            $db->query("UPDATE st_game SET listofmoves = '{$newmovelist}', {$startingspot} = 0, {$finishingspot} = {$piecetomove}, pselected = 0, lastmove = {$move}, battletxt = '{$text}' WHERE st_room={$roomid}");
                            toggle_turn($roomid);
                        }
                    }
                }
            }
        }
    }
}