- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 Eclipse CDT 用 C 语言创建了一个简单的控制台 BlackJack 游戏。我有一个奇怪的问题,运行 eclipse 生成的 DEBUG 可执行文件,程序运行正常,如图所示:
-------
PLAYERS
-------
Player-1: (18) [ eight:spade, king:heart ]
>> (H)it/(C)heck: c
------
DEALER
------
Dealer: (21) [ king:diamond, ace:diamond ]
-------
RESULTS
-------
Player-1: Loss - 18 under dealer's 21.
但是,当我将 Eclipse 切换为构建 RELEASE 而不是 DEBUG 时,生成的可执行文件如下:
-------
PLAYERS
-------
Player-1: (4) [ four:(null), (null):(null) ]
>> (H)it/(C)heck: h
Player-1: (4) [ four:(null), (null):(null), (null):(null) ]
>> (H)it/(C)heck: h
Player-1: (4) [ four:(null), (null):(null), (null):(null), (null):(null) ]
>> (H)it/(C)heck: c
------
DEALER
------
Dealer: (0) [ (null):(null), (null):(null) ]
Dealer: (0) [ (null):(null), (null):(null), (null):(null) ]
Dealer: (0) [ (null):(null), (null):(null), (null):(null), (null):(null) ]
...
...
我希望这更多是 Eclipse/CDT/Build 问题,但如果您需要查看源代码,请告诉我。我没有发布源代码,因为大约有 15 个以上的文件。
预先感谢您的帮助。
编辑:添加源代码。
卡片.h
#ifndef CARD_H_
#define CARD_H_
/* The various ranks of a Card. */
enum Rank {
ACE,
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING
};
/* The various suits of a Card. */
enum Suit {
CLUB, DIAMOND, HEART, SPADE
};
/* A structure which defines a Card. */
struct Card {
enum Rank rank;
enum Suit suit;
};
/*
* Each possible Card rank contains two possible values. This structure can
* be used to contain the two values.
*/
struct CardValue {
int value1;
int value2;
};
/* Creates a Card with a suit and a rank. */
struct Card create_card(const enum Rank rank, const enum Suit suit);
/* Takes a Rank enum and provides a string representation. */
char* rank_to_string(const enum Rank rank);
/*
* Takes a Rank enum and provides a structure containing the rank's possible
* values. Each Rank has two possible values, as the Ace is both 1 and 11.
*/
struct CardValue rank_to_value(const enum Rank rank);
/* Takes a Suit enum and provides a string representation. */
char* suit_to_string(const enum Suit suit);
/* Takes a Card structure and outputs its members as a string. */
char* card_to_string(const struct Card card);
#endif /* CARD_H_ */
卡片.c
#include "card.h"
#include <stdio.h>
#include <stdlib.h>
struct Card create_card(const enum Rank rank, const enum Suit suit)
{
struct Card card;
card.rank = rank;
card.suit = suit;
return card;
}
char* rank_to_string(const enum Rank rank)
{
switch (rank) {
case ACE: return "ace";
case TWO: return "two";
case THREE: return "three";
case FOUR: return "four";
case FIVE: return "five";
case SIX: return "six";
case SEVEN: return "seven";
case EIGHT: return "eight";
case NINE: return "nine";
case TEN: return "ten";
case JACK: return "jack";
case QUEEN: return "queen";
case KING: return "king";
}
return 0;
}
struct CardValue rank_to_value(const enum Rank rank)
{
struct CardValue card_value;
switch (rank) {
case ACE:
card_value.value1 = 1;
card_value.value2 = 11;
break;
case TWO:
card_value.value1 = 2;
card_value.value2 = 2;
break;
case THREE:
card_value.value1 = 3;
card_value.value2 = 3;
break;
case FOUR:
card_value.value1 = 4;
card_value.value2 = 4;
break;
case FIVE:
card_value.value1 = 5;
card_value.value2 = 5;
break;
case SIX:
card_value.value1 = 6;
card_value.value2 = 6;
break;
case SEVEN:
card_value.value1 = 7;
card_value.value2 = 7;
break;
case EIGHT:
card_value.value1 = 8;
card_value.value2 = 8;
break;
case NINE:
card_value.value1 = 9;
card_value.value2 = 9;
break;
case TEN:
case JACK:
case QUEEN:
case KING:
card_value.value1 = 10;
card_value.value2 = 10;
break;
default:
card_value.value1 = 0;
card_value.value2 = 0;
}
return card_value;
}
char* suit_to_string(const enum Suit suit)
{
switch (suit) {
case CLUB: return "club";
case DIAMOND: return "diamond";
case HEART: return "heart";
case SPADE: return "spade";
}
return 0;
}
char* card_to_string(const struct Card card)
{
size_t buffer_size = 15;
char* output = malloc(sizeof(char) * buffer_size);
snprintf(output, sizeof(char) * buffer_size, "%s:%s",
rank_to_string(card.rank), suit_to_string(card.suit));
return output;
}
手.h
#ifndef HAND_H_
#define HAND_H_
#define BUSTED 21
#define MAX_HAND_SIZE 22
/*
* Creates a structure representing a Hand of Cards. The 'cards' member is
* an array of cards within the hand, and the 'index' member is the location
* of the last inserted card.
*/
struct Hand {
struct Card* cards;
int index;
};
/* Creates a structure for a Hand of cards. */
struct Hand create_hand();
/* Adds a Card to a Hand. */
void add_card_to_hand(struct Hand* hand, const struct Card card);
/*
* Finds the highest possible, non-busted value for a Hand of cards. If the
* Hand is busted, the value will be greater than 21.
*/
int hand_to_value(const struct Hand hand);
/* A String representation of a Hand of cards. */
char* hand_to_string(const struct Hand hand);
#endif /* HAND_H_ */
手.c
#include "hand.h"
#include "card.h"
#include <stdlib.h>
#include <string.h>
struct Hand create_hand()
{
struct Hand hand;
hand.cards = malloc(sizeof(struct Card) * MAX_HAND_SIZE);
hand.index = 0;
return hand;
}
void add_card_to_hand(struct Hand* hand, const struct Card card)
{
/*
* The 'index' member of the structure contains the incremented location
* of the last inserted card.
*/
hand->cards[hand->index] = card;
hand->index++;
}
int hand_to_value(const struct Hand hand)
{
/* Create a structure which holds the possible value combinations. */
struct CardValue hand_value;
hand_value.value1 = 0;
hand_value.value2 = 0;
/* Loop through each card in the hand and sum the possible totals. */
int i;
for (i = 0; i < hand.index; i++) {
struct Card card = hand.cards[i];
struct CardValue card_value = rank_to_value(card.rank);
hand_value.value1 += card_value.value1;
hand_value.value2 += card_value.value2;
}
/* If both totals are busted, return the lower of the 2. */
if (hand_value.value1 > BUSTED && hand_value.value2 > BUSTED) {
if (hand_value.value1 < hand_value.value2) {
return hand_value.value1;
} else {
return hand_value.value2;
}
/* If the first total is busted, return the second total. */
} else if (hand_value.value1 > BUSTED) {
return hand_value.value2;
/* If the second total is busted, return the first total. */
} else if (hand_value.value2 > BUSTED) {
return hand_value.value1;
/* No totals are busted, return the highest of the two. */
} else if (hand_value.value1 >= hand_value.value2) {
return hand_value.value1;
} else if (hand_value.value1 < hand_value.value2) {
return hand_value.value2;
}
return 0;
}
char* hand_to_string(const struct Hand hand)
{
/* Allocate memory for a string representation of the Hand. */
size_t buffer_size = 500;
char* output = malloc(sizeof(char) * buffer_size);
memset(output, 0, buffer_size);
/*
* Concatenate the string representations of the cards to the string
* representation of the Hand.
*/
int i;
strcat(output, "[ ");
for (i = 0; i < hand.index; i++) {
struct Card card = hand.cards[i];
char* card_string = card_to_string(card);
strcat(output, card_string);
free(card_string);
if (i + 1 < hand.index) {
strcat(output, ", ");
}
}
strcat(output, " ]\n");
return output;
}
甲板.h
#ifndef DECK_H_
#define DECK_H_
#define DECK_SIZE 52
/*
* Creates a structure representing a Deck of Cards. The 'cards' member is
* an array of cards within the deck, and the 'iterator' member is the location
* of the last accessed card.
*/
struct Deck {
struct Card* cards;
int iterator;
int number_of_decks;
};
/*
* Creates a new Deck of cards. The 'number_of_decks' parameter dictates how
* many 52-card decks will be generated in the Deck.
*/
struct Deck create_deck(const int number_of_decks);
/* Shuffles the cards within the Deck. */
void shuffle_deck(struct Deck* deck);
/*
* Returns the next Card structure in the Deck, or NULL if the iterator has
* reached the end of the Deck.
*/
struct Card* deck_next_card(struct Deck* deck);
#endif /* DECK_H_ */
甲板.c
#include "deck.h"
#include "card.h"
#include <time.h>
#include <stddef.h>
#include <stdlib.h>
struct Deck create_deck(const int number_of_decks)
{
/*
* Allocate memory for a deck of cards, ensuring that the size of a deck,
* as well as the number of decks are considered.
*/
struct Deck deck;
deck.iterator = 0;
deck.number_of_decks = number_of_decks;
int total_number_of_cards = (DECK_SIZE * number_of_decks);
deck.cards = malloc(sizeof(struct Card) * total_number_of_cards);
/*
* For the amount of decks requested, create a card with each possible
* rank and suit combination and add them to the deck. The loop_index will
* be used to access each array member for the deck.
*/
int deck_index;
int suit_index;
int rank_index;
int loop_index = 0;
for (deck_index = 0; deck_index < number_of_decks; deck_index++) {
for (suit_index = CLUB; suit_index <= SPADE; suit_index++) {
for (rank_index = ACE; rank_index <= KING; rank_index++) {
struct Card card = create_card(rank_index, suit_index);
deck.cards[loop_index] = card;
loop_index++;
}
}
}
return deck;
}
void shuffle_deck(struct Deck* deck)
{
/* Use the current time as the seed to our rand() function. */
srand(time(0));
/* Use the 'Fisher-Yates shuffle' algorithm. */
int i;
int total_number_of_cards = (DECK_SIZE * deck->number_of_decks);
for (i = total_number_of_cards - 1; i > 0; i--)
{
int j = rand() % (i + 1);
struct Card temp = deck->cards[j];
deck->cards[j] = deck->cards[i];
deck->cards[i] = temp;
}
}
struct Card* deck_next_card(struct Deck* deck)
{
/*
* The 'iterator' member of the Deck contains the last accessed card.
* Using this variable, we are able to traverse the Deck. If the iterator
* is greater than the amount of cards there are in the deck, we
* return NULL.
*/
struct Card* next_card;
if (deck->iterator < (DECK_SIZE * deck->number_of_decks)) {
struct Card card = deck->cards[deck->iterator];
deck->iterator++;
next_card = &card;
} else {
next_card = NULL;
}
return next_card;
}
最佳答案
在阅读我自己的问题时,我偶然发现了答案。
问题出在 deck.c
文件,deck_next_card
函数中。我们可以看到 next_card
变量只在函数范围内赋值,没有被malloc'd
。
通过分配自己的内存,它在 RELEASE 内工作:
struct Card* deck_next_card(struct Deck* deck)
{
/*
* The 'iterator' member of the Deck contains the last accessed card.
* Using this variable, we are able to traverse the Deck. If the iterator
* is greater than the amount of cards there are in the deck, we
* return NULL.
*/
struct Card* next_card = malloc(sizeof(struct Card)); # edit 1.
if (deck->iterator < (DECK_SIZE * deck->number_of_decks)) {
next_card = &deck->cards[deck->iterator]; # edit 2.
deck->iterator++;
} else {
next_card = NULL;
}
return next_card;
}
让我失望的是它在 DEBUG 版本中如何完美地工作,然后在 RELEASE 中却没有。
关于C 控制台游戏-Eclipse CDT 调试和发布是不同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24723019/
我想在文件系统上手动创建文件夹/文件,以便在 eclipse 的工作区中创建新项目,并在启动 eclipse 并选择工作区时显示在项目资源管理器中。 执行此操作需要创建哪些文件,它们需要位于何处? 请
我正在关注these instructions ,但在运行 mvn eclipse:eclipse 等命令时遇到问题。 我应该如何以及在哪里运行该命令? 我的设置: Windows 7 32 位 面向
是否有任何命令可以在不实际启动 eclipse 的情况下创建 eclipse 工作区?我希望该命令成为脚本的一部分。创建工作区后,将对其进行配置(例如文本编码),然后用于将项目导入到 RTC。我知道下
我想为 Eclipse 插件创建一个自动安装程序(即不通过“更新管理器”)。我的场景很简单:用户关闭 Eclipse,将下载的 JAR 放入 dropins 文件夹,启动 Eclipse,其余的过程是
每当我们想要使用现有源位置创建 Eclipse 项目时,我们将选择现有源位置(根)作为项目位置。 Eclipse 将在该源的根目录中创建所有项目特定文件。 现在,出于某种原因,如果我们想用不同的设置重
可能被问过多次; 有没有办法从控制台(Linux 或 Windows)刷新 Eclipse 工作区文件夹。 我知道有 Ant 任务可以做到这一点。但很想知道是否有命令行技巧。 最佳答案 不,您能做的最
我说的是工具栏上的小图标。 网络上似乎没有任何这样的问题,它们都指的是 android 或自定义应用程序,而不是与 eclipse 捆绑在一起的图标。 我想知道是否有人尝试过这个,或者可以告诉我它不值
如何使用 Eclipse 比较两个文件? (目前我正在使用 WinMerge 。) 最佳答案 要在 Eclipse 中比较两个文件,请首先在 Project Explorer/Package Expl
我正在尝试将我在一个带有数据库的 Eclipse JEE6 项目中所做的所有工作转移到另一个 Eclipse 程序。我知道我将不得不重新配置很多并重建很多库文件,但是尽可能多地传输的最简单方法是什么?
在 Eclipse 中加载我的工作台并启用 TFS 插件时,它挂起。与此类似: http://social.msdn.microsoft.com/Forums/vstudio/en-US/85c1d3
Eclipse 可以通过插件包含许多不同的功能集。您是否在一个 Eclipse 中安装所有插件?或者您是否从 spring 安装 STS,从 adobe 安装 Flex eclipse,甚至从 ecl
我错误地单击了“在 Eclipse 首选项中将目标运行标记为忽略在 Eclipse 构建中(实验)”: 在哪里/如何撤消此操作? 最佳答案 m2e 使用文件 YOUR_WORKSPACE/.metad
我是 Maven 新手。我尝试执行 >mvn eclipse:eclipse -Dwtpversion=2.0。但我收到以下错误: D:\test\CounterWebApp>mvn eclips
当我运行多个 Eclipse 实例时,操作系统不断请求上述权限。 我已经授予了该权限,并且我尝试了多次禁用和启用该权限。 我正在使用, macOS Catalina(版本:10.15.3 (19D76
我有一个 Maven 项目,其中我在项目构建期间使用 wsimport 作为目标来使用 Web 服务。 org.codehaus.mojo
当尝试使用 eclipse 新软件功能安装 eclipse 时,出现此错误: Cannot complete the install because one or more required item
我已经下载了整个 Eclipse Helios/Indigo 版本的源代码。现在我想对它进行一些修改等等。所以我导入了整个源代码,但现在我在编译时遇到了 n 个错误。此外,我正在尝试 RunAs> 插
我已经安装了 eclipse Oxygen 并且正在尝试连接到 Eclipse 市场,以安装插件,它给出了以下异常 - org.eclipse.equinox.p2.core.ProvisionExc
我的 IDE 中安装了来自 Sonatype 的 m2Eclipse 插件。它允许我通过右键单击 pom.xml 文件并导航到“运行方式”菜单来运行各种 Maven 命令(打包、安装等)。 但是,我还
我在 Windows7 64 位上运行 Maven 3 时遇到问题。当我执行maven eclipse:eclipse(我使用maven-eclipse-plugin 2.8)时,maven不会创建任
我是一名优秀的程序员,十分优秀!