Skip to content

Instantly share code, notes, and snippets.

@selman
Created June 14, 2011 20:17
Show Gist options
  • Select an option

  • Save selman/1025766 to your computer and use it in GitHub Desktop.

Select an option

Save selman/1025766 to your computer and use it in GitHub Desktop.
Sayi Tahmini for Hazal
# -*- coding: utf-8 -*-
require 'sinatra'
enable :sessions
# sessiondaki type ve size'a gore yeni sayi tutar
def sayi_tut
case session[:type]
when :oct then ('0'..'7').to_a
when :dec then ('0'..'9').to_a
when :hex then ('0'..'9').to_a.concat(('a'..'f').to_a)
end.shuffle[0..(session[:size] - 1)]
end
# radio buttonlarini oyun turune gore isaretlemek icin
def check?(type)
session[:type] == type ? 'checked="checked"' : nil
end
# tutulan sayi hanesine uygun bir deger atar
def tahmin?(index, value)
if session[:sayi][index] == value
"dogru"
elsif session[:sayi].include?(value)
"iceriyor"
else
"yanlis"
end
end
# eger yoksa yeni bir sayi tutar, tahmin listesini olusturur
get '/' do
session[:size] ||= 4
session[:type] ||= :dec
session[:tahmin] ||= []
session[:sayi] ||= sayi_tut
erb :tahminler
end
# girilen sayiyi tahminlere ekler
post '/' do
tahmin = params[:tahmin].downcase.split("")
session[:tahmin].unshift(tahmin) unless tahmin.empty?
redirect '/'
end
# tutulan sayi ve tahminleri sifirlar
post '/reset' do
session[:size] = params[:size].to_i
session[:type] = params[:type].to_sym
session[:tahmin] = []
session[:sayi] = sayi_tut
redirect '/'
end
__END__
@@ layout
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html>
<head>
<title>Sayı Tahmini Oyunu</title>
<style type="text/css">
body {background:white;color:#2B2922;}
div#game {float:left;height:400px;width:150px;background:#A39770;}
div#tahminler {height:400px;overflow:auto;background:#EFE4BD;}
div#screen {padding:0.5em;width:350px;background:#BAB293;}
input {background:#EFE4BD;color:#A32500;}
.dogru {color:green;}
.iceriyor {color:blue;}
.yanlis {color:red;}
</style>
</head>
<body onload="document.tahmin_gonder.tahmin.focus();">
<div id="screen">
<h1>Sayi Tahmin Oyunu</h1>
<div id="game">
<form name="tahmin_gonder" action="" method="post">
<h2>Tahmin</h2>
<p><input type="text" name="tahmin" maxlength="<%=session[:size]%>" size="<%=session[:size]%>" /> </p>
</form>
</div>
<div id="tahminler">
<%=yield%>
</div>
<pre>
<span class="dogru">dogru yerde dogru rakam</span>
<span class="iceriyor">yanlis yerde dogru rakam</span>
<span class="yanlis">sayi rakami icermiyor</span>
</pre>
<form name="reset" action="reset" method="post">
<p>Hane Sayisi
<input type="text" name="size" value="<%=session[:size]%>" maxlength="1" size="1" /></p>
<fieldset>
<legend>Rakam Seti</legend>
<input type="radio" name="type" value="oct" <%=check?(:oct)%> />Octal
<input type="radio" name="type" value="dec" <%=check?(:dec)%> />Decimal
<input type="radio" name="type" value="hex" <%=check?(:hex)%> />Hexadecimal
</fieldset>
<p><input type="submit" value="Yeniden Baslat" /></p>
</form>
</div>
</body>
</html>
@@ tahminler
<ul>
<%session[:tahmin].each do |t|%>
<li>
<%t.each_index do |i|%>
<span class="<%=tahmin?(i, t[i])%>"><%=t[i]%></span>
<% end %>
</li>
<% end %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment