មេរៀនទី១៥: អនុវត្តបង្កើតហ្គេម


















មេរៀនទី១៥: អនុវត្តបង្កើតហ្គេម

ហ្គេមចាក់លេខ(ចាក់អាប៉ោង) ភាសាC
C C++
//ពេលដំនើរការGame នេះវានិងអោយយើងបញ្ចូលនូវ សំរាប់ ធ្វើការលេង|
//បើចង់ដាក់ទឹកប្រាក់ធ្វើការភ្នាល់ គឺគ្រាន់តែចុចសញ្ញា +(បន្ថែម) រឺ – (បន្ថយ)
//បើចង់Selectយកលេខណាមួយយើងប្រើ w,a,s,d រួចចុច Enter
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int col=1,k;
long int imon=0,tmon;
char ch,ch1;
clrscr();
printf(“Enter Total money=”);scanf(“%ld”,&tmon);
clrscr();
do
{       gotoxy(2,40);printf(“Press ESC to exit!”);
gotoxy(50,2);printf(“Total money=%ld$”,tmon);
gotoxy(50,4);printf(“Input  money=%ld$”,imon);
textbackground(RED);
gotoxy(5,5);cprintf(“1″);
gotoxy(9,5);cprintf(“2″);
gotoxy(13,5);cprintf(“3″);
gotoxy(5,9);cprintf(“4″);
gotoxy(9,9);cprintf(“5″);
gotoxy(13,9);cprintf(“6″);
textbackground(BLACK);
switch(col)
{    case 1:    gotoxy(5,5);cprintf(“1″);break;
case 2:    gotoxy(9,5);cprintf(“2″);break;
case 3:    gotoxy(13,5);cprintf(“3″);break;
case 4:    gotoxy(5,9);cprintf(“4″);break;
case 5:    gotoxy(9,9);cprintf(“5″);break;
case 6:    gotoxy(13,9);cprintf(“6″);break;
}
input:
ch=getch();
if(ch==’s’||ch==’a’||ch==’d’||ch==’w’)
{
switch(ch)
{    case ‘s’:      if(col==1)    col=4;
else if(col==2)    col=5;
else if(col==3)    col=6;
else if(col==4)    col=1;
else if(col==5)    col=2;
else        col=3;
break;
case ‘a’:    if(col==1)    col=3;
else if(col==2)    col=1;
else if(col==3)    col=2;
else if(col==4)    col=6;
else if(col==5)    col=4;
else        col=5;
break;
case ‘d’:    if(col==1)    col=2;
else if(col==2)    col=3;
else if(col==3)    col=1;
else if(col==4)    col=5;
else if(col==5)    col=6;
else        col=4;
break;
case ‘w’:    if(col==1)    col=4;
else if(col==2)    col=5;
else if(col==3)    col=6;
else if(col==4)    col=1;
else if(col==5)    col=2;
else        col=3;
break;
}
}
else if(ch==’+’||ch==’-‘)
{
if(tmon==0&&imon==0)
goto exit2;
else
{    if(tmon>0)
{    if(imon>0)
{    if(imon==0)
{    imon++;
tmon–;
}
else if(ch==’+’)
{    imon++;
tmon–;
}
else
{    imon–;
tmon++;
}
}
else
{    if(ch==’+’)
{    imon++;
tmon–;
}
}
}
else
{    if(ch==’-‘)
{    tmon++;
imon–;
}
}
}
}
else if(ch==27)
exit(0);
else if (ch==13)
{    int i=1,count=1;
for(;;i++)
{       if(i>6)i=1;
gotoxy(20,20);
printf(“Number is %d”,i);
delay(100);
count++;
if(count==20)
{    randomize();
i=random(7);
if(i==0)
i++;
gotoxy(20,20);
textbackground(DARKGRAY);
cprintf(“Number is %d”,i);
delay(200);
break;
}
}
gotoxy(20,22);
textcolor(WHITE);
if(col==i)
{    tmon=tmon+imon*4;
printf(“You win! %ld$”,4*imon);
imon=0;
}
else
{    printf(“You lose!”);
imon=0;
}
gotoxy(20,24);
printf(“Press ‘SPACE’ to play again!”);
again:
ch1=getch();
if(ch1==32)
goto again1;
else if(ch1==27)
exit(0);
else
goto again;
}
else
goto input;
again1:
clrscr();
}while(!kbhit());
exit2:
gotoxy(25,6);
printf(“Don’t have $$ press anykey to exit!”);
getch();
}
//បញ្ចប់
ហ្គេមចងចាំ
C C++

