Google images game
Alec Jacobson
October 25, 2009
I finished coding a rather simple but addictive and challenging puzzle game, I'm calling the Google images game. The player is presented with the image results of a Google images search for some search word or phrase. The player’s objective is to guess and figure out what that search phrase was. Play it now!
The game is an amalgamation of php, javascript and ruby. The ajax javascript for the game is rather complicated by now to handle all of the gaming options, but it's all visible in the game's page source. I use a little ruby program to grab the initial google images thumbnails:
#!/usr/bin/ruby
require 'uri'
require 'net/http'
require 'cgi'
SAFE_VALUES = ["on","moderate","off"]
cgi = CGI.new("html3")
safe = "moderate"
safe = cgi["safe"] if(!cgi["safe"].empty? and SAFE_VALUES.include?(cgi["safe"]))
target = ""
target = cgi["target"] if(!cgi["target"].empty?)
if(!cgi["query"].empty?)
address = "http://images.google.com/images?q="+
cgi["query"].gsub(/ /,"+")+"&safe="+safe
uri = URI(address)
http = Net::HTTP.new(uri.host, uri.port)
headers = {
"User-Agent" => "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)"
}
code = http.head(address, headers).code.to_i
if (code >= 200 && code < 300) then
#the data is available...
response = ""
http.get(address, headers) do |chunk|
response = response + chunk
end
images = []
response.scan(
/imgurl=(.+?)&imgrefurl=(.+?)&[^<]+<img src=(.+?) /
) do |img, ref, thumb|
images<<[img,ref, thumb]
end
images.collect! do |e|
"<a title='"+e[1]+" 'href='"+
CGI.unescape(e[1]).gsub(/'/,"%27").gsub(/ /,"%20")+
"' target='"+target+"'><img src='"+e[2]+"' height='100px'></a>"
end
cgi.out{
images.join(" ")
}
end
end
I use a little php program to return a random line (initial search phrase/word) from a given file:
$difficulty = 'easy';
if (isset($_GET['difficulty'])&&strlen($_GET['difficulty'])>0) {
$difficulty= $_GET['difficulty'];
}
$word_list = 'nouns.txt';
$possible_word_lists = array('nouns.txt','wiki.txt');
if (isset($_GET['word_list'])&&strlen($_GET['word_list'])>0) {
if(in_array($_GET['word_list'],$possible_word_lists)){
$word_list = $_GET['word_list'];
}
}
$query = "";
$word_count = 1;
if(strcmp($word_list,'nouns.txt')==0){
if(strcmp($difficulty,"medium")==0){
$word_count = 2;
}else if(strcmp($difficulty,"hard")==0){
$word_count = 3;
}
}
for($i=0; $i<$word_count; $i=$i+1){
if(strlen($query)>0){
$query= $query."+";
}
$query = $query.`ruby -e "a=File.readlines('$word_list').collect{|line| line.strip.gsub(' ','+').gsub('\'','%27')};puts a[rand*a.leng
$query = trim($query);
}
print trim($query);
As you can see, I gave up on php half way through and cheated by calling a ruby one-liner. Someday I'll fix this up...