简体   繁体   English

为什么我的程序在Xcode上而不在代码块上运行

[英]Why does my program run on Xcode but not on codeblocks

beginner programmer here. 初学者程序员在这里。 I am making a card war game using Xcode. 我正在使用Xcode制作纸牌战争游戏。 However, my prof uses code::blocks to grade. 但是,我的教授使用code :: blocks进行评分。 When I run my code in Xcode it runs perfectly, but when I run in code::blocks it has spits an error: Process returned -1 (0xFFFFFFF). 当我在Xcode中运行代码时,它可以完美运行,但是当我在code :: blocks中运行时,它会吐出一个错误:进程返回-1(0xFFFFFFF)。 Can anyone tell me what's happening and why it's not running from one IDE to the next? 谁能告诉我发生了什么,为什么它不能从一个IDE运行到另一个? It's important that I make it run on codeblocks since that's what I will be graded on. 使它在代码块上运行很重要,因为这就是我的评分依据。

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

#define DECK_SIZE 52

unsigned int rounds_played = 0 ;
unsigned int wars_played = 0 ;

/*
Base data structure: deck. It's a stack that can hold up to
deck_size cards.

A card is represented as a nunmber from 2 to 14. 14 is an Ace.

*/
typedef struct {
  // basically a stack
  char* slots ;
  unsigned int slot_pointer ;
  unsigned int deck_size ;
} Deck ;

/* Operations on deck:

make_deck: creates and initialize a deck that can hold up to *size*
cards.

enqueue(deck, item): puts a card on top of the stack
dequeue(deck): draws a card from the stack
dump_deck(d): views content of deck d. free slots will have value
              of zero (0)
deck_empty(d): inspects a deck to see whether a deck holds any card

*/
Deck* make_deck(unsigned int size) ;
void enqueue(Deck* deck, char item);
char dequeue(Deck* deck) ;
void dump_deck(Deck *d);
int deck_empty(Deck *d) ;

/* make_initial_deck: sets up a 52-card deck, shuffled */
Deck *make_initial_deck() ;

/* deal: cards from deck *sorce get split evenly between decks
         *d1 and *d2 
*/
void deal(Deck *source, Deck *deck1, Deck *deck2);


void play(Deck* d1, Deck* d2, Deck* tmp);
bool game_won(Deck* d1, Deck* d2) ;
unsigned int deck_cards_number(Deck* d) ;
char* print_card(char card) ;

int main(int argc, char **argv) {

  srand(time(NULL));

  Deck* initial_deck = make_initial_deck() ;
  Deck* d1 = make_deck(DECK_SIZE) ;
  Deck* d2 = make_deck(DECK_SIZE) ;

  printf("Dumping initial deck:\n");
  dump_deck(initial_deck) ;

  deal(initial_deck, d1, d2) ;

  printf("Dumping d1:\n");
  dump_deck(d1);

  printf("Dumping d2:\n");
  dump_deck(d2) ;

  Deck* tmp = make_deck(DECK_SIZE) ;
  play(d1, d2, tmp) ;

  return 0 ;
}

/*
 * play: implements the game

*/
void play (Deck* d1, Deck* d2, Deck *tmp) {
  rounds_played++ ;
  char card1, card2 ;
  /* printf("Player 1 deck:\n"); dump_deck(d1) ; */
  /* printf("Player 2 deck:\n"); dump_deck(d2) ; */
  /* printf("tmp deck:\n"); dump_deck(tmp) ; */

  if (d1->slot_pointer == 0) {
    printf("Player 2 wins the game!\n") ;
    printf("Rounds played: %d\n", rounds_played);
    printf("Wars declared: %d\n", wars_played) ;

    return ;
  } else {
    card1 = dequeue(d1);
    printf("[Player 1]: %s\n", print_card(card1));
  }

  if (d2->slot_pointer == 0) {
    printf("Player 1 wins the game!\n") ;
    printf("Rounds played: %d\n", rounds_played);
    printf("Wars declared: %d\n", wars_played) ;
    return ;
  } else {
    card2 = dequeue(d2);
    printf("[Player 2]: %s\n", print_card(card2));
  }


  // recursive case:
  if (card1 == card2) {
    printf("This is War!\n") ;
    wars_played++ ;

    if ( deck_empty(d1) == 0) {
      enqueue(tmp, dequeue(d1)); // face down card
    }
    if (deck_empty(d2) == 0) {
      enqueue(tmp, dequeue(d2)); // face down card
    }
    /* enqueue(tmp, dequeue(d1)); // face down card */
    /* enqueue(tmp, dequeue(d2)); // face down card */

    enqueue(tmp, card1);
    enqueue(tmp, card2);
  } else {
    if (card1 > card2) {
      // printf("Player1 wins the round!\n") ;
      enqueue(tmp, card1) ;
      enqueue(tmp, card2) ;
      printf("Player 1 wins (%d) cards.\n",
         tmp->slot_pointer) ;
      // dump_deck(tmp);
      while (tmp->slot_pointer > 0) {
    enqueue(d1, dequeue(tmp)) ;
      }
    }
    else {
      // printf("Player2 wins the round!\n") ;
      enqueue(tmp, card1) ;
      enqueue(tmp, card2) ;
      printf("Player 2 wins (%d) cards.\n",
         tmp->slot_pointer) ;
      // dump_deck(tmp);
      while (tmp->slot_pointer > 0) {
    enqueue(d2, dequeue(tmp)) ;
      }
    }
  }
  play(d1,d2,tmp);

  return ;
}

/* Given a souce Deck and two destination Decks d1 and d2, it deals cards from
 * source deck to d1 and d2 so that both decks get half of the cards originally
 * in the source deck.
*/
void deal(Deck *source, Deck *deck1, Deck *deck2) {
  char card ; 
  for(int i=0 ; i< (source->deck_size) ; i++) {
    card = dequeue(source) ;
    if ((i%2) == 0){
      enqueue(deck1, card) ;
    } else {
      enqueue(deck2, card) ;
    }
  }
  return ;
}

// INPUT: two decks, one for each player.
// OUTPUT: true or false, depending on whether one of the two decks
//         holds 52 cards
bool game_won(Deck* d1, Deck* d2){
  if ( (d1->slot_pointer == 51) || (d2->slot_pointer == 51) )
    return true ;
  else
    return false ;
}


// INPUT: the desidered deck size
// OUTPUT: an instance of the Deck data structure, properly intialized
Deck* make_deck(unsigned int size){
  Deck *d = malloc(sizeof(Deck));
  d->slots = malloc(size*sizeof(char)) ;
  memset(d->slots, 0, d->deck_size);
  d->slot_pointer = 0 ;
  d->deck_size = size ;

  return d ;
}

// INPUT: an instance of the Deck data structure
// POST: the contents of the deck will be displayed on standard output.
//       available but empty slots will be represented with zeroes.
void dump_deck(Deck* d) {
  for(unsigned int i=1 ; i<=(d->deck_size) ; i++) {
    printf("%2d  ", d->slots[i-1]) ;
    if( i>0 && i % 13 == 0) {
      printf("\n") ;
    }
  }
  printf("\n");
  return ;
}

// INPUT: An instance of the Deck data structure and a number (in a char)
//  representing a card.
// POST: the card will be added to the deck.
void enqueue(Deck* deck, char item) {
  deck->slots[deck->slot_pointer++] = item ;
  return ;
}


// INPUT: an instance of the Deck data structure
// OUTPUT: a number representing a card from the deck.
char dequeue(Deck* deck) {
  deck->slot_pointer-- ;
  char card = deck->slots[deck->slot_pointer] ;
  deck->slots[deck->slot_pointer] = 0 ;
  return card ;
}

// INPUT: an instance of the Deck data structure
// OUTPUT: 1 if the deck is empty, 0 otherwise.
int deck_empty(Deck *d) {
  if (d->slot_pointer == 0) {
    return 1 ;
  } else {
    return 0 ;
  }
}

// OUTPUT: returns a pointer to a newly-created instance of the Deck data
//         structure, holding DECK_SIZE cards, and randomly shuffled.
Deck *make_initial_deck() {
  Deck *d = make_deck(DECK_SIZE);

  for(int i=0 ; i<4 ; i++) {
    for (int j=2 ; j<15 ; j++) {
      enqueue(d, j) ;
    }
  }


  int n = DECK_SIZE;
  for (int i = 0; i < n - 1; i++){
    /// get  random element in the listplayer1Hand[26] = 2;
    int j = i + rand() / (RAND_MAX / (n - i) + 1);

    ///swap these two elements
    char temp = d->slots[j];
    d->slots[j] = d->slots[i];
    d->slots[i] = temp;
  }

  return d;
}

// INPUT: a char holding the numeric representation of the card
// OUTPUT: a pointer to a string holding the representation of the card.
//         The representation can be "Ace", "Jack", "Queen" and "King",
//         and the respective number for other cards.
char* print_card(char card) {
  switch (card) {
  case 14 :
    return "Ace" ;
  case 11 :
    return "Jack" ;
  case 12:
    return "Queen" ;
  case 13:
    return "King" ;
  }

  char* str = malloc(4) ;
  sprintf(str, "%2d", card) ;
  return str ; 
}

There is an obvious bug in this bit of code 这一段代码中有一个明显的错误

Deck* make_deck(unsigned int size){
  Deck *d = malloc(sizeof(Deck));
  d->slots = malloc(size*sizeof(char)) ;
  memset(d->slots, 0, d->deck_size);
  d->slot_pointer = 0 ;
  d->deck_size = size ;

  return d ;
}

When you do the memset , what value does d->deck_size have? 当您执行memset ,d-> deck_size有什么值? It doesn't have any value, which will cause undefined behaviour. 它没有任何值,这将导致不确定的行为。 You should either move the assignment of d->deck_size up to before the memset or use size like you've done in the call to malloc . 您应该移动的分配d->deck_size到之前memset或使用size就像你在调用所做malloc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的代码在代码块中不断失败? - Why does my code keep failing in codeblocks? 为什么同一程序在ideone和代码块中的行为不同? - Why does same program act different in ideone and codeblocks? 为什么我的 CodeBlocks 应用程序总是无法执行代码? - Why does my CodeBlocks app keep failing to execute code? 为什么我的代码在 Visual 中不起作用,但在 CodeBlocks 中起作用? - Why doesn't my Code work in Visual, but does in CodeBlocks? 为什么我的程序在Ubuntu gcc上而不在OSX gcc上运行? - Why does my program run on Ubuntu gcc but not OSX gcc? 为什么我的代码块上没有显示输出? - Why there is no output showing on my codeblocks? 当我尝试在代码块编译器中运行此程序时,strstr()函数每次都返回0。 为什么? - When I was trying to run this program in the codeblocks compiler, the strstr() function was returning 0 everytime. Why? CodeBlocks修改代码后没有刷新我的程序 - CodeBlocks not refreshing my program after modifying code 为什么该程序按其方式运行? - Why does this program run the way it does? 为什么字符序列/// *在代码块12.11中更改了其余源文件的字体? - Why does the character sequence //(* change the font of the rest of my source file in codeblocks 12.11?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM