刽子手游戏中的eksternal文件,用C语言编写 [英] eksternal file in hangman game, in C language

查看:67
本文介绍了刽子手游戏中的eksternal文件,用C语言编写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉这个问题太长了,我已经尝试制作了刽子手游戏的其他部分,但是我不明白如何通过外部文件如txt,单词列表写入一个单词列表文件并将被加载到compilier中,但是应该首先合并到数组中,快速加载,单词必须随机,请帮助用c的语言来做,在void

中建立解决方案

作为一种简单的方法,您可以编写一个文本文件,每行只有一个单词,然后将单词读入数组,最终将数组自身洗牌,例如



  #include   <   stdio.h  >  
< span class =code-keyword> #include < stdlib.h >
#include < string.h >

#define MAX_WORDS 1000
#define MAX_LEN 256

void shuffle( char * pword [], int 字, int 迭代)
{
< span class =code-keyword> int n;

if (单词< = 1 返回;

for (n = 0 ; n< iterations; n ++)
{
int i1,i2;
i1 = rand()%words;
i2 = rand()%words;
char * tmp = pword [i1];
pword [i1] = pword [i2];
pword [i2] = tmp;
}
}

int main()
{
int n;
char buf [MAX_LEN];
char * pword [MAX_WORDS];
int words = 0 ;

FILE * fp = fopen( words.txt r);

if (!fp) return - 1 ;

while (!feof(fp))
{
if (words == MAX_WORDS) break ;
if (!fgets(buf,MAX_LEN,fp)) break ;
buf [strlen(buf) - 1 ] = ' \0' ; // 删除换行符
pword [words] = strdup(buf); // 这会动态分配内存
words ++;
}
fclose(fp);

shuffle(pword,words, 2 * words);

for (n = 0 ; n< words; n ++)
printf( %s \ n,pword [n]);

// cleanup
for (n = 0 ; n< words; n ++)
free(pword [n]);

return 0 ;
}





上述程序读取文件:



aaaaaaaaaaa

bbbbbbbb

cccccccccc

dd

eeeeeeeeeee

fffff

gggggggggggg

hhhhhhhhh

iiiiiiiiiii

jjjjjjjjjj

k

llllllllll

mmmmmmmm

nnnnnnn

ooooooo

p

qqqqqq

rrrrrrrr

sssss

tt

uuuuuu

vvvvvvv

wwwwwww

xxxxxxxxx

yyyyyyyyyyy

z





产生以下输出:





jjjjjjjjjj

aaaaaaaaaaa br / b >
rrrrrrrr

fffff

cccccccccc

sssss

qqqqqq

llllllllll

bbbbbbbb

nnnnnnn

uuuuuu

xxxxxxxxx

iiiiiiiiiii

hhhhhhhhh

eeeeeeeeeee

z

dd

k

wwwwwww

tt

mmmmmmmm

vvvvvvv

yyyyyyyyyyy

p

gggggggggggg

ooooooo


sorry for the question is too long, I''ve tried to make the other parts of hangman game , but I do not understand how to make a list of words through external files such as txt, list of words written in the file and will be loaded in compilier, but should be incorporated into the array first, that fast loaded, word must at random, please help to do it in the language of c, is established in a void

解决方案

As a simple approach, you could write a text file with just one word per line, then read the words into an array, eventually shuffling the array itself, e.g.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_WORDS 1000
#define MAX_LEN 256

void shuffle(char * pword[], int words, int iterations)
{
  int n;

  if ( words <= 1) return;

  for (n=0; n<iterations; n++)
  {
    int i1, i2;
    i1 = rand()%words;
    i2 = rand()%words;
    char * tmp = pword[i1];
    pword[i1] = pword[i2];
    pword[i2] = tmp;
  }
}

int main()
{
  int n;
  char buf[MAX_LEN];
  char * pword[MAX_WORDS];
  int words = 0;

  FILE * fp = fopen("words.txt", "r");

  if ( ! fp ) return -1;

  while (!feof(fp))
  {
    if (words == MAX_WORDS) break;
    if (! fgets(buf, MAX_LEN, fp) ) break;
    buf[strlen(buf)-1] = '\0'; // remove newline
    pword[words] = strdup(buf); // this dynamically allocates memory
    words++;
  }
  fclose(fp);

  shuffle(pword, words, 2*words);

  for (n=0; n<words; n++)
    printf("%s\n", pword[n]);

  // cleanup
  for (n=0; n<words; n++)
    free( pword[n]);

  return 0;
}



The above program reads the file:


aaaaaaaaaaa
bbbbbbbb
cccccccccc
dd
eeeeeeeeeee
fffff
gggggggggggg
hhhhhhhhh
iiiiiiiiiii
jjjjjjjjjj
k
llllllllll
mmmmmmmm
nnnnnnn
ooooooo
p
qqqqqq
rrrrrrrr
sssss
tt
uuuuuu
vvvvvvv
wwwwwww
xxxxxxxxx
yyyyyyyyyyy
z



Producing the following output:


jjjjjjjjjj
aaaaaaaaaaa
rrrrrrrr
fffff
cccccccccc
sssss
qqqqqq
llllllllll
bbbbbbbb
nnnnnnn
uuuuuu
xxxxxxxxx
iiiiiiiiiii
hhhhhhhhh
eeeeeeeeeee
z
dd
k
wwwwwww
tt
mmmmmmmm
vvvvvvv
yyyyyyyyyyy
p
gggggggggggg
ooooooo


这篇关于刽子手游戏中的eksternal文件,用C语言编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