473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Don't understand why my code isn't working

I've got a simple and repetitive bit of code for a function in a C
implementation of the card game 31s I'm working on. BTW, I am a bit of
a novice at C; for the past couple of years I was using Visual Basic,
but I wanted portability, so I chose C.

I am developing this in XCode 2 (on Mac OS 10.4.7) as a normal C
command line utility; so this is basically just GCC. This code compiles
fine, without any errors or warnings but when I run it in XCode it
says: "31s has exited due to signal 11 (SIGSEGV)." and in Terminal it
says "Segmentation Fault". Here is the code of most of the function,
and what I'm pretty sure is the faulty bit. I can post all the code up
if needed; it's all in one .c file.

void print_stats() {
int
location_of_player_card_1,location_of_player_card_ 2,location_of_player_card_3;
int
location_of_CPU1_card_1,location_of_CPU1_card_2,lo cation_of_CPU1_card_3;
int
location_of_CPU2_card_1,location_of_CPU2_card_2,lo cation_of_CPU2_card_3;
int
location_of_middle_card_1,location_of_middle_card_ 2,location_of_middle_card_3;

int x,y;
while (x<52) {
if (where_are_cards[x] = PLAYER_HAND) {
switch (y) {
case 0:
location_of_player_card_1 = x;
y++;
break;
case 1:
location_of_player_card_2 = x;
y++;
break;
case 2:
location_of_player_card_3 = x;
y++;
break;
}
}
x++;
}

x,y = 0;

while (x<52) {
if (where_are_cards[x] = CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU1_card_1 = x;
y++;
break;
case 1:
location_of_CPU1_card_2 = x;
y++;
break;
case 2:
location_of_CPU1_card_3 = x;
y++;
break;
}
}
x++;
}

x,y = 0;

while (x<52) {
if (where_are_cards[x] = CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU2_card_1 = x;
y++;
break;
case 1:
location_of_CPU2_card_2 = x;
y++;
break;
case 2:
location_of_CPU2_card_3 = x;
y++;
break;
}
}
x++;
}

x,y = 0;

while (x<52) {
if (where_are_cards[x] = MIDDLE) {
switch (y) {
case 0:
location_of_middle_card_1 = x;
y++;
break;
case 1:
location_of_middle_card_2 = x;
y++;
break;
case 2:
location_of_middle_card_3 = x;
y++;
break;
}
}
x++;
}

printf("Table:\n");
printf(" Your hand: ");
print_card(pack[location_of_player_card_1]);
print_card(pack[location_of_player_card_2]);
print_card(pack[location_of_player_card_3]);

}

Jul 18 '06 #1
9 1842
Simon wrote:
int x,y;
while (x<52) {
The value of `x` is indeterminate. Undefined behaviour. It
might, for example, be -1066 (assuming it's not a trap
value).
if (where_are_cards[x] = PLAYER_HAND) {
I'm /sure/ this isn't what you meant to say. You've assigned
`PLAYER_HAND` to `where_are_cards[x]`, and if PLAYER_HAND isn't
zero, you do the then-part (the switch below). I think you
meant to write `==`.
switch (y) {
case 0:
location_of_player_card_1 = x;
y++;
break;
case 1:
location_of_player_card_2 = x;
y++;
break;
case 2:
location_of_player_card_3 = x;
y++;
break;
}
}
x++;
}
Wouldn't it be better to have location_of_player_card being an
array so you could just write

location_of_player_card[y] = x;

(if necessary, checking y to be in range)?
x,y = 0;
I don't think that does what you think.

What it does is evaluate `x`, throw the value away, then assign 0
to `y`.

I suspect you need to write

x = y = 0;
while (x<52) {
if (where_are_cards[x] = CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU1_card_1 = x;
y++;
break;
case 1:
location_of_CPU1_card_2 = x;
y++;
break;
case 2:
location_of_CPU1_card_3 = x;
y++;
break;
}
}
x++;
}
Almost exactly the same as the code above. Seek to remove duplication.
x,y = 0;
As above.
>
while (x<52) {
if (where_are_cards[x] = CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU2_card_1 = x;
y++;
break;
case 1:
location_of_CPU2_card_2 = x;
y++;
break;
case 2:
location_of_CPU2_card_3 = x;
y++;
break;
}
}
x++;
}
As other above.
x,y = 0;
Ditto.
while (x<52) {
if (where_are_cards[x] = MIDDLE) {
switch (y) {
case 0:
location_of_middle_card_1 = x;
y++;
break;
case 1:
location_of_middle_card_2 = x;
y++;
break;
case 2:
location_of_middle_card_3 = x;
y++;
break;
}
}
x++;
}
Ditto ditto.
--
Chris "sekr" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Jul 18 '06 #2
Thanks, Chris, this is really helpful. I've cleared up stuff you said
in long-hand just to make sure but it's still not quite working - my
program, that is. I've commented out a bit in shuffle_cards which
randomizes but it's still very wierd with all these "signal" errors.
Here's the complete code:

