home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2001 Mobile
/
Chip_Mobile_2001.iso
/
palm
/
spiele
/
crypto
/
crypto.exe
/
crypto.pc
next >
Wrap
Text File
|
2000-11-18
|
6KB
|
306 lines
// Crypto Demo
include "Ccontrols.c"
include "textwrap.h"
char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char capitals[26] = {'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
string puzzle;
pointer getPuzzlePtr();
replacePuzzle(int oldindex, int newindex);
drawPuzzle();
handleClick(int i);
Chandle buttons[26];
Chandle back, next;
int replaceIndex;
Chandle puzzleLabel, replaceLabel;
int state = 0;
int numRecords;
int currentRecord;
useRecord(int recordNum);
setCaptions();
int upper[26];
int left[26];
int lower[26];
int right[26];
initcontrols(){
// dynamically create array of controls
int i;
int startX = 5;
int startY = 115;
int yPos;
int xPos;
string s;
for(i=0; i<26; i++) {
yPos = startY + i/10 * 15;
xPos = startX + (i%10)*15;
s = alphabet[i];
buttons[i] = Cbutton(xPos,yPos,8,8,1,4);
Csetcontent(buttons[i],s);
left[i] = xPos;
upper[i] = yPos;
right[i] = xPos+8;
lower[i] = yPos+8;
}
back = Cbutton(95, 18, 25, 8, 1, 4);
next = Cbutton(125, 18, 25, 8, 1, 4);
// label
replaceLabel = Clabel(startX, 100, 50, 1, 1, 0);
puzzleLabel = Clabel(startX, 20, 50, 1, 1, 0);
Csetcontent(replaceLabel, "Replace?");
Csetcontent(puzzleLabel, "Puzzle:");
Csetcontent(back, "Prev");
Csetcontent(next, "Next");
}
initscreen(){
int index;
clearg();
title("Crypto .5");
// draw controls
for(index=0; index<26; index++) {
Cdraw(buttons[index]);
}
Cdraw(replaceLabel);
Cdraw(puzzleLabel);
Cdraw(back);
Cdraw(next);
}
int getNextIndex(string str, int index) {
// return ending index of current line
int length;
length = strlen(str);
if(index > length) return -1;
if(index + 20 > length) return length;
return index + 20;
}
drawPuzzle() {
// hide the buttons for letters not used
int i;
string s;
rect(0, 5, 33, 155, 100, 0);
textwrap(5, 33, 155, 100, puzzle);
}
clickBack() {
useRecord(currentRecord-1);
}
clickNext() {
useRecord(currentRecord+1);
}
useRecord(int recordNum) {
// load the puzzle
currentRecord = recordNum;
dbopen("CRYPTO-JSIC");
dbrec(recordNum);
puzzle = dbread('s');
dbclose();
// set state to replace
state = 0;
// set the buttons and captions
drawPuzzle();
setCaptions();
// activate/deactivate back and next buttons
if(recordNum == 0) Cdeactivate(back);
else Cactivate(back);
if(recordNum == numRecords-1) Cdeactivate(next);
else Cactivate(next);
}
main() {
int i, e;
int x,y;
graph_on();
initcontrols();
initscreen();
// open the db
dbopen("CRYPTO-JSIC");
numRecords = dbnrecs();
currentRecord = 0;
useRecord(0);
dbclose();
// message loop
while(1){
e=event(1);
x=penx();
y=peny();
if(e==2) {
for(i=0; i<26; i++) {
if((x>=left[i])&&(x<=right[i])) {
if((y>=upper[i])&&(y<=lower[i])) {
beep(7);
handleClick(i);
break;
}
}
}
}
if(Cevent(back, e)) {
beep(7);
clickBack();
}
if(Cevent(next, e)) {
beep(7);
clickNext();
}
/*
for(i=0; i<26; i++) {
if(Cevent(buttons[i], e)) {
beep(7);
handleClick(i);
}
if(Cevent(back, e)) {
beep(7);
clickBack();
}
if(Cevent(next, e)) {
beep(7);
clickNext();
}
}
*/
}
}
replacePuzzle(int old, int new) {
int length;
pointer ptr;
int i;
string str;
char oldlower, oldupper, newlower, newupper;
char current;
ptr = getPuzzlePtr();
length = strlen(puzzle);
oldlower = alphabet[old];
oldupper = capitals[old];
newlower = alphabet[new];
newupper = capitals[new];
// alert(oldlower + " " + oldupper + " " + " " + newlower + " " + newupper + " " + puzzle);
for(i=0; i<length; i++) {
current = ptr[i];
if(current == oldlower) {
ptr[i]=newlower;
continue;
}
if(current == oldupper) {
ptr[i]=newupper;
continue;
}
if(current == newlower) {
ptr[i]=oldlower;
continue;
}
if(current == newupper) {
ptr[i]=oldupper;
continue;
}
}
puzzle = ctostr(ptr);
free(ptr);
}
handleClick(int click)
{
pointer ptr, i, dbptr;
int length;
int found;
char current;
char upper, lower;
string strdummy;
// they clicked button i
if(state == 0) {
ptr = getPuzzlePtr();
length = strlen(puzzle);
found = 0;
lower = alphabet[click];
upper = capitals[click];
for(i=0; i<length; i++) {
current = ptr[i];
if((current==lower)||(current==upper)) {
found = 1;
break;
}
}
free(ptr);
if(found == 0) {
alert(lower + " is not in the crypto phrase.");
return;
}
// begin replacement
replaceIndex = click;
state = 1;
setCaptions();
}
else {
// replace the characters
replacePuzzle(replaceIndex, click);
state = 0;
drawPuzzle();
setCaptions();
// write puzzle back to database
dbptr = &strdummy;
dbptr[0] = puzzle;
dbopen("CRYPTO-JSIC");
dbrec(currentRecord);
dbwritex(dbptr, "sz");
dbclose();
return;
}
}
pointer getPuzzlePtr() {
int length;
pointer result;
length = strlen(puzzle);
result = malloc(length+1);
settype(result, length+1, 'c');
strtoc(puzzle, result);
return result;
}
setCaptions() {
if(state == 0) {
// in replace state
Cerase(replaceLabel);
Csetcontent(replaceLabel, "Replace?");
Cdraw(replaceLabel);
}
else {
// in with state
Cerase(replaceLabel);
Csetcontent(replaceLabel, "With? (" + alphabet[replaceIndex] + ")" );
Cdraw(replaceLabel);
}
}