My program is checking for a winner in my blackjack game after the user clicks "stand".
When I try calling the winner with AJAX, I want to see if there is a winner. If yes, then alert the winner. Else, allow the dealer to make his move. Any help would be greatly appreciated.
Sample result/output after stand: User hand = dQ, hJ; val = 20 - alerts " wins! in stand"
Expected result after this hand: - go to ajaxDealer(user);
php code:
($checkPlayerHandValue works fine, i.e. if User has C5,HQ, will return 15)
function checkWinner($u, $d, $uValue) {
if($uValue > 21)
return $d;
}
if($checkPlayerHandValue > 0) {
$checkPlayerHandValue = checkPlayerHandValue($user);
$winner = checkWinner($user, $dealer , $checkPlayerHandValue);
}
$row = mysql_fetch_array($checkNextTurn);
$playerTurn = $row['user'];
echo($playerTurn."|");
echo($winner);
js code:
xmlhttpB.onreadystatechange=function() {
if (xmlhttpB.readyState==4 && xmlhttpB.status==200) {
elements = xmlhttpB.responseText.split("|");
user = elements[0];
winner = elements[1];
//alert(winner);
if(winner != "")
ajaxDealer(user);
else
alert(winner + " wins! in stand");
}
clickButton = true;
}
Do yourself a favor and start using the jQuery library with it's AJAX module.
As far as your question goes, can you tell us what the current problem with your code is (what is not happening that should be happening, or vice versa).
Also, I think this logic is incorrect:
if(winner != "")
ajaxDealer(user);
else
alert(winner + " wins! in stand");
And should be flipped:
if(winner == "")
ajaxDealer(user);
else
alert(winner + " wins! in stand");
Related
The input record includes fifteen fields named 'student01', 'student02', student03' ... 'student15'. I need to do the same thing with the value stored in each of the fields. There's got to be a better way than:
if ($student01 != '') {
// process the info in $student01
}
if ($student02 != '') {
// process the info in $student02
}
...
if ($student15 != '') {
// process the info in $student15
}
I was thinking that PHP's variable variables might be the solution, but haven't figured out the right syntax.
Help appreciated.
Thanks!
Change the number 3 in the for statement to be 1 more than the number of students you are going to have.
for ($snum = 1;$snum < 3;$snum++) {
#echo ${'student' . str_pad($snum,2,'0',STR_PAD_LEFT)}; // left to show it works
if (${'student' . str_pad($snum,2,'0',STR_PAD_LEFT)} != '') {
echo 'Do something';
}
}
Come to develop a small tanned algorithm logic and need help from someone to be able to finish it! Let's look at the code:
function likesresults($user_like,$total_like, $postid){
if($user_like == 1 && $total_like == 1){
$resposta_like = '<span id="you'.$_SESSION['user_id'].'">You like this.</span>';
}
if ($user_like == 1 && $total_like >= 2) {
$sub_like = $total_like-1;
if($sub_like -3 == 0) {
} else {
$total = $sub_like - 3;
$resto = ', and others '.$total.'';
}
$resposta_like = '<span id="you'.$_SESSION['user_id'].'">You, user1,user2,user3,user4'.$resto.' like this.';
}
if ($user_like == 0 && $total_like >= 1) {
if($total_like -3 == 0) {
} else {
$total = $sub_like - 3;
$resto = ', and others '.$total.'';
}
$resposta_like = 'user1,user2,user3,user4'.$resto.' like this.';
}
if($total_like == 0){
$resposta_like = "";
}
return $resposta_like;
}
It is simple and easy to understand, the problem is that I would like to show 4 users who also likes such content, and leave in order of "friendship", for example if the user likes it X1, beauty!, Displays it, but if X2 what the user likes and besides all still friends of the logged in user he has to appear before the X1, they are friends!, understood what I meant? I'm too lost in the logic of how to make this work, please give me a light, already thanks!
Resume: I would just like to list who likes that sort who likes content and order of friendship with the logged in user, ie who is friend appears before those who are not, as well as on facebook. .
I think my mind must be going through a Boxing Day mess.
I am building a basic comment section for every game a sports team plays.
So, when no comments are entered (in the MySQL DB), I simply want to display "Be the first to enter a comment"; otherwise, display the comment results table in html format.
I can easily display the comment result table.
For some reason, I can't get the IF no comments to work properly. Feel so amateurish right now . . . :-)
I have declared row count:
$row_count = 0;
I am adding to the count inside the while statement
while($row = mysql_fetch_array($result))
{
// adding to count
$row_count++;
My count is working as I can display the row number to the screen.
Here is IF / ELSE my code:
if ($row_count === 0) {
echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
} else {
// no need to show code as this already works!
you can use
mysql_num_rows($queryReference)
Hope this helps.
Thanks.
Please do one thing print this value using below function and tell me what is output
var_dump($row_count);
or you can use == instead of ===
$query = mysql_query("SELECT * FROM comments");
$c = mysql_num_rows($query);
if($c==0) {
echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
}
else {
while($row = mysql_fetch_array($query))
{
$vars = $row[index];
}
}
$row_count = 0;
I am adding to the count inside the while statement
while($row = mysql_fetch_array($result))
{
// adding to count
$row_count++;
}
My count is working as I can display the row number to the screen.
Here is IF / ELSE code:
if ($row_count == 0) {
echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
} else {
// no need to show code as this already works!
}
We have a system on our website wherein you can have multiple accounts and earn points on each. Those points you can transfer between the accounts on this webpage that I've noticed an error on. Basically, if I have a certain combination of accounts, namely a first and second, it won't let me transfer, it'll just say "Please fill in with a number.". If I have a first and third or all three, it works fine. I've been looking through it for about two hours now and can't find what isn't working... any help would be IMMENSELY appreciated :D
<?php if($a == "exchange")
{
$GetUserInfo = mysql_query("SELECT * FROM members WHERE id = '$userid'") or die(mysql_error());
$GetUserInfo = mysql_fetch_object($GetUserInfo);
$cols = 1; //determines colspan
$status = 1;
$GetMultipleInfo = mysql_query("SELECT * FROM members WHERE id = '".$GetUserInfo->mult_uid."'") or die(mysql_error());
if(mysql_num_rows($GetMultipleInfo) != 0)
{
++$cols;
++$status;
}
$GetMultipleInfo = mysql_fetch_object($GetMultipleInfo);
$GetAdMultipleInfo = mysql_query("SELECT * FROM members WHERE id = '".$GetUserInfo->mult_admin."'") or die(mysql_error());
if(mysql_num_rows($GetAdMultipleInfo) != 0)
{
++$cols;
$status = ($status == 2 ? 4 : 3);
}
$GetAdMultipleInfo = mysql_fetch_object($GetAdMultipleInfo);
// Sparks Transfer
if (isset($_POST['spartrans']))
{
$order = $_POST['sparrecipients'];
if ($order == 'first')
{
$tpoints2 = $_POST['tpoints2'];
$tpoints3 = $_POST['tpoints3'];
$tpoints = $tpoints2 + $tpoints3;
if ($status == 2)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || empty($tpoints1) || empty($tpoints2))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
elseif ($status == 3)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints3)) || empty($tpoints1) || empty($tpoints3))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
elseif ($status == 4)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || (!is_numeric($tpoints3)) || empty($tpoints1) || empty($tpoints2) || empty($tpoints3))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($tpoints2 > $GetMultipleInfo->tpoints)
message("" . getName($GetMultipleInfo->id) . " does not have enough sparks.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($tpoints3 > $GetAdMultipleInfo->tpoints)
message("" . getName($GetAdMultipleInfo->id) . " does not have enough sparks.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($GetUserInfo->mult_uid != 0)
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints - $tpoints2,0) WHERE id = '".$GetMultipleInfo->id."'") or die(mysql_error());
if ($GetUserInfo->mult_admin != 0)
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints - $tpoints3,0) WHERE id = '".$GetAdMultipleInfo->id."'") or die(mysql_error());
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints + $tpoints,0) WHERE id = '$userid'") or die(mysql_error());
message("Successfully transferred $tpoints Sparks to ".getName($userid).".","Enchanted Hogwarts","$PHP_SELF?a=exchange");
}
}
}
?>
In your code here you are establishing a couple of variables.
if ($order == 'first')
{
$tpoints2 = $_POST['tpoints2'];
$tpoints3 = $_POST['tpoints3'];
$tpoints = $tpoints2 + $tpoints3;
But then here you are checking the values of different variables. $tpoints1 was never established.
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || empty($tpoints1) || empty($tpoints2))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
EDIT: That is probably not correct. You probably have register_globals on which would be setting that variable automatically. Not recommended but not the question you asked.
Bottom line you need to figure out which branch of your code is being executed. My guess based on your description is that it is the $order == first branch and then $status == 3. But without actually submitting the form and knowing which selections were made it is impossible for me to tell. And the fact that all the error messages are the same "Please fill in with a number." is not very helpful.
I personally would take this time to refactor some of your code. If I don't understand something I break it apart into smaller chunks until I do. Otherwise you will always be sifting through this page every time something is wrong trying to figure it out.
Every selection of $order validates the values of $tpoints1, $tpoints2, and $tpoints3 exactly the same way. Start by moving that into a function and go from there.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Does anyone know the facemash algorithm that Mark Zuckerberg implemented in his facemash site?
http://www.thecrimson.com/article/2003/11/19/facemash-creator-survives-ad-board-the/
Preferably in PHP & MySQL.
UPDATE:
As I said in the comments, I've added this algerithm to my new website. At first it seemed to work perfectly. But after some weird inputs some strange result began to form.
While debugging I figured out what I was doing wrong. When getting the score for a "direct relationship" (used in the indirect relationship too) between 2 nodes I added the scores together. This was wrong, the score of a direct relationship should be expressed in -1 to +1, where:
-1 = lost everything
+1 = won everything
So if A won 8 times to B, and B won 2 times to A the score should be:
(A wins) 8 + (B wins) 2 = (total matches)10
(delta of -1 and +1 =) 2 / (total matches)10 = (points per win) 0.2
Score of A vs B = (points per win) 0.2 * (wins) 8 - 1 = 0.6
Score of B vs A = (points per win) 0.2 * (wins) 2 - 1 = -0.4
Also I didn't mention this in the original explanation but it is all about triangles. So when we look at the indirect score, you don't need to go any further than 1 hop.
I don't know what algorithm was actually used for the real-world site, but what they write on the window in the movie is based on the Elo rating system, which originated in the chess world and is now also used in many other games.
I recreated it aswell check it out.
Not sure about php but the C# class is
http://lukedurrant.com/2010/11/c-elo-rating-class-used-on-facemash-as-seen-in-the-social-network-movie/
I used it on my
Facemash
Key press code is
$(document).keydown(function(event) {
if (event.keyCode == 37) {
//Voted Face 1
Rate("face1", false);
}
if(event.keyCode == 39) {
//Voted Face 2
Rate("face2", false);
}
});
<?php
//This page is responsible to return a JSON object
//code starts after the functions for those who might get confused xD
header('content-type: application/json; charset=utf-8');
global $responseData;
function AdjustRate($Ra, $Ea, $Sa)
{
//i used my own rules here 32 points for less than 500
if($Ra < 500)
$k = 32;
elseif ($Ra < 1000)//24 points for anything between 500 and 1000
$k = 24;
else
$k = 16;//16 for anything more than 1000
return $Ra + ($k*($Sa - $Ea));
}
function GetExpectedChance($rA, $rB) // the ELO formula taken from http://en.wikipedia.org/wiki/Elo_rating_system
{
return (1/(1+pow(10,(($rB-$rA)/400))));
}
function setNewRates($lastCall) // function I used to update my database tables
{
global $responseData;
$A = $lastCall->p1;
$B = $lastCall->p2;
$C = $lastCall->c;
$A->E = GetExpectedChance($A->rate, $B->rate);
$B->E = GetExpectedChance($B->rate, $A->rate);
// decide who won and who lost
if($A->id == $C){
$winner = $A;
$looser = $B;
}
elseif ($B->id == $C) {
$winner = $B;
$looser = $A;
}
// 3 cases, in all of them winner will get his rate/hits increased by 1
//Case #1: normal case we just update rate/hits for the winner, this applies all the time
$winner->rate += 1;
$winner->hits += 1;
//Case #2 / #3 : here we should adjust the rate after applying case #1
// if he won while he is expected to lose OR if he lost while expected to win
// there should be minimum rate different of 40 between the two
$diff = abs($winner->rate - $looser->rate);
if($diff >= 40 && ($winner->E < 0.5 || $looser->E >= 0.5)) {
$winner->rate = AdjustRate($winner->rate, $winner->E, 1);
$looser->rate = AdjustRate($looser->rate, $looser->E, 0);
}
// update the db to update rates, hits for both winner and looser
$updateQuery = 'UPDATE user SET rate='.$winner->rate.',hits='.$winner->hits.' WHERE id=' . $winner->id;
mysql_query($updateQuery);
$updateQuery = 'UPDATE user SET rate='.$looser->rate.' WHERE id=' . $looser->id;
mysql_query($updateQuery);
// Save to responsedate
$responseData->winner = $winner;
$responseData->looser = $looser;
}
//CODE STARTS HERE :)
// Setup the mysql connection
include 'db.php';
// Part 1: calculate the rate and save to db, if we have a lastcall
// GET the last call data object, it has p1, p2, c, these are the items i recieved from my javascript ajax call
$lastCall = json_decode((string)$_GET['lastCall']); // it was a JSON object so i need to decode it first
// Save last call data, will be sent with the respond as well
$responseData->lastCall = $lastCall;
// if there is a json object, means that there was a rating process and I have to set the new rates
if($lastCall->c != NULL)
{
setNewRates($responseData->lastCall);
}
// Part 3: Select new persons and addthem to our responseData
$q = Array();
$q[0] = 'SELECT id, name, sex, rate, hits FROM user WHERE fm_status=1 AND sex="female" ORDER BY RAND() LIMIT 2';
$q[1] = 'SELECT id, name, sex, rate, hits FROM user WHERE fm_status=1 AND sex="male" ORDER BY RAND() LIMIT 2';
// girls or boys ?
srand(mktime());
$query = $q[array_rand($q)];
$result1 = QueryIntoArray($query);
$responseData->user = $result1;
// Part 4: encode to JSON/JSONP string then respond to the call
$json = json_encode($responseData);
$json = isset($_GET['callback'])? "{$_GET['callback']}($json)" : $json;
echo $json;
mysql_close();
// by Noor Syron :)
//I used this in my www.mimm.me
?>
`I have designed the code in Perl all from google searching and it works.
Here's It
use strict;
use warnings;
use WWW::Mechanize;
use LWP::Simple;
sub images()
{
my $mech = WWW::Mechanize->new();
my ($site,$count,$file,$dir);
print "\t\t\tDesigned By NUMWARZ GAMING\n\n";
print "Enter the name of the site you want to search for images\t:\n";
$site = <STDIN>;
print "Enter the folder where you want to save this\t:\n";
$dir = <STDIN>;
open my $doc, ">" , $dir."sitelist.txt";
$mech->get( $site);
my #links = $mech->images();
$count = 0;
for my $link ( #links )
{
$file = $dir.$count.".jpg";
mirror($link->url,$file);
print $file," : "$link->url,"\n";
print $doc $link->url." ".$file."\n";
$count+=1;
}
close $doc;
exit;
}
images();
No not here not anywhere on the web. The Facemash source code has never been released to the public. The only one, who might still have a copy is Mark Zuckerberg himself.
here is a clone of facemash available http://www.facemash.99k.org
I've recreated it as well, but in a WordPress plugin
http://pofornot.com/
http://codecanyon.net/item/pics-mash-image-rating-tool/3256459?ref=mikemayhem3030