នេះ ​គឺ​ជា​ហ្គេម​តំ​រូវ​អោយ​យើង​ធ្វើ​ការ​​រក​គូ​របស់លេខ​​! ​ លេខ​០​ទាំង​អស់​នេះ​តំ​ណាង​ប្រ​អប់​ដែល​មិន​ទាន់​បាន​បើក​ បើ​យើង​ចង់​បើក​វា​យើង​គ្រាន់​តែ​ចុច​ enter វា​និង​បង្ហាញ​នូវ​លេខ​ដែល​មាន​នៅ​ខាង​ក្នុង​ប្រ​អប់​នោះ​ជា​មិន​ខាន់​ ការ​បើក​ប្រ​អប់​ចំ​នួន​២​លើក​ ​បើ​ការ​បើក​លើ​ក​ទី​១​ និង​លើក​ទី​២​មាន​លេខ​មិន​ដូច​គ្នា​ទេ​ វា​និង​បិទ​​លេខ​ដែល​បង្ហាញ​នោះ​ទៅ​វិញ​ដោយ​បង្ហាញ​លេខ​០ តែ​បើ​​ការ​បើក​ទាំង​២​លើក​មាន​លេខ​ដូច​គ្នា​ វា​និង​បង្ហាញ​លេខ​នោះ​​រហូត​។
ពេល​ដែល​យើង​ចាប់​ផ្តើម​លេង​ វា​និង​បង្ហាញ​លេខ​ដែល​មាន​នៅ​ក្នុង​គ្រប់​ប្រ​អប់​បន្តិច​សិន​រួច​ហើយ​ទើប​​ចាប់​ផ្តើម​លេង
ចំ​ណាំៈ​ ដើម្បី​ធ្វើ​ការ​ move យើង​ប្រើ​ w,a,s,d យើង​ត្រូវ​លេង​វា​តាម​tc មិន​ត្រូវ​បើ​ក​ការ​ file exeទេវា​ត្រូវ​ការ​​ screen ធំ​
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
void main()
{       clrscr();
int r=10,c=10,i,j,b[10][10],x=0,y=0,count=0,x1,x2,y1,y2,win=0;
char ch=27;
_setcursortype(_NOCURSOR);
char level[3][10]={“Easy”,”Medium”,”Hard”},levelch;
int levelmove=0;
do
{    for(i=0;i<3;i++)
if(levelmove==i)
{    textcolor(RED);
cprintf(“\n\n\r\r%s”,level[i]);
}
else
{    textcolor(WHITE);
cprintf(“\n\n\r\r%s”,level[i]);
}
levelch=getch();
switch(levelch)
{    case ‘w’:    levelmove–;break;
case ‘s’:    levelmove++;break;
case 13:        goto daro;
}
if(levelmove>=3)
levelmove=0;
if(levelmove<0)
levelmove=2;
clrscr();
}while(1);
daro:
int a[10][10];
if(levelmove==0)
{    int  a0[10][10]={    {1,5,1,5,2,4,1,2,1,5},
{1,3,3,2,4,5,1,3,1,3},
{2,2,4,2,5,3,5,5,2,4},
{1,1,3,5,4,2,1,3,5,5},
{2,3,3,1,4,2,1,3,1,5},
{1,2,4,3,4,1,3,4,2,4},
{3,2,4,5,1,2,3,3,2,1},
{2,4,2,5,3,4,5,4,1,4},
{4,3,5,4,5,3,2,1,5,5},
{2,2,4,5,3,4,4,1,3,5},
};
for(i=0;i<10;i++)
for(j=0;j<10;j++)
a[i][j]=a0[i][j];
}
else if(levelmove==1)
{     int     a1[10][10]={    {4,2,1,5,4,4,5,4,7,5},
{2,3,4,2,1,5,4,3,5,6},
{3,5,4,2,1,3,5,3,4,1},
{6,7,3,5,1,4,3,2,2,5},
{6,4,7,3,5,6,1,2,6,4},
{7,3,2,6,6,7,1,6,4,2},
{6,5,6,7,7,1,7,1,6,1},
{7,4,7,5,5,1,6,3,3,2},
{6,1,2,7,2,1,7,2,3,3},
{7,1,5,7,6,1,1,2,4,3},
};
for(i=0;i<10;i++)
for(j=0;j<10;j++)
a[i][j]=a1[i][j];
}
else if(levelmove==2)
{      int    a2[10][10]={    {6,1,5,4,3,9,5,10,6,2},
{10,3,3,6,9,9,8,2,5,3},
{7,4,3,4,2,10,4,3,2,5},
{2,1,6,10,9,1,5,1,4,5},
{7,3,8,1,7,2,10,4,5,6},
{6,4,3,5,8,1,10,1,4,5},
{7,8,6,5,1,2,10,3,6,8},
{1,2,7,2,8,9,6,10,4,9},
{9,9,7,10,2,7,3,7,8,7},
{8,1,8,7,9,8,6,4,9,10},
};
for(i=0;i<10;i++)
for(j=0;j<10;j++)
a[i][j]=a2[i][j];
}
clrscr();
for(i=0;i
{    for(j=0;j<c;j++)
{          b[i][j]=0;
printf(“%5d”,a[i][j]);
}
printf(“\n\n\n\n”);
}
delay(1000);
clrscr();
do
{        for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(a[i][j]==b[i][j])
win++;
if(win==(c*r))
{      clrscr();
textcolor(RED+128);
cprintf(“Congratulation \n\rYou win!”);
cprintf(“\n\rThank for playing”);
cprintf(“\n\rPress any key to exit”);
getch();
exit(1);
}
else
win=0;
for(i=0;i<r;i++)
{    for(j=0;j<c;j++)
if(i==x&&j==y)
{    textcolor(RED);
cprintf(“%5d”,b[i][j]);
}
else
{       textcolor(WHITE);
cprintf(“%5d”,b[i][j]);
}
printf(“\n\n\n\n”);
}
ch=getch();
switch(ch)
{
case ‘d’:y++;
break;
case ‘s’:x++;
break;
case ‘a’:y–;
break;
case ‘w’:x–;
break;
case 13: if(count==0&&b[x][y]==0)
{    count=1;
x1=x;y1=y;
b[x][y]=a[x][y];
}
else if(count==1&&b[x][y]==0)
{      if(x1==x&&y1==y)
break;
b[x][y]=a[x][y];
clrscr();
for(i=0;i<r;i++)
{    for(j=0;j<c;j++)
if(x==i&&y==j)
{    textcolor(RED);
cprintf(“%5d”,b[i][j]);
}
else
{    textcolor(WHITE);
cprintf(“%5d”,b[i][j]);
}
printf(“\n\n\n\n”);
}
delay(400);
if(b[x1][y1]!=b[x][y])
{    b[x1][y1]=0;
b[x][y]=0;
}
count=0;
}
break;
}
if(y<0)   y=c-1;
if(y>c-1) y=0;
if(x<0)   x=r-1;
if(x>r-1) x=0;
clrscr();
}while(ch!=27);
getch();
}

ហ្គេមតំរៀបលេខ ភាសាC
C C++
//ទាំងនេះជាហ្គេមតំរៀបលេខដែលសរសេរក្នុងភាសាC ពេលចាប់ផ្តើមលេងវានិងអោយយើង
//វាយលេខចំនួនជួរឈរនិងជួរដេក រួចចាប់ផ្តើមលេង។ លេខត្រូវបានតំរៀបដោយ​ random
//បើ​អ្នក​តំរៀប​លេខ​នោះ​តាម​លំដាប់​កើនចប់​សព្វ​គ្រប់​នោះ​អ្នក​​​និងឈ្នះដោយ​បង្ហាញ​ message យើងអាចធ្វើការរំកិលលេខដោយប្រើ w,a,s,d
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
void ran(int [20][20],int r,int c);
void main()
{
int a[20][20],r,c,i,j;
again:
clrscr();
printf(“Please enter rows:”);
scanf(“%d”,&r);
printf(“Please enter colums:”);
scanf(“%d”,&c);
ran(a,r,c);
clrscr();
char ch;
do
{    clrscr();
printf(“To EXIT press ‘ESC’!\ta=left,w=up,s=down,d=right\n\n\n”);
for( i=0;i
{    for( j=0;j<c;j++)
{    if(a[i][j]==0)
printf(”     “);
else
printf(“%5d”,a[i][j]);
}
printf(“\n\n\n”);
}
int z=1;
for(i=0;i
for(j=0;j<c;j++)
if(z==a[i][j])
z++;
else
goto a;
a:
if(z==(r*c))
{    printf(“You win!”);
printf(“\nDo u want to play again(y/n):”);
ch=getch();
if(ch==’y’||ch==’Y’)
goto again;
else
goto exit1;
}
ch=getch();
if(ch==’a’||ch==’d’||ch==’s’||ch==’w’)
{    switch(ch)
{    case ‘a’:    for(i=0;i
for(j=0;j<c;j++)
if(a[i][j]==0)
{    if(j==(c-1))
goto exit;
else
{    a[i][j]=a[i][j+1];
a[i][j+1]=0;
}
goto exit;
}
break;
case ‘d’:    for(i=0;i
for(j=0;j<c;j++)
if(a[i][j]==0)
{       if(j==0)
goto exit;
else
{    a[i][j]=a[i][j-1];
a[i][j-1]=0;
}
goto exit;
}
break;
case ‘w’:    for(i=0;i
for(j=0;j<c;j++)
if(a[i][j]==0)
{
if(i==(r-1))
goto exit;
else
{    a[i][j]=a[i+1][j];
a[i+1][j]=0;
}
goto exit;
}
break;
case ‘s’:    for(i=0;i
{    for(j=0;j<c;j++)
if(a[i][j]==0)
{       if(i==0)
goto exit;
else
{    a[i][j]=a[i-1][j];
a[i-1][j]=0;
}
goto exit;
}
}
break;
}
exit:
}
}
while(ch!=27);
getch();
exit1:
}
void ran(int a[20][20],int r,int c)
{  printf(“Please wait while loading….”);
int b[100],e[100],t=0,j,i;
for(i=0;i<100;i++)
b[i]=0;
for(i=0;i<r*c;i++)
{    again:
randomize();
e[i]=random(10000)%(r*c+1);
for(int j=0;j<=i;j++)
if(b[j]!=e[i])    t++;
else    goto again;
if(t<=i)
{  t=0;
goto again;
}
t=0;
b[i]=e[i];
}
int cre=0,r1,c1,max=r*c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{    a[i][j]=e[cre];
cre++;
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(max==a[i][j])
{    r1=i;
c1=j;
}
a[r1][c1]=0;
}

លេងត្ររ(OX) នៅលើ Turbo C

C C++
/*នេះជាGame ត្រូវមានអ្នកលេង២នាក់ ពេលចាប់ផ្តើមវា វាអោយយើងវាឈ្មោះរួចអោយយើងជ្រើសរើសការលេង មាន២យ៉ាងគឺ
ការលេងមានម៉ោងកំណត់ និងមិនមានម៉ោងកំណត់។
ចំណាំ​​​ ដើម្បីធ្វើការរំកិលយើងប្រើ w,a,s,d រួចលេងពេលដាក់គ្រាប់ចុច Enter ហើយយើងត្រូវប្រើប្រាស់ turbo C លេង វាត្រូរការscreen ធំ មិនត្រូវបើកតាមfile exe ទេ*/
#include<stdio.h>
#include<dos.h>
#include<conio.h>
void main()
{    clrscr();
int row=16,col=16,i,j,x=1,y=1,time,time1;
char player1[50],player2[50],ch;
textcolor(WHITE);cprintf(“Player1 is:”);
textcolor(RED);  cprintf(“o”);
textcolor(WHITE);cprintf(“\n\rPlayer2 is:”);
textcolor(BLUE); cprintf(“x”);
textcolor(WHITE);
cprintf(“\n\n\r\rEnter player1’s name:”);gets(player1);
cprintf(“\n\n\r\rEnter player2’s name:”);gets(player2);
cprintf(“\n\n\r\rChose type of play:”);
cprintf(“\n\rPress 1:Play with limit time”);
cprintf(“\n\rPress 2:Play without limit”);
ch=getch();
if(ch==’1′)
{    clrscr();
cprintf(“\n\n\r\rMaximum time is 9999s if u set larger than 9999s.”);
cprintf(“\n\rTime is automatically set 9999s.”);
cprintf(“\n\n\r\rPlease Set time=”);
scanf(“%d”,&time1);
if(time1>9999)
time1=9999;
}
restart:
time=time1;
char A[100][100],turn=’o’;
_setcursortype(_NOCURSOR);
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
A[i][j]=’ ‘;
clrscr();
char move;
do
{
switch(move)
{    case ‘w’:y–;break;
case ‘s’:y++;break;
case ‘a’:x–;break;
case ‘d’:x++;break;
case 13 :if(A[x][y]==’o’||A[x][y]==’x’)
break;
if(turn==’o’)
{      time=time1;
turn=’x’;
A[x][y]=’o’;
break;
}
else
{      time=time1;
turn=’o’;
A[x][y]=’x’;
break;
}
defult:     break;
}
if(x>col)x=1;
if(x<1)x=col;
if(y>row)y=1;
if(y<1)y=row;
clrscr();
for(i=1;i<=row;i++)
{          for(j=1;j<=col;j++)
{    if(x==i&&y==j)
textcolor(GREEN);
else
textcolor(7);
gotoxy(i*3,j*3);
cprintf(“³ ³”);
gotoxy((i*3),(j*3)-1);
cprintf(“ÚÄ¿”);
gotoxy(i*3,(j*3)+1);
cprintf(“ÀÄÙ”);
gotoxy((i*3)+1,j*3);
if(A[i][j]==’o’)
{       textcolor(RED);
cprintf(“%c”,A[i][j]);
}
if(A[i][j]==’x’)
{       textcolor(BLUE);
cprintf(“%c”,A[i][j]);
}
}
}
gotoxy(51,49);
textcolor(RED);
cprintf(“Note:To restart play=Press ‘r’”);
if(turn==’o’)
{    textcolor(RED);
gotoxy(55,25);
cprintf(“%s turn!”,player1);
}
else
{       textcolor(RED);
gotoxy(55,25);
cprintf(“%s turn!”,player2);
}
textcolor(YELLOW);
gotoxy(55,3);
cprintf(“Player1’s name:%s”,player1);
gotoxy(55,4);
textcolor(WHITE);cprintf(“Player1:”);
textcolor(RED);  cprintf(“o”);
textcolor(YELLOW);
gotoxy(55,48);
cprintf(“Player2’s name:%s”,player2);
gotoxy(55,47);
textcolor(WHITE);cprintf(“Player2:”);
textcolor(BLUE); cprintf(“x”);
if(ch==’1′)
while(!kbhit())
{          gotoxy(55,27);
textcolor(YELLOW);
if(time>=10)
if(time>=100)
if(time>=1000)
cprintf(“Time left:%d”,time);
else
cprintf(“Time left:0%d”,time);
else
cprintf(“Time left:00%d”,time);
else
cprintf(“Time left:000%d”,time);
delay(700);
time–;
if(time<=0)
{       gotoxy(55,27);
textcolor(YELLOW);
cprintf(“Time left:000%d”,time);
gotoxy(55,28);
textcolor(YELLOW);
cprintf(“Game over!”);
gotoxy(55,29);
if(turn==’o’)
cprintf(“The Winner is: %s”,player2);
else
cprintf(“The Winner is: %s”,player1);
gotoxy(55,30);
cprintf(“Press any key to restart!”);
getch();

goto restart;
}
}
move=getch();
if(move==’r’)
goto restart;
}while(move!=27);
}