刽子手游戏中的eksternal文件 [英] eksternal file in hangman game

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

问题描述

#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;>  {
    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;>    printf("%s\n", pword[n]);
 
  // cleanup
  for (n=0; n<words;>    free( pword[n]);
 
  return 0;
}



如果我想选择一个单词表格对于刽子手游戏这个无效,我应该写什么?我应该在什么参数中无效?



代码块添加[/编辑]


if i want to pick one word form this void to the hangman game, what should i write? and void in what parameters should I have?

Code block added[/Edit]

推荐答案

这看起来像是家庭作业。



如果你做了自己的功课,那将是最好的。你这样学习的东西,想想所教的内容。阅读你的教科书并试一试。



一旦你有代码并遇到问题,你可以随时回到这里提出一个更具体的问题,社区将尽力帮助你。不要忘记仅发布造成问题的相关代码位,因为代码转储通常对帮助/协助您没有帮助。



祝你好运,编码愉快。
This looks like homework.

It would be best if you did your own homework. It is given to you so that you will learn something, think about what have been taught. Read your text books and give it a try.

Once you have code and run into problems you can always come back here with a more specific question and the community will do its best to help you. Don''t forget to post only the relevant code bits that pose the problem as a code dump is not usually helpful at all in getting someone to help/assist you.

Good luck and happy coding.


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

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