[code=C/C++]
/* Author: ZHAO,Chao(Ryan)
* Data & Time: 29/09/09 15:31
* Assignment 1:join to dots*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
void clrscr();
void gotoxy(int,int);
int getch();
void putchxy(int x, int y, int );
void _conio_gettext(int, int, int, int, char*);
void cputsxy(int,int,char*);
int rand(void);
int main(void)
{
int key;
char name[10]="";
char value[4];
//char charnum;
//int number=0;
int score=200;
int x,y;
int i=0,j=0;
x=10;
y=10;
int rx,ry;
int num=0;
srand((unsigned)time (NULL));
printf("JOIN THE DIGITS/n/n");
printf("The aim is to move the pencil +/naround the screen using the number keys on your numberic keypad./n");
printf("around the screen using the number keys on your numberic keypad/n");
printf("You must join the digits IN ORDER to get a good score./n");
printf("Start with Number 0/n");
printf("Press ESC at any time to quit./n");
printf("Enter your name:");
scanf("%s",&name);
clrscr();/*clean screen*/
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
/*todo: modfied this to ramdon number*/
printf("/-----------------------------------------------------------------------------///n");
printf("| 1 |/n");
printf("| 0 |/n");
printf("| |/n");
printf("| |/n");
printf("| 3 |/n");
printf("| |/n");
printf("| 4 |/n");
printf("| |/n");
printf("| 5 |/n");
printf("| |/n");
printf("| |/n");
printf("| |/n");
printf("| |/n");
printf("| 6 |/n");
printf("| 7 |/n");
printf("| |/n");
printf("| |/n");
printf("| 8 |/n");
printf("| 9 |/n");
printf("| |/n");
printf("| 2 |/n");
printf("//-----------------------------------------------------------------------------//n");
gotoxy(x,y);
do
{
/* get the user's input from keybord,but not display*/
key = getch();
// if press ESC,exit game
if (key == 27 )
{
getchar();
system("PAUSE");
return 0;
break;
}
/* if press 8 in number keypad, the cursor move up*/
if (key == '8' && y >3)
{
y--;
score--;
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
/* if press 4 in number keypad, the cursor move left*/
if (key == '4' && x > 2)
{
x--;
score--;
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
if (key == '2' && y < 23)
{
y++;
score--;
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
if (key == '6' && x < 77)
{
x++;
score--;
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
_conio_gettext(x, y, x, y, value); /* get the value of the position which curson on*/
/*put '.' in the old positions*/
if(key=='8')
{
putchxy(x,y+1,'.');
}
else if(key=='2')
{
putchxy(x,y-1,'.');
}
else if(key=='6')
{
putchxy(x-1,y,'.');
}
else if(key=='4')
{
putchxy(x+1,y,'.');
}
putchxy(x, y, '+'); /* put '+' in the new positions*/
num = (int)value[0];/*convert char to int*/
/*match the int with dec */
for(j=48;j<=57;j++)
{
if(num==j)
{
num-=48;
gotoxy(3,3);
break;
}/*end if if(num==j)*/
}/*end of for loop*/
if(num == i)/*if this is a correct number score increase 100 points*/
{
score += 100;
i++;
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
else if(num!=i)
{
if(num==32)/* 32 is a space in ASCII*/
{
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
else if(num==46)/*this is a '.'*/
{
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
else if(num==43)/* this is a '+'*/
{
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
else/* if this is not a correct number, move it in ramdon number*/
{
rx=rand()%73+3; /*make rx,ry ramdon number*/
ry=rand()%21+3; /*number is bewteen 3-75 and 3-23*/
if(i!=num)
{
putchxy(rx,ry,value[0]);
}
score -= 50;
gotoxy(1,1);
printf("Sorce: %i/tGo to : %i/tPlayer:%s/n",score,i,name);
}
}
if(i == 10)/* if system go to last number and correct, finish game*/
{
clrscr();
printf("You won/n");
printf("Score is %i",score);
getchar();
system("PAUSE");
return 0;
}
else if (score<0)/* if score is less than 0,game over*/
{
clrscr();
printf("Oops! Your score is below ZERO/n");
getchar();
system("PAUSE");
return 0;
}
} while(1);/*end of do while loop*/
clrscr();
printf("You Quit Game");
return 0;
}
[/code]