Nim Game | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

14th Nov 2018, 3:31 AM
Tuyen Pham
Tuyen Pham - avatar
15 Answers
+ 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.
14th Nov 2018, 3:44 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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("*"); }
14th Nov 2018, 5:23 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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.
14th Nov 2018, 4:34 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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; }
14th Nov 2018, 5:52 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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.
14th Nov 2018, 4:35 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
You're welcome ☺
14th Nov 2018, 10:54 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Did you try this? if (piles[0]+piles[1]+piles[2]==1) { ... }
14th Nov 2018, 4:06 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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
14th Nov 2018, 4:10 AM
Tuyen Pham
Tuyen Pham - avatar
+ 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
14th Nov 2018, 4:16 AM
Tuyen Pham
Tuyen Pham - avatar
+ 1
@Kishalaya Saha i got my mistake and i did fix it. Thank you so much!!
14th Nov 2018, 7:04 PM
Tuyen Pham
Tuyen Pham - avatar
0
wait can you show me in the code?
14th Nov 2018, 4:38 AM
Tuyen Pham
Tuyen Pham - avatar
0
yes, it is. so how can i fix it?
14th Nov 2018, 4:39 AM
Tuyen Pham
Tuyen Pham - avatar
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?
14th Nov 2018, 3:08 PM
Tuyen Pham
Tuyen Pham - avatar
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 ***
14th Nov 2018, 4:26 PM
Tuyen Pham
Tuyen Pham - avatar
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: *****
14th Nov 2018, 4:42 PM
Tuyen Pham
Tuyen Pham - avatar