0
Nim Game
I am stuck in this; Make your program detect when there is only one counter left and declare the winner one turn earlier. ...a game already in progress. A: 0 B: 2 C: 2 Bob, choose a pile: B How many to remove from pile B: 1 A: 0 B: 1 C: 2 Alice, choose a pile: C How many to remove from pile C: 2 A: 0 B: 1 C: 0 Bob, you must take the last remaining counter, so you lose. Alice wins! My code is: https://code.sololearn.com/cVAGG3mJul71/#c Can someone help me to figure out it? Thanks
15 Respostas
+ 3
You should initialize the piles array like you were doing before: piles[0]=3 etc. And in the for loops for printing the *, you can do "for (i=0; i<piles[i]; ++i)". Then checking all the conditions should be fine, because the information is stored in the array.
Bur you have other bugs in your code. For example, in line 57, a[curr+1] is not right. What if curr=1? It should be a[1-curr]. Again line 63, the winner is probably a[1-curr] and not a[curr]. And the lines 72-77 are unnecessary now.
+ 3
You should initialize the piles (like piles[0]=3 etc) before the while loop. Otherwise they'd be reset everytime. And to print A, B, and C, do something like
printf("A: ");
for (i=0; i<piles[0]; ++i){
printf("*");
}
printf("\nB:");
for (i=0; i<piles[1]; ++i){
printf("*");
}
printf("\nC:");
for (i=0; i<piles[2]; ++i){
printf("*");
}
+ 2
Tuyen Pham
Cool! Yes, I was going to say that you'll need to exit the loop in that case too. But you figured it out! Congrats! đ
One question: if there's only one counter left, doesn't your code again print " there are no counters left so you win" from the last part? If yes, then it might be better to put that in the final else block.
+ 2
I cannot run this code properly because Sololearn doesn't accept dynamic intputs. So there may be small errors. But it should be something like this from line 41:
curr = 1 - curr;
if(piles[0] + piles[1] + piles[2] == 1){
printf("%s, you must take the last remaining counter so you lose. %s wins! ", a[curr], a[1-curr]);
break;
} else
if(piles[0] + piles[1] + piles[2] == 0){
printf("There are no counters left so %s wins", a[curr]);
break;
}
}
return 0;
}
+ 2
Put the for loops that print the piles with "*" inside the while loop, instead of
printf("\nA:%d B:%d C:%d",piles[0],piles[1],piles[2]);
You don't need it before the while loop. That's just printing it the first time, and not the subsequent ones.
+ 2
You're welcome âș
+ 1
Did you try this?
if (piles[0]+piles[1]+piles[2]==1) {
...
}
+ 1
I write this:
if(piles[0]>0 || piles[1]>0 || piles[2]>0){
keep_playing=1;
} else if(piles[0]+piles[1]+piles[2]==1){
printf("%s, must take the last remaining counter so you loose. %s wins! ",a[curr],a[curr+1]);
}else{
printf("\nA:%d B:%d C:%d",0,0,0);
break;
}
It does not work
+ 1
I change to this:
if(piles[0]+piles[1]+piles[2]==1){
printf("%s, must take the last remaining counter so you loose. %s wins! ",a[curr],a[curr+1]);
break;
} else
if(piles[0]>0 || piles[1]>0 || piles[2]>0){
keep_playing=1;
} else{
printf("\nA:%d B:%d C:%d",0,0,0);
break;
It works now. Thanks @Kishalaya Saha
+ 1
@Kishalaya Saha i got my mistake and i did fix it. Thank you so much!!
0
wait can you show me in the code?
0
yes, it is. so how can i fix it?
0
Thanks @Kishalaya Saha. I did fix the error but I have another question. If I change number of piles into "*" look like this:
the counters in rows instead of just showing a number. You must use loops for this.
A: ***
B: ****
C: *****
Alice, choose a pile: A
How many to remove from pile A: 2
A: *
B: ****
C: *****
Bob, choose a pile: C
How many to remove from pile C: 3
A: *
B: ****
C: **
I used the for loop in the code: https://code.sololearn.com/cc68fgGs49a3/#c
but it does not work so how can I figure out with the value while keep_playing the game?
0
I did like this:
int piles[3], curr=0, keep_playing=1;
piles[0] = 3;
for (i=0; i<piles[0]; ++i){
printf("*");
}
piles[1]=4;
for (i=0; i<piles[1]; ++i){
printf("*");
}
piles[2]=5;
for (i=0; i<piles[2]; ++i){
printf("*");
}
while(keep_playing!=0){
printf("\nA:%d B:%d C:%d",piles[0],piles[1],piles[2]);
....
}
but the computers print out piles[0]=3 not my expectation ***
0
It looks like this.Is it right?
while(keep_playing!=0){
piles[0] = 3;
for (i=0; i<piles[0]; ++i){
printf("*");
}
piles[1]=4;
for (i=0; i<piles[1]; ++i){
printf("*");
}
piles[2]=5;
for (i=0; i<piles[2]; ++i){
printf("*");
}
but how can it prints out:
A: ***
B: ****
C: *****