#include <stdio.h>
#include <stdlib.h // for srand and rand
#include <time.h // for time

typedef struct {
int suit; /* The suit of the playing card */
int value; /* The value of the playing card */
} CARD;

/* Set up an integer to represent each suit */
enum {CLUBS = 4, DIAMONDS = 3, HEARTS = 2, SPADES= 1};

/* Set up integers to represent non numeric cards */
enum {ACE = 14, JACK = 11, QUEEN = 12, KING = 13};
CARD pack[51];
int where_are_cards[51];
enum {PACK = 0, PLAYER_HAND = 1, CPU1_HAND = 2, CPU2_HAND = 3, MIDDLE =
4};
void print_card(CARD card_to_print) {
if ( card_to_print.value < 11 ) { printf("%d",card_to_print.value); }
if ( card_to_print.value == 11 ) { printf("Jack"); }
if ( card_to_print.value == 12 ) { printf("Queen"); }
if ( card_to_print.value == 13 ) { printf("King"); }
if ( card_to_print.value == 14 ) { printf("Ace"); }

printf(" of ");

if ( card_to_print.suit == 1 ) { printf("Spades"); }
if ( card_to_print.suit == 2 ) { printf("Hearts"); }
if ( card_to_print.suit == 3 ) { printf("Diamonds"); }
if ( card_to_print.suit == 4 ) { printf("Clubs"); }
printf("\n");
}

void shuffle_pack() {
srand(time(0)); // initialize seed "randomly"
int i,x,y,z = 0;

while (x < 4) {
while (y < 13) {
pack[x*y].value = y;
pack[x*y].suit = x;
y++;
}
y = 0;
x++;
}
/*
while (z<52) {
int r = rand() % 52; // generate a random position
CARD temp = pack[i]; pack[i] = pack[r]; pack[r] = temp;
z++;
}*/
}

void print_stats() {
int
location_of_player_card_1,location_of_player_card_ 2,location_of_player_card_3;
int
location_of_CPU1_card_1,location_of_CPU1_card_2,lo cation_of_CPU1_card_3;
int
location_of_CPU2_card_1,location_of_CPU2_card_2,lo cation_of_CPU2_card_3;
int
location_of_middle_card_1,location_of_middle_card_ 2,location_of_middle_card_3;

int x,y = 0;
while (x<52) {
if (where_are_cards[x] == PLAYER_HAND) {
switch (y) {
case 0:
location_of_player_card_1 = x;
y++;
break;
case 1:
location_of_player_card_2 = x;
y++;
break;
case 2:
location_of_player_card_3 = x;
y++;
break;
}
}
x++;
}

x = 0;
x = 0;

while (x<52) {
if (where_are_cards[x] == CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU1_card_1 = x;
y++;
break;
case 1:
location_of_CPU1_card_2 = x;
y++;
break;
case 2:
location_of_CPU1_card_3 = x;
y++;
break;
}
}
x++;
}

x = 0;
x = 0;

while (x<52) {
if (where_are_cards[x] == CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU2_card_1 = x;
y++;
break;
case 1:
location_of_CPU2_card_2 = x;
y++;
break;
case 2:
location_of_CPU2_card_3 = x;
y++;
break;
}
}
x++;
}

x = 0;
x = 0;

while (x<52) {
if (where_are_cards[x] == MIDDLE) {
switch (y) {
case 0:
location_of_middle_card_1 = x;
y++;
break;
case 1:
location_of_middle_card_2 = x;
y++;
break;
case 2:
location_of_middle_card_3 = x;
y++;
break;
}
}
x++;
}

printf("Table:\n");
printf(" Your hand: "); print_card(pack[location_of_player_card_1]);
printf(" "); print_card(pack[location_of_player_card_2]);
printf(" "); print_card(pack[location_of_player_card_3]);

}

void play_game() {
printf("\nYou will be playing against two computer players.\n");
printf("Shuffling pack... "); shuffle_pack(); printf("Done!\n");

int i=0; while (i<52) { where_are_cards[i] = PACK; i++; } //
Initialize where_are_cards[51] array
i=0; while (i<3) { where_are_cards[i] = PLAYER_HAND; i++; } // Give
player 3 cards
i=3; while (i<3) { where_are_cards[i] = CPU1_HAND; i++; } // Give CPU1
3 cards
i=6; while (i<3) { where_are_cards[i] = CPU2_HAND; i++; } // Give CPU2
3 cards
i=9; while (i<3) { where_are_cards[i] = MIDDLE; i++; } // Put 3 cards
in the middle

print_card(pack[5]);

print_stats();
}

int main (int argc, const char * argv[]) {
printf("Welcome to the game of 31s!\n");
printf("This was created by Simon Goldring in 2006.\n");

int menu;
while ( menu != 1 ) {
printf("\nMAIN MENU:\n");
printf(" 0 - Play a game of 31s with the computer\n");
printf(" 1 - Quit 31s\n");
printf("? ");
scanf("%d", &menu);
if ( menu == 0 ) {
play_game();
}
}

printf("\nThanks for playing!");

return 0;
}

