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 "Segmentati on 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_pla yer_card_1,loca tion_of_player_ card_2,location _of_player_card _3;
int
location_of_CPU 1_card_1,locati on_of_CPU1_card _2,location_of_ CPU1_card_3;
int
location_of_CPU 2_card_1,locati on_of_CPU2_card _2,location_of_ CPU2_card_3;
int
location_of_mid dle_card_1,loca tion_of_middle_ card_2,location _of_middle_card _3;
int x,y;
while (x<52) {
if (where_are_card s[x] = PLAYER_HAND) {
switch (y) {
case 0:
location_of_pla yer_card_1 = x;
y++;
break;
case 1:
location_of_pla yer_card_2 = x;
y++;
break;
case 2:
location_of_pla yer_card_3 = x;
y++;
break;
}
}
x++;
}
x,y = 0;
while (x<52) {
if (where_are_card s[x] = CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU 1_card_1 = x;
y++;
break;
case 1:
location_of_CPU 1_card_2 = x;
y++;
break;
case 2:
location_of_CPU 1_card_3 = x;
y++;
break;
}
}
x++;
}
x,y = 0;
while (x<52) {
if (where_are_card s[x] = CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU 2_card_1 = x;
y++;
break;
case 1:
location_of_CPU 2_card_2 = x;
y++;
break;
case 2:
location_of_CPU 2_card_3 = x;
y++;
break;
}
}
x++;
}
x,y = 0;
while (x<52) {
if (where_are_card s[x] = MIDDLE) {
switch (y) {
case 0:
location_of_mid dle_card_1 = x;
y++;
break;
case 1:
location_of_mid dle_card_2 = x;
y++;
break;
case 2:
location_of_mid dle_card_3 = x;
y++;
break;
}
}
x++;
}
printf("Table:\ n");
printf(" Your hand: ");
print_card(pack[location_of_pla yer_card_1]);
print_card(pack[location_of_pla yer_card_2]);
print_card(pack[location_of_pla yer_card_3]);
} 9 1867
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_card s[x] = PLAYER_HAND) {
I'm /sure/ this isn't what you meant to say. You've assigned
`PLAYER_HAND` to `where_are_card s[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_pla yer_card_1 = x;
y++;
break;
case 1:
location_of_pla yer_card_2 = x;
y++;
break;
case 2:
location_of_pla yer_card_3 = x;
y++;
break;
}
}
x++;
}
Wouldn't it be better to have location_of_pla yer_card being an
array so you could just write
location_of_pla yer_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_card s[x] = CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU 1_card_1 = x;
y++;
break;
case 1:
location_of_CPU 1_card_2 = x;
y++;
break;
case 2:
location_of_CPU 1_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_card s[x] = CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU 2_card_1 = x;
y++;
break;
case 1:
location_of_CPU 2_card_2 = x;
y++;
break;
case 2:
location_of_CPU 2_card_3 = x;
y++;
break;
}
}
x++;
}
As other above.
x,y = 0;
Ditto.
while (x<52) {
if (where_are_card s[x] = MIDDLE) {
switch (y) {
case 0:
location_of_mid dle_card_1 = x;
y++;
break;
case 1:
location_of_mid dle_card_2 = x;
y++;
break;
case 2:
location_of_mid dle_card_3 = x;
y++;
break;
}
}
x++;
}
Ditto ditto.
--
Chris "sekr" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
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.v alue < 11 ) { printf("%d",car d_to_print.valu e); }
if ( card_to_print.v alue == 11 ) { printf("Jack"); }
if ( card_to_print.v alue == 12 ) { printf("Queen") ; }
if ( card_to_print.v alue == 13 ) { printf("King"); }
if ( card_to_print.v alue == 14 ) { printf("Ace"); }
printf(" of ");
if ( card_to_print.s uit == 1 ) { printf("Spades" ); }
if ( card_to_print.s uit == 2 ) { printf("Hearts" ); }
if ( card_to_print.s uit == 3 ) { printf("Diamond s"); }
if ( card_to_print.s uit == 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_pla yer_card_1,loca tion_of_player_ card_2,location _of_player_card _3;
int
location_of_CPU 1_card_1,locati on_of_CPU1_card _2,location_of_ CPU1_card_3;
int
location_of_CPU 2_card_1,locati on_of_CPU2_card _2,location_of_ CPU2_card_3;
int
location_of_mid dle_card_1,loca tion_of_middle_ card_2,location _of_middle_card _3;
int x,y = 0;
while (x<52) {
if (where_are_card s[x] == PLAYER_HAND) {
switch (y) {
case 0:
location_of_pla yer_card_1 = x;
y++;
break;
case 1:
location_of_pla yer_card_2 = x;
y++;
break;
case 2:
location_of_pla yer_card_3 = x;
y++;
break;
}
}
x++;
}
x = 0;
x = 0;
while (x<52) {
if (where_are_card s[x] == CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU 1_card_1 = x;
y++;
break;
case 1:
location_of_CPU 1_card_2 = x;
y++;
break;
case 2:
location_of_CPU 1_card_3 = x;
y++;
break;
}
}
x++;
}
x = 0;
x = 0;
while (x<52) {
if (where_are_card s[x] == CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU 2_card_1 = x;
y++;
break;
case 1:
location_of_CPU 2_card_2 = x;
y++;
break;
case 2:
location_of_CPU 2_card_3 = x;
y++;
break;
}
}
x++;
}
x = 0;
x = 0;
while (x<52) {
if (where_are_card s[x] == MIDDLE) {
switch (y) {
case 0:
location_of_mid dle_card_1 = x;
y++;
break;
case 1:
location_of_mid dle_card_2 = x;
y++;
break;
case 2:
location_of_mid dle_card_3 = x;
y++;
break;
}
}
x++;
}
printf("Table:\ n");
printf(" Your hand: "); print_card(pack[location_of_pla yer_card_1]);
printf(" "); print_card(pack[location_of_pla yer_card_2]);
printf(" "); print_card(pack[location_of_pla yer_card_3]);
}
void play_game() {
printf("\nYou will be playing against two computer players.\n");
printf("Shuffli ng 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("\nThank s for playing!");
return 0;
}
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/
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
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>
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.
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)
Simon wrote:
aha! Cheers!
Please quote enough of the previous message for context. Google does
this automatically now, so you have no excuse.
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. Yahoo store was originally written in Lisp.
c. Emacs
The issues with these will probably come up, so I might as well
mention them myself (which will also make this a more balanced
|
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 at the
<BODY> Tag of my code so I'm thinking the problem is with my
InitPage() function. I appreciate the advice. Here is my code.
<SCRIPT LANGUAGE="Javascript">
<!--
var flag = false;
|
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 block.
Another page I have a series of thumbnails which are themselves a DIV
block wrapping both an image and a caption (float: left) inside a DIV
block with a fixed width which is smaller than the width of the
browser and that DIV block is in...
|
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 understand something, so I'm hoping some experienced people can clarify my position
I write shareware apps that I only distribute through my own website. Download bandwidth is a major concern because that will cost me money
Here are my main concerns
*...
|
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 ReuseParameterValuesOnRefresh="True" in a viewer, but it
still doesn't work. Did anyone run into this problem. What's the solution?
Please help.
Thank you
| |
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 different sites, adding my own info in where needed, but it just isn't working.
What I want to do is, using an existing database setup for a calendar application, pull information out of the database and display a title of an entry that corresponds...
|
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 kept saying, "I'm
afraid to say it's 'OK' to change it because everything I did was for
a reason." Well, I know better.
My boss' old code is written in some cryptic C file with Oracle
precompiler macros barfed all over it. Once I figured out how...
|
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 well it already worked in 2.4.
but exchanging the function with the statement yields exactly the same
results
Yes ;-)
done.
|
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 code in searches or on the MVP and tool sites I visited -
though I might not understand that there is code that just needs
adaptation. ALL help is appreciated.
He wants a routine (that I'd put into a module) to do the following:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |