Game Sudoku

Berawal dari sebuah permainan game mengasah otak, dari sekian game pengasah otak saat ini permainan "Sudoku" tetap menjadi yang number one untuk dinikmati, khususnya saat seorang 2 cowok ganteng aslab internet dasar mengisi waktu luangnya. Keisengan mencoba untuk membuat game ini pun muncul disaat genting gentingnya H-3 menjelang Ujian Utama yang tidak ada bekalnya materi satupun dikelas saya. (STOP dong Curhatnya!!).. hehe

Outputnya akan dihasilkan sebagai berikut:


Nah Berikut ini Source COde dari Sudoku Versi Pascalnya:

program sudoku;
uses crt,dos;
type
puzzle=array[0..80]of integer;
arraysol=array[0..2]of puzzle;
waktu=record
jam,menit,detik,sec:word;
end;
const
database:arraysol=(
(4,2,9,3,1,6,5,7,8
,8,6,7,5,2,4,1,9,3
,5,1,3,8,9,7,2,4,6
,9,3,1,7,8,5,6,2,4
,6,8,2,9,4,1,7,3,5
,7,4,5,2,6,3,9,8,1
,3,5,4,6,7,2,8,1,9
,1,7,8,4,5,9,3,6,2
,2,9,6,1,3,8,4,5,7),
(9,6,5,4,1,8,7,3,2
,1,4,3,2,6,7,9,5,8
,8,2,7,9,5,3,6,1,4
,5,7,9,3,8,4,1,2,6
,4,1,2,6,9,5,3,8,7
,6,3,8,1,7,2,4,9,5
,3,5,4,7,2,1,8,6,9
,7,8,6,5,3,9,2,4,1
,2,9,1,8,4,6,5,7,3),
(1,2,5,8,9,7,6,3,4
,6,7,4,5,1,3,9,2,8
,3,9,8,4,2,6,1,5,7
,4,8,2,6,5,9,7,1,3
,7,6,9,2,3,1,4,8,5
,5,3,1,7,8,4,2,9,6
,2,4,3,9,7,5,8,6,1
,9,5,6,1,4,8,3,7,2
,8,1,7,3,6,2,5,4,9));
var
arrayrandom:array[1..21]of integer;
rdonly:array[0..80]of boolean;
currentpuzzle:puzzle;
jawaban:puzzle;
nama:string;
level:integer;
tombol:char;
waktuawal,waktuakhir:waktu;

function chartoint(x:char):integer;
begin
chartoint:=ord(x)-48;
end;

procedure generatearrayrandom;
var i,j,tmp:integer;
valid:boolean;
begin
i:=0;
randomize;
while i<=21 do
begin
valid:=true;
tmp:=random(81);
for j:=1 to i do
begin
if arrayrandom[j]=tmp then
valid:=false;
end;
if valid then
begin
arrayrandom[i]:=tmp;
inc(i);
end;
end;
end;

procedure tampilbox;
var i,j:integer;
begin
clrscr;
for i:=1 to 19 do
begin
for j:=1 to 19 do
begin
if (i=1) or (i=19) or (j=1) or (j=19)
or (i mod 2 <>0) or (j mod 2 <>0)
then
begin
gotoxy(j,i);write('#');
end;
end;
writeln;
end;
gotoxy(2,2);
end;
procedure inputnama;
begin
clrscr;
write('Masukkan nama anda : ');
read(nama);
end;

procedure pilihjawaban;
begin
randomize;
currentpuzzle:=database[random(3)];
end;

procedure transform(tipe: integer);
var i,tmp,tmpindx,mod9,div9:integer;
begin
case tipe of
0:for i:=0 to 80 do {vertikal}
if(i mod 9 < 4) then
begin
tmp:=currentpuzzle[i];
div9:=i div 9;
tmpindx:=(9*div9+8)-(i-(9*div9));
currentpuzzle[i]:=currentpuzzle[tmpindx];
currentpuzzle[tmpindx]:=tmp;
end;
1:for i:=0 to 80 do{diagonal kanan}
if(i div 9 + i mod 9 < 8) then
begin
mod9:=i mod 9;
div9:=i div 9;
tmp:=currentpuzzle[i];
tmpindx:=(8-mod9) * 9 + 8 - div9;
currentpuzzle[i]:=currentpuzzle[tmpindx];
currentpuzzle[tmpindx]:=tmp;
end;
2:for i:=0 to 80 do{diagonal kiri}
if(i div 9 > i mod 9) then
begin
mod9:=i mod 9;
div9:=i div 9;
tmp:=currentpuzzle[i];
tmpindx:=div9+mod9*9;
currentpuzzle[i]:=currentpuzzle[tmpindx];
currentpuzzle[tmpindx]:=tmp;
end;
3:for i:=0 to 80 do {horizontal}
if(i div 9 < 4) then
begin
mod9:=i mod 9;
div9:=i div 9;
tmp:=currentpuzzle[i];
tmpindx:=mod9+(8-div9)*9;
currentpuzzle[i]:=currentpuzzle[tmpindx];
currentpuzzle[tmpindx]:=tmp;
end;
end;
end;
procedure randomjawaban;
var i:integer;
begin
randomize;
for i:=1 to 7 do
transform(random(4));
end;

procedure pilihlevel;
begin
write('Pilih level yang anda inginkan [1--7] : ');
read(level);
if not level in [1,2,3,4,5,6,7] then
level:=7;
end;

function checkrandom(data:integer):boolean;
var
tmp:boolean;
i:integer;
begin
tmp:=true;
for i:=1 to level*3 do
if arrayrandom[i]=data then
begin
tmp:=false;
break;
end;
checkrandom:=tmp;

end;

procedure tampilsebagian;
var i:integer;
begin
generatearrayrandom;
for i:=0 to 80 do
begin
if checkrandom(i) then
begin
gotoxy(2+2*(i mod 9),2+2*(i div 9));
write(currentpuzzle[i]);
jawaban[i]:=currentpuzzle[i];
rdonly[i]:=true;
end;
end;
gotoxy(2,2);
end;
function checkjawaban:boolean;
var i:integer;
tmp:boolean;
begin
tmp:=true;
for i:=0 to 80 do
if jawaban[i]<>currentpuzzle[i] then
begin
tmp:=false;
break;
end;
checkjawaban:=tmp;
end;
begin
textcolor(green);
inputnama;
pilihlevel;
tampilbox;
pilihjawaban;
randomjawaban;
tampilsebagian;
textcolor(red);
gettime(waktuawal.jam,waktuawal.menit,waktuawal.detik,waktuawal.sec);
repeat
tombol:=readkey;
if ord(tombol)=0 then
begin
tombol:=readkey;
case ord(tombol) of(*atas bawah kiri kanan*)
72:if wherey<>2 then
gotoxy(wherex,wherey-2);
80:if wherey<>18 then
gotoxy(wherex,wherey+2);
75:if wherex<>2 then
gotoxy(wherex-2,wherey);
77:if wherex<>18 then
gotoxy(wherex+2,wherey);
end;
end;
if (tombol>='0') and (tombol<='9') and not (rdonly[(wherex div 2 - 1)+(wherey div 2 - 1)*9]) then
begin
jawaban[(wherex div 2 -1)+(wherey div 2 - 1)*9]:=chartoint(tombol);
write(tombol);
gotoxy(wherex-1,wherey);
end;
until (ord(tombol)=27) or checkjawaban;
if checkjawaban then
begin
clrscr;
gettime(waktuakhir.jam,waktuakhir.menit,waktuakhir.detik,waktuakhir.sec);
writeln('selamat, ',nama,' anda sukses pada sudoku level ',level);
write('waktu anda : ');
if waktuakhir.menit-waktuawal.menit<>0 then
write(waktuakhir.menit-waktuawal.menit,' menit');
write(waktuakhir.detik-waktuawal.detik,' detik');
end;
readln;
readln;
normvideo;
end.

Sekian dari Fauzi Online, Semoga bermanfaat yah... ^_^

0 comments:

Post a Comment