Jul 18 '06 #3
Simon wrote:
Thanks, Chris, this is really helpful. I've cleared up stuff you said
in long-hand just to make sure but it's still not quite working - my
program, that is. I've commented out a bit in shuffle_cards which
randomizes but it's still very wierd with all these "signal" errors.
Those are likely array-indexing errors. (You don't get a nice message
when that happens in C - things will go loudly wrong if you're lucky,
quietly wrong if you're unlucky.)

You should read a decent introductory C book - your previous language
is interfering and you need a bit of reprogramming!

Just a few quick notes at days end:
void shuffle_pack() {
srand(time(0)); // initialize seed "randomly"
int i,x,y,z = 0;
Only initialises `z`. `i`, `x`, and `y` are all junk.

int i = 0, x = 0, y = 0, z = 0;
or
int i = 0;
int x = 0;
int y = 0;
int z = 0;

int x,y = 0;
Ditto.
>
x = 0;
x = 0;
One of those should be `y = 0`?

x = y = 0;
x = 0;
x = 0;
Ditto.
x = 0;
x = 0;
Ditto.

--
Chris "seeker" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Jul 18 '06 #4
On 18 Jul 2006 07:57:15 -0700, in comp.lang.c , "Simon"
<Si***********@gmail.comwrote:
>Thanks, Chris, this is really helpful. I've cleared up stuff you said
in long-hand just to make sure but it's still not quite working - my
program, that is. I've commented out a bit in shuffle_cards which
randomizes but it's still very wierd with all these "signal" errors.
Here's the complete code:
int i,x,y,z = 0;
This only initialises z to zero. Same problem appears several other
times.

Top tip: do not declare more than one variable on a line;
int i = 0;
int x = 0;
that way, you cannot make this sort of mistake.
(or this one:
FILE *a, b;
)
By the way, while some C compilers now allow you to declare variables
where used, most do not - you must declare all variables at the start
of a block. I worry that you are accidentally compiling this code as
C++.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 18 '06 #5
Simon wrote:
[...]
CARD pack[51];
int where_are_cards[51];
[...]

Last I checked, there are 52 cards in the deck.

CARD pack[52];
int where_are_cards[52];
int i,x,y,z = 0;
i, x, and y are not initialized here.

int i=0, x=0, y=0, z=0;
>
while (x < 4) {
while (y < 13) {
pack[x*y].value = y;
pack[x*y].suit = x;
y++;
}
y = 0;
x++;
}
You probably want "pack[x*13+y]" here.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jul 18 '06 #6
I'm changing all the initializing stuff, but I don't understand one
thing; doesn't an array tart at 0, so 52 items would be 0-51?

Thanks for all the help.

Jul 18 '06 #7
Simon said:
I'm changing all the initializing stuff, but I don't understand one
thing; doesn't an array tart at 0, so 52 items would be 0-51?
Sure, 52 items would be 0-51, but if you define it as being CARD pack[51],
that's 51 items, 0-50. In an array definition, the number between [ and ]
is the number of objects you want the array to contain, not the number of
the highest index in the array.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 18 '06 #8
aha! Cheers!

Jul 18 '06 #9
Simon wrote:
aha! Cheers!

Please quote enough of the previous message for context. Google does
this automatically now, so you have no excuse.


Brian
Jul 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
4
by: Danny | last post by:
Hi I don't understand why I keep getting "Error: Object expected" I tried different things and haven't been able to solve it. I'm a newbie so I'm not sure what I'm doing wrong. The debugger breaks...
7
by: Robert Nicholson | last post by:
So I've got one page where I have an image inside a DIV with text-align: center and the image is correctly centered in that block. Making me think that text-align will center the contents of a...
3
by: Lars | last post by:
Hello I write all my applications in VB6-- and I don't understand the reason to move to .NET (other than MS might break VB6 apps in future updates). I'm taking the position that I don't...
19
by: LP | last post by:
I am using (trying to) CR version XI, cascading parameters feature works it asks user to enter params. But if page is resubmitted. It prompts for params again. I did set...
20
by: johnnyboy7 | last post by:
I don't know a lot about ASP and I know even less about SQL. I have been trying to search the web for information to be able to get done what I need. I've tried piecing things together from many...
21
by: jehugaleahsa | last post by:
Hello: I had an hour-long discussion with my boss today. Last night, right before I dozed off, I realized some of his code resulted in duplicate processing. I tried to explain it to him and he...
0
by: Stef Mientki | last post by:
Terry Reedy wrote: sorry, don't know how this happened, as I always copy/paste ? AFAIK locals() == sys._getframe(0).f_locals AFAIK, again one level up weird, I use it in 2.5 and if I remember...
7
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.