คือผมทำเกมส์ฝึกพิมพ์ครับแบบเลือกระดับความยาก 1 2 3 รูปแบบเกมส์คือถ้าพิมพ์ถูกนับคะแนน แล้วเล่นไปเลื่อยๆจนจบครับ แต่ผมไม่รู้จะเอาไฟล์คำศัพท์มาใส่ยังไงครับ
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#define MAX 1100
int numLine = 0;
int score = 0;
char* word[MAX];
struct Player
{
char username[50];
int score;
char level[20];
};
enum Level
{
Basic = 5,
Medium = 10,
Hard = 15,
};
struct Player player1;
int main()
{
char username[50];
int selectLevel;
printf("---------------------------------\n");
printf("---------- Typing Game ----------\n");
printf("---------------------------------\n");
printf("Enter Username : ");
scanf("%s", &username);
strcpy(player1.username,username);
printf("\n\n0_0 [ Hello %s ] ^o^\n\n",player1.username);
printf("============= Level =============\n");
printf("1: Basic [ %d word ] \n",Basic);
printf("2: Medium [ %d word ] \n",Medium);
printf("3: Hard [ %d word ] \n",Hard);
printf("Enter level what you want to play : ");
scanf("%d", &selectLevel);
if (selectLevel > 0)
{
int gameStart(selectLevel);
}
printf("============ Game End ===========\n");
getch();
return 0;
}
int randomNumber()
{
srand(time(0));
int rand_num = rand() % numLine;
return rand_num;
}
gameStart(int selectLevel)
{
int msec = 0;
int w = 0;
char key[MAX]; // char key[1100]
readFile();
time_t start,end;
double dif;
start = clock();
if(selectLevel == 1)
{
selectLevel = Basic;
strcpy(player1.level,"Basic");
}
else if (selectLevel == 2)
{
selectLevel = Medium;
strcpy(player1.level,"Medium");
}
else if(selectLevel == 3)
{
selectLevel = Hard;
strcpy(player1.level,"Hard");
}
else
{
selectLevel = Basic;
strcpy(player1.level,"Basic");
}
while(w < selectLevel)
{
int rand_num = randomNumber(); // 1 0 1 2
printf("%d: %s\n", w+1,word[rand_num]); // word = ["xxx","yyy","zzzz"]
printf("Enter : ");
scanf("%s", &key); // zzzz
if(strcmp(key,word[rand_num]) == 0)
{
score++;
}
w++;
}
end = clock();
dif = ((double) (end - start)) / CLOCKS_PER_SEC;
player1.score = score;
printf("=================================\n");
printf("Username : %s \nLevel : %s \nScore : %d / %d words.\n",player1.username,player1.level, player1.score, selectLevel);
printf("Time used : %f\n", dif);
saveFile();
}
saveFile()
{
FILE *fp;
fp = fopen("dictionary.txt", "a");
fprintf(fp,"Player: %s,Score: %d,Level: %s\n",player1.username,player1.score,player1.level);
fclose(fp);
}
readFile()
{
FILE* filePointer;
int bufferLength = 255;
char buffer[bufferLength];
filePointer = fopen("hallOfFrame.txt", "r");
while(fgets(buffer, bufferLength, filePointer))
{
buffer[strcspn(buffer, "\n")] = 0;
word[numLine] = strdup(buffer);
numLine++;
}
fclose(filePointer);
}
คือผมรัน code dev c++ ไม่ได้ครับ งานแก้มานานแล้วต้องส่งอาจารย์ครับ
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#define MAX 1100
int numLine = 0;
int score = 0;
char* word[MAX];
struct Player
{
char username[50];
int score;
char level[20];
};
enum Level
{
Basic = 5,
Medium = 10,
Hard = 15,
};
struct Player player1;
int main()
{
char username[50];
int selectLevel;
printf("---------------------------------\n");
printf("---------- Typing Game ----------\n");
printf("---------------------------------\n");
printf("Enter Username : ");
scanf("%s", &username);
strcpy(player1.username,username);
printf("\n\n0_0 [ Hello %s ] ^o^\n\n",player1.username);
printf("============= Level =============\n");
printf("1: Basic [ %d word ] \n",Basic);
printf("2: Medium [ %d word ] \n",Medium);
printf("3: Hard [ %d word ] \n",Hard);
printf("Enter level what you want to play : ");
scanf("%d", &selectLevel);
if (selectLevel > 0)
{
int gameStart(selectLevel);
}
printf("============ Game End ===========\n");
getch();
return 0;
}
int randomNumber()
{
srand(time(0));
int rand_num = rand() % numLine;
return rand_num;
}
gameStart(int selectLevel)
{
int msec = 0;
int w = 0;
char key[MAX]; // char key[1100]
readFile();
time_t start,end;
double dif;
start = clock();
if(selectLevel == 1)
{
selectLevel = Basic;
strcpy(player1.level,"Basic");
}
else if (selectLevel == 2)
{
selectLevel = Medium;
strcpy(player1.level,"Medium");
}
else if(selectLevel == 3)
{
selectLevel = Hard;
strcpy(player1.level,"Hard");
}
else
{
selectLevel = Basic;
strcpy(player1.level,"Basic");
}
while(w < selectLevel)
{
int rand_num = randomNumber(); // 1 0 1 2
printf("%d: %s\n", w+1,word[rand_num]); // word = ["xxx","yyy","zzzz"]
printf("Enter : ");
scanf("%s", &key); // zzzz
if(strcmp(key,word[rand_num]) == 0)
{
score++;
}
w++;
}
end = clock();
dif = ((double) (end - start)) / CLOCKS_PER_SEC;
player1.score = score;
printf("=================================\n");
printf("Username : %s \nLevel : %s \nScore : %d / %d words.\n",player1.username,player1.level, player1.score, selectLevel);
printf("Time used : %f\n", dif);
saveFile();
}
saveFile()
{
FILE *fp;
fp = fopen("dictionary.txt", "a");
fprintf(fp,"Player: %s,Score: %d,Level: %s\n",player1.username,player1.score,player1.level);
fclose(fp);
}
readFile()
{
FILE* filePointer;
int bufferLength = 255;
char buffer[bufferLength];
filePointer = fopen("hallOfFrame.txt", "r");
while(fgets(buffer, bufferLength, filePointer))
{
buffer[strcspn(buffer, "\n")] = 0;
word[numLine] = strdup(buffer);
numLine++;
}
fclose(filePointer);
}